'use strict'; const TABLE_NAME = 'wms_after_sale'; 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: '主键', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '货主ID', }, after_sale_no: { type: Sequelize.STRING(50), allowNull: false, comment: '售后单号,唯一,生成规则:AS+YYYYMMDD+6位序号', }, type: { type: Sequelize.STRING(20), allowNull: false, comment: '售后类型:order/receiving/entry/stockout', }, sub_type: { type: Sequelize.STRING(50), allowNull: false, comment: '售后子类型,与 type 组成二级分类', }, status: { type: Sequelize.STRING(20), allowNull: false, defaultValue: 'pending', comment: '售后状态:pending/processing/completed/closed/cancelled', }, priority: { type: Sequelize.INTEGER(11), allowNull: false, defaultValue: 0, comment: '优先级,数值越大越优先(0=普通,10=紧急)', }, title: { type: Sequelize.STRING(200), allowNull: false, comment: '售后标题/简要描述', }, description: { type: Sequelize.TEXT, allowNull: true, defaultValue: null, comment: '售后详细描述', }, creator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '创建人账户ID', }, role_group: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, comment: '当前处理角色组:customer_service/warehouse/finance', }, inbox_message_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '关联站内信消息ID(wms_inbox_message.id)', }, resolution: { type: Sequelize.TEXT, allowNull: true, defaultValue: null, comment: '最终处理结果说明', }, resolved_at: { type: Sequelize.DATE, allowNull: true, defaultValue: null, comment: '完结时间(状态变为 completed 时写入)', }, claim_needed: { type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 0, comment: '仓库理赔标记:0=不需要,1=需要', }, claim_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: true, defaultValue: null, comment: '仓库理赔金额,单位:元', }, claim_status: { type: Sequelize.STRING(20), allowNull: true, defaultValue: null, comment: '仓库理赔状态:pending/reviewing/approved/paid/monthly/rejected', }, carrier_claim_needed: { type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 0, comment: '运方理赔标记:0=不需要,1=需要', }, carrier_claim_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: true, defaultValue: null, comment: '运方理赔金额,单位:元', }, carrier_claim_status: { type: Sequelize.STRING(20), allowNull: true, defaultValue: null, comment: '运方理赔状态:pending/reviewing/approved/paid/monthly/rejected', }, waybill_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '运单ID(关联 wms_order_express.id)', }, waybill_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, comment: '运单号(冗余)', }, order_delivery_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '发货订单ID(关联 wms_order_delivery.id)', }, order_delivery_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, comment: '发货订单号(冗余)', }, entry_order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '入库单ID(关联 wms_entry_order.id)', }, entry_order_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, comment: '入库单号(冗余)', }, order_stockout_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '出库单ID(关联 wms_order_stockout.id)', }, order_stockout_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, comment: '出库单号(冗余)', }, receiving_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '收货单ID(关联 wms_receiving.id)', }, receiving_no: { type: Sequelize.STRING(50), allowNull: true, defaultValue: null, 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, defaultValue: null, comment: '软删除时间', }, }); // 唯一索引 — 售后单号 await queryInterface.addIndex(TABLE_NAME, ['after_sale_no'], { unique: true, name: 'uk_after_sale_no', }); // 仓库+状态查询(最高频) await queryInterface.addIndex(TABLE_NAME, ['warehouse_id', 'status', 'deleted_at'], { name: 'idx_warehouse_status', }); // 仓库+类型筛选 await queryInterface.addIndex(TABLE_NAME, ['warehouse_id', 'type', 'deleted_at'], { name: 'idx_warehouse_type', }); // 仓库+类型+子类型筛选 await queryInterface.addIndex(TABLE_NAME, ['warehouse_id', 'type', 'sub_type', 'deleted_at'], { name: 'idx_type_subtype', }); // 货主查询 await queryInterface.addIndex(TABLE_NAME, ['store_id', 'deleted_at'], { name: 'idx_store_id', }); // 创建人查询 await queryInterface.addIndex(TABLE_NAME, ['creator_id', 'deleted_at'], { name: 'idx_creator_id', }); // 角色组+状态查询 await queryInterface.addIndex(TABLE_NAME, ['role_group', 'status', 'deleted_at'], { name: 'idx_role_group', }); // 仓库+时间排序分页 await queryInterface.addIndex(TABLE_NAME, ['warehouse_id', 'created_at'], { name: 'idx_created_at', }); // 运单反查 await queryInterface.addIndex(TABLE_NAME, ['waybill_id'], { name: 'idx_waybill_id', }); // 发货订单反查 await queryInterface.addIndex(TABLE_NAME, ['order_delivery_id'], { name: 'idx_order_delivery_id', }); // 入库单反查 await queryInterface.addIndex(TABLE_NAME, ['entry_order_id'], { name: 'idx_entry_order_id', }); // 出库单反查 await queryInterface.addIndex(TABLE_NAME, ['order_stockout_id'], { name: 'idx_order_stockout_id', }); // 收货单反查 await queryInterface.addIndex(TABLE_NAME, ['receiving_id'], { name: 'idx_receiving_id', }); // 运方理赔状态筛选 await queryInterface.addIndex(TABLE_NAME, ['carrier_claim_status'], { name: 'idx_carrier_claim_status', }); // 站内信消息反查 await queryInterface.addIndex(TABLE_NAME, ['inbox_message_id'], { name: 'idx_inbox_message_id', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };