'use strict'; const TABLE_NAME = 'wms_after_sale_work_order'; module.exports = { up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { 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_NAME, ['work_order_no'], { unique: true, name: 'uk_work_order_no', }); // 仓库隔离查询 await queryInterface.addIndex(TABLE_NAME, ['warehouse_id'], { name: 'idx_warehouse_id', }); // 货主隔离查询 await queryInterface.addIndex(TABLE_NAME, ['store_id'], { name: 'idx_store_id', }); // 按业务类型+子类型+单据ID反查工单 await queryInterface.addIndex(TABLE_NAME, ['business_type', 'business_sub_type', 'business_id'], { name: 'idx_business_type_subtype_id', }); // 按状态筛选 await queryInterface.addIndex(TABLE_NAME, ['status'], { name: 'idx_status', }); // 按处理人查询 await queryInterface.addIndex(TABLE_NAME, ['assignee_id'], { name: 'idx_assignee_id', }); // 按父工单查子工单 await queryInterface.addIndex(TABLE_NAME, ['parent_work_order_id'], { name: 'idx_parent_work_order_id', }); // 按优先级排序 await queryInterface.addIndex(TABLE_NAME, ['priority'], { name: 'idx_priority', }); // 按理赔状态筛选 await queryInterface.addIndex(TABLE_NAME, ['claim_status'], { name: 'idx_claim_status', }); // 按时间排序 await queryInterface.addIndex(TABLE_NAME, ['created_at'], { name: 'idx_created_at', }); // 软删除查询优化 await queryInterface.addIndex(TABLE_NAME, ['deleted_at'], { name: 'idx_deleted_at', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };