WMS_MAIN/database/migrations/20260929120000-split-stock-check-detail-create-record.js

185 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 拆分盘点明细表:明细表只保留盘点商品(stock_id + 商品快照),
* 盘点作业产生的库位/数量信息下沉到新建的 wms_stock_check_record(盘点作业记录表)。
*
* 变更内容:
* 1. wms_stock_check_detail 移除作业字段:
* location_id / location_code(库位信息)
* check_quantity / diff_quantity / is_diff(实盘数量与差异)
* status / checker_id / checker_name / checked_at(盘点状态与盘点人)
* 2. 新建 wms_stock_check_record(盘点作业记录,一个明细可在多个库位多次上报):
* check_id(关联主表,冗余)、warehouse_id(冗余隔离)、detail_id(关联明细)、
* stock_id(冗余,商品维度查询)、location_id(space_code 匹配时回填)、
* location_code(库位编码)、quantity(实盘数量)、checker_id / checker_name、
* checked_at(上报时间)、remark、created_at / updated_at。
*
* 索引(wms_stock_check_record):
* idx_check_id(按任务查记录)
* idx_detail_id(按明细查记录)
* idx_stock_id(按商品反查)
* idx_warehouse_location(仓库+库位)
*/
const DETAIL_TABLE_NAME = 'wms_stock_check_detail';
const RECORD_TABLE_NAME = 'wms_stock_check_record';
/** 明细表需要移除的作业字段 */
const DROP_COLUMNS = [
'location_id',
'location_code',
'check_quantity',
'diff_quantity',
'is_diff',
'status',
'checker_id',
'checker_name',
'checked_at',
];
module.exports = {
up: async (queryInterface, Sequelize) => {
const { BIGINT, STRING, INTEGER, DATE } = Sequelize;
// 1) 明细表移除作业字段
for (const columnName of DROP_COLUMNS) {
await queryInterface.removeColumn(DETAIL_TABLE_NAME, columnName);
}
// 2) 新建盘点作业记录表
const tables = await queryInterface.showAllTables();
const exists = tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === RECORD_TABLE_NAME);
if (!exists) {
await queryInterface.createTable(RECORD_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(冗余,隔离查询)',
},
detail_id: {
type: BIGINT(20).UNSIGNED,
allowNull: false,
comment: '关联盘点明细 wms_stock_check_detail.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: '库位编码(盘点上报的库位)',
},
quantity: {
type: INTEGER(12),
allowNull: false,
defaultValue: 0,
comment: '实盘数量(不小于0的整数)',
},
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(RECORD_TABLE_NAME, [ 'check_id' ], { name: 'idx_check_id' });
await queryInterface.addIndex(RECORD_TABLE_NAME, [ 'detail_id' ], { name: 'idx_detail_id' });
await queryInterface.addIndex(RECORD_TABLE_NAME, [ 'stock_id' ], { name: 'idx_stock_id' });
await queryInterface.addIndex(RECORD_TABLE_NAME, [ 'warehouse_id', 'location_code' ], { name: 'idx_warehouse_location' });
}
},
down: async (queryInterface, Sequelize) => {
const { BIGINT, STRING, INTEGER, DATE, TINYINT } = Sequelize;
// 删除记录表
await queryInterface.dropTable(RECORD_TABLE_NAME);
// 恢复明细表作业字段
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'location_id', {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '库位ID,关联 wms_location_space.id;无库位商品为空',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'location_code', {
type: STRING(50),
allowNull: true,
comment: '库位编码快照(space_code)',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'check_quantity', {
type: INTEGER(12),
allowNull: true,
comment: '实盘数量;未盘点时为空',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'diff_quantity', {
type: INTEGER(12),
allowNull: true,
comment: '差异数量 = 实盘数量 - 账面数量;正数盘盈,负数盘亏;未盘点时为空',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'is_diff', {
type: TINYINT(1),
allowNull: false,
defaultValue: 0,
comment: '是否有差异:1=有差异,0=无差异',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'status', {
type: STRING(20),
allowNull: false,
defaultValue: 'unchecked',
comment: '明细状态:unchecked=未盘,checked=已盘,confirmed=已确认',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'checker_id', {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '盘点人账户ID',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'checker_name', {
type: STRING(50),
allowNull: true,
comment: '盘点人名称快照',
});
await queryInterface.addColumn(DETAIL_TABLE_NAME, 'checked_at', {
type: DATE,
allowNull: true,
comment: '盘点时间',
});
},
};