'use strict'; const TABLE_MAIN = 'wms_after_sale_work_order'; const TABLE_STEP = 'wms_after_sale_work_order_step'; module.exports = { up: async (queryInterface, Sequelize) => { // 先删子表(步骤表),再删主表(工单表) await queryInterface.dropTable(TABLE_STEP); await queryInterface.dropTable(TABLE_MAIN); }, down: async (queryInterface, Sequelize) => { // ========== 1. 重建主表:wms_after_sale_work_order ========== await queryInterface.createTable(TABLE_MAIN, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, work_order_no: { type: Sequelize.STRING(50), allowNull: false, comment: '售后工单号,唯一', }, business_type: { type: Sequelize.STRING(50), allowNull: false, comment: '关联业务类型', }, business_sub_type: { type: Sequelize.STRING(50), allowNull: false, comment: '关联业务子类型', }, business_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '关联业务单据主键ID(多态关联)', }, business_no: { type: Sequelize.STRING(100), allowNull: true, comment: '关联业务单据号(冗余)', }, direction: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '记账方向:0=未确定 1=入库 2=出库', }, status: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '工单状态:0=待处理 1=处理中 2=已完成 3=已关闭 9=已取消', }, priority: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 50, comment: '优先级:1~100,数字越大优先级越高', }, title: { type: Sequelize.STRING(200), allowNull: false, comment: '工单标题/摘要', }, description: { type: Sequelize.TEXT, allowNull: true, comment: '问题描述', }, creator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '创建人ID,关联 account.id;0 表示 AI 自动接待', }, creator_snapshot: { type: Sequelize.TEXT, allowNull: true, comment: '创建人信息快照(JSON格式)', }, assignee_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '当前处理人ID,关联 account.id', }, parent_work_order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '父工单ID,自关联本表;非空时表示子工单', }, current_step: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '当前进度,值为当前正在处理的 step_order 序号', }, claim_needed: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '是否需要理赔:0=不需要 1=需要', }, claim_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: true, defaultValue: 0.00, comment: '理赔金额(元)', }, claim_status: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '理赔状态:0=未理赔 1=已理赔(单笔结清) 2=月结', }, resolution: { type: Sequelize.TEXT, allowNull: true, comment: '处理结果说明', }, resolved_at: { type: Sequelize.DATE, allowNull: true, comment: '解决时间', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }); await queryInterface.addIndex(TABLE_MAIN, ['work_order_no'], { unique: true, name: 'uk_work_order_no' }); await queryInterface.addIndex(TABLE_MAIN, ['warehouse_id'], { name: 'idx_warehouse_id' }); await queryInterface.addIndex(TABLE_MAIN, ['store_id'], { name: 'idx_store_id' }); await queryInterface.addIndex(TABLE_MAIN, ['business_type', 'business_sub_type', 'business_id'], { name: 'idx_business_type_subtype_id' }); await queryInterface.addIndex(TABLE_MAIN, ['status'], { name: 'idx_status' }); await queryInterface.addIndex(TABLE_MAIN, ['assignee_id'], { name: 'idx_assignee_id' }); await queryInterface.addIndex(TABLE_MAIN, ['parent_work_order_id'], { name: 'idx_parent_work_order_id' }); await queryInterface.addIndex(TABLE_MAIN, ['priority'], { name: 'idx_priority' }); await queryInterface.addIndex(TABLE_MAIN, ['claim_status'], { name: 'idx_claim_status' }); await queryInterface.addIndex(TABLE_MAIN, ['created_at'], { name: 'idx_created_at' }); await queryInterface.addIndex(TABLE_MAIN, ['deleted_at'], { name: 'idx_deleted_at' }); // ========== 2. 重建子表:wms_after_sale_work_order_step ========== await queryInterface.createTable(TABLE_STEP, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, work_order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '工单ID,关联 wms_after_sale_work_order.id', }, step_order: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '步骤序号(从1开始递增)', }, step_name: { type: Sequelize.STRING(100), allowNull: false, comment: '步骤名称', }, step_status: { type: Sequelize.TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '步骤状态:0=待开始 1=进行中 2=已完成 3=已跳过', }, operator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '操作员工ID,关联 account.id', }, operator_snapshot: { type: Sequelize.TEXT, allowNull: true, comment: '操作人信息快照(JSON格式)', }, action: { type: Sequelize.STRING(100), allowNull: true, comment: '操作动作描述', }, remark: { type: Sequelize.TEXT, allowNull: true, comment: '处理备注', }, started_at: { type: Sequelize.DATE, allowNull: true, comment: '步骤开始时间', }, completed_at: { type: Sequelize.DATE, allowNull: true, comment: '步骤完成时间', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }); await queryInterface.addIndex(TABLE_STEP, ['work_order_id'], { name: 'idx_work_order_id' }); await queryInterface.addIndex(TABLE_STEP, ['operator_id'], { name: 'idx_operator_id' }); await queryInterface.addIndex(TABLE_STEP, ['deleted_at'], { name: 'idx_deleted_at' }); await queryInterface.addIndex(TABLE_STEP, ['work_order_id', 'step_order', 'deleted_at'], { unique: true, name: 'uk_work_order_step' }); }, };