import { Service } from 'egg' ; import { BaseModelStatic } from './model' ; import SqlUtils from '../utils/sql' ; import { Model , FindAndCountOptions , WhereAttributeHash } from 'sequelize' ; import { PageParams , PageInfo } from '../../typings' ; import Page from '../utils/page' ; /**
 * BaseService service 基础类, 提供四个基础方法
 * 提供基础方法
 *
 *
 * 需要在每个继承该基础 service 的类构造函数中，指定 model 为当前 service 的 model
 */ export default class BaseService < T extends Model > extends Service { model : BaseModelStatic < T > ; public async findById ( id ) { const model = await this . model . findOne ( { ... SqlUtils . queryOptions ( ) , where : { id , } , } ) ; if ( model ) { return model . get ( { plain : true } ) as T ; } return null ; } public async findListByKey ( where , pageParams , options ? ) { const page = new Page ( pageParams ) ; const { rows , count } = await this . model . findAndCountAll ( { where : where as WhereAttributeHash , ... SqlUtils . queryOptions ( ) , ... page . buildOptions ( ) , ... options , } ) ; page . setTotal ( count ) ; return { list : rows , page : page . getData ( ) , } ; } public async removeById ( id ) { const result = await this . model . destroy ( { where : { id , } , } ) ; console . log ( result ) ; if ( result === 1 ) { return true ; } return false ; } public async updateById ( instance , id ) { if ( instance . id ) { delete instance . id ; } const [ result ] = await this . model . update ( SqlUtils . updateOptions < T > ( instance as T , this . ctx . user ) , { where : { id , } , } ) ; if ( result === 1 ) { return true ; } return false ; } public async createInstance ( instance ) { const model = await this . model . create ( SqlUtils . createOptions < T > ( instance as T , this . ctx . user ) ) ; return model . get ( { plain : true } ) as T ; } }