feat(stock.check): 移除 all 盘点范围并迁移为 location/goods

- 模型 type 默认值 all -> location,注释同步为 location/store/goods

- 迁移 20261027010000:有商品明细的 all 任务归 goods(保留原 mode),其余归 location 且 mode=blind;列默认值与注释同步更新

- 补充盘点集成测试与分区编码唯一性集成测试(默认跳过,需 DISPATCH_FACTORY_TEST_MYSQL=1 才连接本地开发库)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-10-27 03:09:16 +08:00
parent 7414a8f2a1
commit c02f2f0d56
4 changed files with 8500 additions and 2 deletions

View File

@ -33,8 +33,8 @@ module.exports = app => {
type: {
type: STRING(20),
allowNull: false,
defaultValue: 'all',
comment: '盘点范围类型:all=全盘,store=按货主,goods=按商品,location=按库位',
defaultValue: 'location',
comment: '盘点范围类型:location=按库位(未传库位时自动全盘),store=按货主,goods=按商品',
},
mode: {
type: STRING(20),

View File

@ -0,0 +1,53 @@
'use strict';
/**
* 移除盘点范围类型 all(与 location 语义重复:两者都按库位盘点、都强制盲盘、
* 明细都是 scope_type='location';all 唯一的额外能力「未传库位时自动拉全仓库启用库位」
* 已合并进 location):
*
* wms_stock_check:
* 1. 存量数据:
* - 存在商品明细行(scope_type='goods')的 type='all' 任务 = 2026-09-30 改造前的
* 「全仓按商品」口径,改为 type='goods'(mode 保持原值,改造前商品口径允许明盘);
* - 其余 type='all' 任务统一改为 'location',mode 归一到 'blind'(按库位盘点一律盲盘)。
* 2. 列定义:默认值 all → location,comment 去掉 all。
*
* 发布前核查 SQL(确认两类任务各有多少):
* SELECT c.type, d.scope_type, COUNT(DISTINCT c.id)
* FROM wms_stock_check c LEFT JOIN wms_stock_check_detail d ON d.check_id = c.id
* WHERE c.type = 'all' GROUP BY c.type, d.scope_type;
*/
const TABLE_NAME = 'wms_stock_check';
module.exports = {
up: async queryInterface => {
// 改造前的「全仓按商品」任务(明细为商品行)按真实语义归入 goods,保留原 mode
await queryInterface.sequelize.query(`
UPDATE \`${TABLE_NAME}\` c
JOIN (
SELECT DISTINCT check_id FROM wms_stock_check_detail WHERE scope_type = 'goods'
) d ON d.check_id = c.id
SET c.\`type\` = 'goods'
WHERE c.\`type\` = 'all'
`);
// 其余(库位口径,含无明细的空任务)统一并入 location,并按库位口径归一为盲盘
await queryInterface.sequelize.query(
`UPDATE \`${TABLE_NAME}\` SET \`type\` = 'location', \`mode\` = 'blind' WHERE \`type\` = 'all'`
);
await queryInterface.sequelize.query(`
ALTER TABLE \`${TABLE_NAME}\`
MODIFY COLUMN \`type\` VARCHAR(20) NOT NULL DEFAULT 'location'
COMMENT '盘点范围类型:location=按库位(未传库位时自动全盘),store=按货主,goods=按商品'
`);
},
down: async queryInterface => {
// 数据不可回滚(all 已并入 location/goods,无法区分原值),仅恢复列定义
await queryInterface.sequelize.query(`
ALTER TABLE \`${TABLE_NAME}\`
MODIFY COLUMN \`type\` VARCHAR(20) NOT NULL DEFAULT 'all'
COMMENT '盘点范围类型:all=全盘,store=按货主,goods=按商品,location=按库位'
`);
},
};

View File

@ -0,0 +1,130 @@
'use strict';
/**
* 分区编码唯一性(真实库集成测试)。
*
* 覆盖:同仓库同 zone_code 只允许一条未归档分区(归档分区不占用编码)。
* 背景:uk_warehouse_zone_code 含 deleted_at,未删除行 deleted_at 为 NULL 不参与去重,
* 同 zone_code 并存会导致其下库位 space_code 重复(如 E02-06-01-01 多条),
* 盘点/库存按编码定位会命中历史行。
*
* 注意:本文件会启动 egg-mock app,需单独运行(与其它同样启动 app 的用例在同一 mocha 进程内
* 会触发 egg loader "can't overwrite property" 冲突,为仓库既有约束,如 return_received 系列)。
*/
const assert = require('node:assert/strict');
const mm = require('egg-mock');
const REAL_DB_ENABLED = process.env.DISPATCH_FACTORY_TEST_MYSQL === '1';
const runner = REAL_DB_ENABLED ? describe : describe.skip;
const TEST_WAREHOUSE_ID = 100000;
runner('test/app/service/systems/location_zone.zoneCodeUnique.test.js(分区编码唯一性)', function() {
this.timeout(120000);
let app;
let ctx;
const createdZoneIds = [];
before(async () => {
app = mm.app({
baseDir: process.cwd(),
env: 'local',
clean: false,
cache: false,
});
await app.ready();
ctx = app.createAnonymousContext();
ctx.user = { id: 4, store_id: TEST_WAREHOUSE_ID, employees_name: '阿猫' };
});
after(async () => {
for (const zoneId of createdZoneIds) {
await ctx.model.LocationZone.destroy({ where: { id: zoneId }, force: true });
}
await app.close();
});
afterEach(() => {
mm.restore();
});
/**
* 构造创建仓库类型分区的入参(warehouse 类型无需父元素,编码必填)。
*
* @param {string} zoneCode 分区编码(1-5 位大写字母/数字)。
* @return {Object} createZone 入参。
*/
function buildWarehouseZonePayload(zoneCode) {
return {
zone_type: 'warehouse',
zone_name: `测试仓_${zoneCode}`,
zone_code: zoneCode,
parent_id: 0,
x: 0,
y: 0,
z: 0,
length: 100,
width: 100,
height: 100,
};
}
/**
* 生成当前仓库未占用的随机编码(1-5 位大写字母/数字)。
*
* @return {string} 分区编码。
*/
function uniqueZoneCode() {
const suffix = String(Date.now()).slice(-4);
return `Z${suffix}`.slice(0, 5).toUpperCase();
}
it('同仓库同 zone_code 重复创建报错;归档后编码可复用', async () => {
const zoneCode = uniqueZoneCode();
const created = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
assert.equal(created.success, true);
const firstZoneId = Number(created.data.id);
assert.ok(firstZoneId > 0, '前置:分区应创建成功');
createdZoneIds.push(firstZoneId);
// 同编码再次创建 → 拒绝
await assert.rejects(async () => {
await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
}, error => /分区编码已存在/.test(error.message));
// 编码格式校验(沿用规则:1-5 位大写字母/数字)
await assert.rejects(async () => {
await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload('bad-code'));
}, error => /编码必须是1-5位大写字母或数字/.test(error.message));
// 归档(软删除)后同编码可再次创建:归档行不占用编码
await ctx.model.LocationZone.destroy({ where: { id: firstZoneId } });
const recreated = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(zoneCode));
assert.equal(recreated.success, true);
assert.notEqual(Number(recreated.data.id), firstZoneId, '归档后新建应为新关系');
createdZoneIds.push(Number(recreated.data.id));
});
it('更新分区编码:改为已占用编码报错,改回自身编码不报错', async () => {
const codeA = uniqueZoneCode();
const zoneA = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(codeA));
createdZoneIds.push(Number(zoneA.data.id));
// 另建一个分区占位
const codeB = `Y${String(Date.now()).slice(-4)}`.slice(0, 5).toUpperCase();
const zoneB = await ctx.service.systems.locationZone.createZone(buildWarehouseZonePayload(codeB));
createdZoneIds.push(Number(zoneB.data.id));
// 把 A 改成 B 的编码 → 报错
await assert.rejects(async () => {
await ctx.service.systems.locationZone.updateZone(Number(zoneA.data.id), { zone_code: codeB });
}, error => /分区编码已存在/.test(error.message));
// 改回自身编码 → 正常
const updated = await ctx.service.systems.locationZone.updateZone(Number(zoneA.data.id), { zone_code: codeA });
assert.ok(updated, '自身编码不应被判为冲突');
});
});

File diff suppressed because it is too large Load Diff