import { Injectable , MgModel , MgModelType } from "../core/decorator" ; import { ProjectDto } from "../dto" ; import ProjectEntity from "../entity/ProjectEntity" ; import AppError from "../common/AppError" ; import { createStringDate } from "../common/utils" ; import BaseService from "./BaseService" ; @ Injectable ( ) export default class ProjectService extends BaseService { @ MgModel ( ProjectEntity ) private model ! : MgModelType < ProjectEntity > ; param data async add ( data ) { const rel = await this . model . findOne ( { name : data . name , status : 1 } ) . exec ( ) ; if ( rel ) { AppError . assert ( "项目已存在" ) ; } if ( ! data . name ) { AppError . assert ( "请输入项目名称" ) ; return ; } const project = { name : data . name , createTime : createStringDate ( ) , updateTime : createStringDate ( ) , status : 1 , } ; const { _id : id } = await this . model . create ( project ) ; return id ; } /**
     * 查询所有项目
     */ async findAll ( ) { const rel = await this . model . find ( { status : 1 } ) . sort ( { createTime : - 1 } ) . exec ( ) ; return rel . map ( ( v ) => this . toDtoObject < ProjectDto > ( v ) ) ; } param name async findByName ( name ) { const rel = await this . model . findOne ( { status : 1 , name } ) . sort ( { createTime : - 1 } ) . exec ( ) ; return this . toDtoObject < ProjectDto > ( rel ) ; } param data async update ( data ) { let rel = await this . model . findOne ( { name : data . name , status : 1 } ) . exec ( ) ; if ( rel && rel . id !== data . id ) { AppError . assert ( "项目已存在" ) ; } rel = await this . model . findByIdAndUpdate ( data . id , { name : data . name , updateTime : createStringDate ( ) } , { omitUndefined : true } ) ; return rel ; } param id async delete ( id ) { const rel = await this . model . findByIdAndUpdate ( id , { status : 0 } ) ; return rel ; } }