'use strict'; /** * 发货单表补充「站内字段」(OMS 自用,不参与对外报文): * - 统计:item_total 商品总数(明细 plan_qty 合计)、item_kind 商品种类数 * —— 对应 WMS 发货单 items_total / items_count,口径对齐出入库单 * - 状态流转: * audit_by / audit_time 审核(待处理 1 → 已审核 2) * void_at / void_by / void_reason 作废(已审核 2 → 已作废 5),与出入库单约定一致 * - 主体隔离:principal_id / shop_id / warehouse_id 已在建表时具备,此处仅补组合索引 * (列表默认按主体 + 状态过滤;warehouse_id/shop_id 的归属校验在控制器实现) */ module.exports = { async up(queryInterface, Sequelize) { const cols = await queryInterface.describeTable('order_deliveries'); if (!cols.item_total) { await queryInterface.addColumn('order_deliveries', 'item_total', { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '商品总数(明细计划数量合计)', }); } if (!cols.item_kind) { await queryInterface.addColumn('order_deliveries', 'item_kind', { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: '商品种类数(不重复商品数)', }); } if (!cols.audit_by) { await queryInterface.addColumn('order_deliveries', 'audit_by', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '审核人ID(关联 users,状态流转:待处理→已审核时写入)', }); } if (!cols.audit_time) { await queryInterface.addColumn('order_deliveries', 'audit_time', { type: Sequelize.DATE, allowNull: true, comment: '审核时间', }); } if (!cols.void_at) { await queryInterface.addColumn('order_deliveries', 'void_at', { type: Sequelize.DATE, allowNull: true, comment: '作废时间(状态 5=已作废)', }); } if (!cols.void_by) { await queryInterface.addColumn('order_deliveries', 'void_by', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '作废人ID(关联 users)', }); } if (!cols.void_reason) { await queryInterface.addColumn('order_deliveries', 'void_reason', { type: Sequelize.STRING(255), allowNull: true, comment: '作废原因', }); } const indexes = await queryInterface.showIndex('order_deliveries').catch(() => []); const hasIndex = name => Array.isArray(indexes) && indexes.some(idx => idx.name === name); if (!hasIndex('idx_od_principal_status')) { await queryInterface.addIndex('order_deliveries', { fields: [ 'principal_id', 'status' ], name: 'idx_od_principal_status', }); } }, async down(queryInterface) { const cols = await queryInterface.describeTable('order_deliveries'); for (const c of [ 'void_reason', 'void_by', 'void_at', 'audit_time', 'audit_by', 'item_kind', 'item_total' ]) { if (cols[c]) await queryInterface.removeColumn('order_deliveries', c); } await queryInterface.removeIndex('order_deliveries', 'idx_od_principal_status').catch(() => {}); }, };