fix(pack): 打包人未知账号写NULL并去重,避免多人打包触发唯一索引冲突中断打包流程
This commit is contained in:
parent
59eb736740
commit
33a92bdc8d
@ -17,8 +17,8 @@ module.exports = app => {
|
||||
},
|
||||
account_id: {
|
||||
type: BIGINT(20).UNSIGNED,
|
||||
allowNull: false,
|
||||
comment: '账号ID,关联 wms_account.id',
|
||||
allowNull: true,
|
||||
comment: '账号ID,关联 wms_account.id;未知账号为 NULL(唯一索引对 NULL 不生效,允许多名无账号打包人)',
|
||||
},
|
||||
account_name: {
|
||||
type: STRING(128),
|
||||
|
||||
@ -684,13 +684,30 @@ class WorkbenchService extends Service {
|
||||
const users = Array.isArray(usersParam) ? usersParam : [];
|
||||
let operatorText = '';
|
||||
if (users.length > 0) {
|
||||
const packerRecords = users.map((user, idx) => ({
|
||||
pack_task_id: packTask.id,
|
||||
account_id: user.wms_account_id != null ? parseInt(user.wms_account_id) || 0 : 0,
|
||||
account_name: user.real_name || '',
|
||||
seq: idx,
|
||||
}));
|
||||
await ctx.model.PackTaskPacker.bulkCreate(packerRecords);
|
||||
// account_id 未知时写 null(表列允许 NULL):唯一索引 uix_pack_task_account 对
|
||||
// NULL 不生效,避免多人打包(account_id 全为 0)时违反唯一约束导致主流程中断。
|
||||
// 真实账号仍按 account_id 去重;无账号时按姓名去重。
|
||||
const seenPackerKeys = new Set();
|
||||
const packerRecords = [];
|
||||
for (const user of users) {
|
||||
const accountIdRaw = user.wms_account_id != null
|
||||
? user.wms_account_id
|
||||
: (user.id != null ? user.id : null);
|
||||
const accountId = accountIdRaw != null ? (parseInt(accountIdRaw) || null) : null;
|
||||
const accountName = user.real_name || '';
|
||||
const dedupeKey = accountId !== null ? `id:${accountId}` : `name:${accountName}`;
|
||||
if (seenPackerKeys.has(dedupeKey)) continue;
|
||||
seenPackerKeys.add(dedupeKey);
|
||||
packerRecords.push({
|
||||
pack_task_id: packTask.id,
|
||||
account_id: accountId,
|
||||
account_name: accountName,
|
||||
seq: packerRecords.length,
|
||||
});
|
||||
}
|
||||
if (packerRecords.length > 0) {
|
||||
await ctx.model.PackTaskPacker.bulkCreate(packerRecords);
|
||||
}
|
||||
operatorText = users.map(u => u.real_name || '').filter(Boolean).join(',');
|
||||
// 回写 operator_text 到打包任务
|
||||
await packTask.update({ operator_text: operatorText });
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
'use strict';
|
||||
|
||||
const TABLE_NAME = 'wms_pack_task_packer';
|
||||
|
||||
/**
|
||||
* 允许 account_id 为 NULL。
|
||||
* 背景:打包任务写入多名打包人时,PDA 上传的 wms_account_id 可能为 null,
|
||||
* 旧代码统一兜底为 0,多人场景下多条 (pack_task_id, account_id=0) 违反
|
||||
* 唯一索引 uix_pack_task_account,导致 bulkCreate 抛 UniqueConstraintError,
|
||||
* 进而中断 _packOrder 后续步骤(OSD 下发、操作日志)。
|
||||
* MySQL 唯一索引对 NULL 不生效,故未知账号改为写 NULL。
|
||||
*/
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
|
||||
const col = tableInfo.account_id;
|
||||
if (!col) {
|
||||
throw new Error(`[Migration] ${TABLE_NAME}.account_id 不存在`);
|
||||
}
|
||||
if (col.allowNull === false) {
|
||||
await queryInterface.changeColumn(TABLE_NAME, 'account_id', {
|
||||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||||
allowNull: true,
|
||||
comment: '账号ID,关联 wms_account.id;未知账号为 NULL',
|
||||
});
|
||||
console.log(`[Migration] ${TABLE_NAME}.account_id 已改为允许 NULL`);
|
||||
} else {
|
||||
console.log(`[Migration] ${TABLE_NAME}.account_id 已允许 NULL,跳过`);
|
||||
}
|
||||
},
|
||||
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
|
||||
const col = tableInfo.account_id;
|
||||
if (!col) return;
|
||||
|
||||
if (col.allowNull === true) {
|
||||
// 回滚前把 NULL 归位为 0,避免改回 NOT NULL 失败
|
||||
await queryInterface.sequelize.query(
|
||||
`UPDATE ${TABLE_NAME} SET account_id = 0 WHERE account_id IS NULL`
|
||||
);
|
||||
await queryInterface.changeColumn(TABLE_NAME, 'account_id', {
|
||||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||||
allowNull: false,
|
||||
comment: '账号ID,关联 wms_account.id',
|
||||
});
|
||||
console.log(`[Migration] ${TABLE_NAME}.account_id 已恢复为 NOT NULL`);
|
||||
}
|
||||
},
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user