193 lines
6.2 KiB
JavaScript
193 lines
6.2 KiB
JavaScript
// app/service/systems/operation_log.js
|
||
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
const OPERATION_LOG_ATTRIBUTES = [
|
||
'id',
|
||
'operation_code',
|
||
'operator_name',
|
||
'created_at',
|
||
'external_log_type',
|
||
'external_log_id',
|
||
'platform_code',
|
||
'message',
|
||
];
|
||
|
||
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
|
||
* @param {string} params.extras.message 日志信息
|
||
*
|
||
*/
|
||
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',
|
||
message: extras.message || null,
|
||
};
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 按业务类型查询操作日志分页数据。
|
||
* @param {Object} query 查询参数。
|
||
* @param {string|number} query.id 业务ID。
|
||
* @param {string|number} query.warehouseId 仓库ID。
|
||
* @param {number} [query.page=1] 页码。
|
||
* @param {number} [query.pageSize=20] 每页数量。
|
||
* @param {string} businessType 业务类型。
|
||
* @return {Promise<{list: Object[], pagination: Object}>} 分页结果。
|
||
*/
|
||
async queryLogsByBusinessType(query = {}, businessType) {
|
||
const { ctx, app } = this;
|
||
const requestQuery = query && typeof query === 'object' ? query : {};
|
||
const {
|
||
id,
|
||
warehouseId,
|
||
page = 1,
|
||
pageSize = 20,
|
||
} = requestQuery;
|
||
|
||
const where = {
|
||
business_type: businessType,
|
||
};
|
||
|
||
if (!id) {
|
||
throw new Error('id不能为空');
|
||
}
|
||
where.business_id = id;
|
||
|
||
if (!warehouseId) {
|
||
throw new Error('仓库id不能为空');
|
||
}
|
||
where.warehouse_id = warehouseId;
|
||
|
||
const currentPage = Number.parseInt(page, 10) || 1;
|
||
const currentPageSize = Number.parseInt(pageSize, 10) || 20;
|
||
const offset = (currentPage - 1) * currentPageSize;
|
||
|
||
const result = await ctx.model.OperationLog.findAndCountAll({
|
||
where,
|
||
order: [[ 'created_at', 'DESC' ]],
|
||
offset,
|
||
attributes: OPERATION_LOG_ATTRIBUTES,
|
||
limit: currentPageSize,
|
||
});
|
||
|
||
const rows = Array.isArray(result.rows) ? result.rows : [];
|
||
|
||
return {
|
||
list: rows.map(item => {
|
||
const itemTmp = JSON.parse(JSON.stringify(item));
|
||
itemTmp.operation_name = app.getOperationCodeName(item.operation_code);
|
||
itemTmp.platform_name = app.getPlatformName(item.platform_code);
|
||
itemTmp.created_at = app.formatDate(itemTmp.created_at);
|
||
return itemTmp;
|
||
}),
|
||
pagination: {
|
||
page: currentPage,
|
||
pageSize: currentPageSize,
|
||
total: result.count,
|
||
totalPages: Math.ceil(result.count / currentPageSize),
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 查询库存操作日志。
|
||
* @param {Object} query 查询参数。
|
||
* @param {string|number} query.id 库存业务ID。
|
||
* @param {string|number} query.warehouseId 仓库ID。
|
||
* @param {number} [query.page=1] 页码。
|
||
* @param {number} [query.pageSize=20] 每页数量。
|
||
* @return {Promise<{list: Object[], pagination: Object}>} 分页结果。
|
||
*/
|
||
async queryStockLogs(query = {}) {
|
||
return await this.queryLogsByBusinessType(query, 'stock');
|
||
}
|
||
|
||
/**
|
||
* 查询订单操作日志。
|
||
* @param {Object} query 查询参数。
|
||
* @param {string|number} query.id 订单ID。
|
||
* @param {string|number} query.warehouseId 仓库ID。
|
||
* @param {number} [query.page=1] 页码。
|
||
* @param {number} [query.pageSize=20] 每页数量。
|
||
* @return {Promise<{list: Object[], pagination: Object}>} 分页结果。
|
||
*/
|
||
async queryOrderLogs(query = {}) {
|
||
return await this.queryLogsByBusinessType(query, 'order');
|
||
}
|
||
|
||
/**
|
||
* 查询收货单操作日志。
|
||
* @param {Object} query 查询参数。
|
||
* @param {string|number} query.id 收货单ID。
|
||
* @param {string|number} query.warehouseId 仓库ID。
|
||
* @param {number} [query.page=1] 页码。
|
||
* @param {number} [query.pageSize=20] 每页数量。
|
||
* @return {Promise<{list: Object[], pagination: Object}>} 分页结果。
|
||
*/
|
||
async queryReceivingLogs(query = {}) {
|
||
return await this.queryLogsByBusinessType(query, 'receiving');
|
||
}
|
||
|
||
async getMessageDetail(data) {
|
||
const { id, type } = data;
|
||
const { ctx } = this;
|
||
let LogModel = null;
|
||
switch (type) {
|
||
case 'QIMEN':
|
||
LogModel = ctx.model.QimenLog;
|
||
break;
|
||
default:
|
||
throw new Error(`不支持的日志类型: ${type}`);
|
||
}
|
||
const result = await LogModel.findOne({ where: { id } });
|
||
return result;
|
||
}
|
||
}
|
||
|
||
module.exports = operationLogService;
|