操作日志: - 新增 operation_logs 表与 model/service/controller,接口 GET /api/operation-logs (按 target_type + target_id 分页查询,主体隔离,按时间倒序) - 店铺商品:采集进系统、平台已无此商品被移出/下架、绑定/解绑均留痕(含原绑定与受影响订单数) - 系统商品:新增 / 修改(记录变更字段与前后值)/ 删除 - 组合商品:新增 / 修改(含子商品构成前后值)/ 删除 - 日志为旁路能力:写入失败只告警,不影响业务操作 明细口径修正: - 已审核订单(待推送及之后)的明细在审核时已锁定,重算时按状态过滤, 重新拉单与改绑定都不再波及(原先会覆盖老表 order_items 的解析结果) - 组合商品构成/系统商品资料变更后,重建绑定方的待审核订单明细与商品摘要; 并修正重建时读不到同事务内未提交构成的问题(loadBindings/loadComboItems 传事务) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
130 lines
4.7 KiB
JavaScript
130 lines
4.7 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
const { resolveAccountName } = require('../lib/accountProfile');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/** 来源:界面操作 / 自动同步 / 系统任务 */
|
||
const SOURCES = {
|
||
UI: 'ui',
|
||
SYNC: 'sync',
|
||
SYSTEM: 'system',
|
||
};
|
||
|
||
/**
|
||
* 操作日志服务
|
||
*
|
||
* 统一记录三类对象(店铺商品 / 系统商品 / 组合商品)的关键操作,
|
||
* 并提供按对象分页查询,供页面底部的「操作记录」面板展示。
|
||
*
|
||
* 记录失败不影响主流程(日志是旁路能力,不能因为写日志失败让业务操作报错)。
|
||
*/
|
||
class OperationLogService extends Service {
|
||
/**
|
||
* 写入一条操作日志
|
||
* @param {Object} p 参数
|
||
* @param {string} p.targetType 对象类型:shop_product 店铺商品 / product 系统商品 / combo_product 组合商品
|
||
* @param {number} p.targetId 对象ID
|
||
* @param {string} p.action 动作码:create / update / bind / unbind / remove / offline / delete
|
||
* @param {string} p.title 操作说明(直接展示)
|
||
* @param {Object} [p.detail] 明细(前后值等)
|
||
* @param {string} [p.source] 来源,默认 ui
|
||
* @param {number} [p.principalId] 主体ID,默认取上下文
|
||
* @param {Object} [p.transaction] 事务
|
||
* @return {Promise<Object|null>} 创建出的日志(失败或参数不全时返回 null)
|
||
*/
|
||
async record(p = {}) {
|
||
const { ctx } = this;
|
||
const principalId = Number(p.principalId || ctx.state.principalId);
|
||
const targetId = Number(p.targetId);
|
||
if (!principalId || !targetId || !p.targetType || !p.action || !p.title) return null;
|
||
|
||
try {
|
||
const operatorId = Number(ctx.userId) || null;
|
||
const operatorName = operatorId
|
||
? (await resolveAccountName(ctx, operatorId, principalId)) || null
|
||
: null;
|
||
|
||
return await ctx.model.OperationLog.create({
|
||
principal_id: principalId,
|
||
target_type: p.targetType,
|
||
target_id: targetId,
|
||
action: p.action,
|
||
title: String(p.title).slice(0, 255),
|
||
detail: p.detail && typeof p.detail === 'object' ? p.detail : null,
|
||
source: p.source || SOURCES.UI,
|
||
operator_id: operatorId,
|
||
operator_name: operatorName,
|
||
}, { transaction: p.transaction });
|
||
} catch (err) {
|
||
ctx.logger.warn('[operation-log] 写入失败:%s', err.message);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量写入(自动同步时会一次新增/移除多行)
|
||
* @param {Array<Object>} list 日志参数数组(结构同 record)
|
||
* @param {Object} [options] { transaction, source }
|
||
* @return {Promise<number>} 写入条数
|
||
*/
|
||
async recordMany(list, options = {}) {
|
||
const { ctx } = this;
|
||
const rows = (list || []).filter(item => item && item.targetType && Number(item.targetId) && item.title);
|
||
if (!rows.length) return 0;
|
||
try {
|
||
const principalId = Number(ctx.state.principalId);
|
||
const operatorId = Number(ctx.userId) || null;
|
||
const operatorName = operatorId && principalId
|
||
? (await resolveAccountName(ctx, operatorId, principalId)) || null
|
||
: null;
|
||
|
||
const created = await ctx.model.OperationLog.bulkCreate(
|
||
rows.map(item => ({
|
||
principal_id: Number(item.principalId || principalId),
|
||
target_type: item.targetType,
|
||
target_id: Number(item.targetId),
|
||
action: item.action,
|
||
title: String(item.title).slice(0, 255),
|
||
detail: item.detail && typeof item.detail === 'object' ? item.detail : null,
|
||
source: item.source || options.source || SOURCES.SYNC,
|
||
operator_id: operatorId,
|
||
operator_name: operatorName,
|
||
})),
|
||
{ transaction: options.transaction }
|
||
);
|
||
return created.length;
|
||
} catch (err) {
|
||
ctx.logger.warn('[operation-log] 批量写入失败:%s', err.message);
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分页查询某个对象的操作记录
|
||
* @param {Object} p { targetType, targetId, page, pageSize, principalId }
|
||
* @return {Promise<Object>} { list, total, page, pageSize }
|
||
*/
|
||
async page(p = {}) {
|
||
const { ctx } = this;
|
||
const principalId = Number(p.principalId || ctx.state.principalId);
|
||
const targetId = Number(p.targetId);
|
||
const { page, pageSize, offset, limit } = parsePagination(p);
|
||
|
||
if (!principalId || !targetId || !p.targetType) {
|
||
return buildPageResult([], 0, { page, pageSize });
|
||
}
|
||
|
||
const { count, rows } = await ctx.model.OperationLog.findAndCountAll({
|
||
where: { principal_id: principalId, target_type: p.targetType, target_id: targetId },
|
||
order: [[ 'id', 'DESC' ]],
|
||
offset,
|
||
limit,
|
||
});
|
||
|
||
return buildPageResult(rows.map(row => row.get({ plain: true })), count, { page, pageSize });
|
||
}
|
||
}
|
||
|
||
module.exports = OperationLogService;
|