import { mongoose } from "@typegoose/typegoose" ; import { Injectable , MgModel , MgModelType } from "../core/decorator" ; import ComponentTypeEntity from "../entity/ComponentTypeEntity" ; import ComponentEntity from "../entity/ComponentEntity" ; import { ComponentDto , ComponentTypeDto } from "../dto" ; import { createStringDate } from "../common/utils" ; import BaseService from "./BaseService" ; import AppError from "../common/AppError" ; @ Injectable ( ) export default class ComponentService extends BaseService { @ MgModel ( ComponentTypeEntity ) private typeModel ! : MgModelType < ComponentTypeEntity > ; @ MgModel ( ComponentEntity ) private model ! : MgModelType < ComponentEntity > ; /**
     * 查询所有分类
     */ async findTypes ( ) { const rel = await this . typeModel . find ( { status : 1 } ) . exec ( ) ; if ( ! rel || rel . length === 0 ) { const name = "自定义" ; const icon = "icon-zidingyi" ; const { _id : id } = await this . typeModel . create ( { name , icon , status : 1 , } ) ; return [ { id , name , icon } ] ; } return rel . map ( ( v ) => this . toDtoObject < ComponentTypeDto > ( v ) ) ; } param data returns async addType ( data ) { const rel = await this . typeModel . findOne ( { status : 1 , name : data . name , pid : data . pid } ) . exec ( ) ; if ( rel ) { AppError . assert ( `${ data . name }已经存在` ) ; } const { _id : id } = await this . typeModel . create ( { ... data , status : 1 , } ) ; return id ; } param data returns async delType ( id ) { let rel = await this . typeModel . findOne ( { pid : id , status : 1 } ) . exec ( ) ; if ( rel ) { AppError . assert ( "当前分类下下存在多个子类，请先删除子类" ) ; } const comp = await this . model . findOne ( { type : id } ) . exec ( ) ; if ( comp ) { AppError . assert ( "当前分类下存在多个组件，请先迁移组件" ) ; } rel = await this . typeModel . findByIdAndUpdate ( id , { status : 0 } ) ; return rel ; } param data returns async updateType ( data ) { const rel = await this . typeModel . findByIdAndUpdate ( data . id , { ... data , status : undefined , } , { omitUndefined : true } ) ; return rel ; } param type async findAll ( type ? ) { const rel = await this . model . find ( { type } ) . exec ( ) ; return rel ? rel . map ( ( v ) => this . toDtoObject < ComponentDto > ( v ) ) : [ ] ; } async findByName ( name , version ) { const rel = await this . model . findOne ( { name , version } ) . exec ( ) ; return this . toDtoObject < ComponentDto > ( rel ) ; } async findById ( id ) { const rel = await this . model . findOne ( { _id : id } ) . exec ( ) ; return this . toDtoObject < ComponentDto > ( rel ) ; } async findByIds ( ids ) { const orIds = ids . map ( ( id ) => ( { _id : id } ) ) ; const rel = await this . model . find ( { $or : orIds } ) . exec ( ) ; return rel . map ( ( v ) => this . toDtoObject < ComponentDto > ( v ) ) ; } async add ( data ) { const { _id : id } = await this . model . create ( { ... data , type : new mongoose . Types . ObjectId ( data . type ) , createTime : createStringDate ( ) , updateTime : createStringDate ( ) , } ) ; return id ; } async update ( data ) { const rel = await this . model . findByIdAndUpdate ( data . id , { ... data , id : undefined , createTime : undefined , type : new mongoose . Types . ObjectId ( data . type ) , updateTime : createStringDate ( ) , } , { omitUndefined : true } ) ; return rel ; } async delete ( id ) { const rel = await this . model . findOneAndDelete ( { _id : id } ) ; return rel ; } }