import { ReturnModelType } from '@typegoose/typegoose'; import { AnyParamConstructor } from '@typegoose/typegoose/lib/types'; import { Application, Context } from '@midwayjs/koa'; import { App, Inject } from '@midwayjs/decorator'; import _ = require('lodash'); import { FrameworkErrorEnum, ServiceError } from '../error/service.error'; import { GetModel } from '../util/getModel'; import { SearchBase } from '../../dist'; /** * Service基类,实现了一些基础的crud */ export abstract class BaseService> { @App() app: Application; @Inject() ctx: Context; // 要求继承基类的service,必须给上默认的model abstract model: ReturnModelType; /** * 列表查询 * @param {SearchBase} filter 查询条件 * @param {object} pageOptions 分页条件 * @param {Boolean} lean 是否使用JavaScript形式数据;false为mongoose的模型实例数据 * @param {Boolean} populate 是否进行ref关联数据 * @returns {Promise} 返回列表 */ async query(filter: SearchBase, pageOptions: object = {}, lean = true, populate = true): Promise { const dup = _.cloneDeep(filter.getFilter()); const data = await this.model.find(dup, {}, { ...pageOptions }).lean(lean); return data; } /** * 数据总数查询 * @param {SearchBase} filter 查询条件 * @returns {number} 数据总数 */ async count(filter: SearchBase): Promise { const dup = _.cloneDeep(filter.getFilter()); const total = await this.model.count(dup); return total; } /** * 单查询-通过id * @param {string} id 查询对象数据id * @param {boolean} lean 是否使用JavaScript形式数据 * @returns {object|undefined} */ async fetch(id: string, lean = true): Promise { const data = await this.model.findById(id).lean(lean); return data; } /** * 单修改-通过id * @param {string} id 修改对象数据id * @param {object} body 要修改的内容 * @returns {object} */ async updateOne(id: string, body: object): Promise { if (!id) throw new ServiceError('缺少查询信息', FrameworkErrorEnum.NEED_PARAMS); const num = await this.model.count({ _id: id }); if (num <= 0) throw new ServiceError('未找到要修改的数据', FrameworkErrorEnum.NOT_FOUND_DATA); await this.model.updateOne({ _id: id }, body); return 'ok'; } /** * 多修改 * @param {SearchBase} filter 要修改的对象查询条件 * @param {object} body 要修改的内容 * @returns {object} */ async updateMany(filter: SearchBase, body: object): Promise { if (!body) throw new ServiceError('缺少修改信息', FrameworkErrorEnum.NEED_BODY); const dup = _.cloneDeep(filter.getFilter()); await this.model.updateMany(dup, body); return 'ok'; } /** * 单删除-通过id * @param id 要删除的数据id * @returns {object} */ async delete(id: string): Promise { if (!id) throw new ServiceError('缺少数据信息', FrameworkErrorEnum.NEED_PARAMS); await this.model.deleteOne({ _id: id }); return 'ok'; } /** * 多删除 * @param {SearchBase} filter 要删除的数据查询条件 * @returns {object} */ async deleteMany(filter: SearchBase): Promise { const keys = Object.keys(filter); if (keys.length <= 0) throw new ServiceError('暂不提供清库服务', FrameworkErrorEnum.SERVICE_FAULT); const dup = _.cloneDeep(filter.getFilter()); await this.model.deleteMany(dup); return 'ok'; } /** * 单创建 * @param body 要创建的数据内容 * @returns {object} */ async create(body: object): Promise { const data = await this.model.create(body); return data; } /** * 多创建 * @param body 要创建的多个数据 * @returns {object[]} */ async createMany(body: object[]): Promise { const data = await this.model.insertMany(body); return data; } /** * 获取本服务默认表的ref关系 */ async getRefs() { const schema = _.get(this.model, 'schema.tree'); const refs = []; for (const key in schema) { const f = schema[key]; if (_.isObject(f) && _.get(f, 'ref')) { const model = GetModel(key); const path = key; refs.push({ path, model }); } } return refs; } }