|
@@ -0,0 +1,137 @@
|
|
|
+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';
|
|
|
+/**
|
|
|
+ * Service基类
|
|
|
+ */
|
|
|
+export abstract class BaseService<T extends AnyParamConstructor<any>> {
|
|
|
+ @App()
|
|
|
+ app: Application;
|
|
|
+ @Inject()
|
|
|
+ ctx: Context;
|
|
|
+
|
|
|
+ // 要求继承基类的service,必须给上默认的model
|
|
|
+ abstract model: ReturnModelType<T>;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表查询
|
|
|
+ * @param {object} filter 查询条件
|
|
|
+ * @param {object} pageOptions 分页条件
|
|
|
+ * @param {Boolean} lean 是否使用JavaScript形式数据;false为mongoose的模型实例数据
|
|
|
+ * @returns {Promise<object>} 返回列表
|
|
|
+ */
|
|
|
+ async query(
|
|
|
+ filter: object = {},
|
|
|
+ pageOptions: object = {},
|
|
|
+ lean = true
|
|
|
+ ): Promise<object> {
|
|
|
+ const dup = _.cloneDeep(filter);
|
|
|
+ const data = await this.model.find(dup, {}, { ...pageOptions }).lean(lean);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据总数查询
|
|
|
+ * @param {object} filter 查询条件
|
|
|
+ * @returns {number} 数据总数
|
|
|
+ */
|
|
|
+ async count(filter: object = {}): Promise<number> {
|
|
|
+ const dup = _.cloneDeep(filter);
|
|
|
+ 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<object | undefined> {
|
|
|
+ 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<string> {
|
|
|
+ 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 {object} filter 要修改的对象查询条件
|
|
|
+ * @param {object} body 要修改的内容
|
|
|
+ * @returns {object}
|
|
|
+ */
|
|
|
+ async updateMany(filter: object = {}, body: object): Promise<string> {
|
|
|
+ if (!body)
|
|
|
+ throw new ServiceError('缺少修改信息', FrameworkErrorEnum.NEED_BODY);
|
|
|
+ await this.model.updateMany(filter, body);
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单删除-通过id
|
|
|
+ * @param id 要删除的数据id
|
|
|
+ * @returns {object}
|
|
|
+ */
|
|
|
+ async delete(id: string): Promise<string> {
|
|
|
+ if (!id)
|
|
|
+ throw new ServiceError('缺少数据信息', FrameworkErrorEnum.NEED_PARAMS);
|
|
|
+ await this.model.deleteOne({ _id: id });
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 多删除
|
|
|
+ * @param filter 要删除的数据查询条件
|
|
|
+ * @returns {object}
|
|
|
+ */
|
|
|
+ async deleteMany(filter: object = {}): Promise<string> {
|
|
|
+ const keys = Object.keys(filter);
|
|
|
+ if (keys.length <= 0)
|
|
|
+ throw new ServiceError(
|
|
|
+ '暂不提供清库服务',
|
|
|
+ FrameworkErrorEnum.SERVICE_FAULT
|
|
|
+ );
|
|
|
+ await this.model.deleteMany(filter);
|
|
|
+ return 'ok';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单创建
|
|
|
+ * @param body 要创建的数据内容
|
|
|
+ * @returns {object}
|
|
|
+ */
|
|
|
+ async create(body: object): Promise<object> {
|
|
|
+ const data = await this.model.create(body);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 多创建
|
|
|
+ * @param body 要创建的多个数据
|
|
|
+ * @returns {object[]}
|
|
|
+ */
|
|
|
+ async createMany(body: object[]): Promise<object> {
|
|
|
+ const data = await this.model.insertMany(body);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+}
|