import { Injectable , HttpException } from '@nestjs/common' ; import { CreateClassify } from '../interfaces/classify.interface' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Classify } from '../entities/classify.entity' ; import { Article } from '../entities/article.entity' ; import { TreeRepository , Repository , In } from 'typeorm' ; @ Injectable ( ) export class ClassifyService { constructor ( @ InjectRepository ( Classify ) private readonly claRepository , @ InjectRepository ( Article ) private readonly artRepository ) { } param classify async addClassify ( classify ) { try { const ignore = await this . claRepository . count ( ) ; if ( ! classify . parent . value || ignore <= 0 ) { await this . claRepository . save ( this . claRepository . create ( { label : '总分类' , value : 'root' , onlyChildrenArt : true } ) ) ; return { code : 200 , message : '创建成功' } ; } if ( classify . parent ) { const exist = await this . claRepository . findOne ( { value : classify . parent . value } ) ; if ( ! exist ) { return { code : 405 , message : '当前分类父节点不存在' } ; } classify . parent = exist ; } if ( classify . value !== 'root' ) { const result = await this . claRepository . findOne ( { where : { value : classify . value } } ) ; if ( result ) { throw new HttpException ( '别名重复!' , 406 ) ; } } await this . claRepository . save ( await this . claRepository . create ( classify ) ) ; } catch ( err ) { throw new HttpException ( err . toString ( ) , 500 ) ; } } param id async delClassify ( id ) { const classify = await this . claRepository . findOne ( { id } ) ; if ( ! classify ) { return { code : 404 , message : '当前分类不存在' } ; } const array = await this . getAllClassifyIds ( id ) ; const articles = await this . artRepository . count ( { where : { classify : In ( array ) } } ) ; if ( articles > 0 ) { throw new HttpException ( '当前分类下有文章,不能删除' , 403 ) ; } array . splice ( array . indexOf ( id ) , 1 ) ; if ( array . length ) { throw new HttpException ( '当前分类下有子分类,不能删除' , 403 ) ; } await this . claRepository . remove ( classify ) ; return { code : 200 , message : '删除成功' } ; } param idNum async getAllClassifyIds ( idNum ) { const array = [ ] ; const classify = await this . claRepository . findOne ( { id : idNum } ) ; await this . claRepository . findDescendants ( classify ) . then ( a => { if ( a ) { a . map ( a => { array . push ( a . id ) ; } ) ; } } ) ; return array ; } param classify async updateClassify ( classify ) { const exist = await this . claRepository . findOne ( { id : classify . id } ) ; if ( ! exist ) { return { code : 404 , message : '当前分类不存在!' } ; } if ( classify . value && classify . value !== exist . value ) { if ( await this . claRepository . findOne ( { where : { value : classify . value } } ) ) { throw new HttpException ( '该分类别名已存在!' , 409 ) ; } } const parent = await this . claRepository . findOne ( { value : classify . parent . value } ) ; if ( ! parent ) { throw new HttpException ( '该上级分类不存在' , 404 ) ; } try { classify . parent = parent ; await this . claRepository . save ( await this . claRepository . create ( classify ) ) ; } catch ( err ) { throw new HttpException ( err . toString ( ) , 500 ) ; } } param id async getAllClassify ( id ) { if ( id ) { const exist = await this . claRepository . findOne ( id ) ; if ( ! exist ) { throw new HttpException ( '该分类不存在!' , 404 ) ; } return await this . claRepository . findDescendantsTree ( exist ) ; } else { return await this . claRepository . findTrees ( ) ; } } param id async getOneClassify ( value ) { const exist = await this . claRepository . findOne ( { where : { value } } ) ; if ( ! exist ) { throw new HttpException ( '该分类不存在!' , 404 ) ; } const data = await this . claRepository . findAncestorsTree ( exist ) ; return data ; } param id async getParentClassify ( id ) { const exist = await this . claRepository . findOne ( { id } ) ; if ( ! exist ) { throw new HttpException ( '该分类不存在!' , 404 ) ; } const data = await this . claRepository . findAncestorsTree ( exist ) ; return { code : 200 , message : '查询成功!' , data : [ data ] } ; } param classifyId param newClassifyId async mobileArticles ( classifyId , newClassifyId ) { const exist = await this . claRepository . findOne ( { where : { id : classifyId } } ) ; if ( ! exist ) { return { code : 404 , message : '原分类不存在!' } ; } const newClassify = await this . claRepository . findOne ( { where : { id : newClassifyId } } ) ; if ( ! newClassify ) { return { code : 404 , message : '所选分类不存在!' } ; } const array = await this . getAllClassifyIds ( classifyId ) ; const ids = await this . artRepository . createQueryBuilder ( 'art' ) . where ( '"art"."classifyId" in(:...id)' , { id : array } ) . getMany ( ) ; if ( ! ids . length ) { return { code : 404 , message : '原分类下不存在文章!' } ; } try { for ( const i of ids ) { await this . artRepository . update ( i . id , { classify : newClassify } ) ; } } catch ( err ) { throw new HttpException ( err . toString ( ) , 500 ) ; } } }