|
@@ -4,6 +4,7 @@ import * as toqm from 'typeorm';
|
|
|
import { get, isNull, isObject, isUndefined, pick } from 'lodash';
|
|
|
import { App, Inject } from '@midwayjs/core';
|
|
|
import { Opera } from './dbOpera';
|
|
|
+import { OperaService } from '../service/log/opera.service';
|
|
|
|
|
|
export interface QueryOpera {
|
|
|
column: string;
|
|
@@ -11,12 +12,16 @@ export interface QueryOpera {
|
|
|
/**采用数组的形式,依次嵌套操作 */
|
|
|
opera: Opera[];
|
|
|
}
|
|
|
-
|
|
|
+/**
|
|
|
+ * create,udpate,delete: 创建,修改,删除都会记录日志,所以需要用BaseService的直接操作方法,要不不会产生日志
|
|
|
+ */
|
|
|
export abstract class BaseService<T> {
|
|
|
@App()
|
|
|
app: Application;
|
|
|
@Inject()
|
|
|
ctx: Context;
|
|
|
+ @Inject()
|
|
|
+ operaLogsService: OperaService;
|
|
|
abstract model: Repository<T>;
|
|
|
|
|
|
/**
|
|
@@ -127,6 +132,9 @@ export abstract class BaseService<T> {
|
|
|
// 设置 创建数据的人
|
|
|
if (this.ctx.user) Object.assign(data, { data_owner: this.ctx.user.id });
|
|
|
const result = await this.model.insert(data);
|
|
|
+ const mn = this.model.metadata.tableName;
|
|
|
+ // 添加,没有原数据,只有新数据
|
|
|
+ await this.operaLogsService.toMakeLogs(null, { [mn]: [data] });
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -148,12 +156,25 @@ export abstract class BaseService<T> {
|
|
|
if (isNull(val) || isUndefined(val)) continue;
|
|
|
updateData[column] = val;
|
|
|
}
|
|
|
+ // 找到原数据
|
|
|
+ const origin_data = await this.model.find({ where });
|
|
|
const result = await this.model.update(where, updateData);
|
|
|
+ // 修改成新的数据,result只是结果,需要重新查一遍
|
|
|
+ const new_data = await this.model.find({ where });
|
|
|
+ const mn = this.model.metadata.tableName;
|
|
|
+ // 日志
|
|
|
+ await this.operaLogsService.toMakeLogs({ [mn]: origin_data }, { [mn]: new_data });
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**删除,单删多删都行,假删除,把删除的数据移到指定删除表中 */
|
|
|
- async delete(query: object) {
|
|
|
- return 'in delete';
|
|
|
+ async delete(where: object) {
|
|
|
+ // 删除前,先查出来数据
|
|
|
+ const origin_data = await this.model.find({ where });
|
|
|
+ const result = await this.model.delete(where);
|
|
|
+ const mn = this.model.metadata.tableName;
|
|
|
+ // 日志,只有原数据没有新数据
|
|
|
+ await this.operaLogsService.toMakeLogs({ [mn]: origin_data });
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|