oms/backend/database/migrations/20261103000200-create-operation-logs.js
liufumingren a9cca6aa6f feat(log): 新增操作日志(店铺商品/系统商品/组合商品),并修正明细口径
操作日志:
- 新增 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>
2026-11-03 03:43:30 +08:00

46 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 操作日志表。
*
* 记录「店铺商品 / 系统商品 / 组合商品」的关键操作,用于在页面下方展示操作记录:
* 何时被采集进系统、何时绑定/解绑、谁操作的、来源是界面还是自动同步。
*
* 设计要点:
* - target_type + target_id 指向被操作的对象(三类对象共用一张表,避免三张表结构重复)
* - action 为机器可读动作码,title 为给人看的完整描述(列表直接展示,无需前端再拼)
* - detail 存前后值等明细(JSON),便于追溯「改了什么」
* - operator_name 冗余存档,账号改名后历史记录仍保留当时的操作人名称
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('operation_logs', {
id: { type: Sequelize.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
target_type: { type: Sequelize.STRING(32), allowNull: false, comment: '对象类型:shop_product 店铺商品 / product 系统商品 / combo_product 组合商品' },
target_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '对象ID' },
action: { type: Sequelize.STRING(32), allowNull: false, comment: '动作码:create 新增 / update 修改 / bind 绑定 / unbind 解绑 / remove 移出 / offline 下架 / delete 删除' },
title: { type: Sequelize.STRING(255), allowNull: false, comment: '操作说明(直接展示给用户)' },
detail: { type: Sequelize.JSON, allowNull: true, comment: '操作明细(前后值等)' },
source: { type: Sequelize.STRING(20), allowNull: false, defaultValue: 'ui', comment: '来源:ui 界面操作 / sync 自动同步 / system 系统任务' },
operator_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '操作人用户ID(自动同步为空)' },
operator_name: { type: Sequelize.STRING(50), allowNull: true, comment: '操作人名称(冗余存档)' },
created_at: { type: Sequelize.DATE, allowNull: true, comment: '创建时间' },
updated_at: { type: Sequelize.DATE, allowNull: true, comment: '更新时间' },
});
await queryInterface.addIndex('operation_logs', [ 'principal_id', 'target_type', 'target_id', 'id' ], {
name: 'idx_operation_logs_target',
});
await queryInterface.addIndex('operation_logs', [ 'principal_id', 'created_at' ], {
name: 'idx_operation_logs_principal_time',
});
},
async down(queryInterface) {
await queryInterface.removeIndex('operation_logs', 'idx_operation_logs_principal_time');
await queryInterface.removeIndex('operation_logs', 'idx_operation_logs_target');
await queryInterface.dropTable('operation_logs');
},
};