'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'); }, };