'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} 创建出的日志(失败或参数不全时返回 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} list 日志参数数组(结构同 record) * @param {Object} [options] { transaction, source } * @return {Promise} 写入条数 */ 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} { 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;