From 30f2177f69a062e958ab01304d74f04fc08f6b95 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 29 Sep 2026 03:50:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(stock):=20=E6=96=B0=E5=A2=9E=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E7=9B=98=E7=82=B9=E4=B8=BB=E8=A1=A8=E4=B8=8E=E7=9B=98?= =?UTF-8?q?=E7=82=B9=E6=98=8E=E7=BB=86=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/model/stock_check.js | 141 +++++++++++++++++ app/model/stock_check_detail.js | 142 +++++++++++++++++ .../20260929100000-create-wms-stock-check.js | 147 ++++++++++++++++++ ...929101000-create-wms-stock-check-detail.js | 143 +++++++++++++++++ 4 files changed, 573 insertions(+) create mode 100644 app/model/stock_check.js create mode 100644 app/model/stock_check_detail.js create mode 100644 database/migrations/20260929100000-create-wms-stock-check.js create mode 100644 database/migrations/20260929101000-create-wms-stock-check-detail.js diff --git a/app/model/stock_check.js b/app/model/stock_check.js new file mode 100644 index 0000000..3b4d6cc --- /dev/null +++ b/app/model/stock_check.js @@ -0,0 +1,141 @@ +'use strict'; + +/** + * 库存盘点主表模型。 + * @param {import('egg').Application} app Egg 应用实例。 + * @return {import('sequelize').ModelCtor} Sequelize 模型。 + */ +module.exports = app => { + const { BIGINT, STRING, INTEGER, DATE, JSON: JSON_TYPE } = app.Sequelize; + + const StockCheck = app.model.define('wms_stock_check', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + comment: '主键', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID(数据隔离维度)', + }, + check_no: { + type: STRING(50), + allowNull: false, + comment: '盘点单号,唯一,生成规则:PD+YYYYMMDD+6位序号', + }, + title: { + type: STRING(200), + allowNull: true, + comment: '盘点名称/主题,非必填', + }, + type: { + type: STRING(20), + allowNull: false, + defaultValue: 'all', + comment: '盘点范围类型:all=全盘,store=按货主,goods=按商品,location=按库位', + }, + mode: { + type: STRING(20), + allowNull: false, + defaultValue: 'open', + comment: '盘点方式:open=明盘(可见账面数量),blind=盲盘(不可见账面数量)', + }, + store_id: { + type: BIGINT(20).UNSIGNED, + allowNull: true, + comment: '货主ID,按货主盘点时必填(type=store)', + }, + scope_snapshot: { + type: JSON_TYPE, + allowNull: true, + comment: '盘点范围条件快照(JSON),如商品ID列表/库位ID列表,供盘点对比与追溯', + }, + status: { + type: STRING(20), + allowNull: false, + defaultValue: 'pending', + comment: '盘点状态:pending=待盘点,in_progress=盘点中,checked=已盘待确认,confirmed=已确认,completed=已完成,cancelled=已取消', + }, + detail_status: { + type: STRING(30), + allowNull: false, + defaultValue: 'none_checked', + comment: '明细盘点进度:none_checked=未盘,partial_checked=部分已盘,all_checked=全部已盘', + }, + check_date: { + type: DATE, + allowNull: false, + comment: '盘点日期', + }, + start_time: { + type: DATE, + allowNull: true, + comment: '实际开始时间', + }, + end_time: { + type: DATE, + allowNull: true, + comment: '实际结束时间', + }, + creator_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '创建人账户ID', + }, + creator_name: { + type: STRING(50), + allowNull: true, + comment: '创建人名称快照', + }, + total_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + comment: '明细行数(盘点任务总行数)', + }, + checked_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + comment: '已盘行数', + }, + diff_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + comment: '差异行数(账面与实盘不一致的行数)', + }, + remark: { + type: STRING(500), + allowNull: true, + comment: '备注', + }, + }, { + tableName: 'wms_stock_check', + comment: '库存盘点主表', + timestamps: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + paranoid: false, + indexes: [ + { name: 'uk_check_no', unique: true, fields: [ 'check_no' ] }, + { name: 'idx_warehouse_check_date', fields: [ 'warehouse_id', 'check_date' ] }, + { name: 'idx_warehouse_status', fields: [ 'warehouse_id', 'status' ] }, + { name: 'idx_store_id', fields: [ 'store_id' ] }, + { name: 'idx_creator_id', fields: [ 'creator_id' ] }, + ], + }); + + StockCheck.associate = function() { + const { StockCheckDetail } = app.model; + // 一个盘点单对应多条盘点明细 + StockCheck.hasMany(StockCheckDetail, { + foreignKey: 'check_id', + as: 'details', + }); + }; + + return StockCheck; +}; diff --git a/app/model/stock_check_detail.js b/app/model/stock_check_detail.js new file mode 100644 index 0000000..3df1e01 --- /dev/null +++ b/app/model/stock_check_detail.js @@ -0,0 +1,142 @@ +'use strict'; + +/** + * 库存盘点明细表模型。 + * @param {import('egg').Application} app Egg 应用实例。 + * @return {import('sequelize').ModelCtor} Sequelize 模型。 + */ +module.exports = app => { + const { BIGINT, STRING, INTEGER, DATE, TINYINT } = app.Sequelize; + + const StockCheckDetail = app.model.define('wms_stock_check_detail', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + 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: '备注(如差异原因说明)', + }, + }, { + tableName: 'wms_stock_check_detail', + comment: '库存盘点明细表', + timestamps: true, + createdAt: 'created_at', + updatedAt: 'updated_at', + paranoid: false, + indexes: [ + { name: 'idx_check_id', fields: [ 'check_id' ] }, + { name: 'idx_warehouse_stock', fields: [ 'warehouse_id', 'stock_id' ] }, + { name: 'idx_stock_id', fields: [ 'stock_id' ] }, + { name: 'idx_location_id', fields: [ 'location_id' ] }, + { name: 'idx_is_diff', fields: [ 'is_diff' ] }, + ], + }); + + StockCheckDetail.associate = function() { + const { StockCheck, StockWarehouse } = app.model; + // 关联盘点主表 + StockCheckDetail.belongsTo(StockCheck, { + foreignKey: 'check_id', + as: 'check', + }); + // 关联库存商品 + StockCheckDetail.belongsTo(StockWarehouse, { + foreignKey: 'stock_id', + as: 'stockWarehouse', + }); + }; + + return StockCheckDetail; +}; diff --git a/database/migrations/20260929100000-create-wms-stock-check.js b/database/migrations/20260929100000-create-wms-stock-check.js new file mode 100644 index 0000000..60d19a9 --- /dev/null +++ b/database/migrations/20260929100000-create-wms-stock-check.js @@ -0,0 +1,147 @@ +'use strict'; + +/** + * 创建库存盘点主表 wms_stock_check。 + * + * 字段:仓库隔离 warehouse_id、盘点单号 check_no(PD+YYYYMMDD+6位序号,唯一)、 + * 盘点名称 title、盘点范围类型 type(all全盘/store按货主/goods按商品/location按库位)、 + * 盘点方式 mode(open明盘/blind盲盘)、货主 store_id、范围条件快照 scope_snapshot(JSON)、 + * 状态 status(pending/in_progress/checked/confirmed/completed/cancelled)、 + * 明细盘点状态 detail_status(none_checked/partial_checked/all_checked)、 + * 盘点日期 check_date、起止时间 start_time/end_time、创建人 creator_id/creator_name、 + * 汇总 total_items/checked_items/diff_items、备注 remark。 + * + * 索引: + * uk_check_no(单号唯一) + * idx_warehouse_check_date(仓库+盘点日期隔离查询) + * idx_warehouse_status(仓库+状态) + * idx_store_id / idx_creator_id + */ + +const TABLE_NAME = 'wms_stock_check'; + +module.exports = { + // eslint-disable-next-line no-unused-vars + async up(queryInterface, Sequelize) { + const { BIGINT, STRING, INTEGER, DATE, JSON: JSON_TYPE } = Sequelize; + await queryInterface.createTable(TABLE_NAME, { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID(数据隔离维度)', + }, + check_no: { + type: STRING(50), + allowNull: false, + comment: '盘点单号,唯一,生成规则:PD+YYYYMMDD+6位序号', + }, + title: { + type: STRING(200), + allowNull: true, + comment: '盘点名称/主题,非必填', + }, + type: { + type: STRING(20), + allowNull: false, + defaultValue: 'all', + comment: '盘点范围类型:all=全盘,store=按货主,goods=按商品,location=按库位', + }, + mode: { + type: STRING(20), + allowNull: false, + defaultValue: 'open', + comment: '盘点方式:open=明盘(可见账面数量),blind=盲盘(不可见账面数量)', + }, + store_id: { + type: BIGINT(20).UNSIGNED, + allowNull: true, + comment: '货主ID,按货主盘点时必填(type=store)', + }, + scope_snapshot: { + type: JSON_TYPE, + allowNull: true, + comment: '盘点范围条件快照(JSON),如商品ID列表/库位ID列表,供盘点对比与追溯', + }, + status: { + type: STRING(20), + allowNull: false, + defaultValue: 'pending', + comment: '盘点状态:pending=待盘点,in_progress=盘点中,checked=已盘待确认,confirmed=已确认,completed=已完成,cancelled=已取消', + }, + detail_status: { + type: STRING(30), + allowNull: false, + defaultValue: 'none_checked', + comment: '明细盘点进度:none_checked=未盘,partial_checked=部分已盘,all_checked=全部已盘', + }, + check_date: { + type: DATE, + allowNull: false, + comment: '盘点日期', + }, + start_time: { + type: DATE, + allowNull: true, + comment: '实际开始时间', + }, + end_time: { + type: DATE, + allowNull: true, + comment: '实际结束时间', + }, + creator_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '创建人账户ID', + }, + creator_name: { + type: STRING(50), + allowNull: true, + comment: '创建人名称快照', + }, + total_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + comment: '明细行数(盘点任务总行数)', + }, + checked_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + comment: '已盘行数', + }, + diff_items: { + type: INTEGER(11), + allowNull: false, + defaultValue: 0, + 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_no' ], { unique: true, name: 'uk_check_no' }); + await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'check_date' ], { name: 'idx_warehouse_check_date' }); + await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'status' ], { name: 'idx_warehouse_status' }); + await queryInterface.addIndex(TABLE_NAME, [ 'store_id' ], { name: 'idx_store_id' }); + await queryInterface.addIndex(TABLE_NAME, [ 'creator_id' ], { name: 'idx_creator_id' }); + }, + + // eslint-disable-next-line no-unused-vars + async down(queryInterface, Sequelize) { + await queryInterface.dropTable(TABLE_NAME); + }, +}; diff --git a/database/migrations/20260929101000-create-wms-stock-check-detail.js b/database/migrations/20260929101000-create-wms-stock-check-detail.js new file mode 100644 index 0000000..400d5d1 --- /dev/null +++ b/database/migrations/20260929101000-create-wms-stock-check-detail.js @@ -0,0 +1,143 @@ +'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); + }, +};