'use strict'; /** * 创建库存盘点明细表 wms_stock_check_detail。 * * 字段:关联主表 check_id、仓库冗余 warehouse_id、库存商品 stock_id(关联 wms_stock_warehouse.id)、 * 库位 location_id/location_code(关联 wms_location_space.id + 编码快照)、 * 商品快照 item_code/item_name/spec、批次号 batch_no、账面数量 system_quantity、 * 实盘数量 check_quantity(可空=未盘)、差异数量 diff_quantity(=实盘-账面,正盘盈负盘亏)、 * 差异标记 is_diff、明细状态 status(unchecked/checked/confirmed)、 * 盘点人 checker_id/checker_name、盘点时间 checked_at、备注 remark。 * * 索引: * idx_check_id(按盘点单查明细,最高频) * idx_warehouse_stock(仓库+商品) * idx_stock_id(按商品反查) * idx_location_id(按库位反查) * idx_is_diff(差异行筛选) */ const TABLE_NAME = 'wms_stock_check_detail'; module.exports = { // eslint-disable-next-line no-unused-vars async up(queryInterface, Sequelize) { const { BIGINT, STRING, INTEGER, DATE, TINYINT } = Sequelize; await queryInterface.createTable(TABLE_NAME, { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键', }, check_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '关联盘点主表 wms_stock_check.id', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID(冗余,隔离查询)', }, stock_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '库存商品ID,关联 wms_stock_warehouse.id', }, location_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '库位ID,关联 wms_location_space.id;无库位商品为空', }, location_code: { type: STRING(50), allowNull: true, comment: '库位编码快照(space_code)', }, item_code: { type: STRING(50), allowNull: false, comment: '商品编码快照', }, item_name: { type: STRING(255), allowNull: true, comment: '商品名称快照', }, spec: { type: STRING(100), allowNull: true, comment: '规格快照(sku_property)', }, batch_no: { type: STRING(100), allowNull: true, comment: '批次号,批次管理商品时记录', }, system_quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '账面数量(生成盘点任务时的系统库存快照)', }, check_quantity: { type: INTEGER(12), allowNull: true, comment: '实盘数量;未盘点时为空', }, diff_quantity: { type: INTEGER(12), allowNull: true, comment: '差异数量 = 实盘数量 - 账面数量;正数盘盈,负数盘亏;未盘点时为空', }, is_diff: { type: TINYINT(1), allowNull: false, defaultValue: 0, comment: '是否有差异:1=有差异,0=无差异', }, status: { type: STRING(20), allowNull: false, defaultValue: 'unchecked', comment: '明细状态:unchecked=未盘,checked=已盘,confirmed=已确认', }, checker_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '盘点人账户ID', }, checker_name: { type: STRING(50), allowNull: true, comment: '盘点人名称快照', }, checked_at: { type: DATE, allowNull: true, comment: '盘点时间', }, remark: { type: STRING(500), allowNull: true, comment: '备注(如差异原因说明)', }, created_at: { type: DATE, allowNull: false }, updated_at: { type: DATE, allowNull: false }, }); await queryInterface.addIndex(TABLE_NAME, [ 'check_id' ], { name: 'idx_check_id' }); await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'stock_id' ], { name: 'idx_warehouse_stock' }); await queryInterface.addIndex(TABLE_NAME, [ 'stock_id' ], { name: 'idx_stock_id' }); await queryInterface.addIndex(TABLE_NAME, [ 'location_id' ], { name: 'idx_location_id' }); await queryInterface.addIndex(TABLE_NAME, [ 'is_diff' ], { name: 'idx_is_diff' }); }, // eslint-disable-next-line no-unused-vars async down(queryInterface, Sequelize) { await queryInterface.dropTable(TABLE_NAME); }, };