131 lines
4.0 KiB
JavaScript
131 lines
4.0 KiB
JavaScript
// app/service/operationLog.js
|
||
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
class operationLogService extends Service {
|
||
/**
|
||
* 通用操作日志记录方法
|
||
* @param {Object} params 日志参数
|
||
* @param {string} params.businessType 业务类型: product, order, stock, wave, express, storage
|
||
* @param {string} params.businessId 业务ID
|
||
* @param {string} params.operationCode 操作编码
|
||
* @param {Object} params.operator 操作人信息
|
||
* @param {string} params.operator.id 操作人ID
|
||
* @param {string} params.operator.name 操作人名称
|
||
* @param {string} params.operationAction 操作动作: CREATE, UPDATE, DELETE, OTHER
|
||
* @param {string} params.changeSummary 变更描述
|
||
* @param {Object} params.keyChanges 关键变更字段
|
||
* @param {Object} params.extras 额外参数
|
||
* @param {string} params.extras.externalLogType 外部日志类型
|
||
* @param {number} params.extras.externalLogId 外部日志ID
|
||
* @param {number} params.extras.warehouseId 仓库ID
|
||
*/
|
||
async createOperationLog(params) {
|
||
const { ctx } = this;
|
||
const {
|
||
businessType,
|
||
businessId,
|
||
operationCode,
|
||
operator = {},
|
||
beforeChanges = null,
|
||
extras = {},
|
||
} = params;
|
||
try {
|
||
const logData = {
|
||
business_type: businessType,
|
||
business_id: String(businessId),
|
||
operation_code: operationCode,
|
||
operator_id: operator?.id || null,
|
||
operator_name: operator?.name || null,
|
||
external_log_type: extras.externalLogType || 'NONE',
|
||
external_log_id: extras.externalLogId || null,
|
||
warehouse_id: extras.warehouseId || null,
|
||
before_changes: beforeChanges,
|
||
platform_code: ctx.user?.platform_code || 'SYSTEM',
|
||
};
|
||
|
||
const result = await ctx.model.OperationLog.create(logData);
|
||
ctx.logger.info(`[OperationLog] 记录操作日志成功,ID: ${result.id}, Business: ${businessType}/${businessId}`);
|
||
return result;
|
||
} catch (error) {
|
||
ctx.logger.error(`[OperationLog] 记录操作日志失败: ${error.message}`, error);
|
||
// 生产环境建议添加降级处理,如写入文件或发送到监控系统
|
||
return null;
|
||
}
|
||
}
|
||
|
||
async queryStockLogs(query) {
|
||
|
||
try {
|
||
const { ctx, app } = this;
|
||
const {
|
||
id,
|
||
warehouseId,
|
||
page = 1,
|
||
pageSize = 20,
|
||
} = query;
|
||
const where = {};
|
||
where.business_type = 'stock';
|
||
if (!id) {
|
||
throw new Error('id不能为空');
|
||
} else {
|
||
where.business_id = id;
|
||
}
|
||
if (!warehouseId) {
|
||
throw new Error('仓库id不能为空');
|
||
} else {
|
||
where.warehouse_id = warehouseId;
|
||
}
|
||
|
||
const offset = (page - 1) * pageSize;
|
||
|
||
const result = await ctx.model.OperationLog.findAndCountAll({
|
||
where,
|
||
order: [[ 'created_at', 'DESC' ]],
|
||
offset,
|
||
attributes: [ 'id', 'operation_code', 'operator_name', 'created_at', 'external_log_type', 'external_log_id', 'platform_code' ],
|
||
limit: pageSize,
|
||
});
|
||
|
||
return {
|
||
list: result.rows.map(item => {
|
||
const item_tmp = JSON.parse(JSON.stringify(item));
|
||
item_tmp.operation_name = app.getOperationCodeName(item.operation_code);
|
||
item_tmp.platform_name = app.getPlatformName(item.platform_code);
|
||
item_tmp.created_at = new Date(item_tmp.created_at).format();
|
||
return item_tmp;
|
||
}),
|
||
pagination: {
|
||
page,
|
||
pageSize,
|
||
total: result.count,
|
||
totalPages: Math.ceil(result.count / pageSize),
|
||
},
|
||
};
|
||
} catch (e) {
|
||
console.log(e);
|
||
throw e;
|
||
}
|
||
}
|
||
async queryOrderLogs() {
|
||
|
||
}
|
||
async getMessageDetail(data) {
|
||
const { id, type } = data;
|
||
const { ctx } = this;
|
||
const warehouseId = ctx.user.store_id;
|
||
let LogModel = null;
|
||
switch (type) {
|
||
case 'QIMEN':
|
||
// code
|
||
LogModel = ctx.model.QimenLog;
|
||
break;
|
||
}
|
||
const result = await LogModel.findOne({ where: { id } });
|
||
return result;
|
||
}
|
||
}
|
||
|
||
module.exports = operationLogService;
|