fix(pack): 打包人未知账号写NULL并去重,避免多人打包触发唯一索引冲突中断打包流程
This commit is contained in:
parent
59eb736740
commit
33a92bdc8d
@ -17,8 +17,8 @@ module.exports = app => {
|
|||||||
},
|
},
|
||||||
account_id: {
|
account_id: {
|
||||||
type: BIGINT(20).UNSIGNED,
|
type: BIGINT(20).UNSIGNED,
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
comment: '账号ID,关联 wms_account.id',
|
comment: '账号ID,关联 wms_account.id;未知账号为 NULL(唯一索引对 NULL 不生效,允许多名无账号打包人)',
|
||||||
},
|
},
|
||||||
account_name: {
|
account_name: {
|
||||||
type: STRING(128),
|
type: STRING(128),
|
||||||
|
|||||||
@ -684,13 +684,30 @@ class WorkbenchService extends Service {
|
|||||||
const users = Array.isArray(usersParam) ? usersParam : [];
|
const users = Array.isArray(usersParam) ? usersParam : [];
|
||||||
let operatorText = '';
|
let operatorText = '';
|
||||||
if (users.length > 0) {
|
if (users.length > 0) {
|
||||||
const packerRecords = users.map((user, idx) => ({
|
// account_id 未知时写 null(表列允许 NULL):唯一索引 uix_pack_task_account 对
|
||||||
pack_task_id: packTask.id,
|
// NULL 不生效,避免多人打包(account_id 全为 0)时违反唯一约束导致主流程中断。
|
||||||
account_id: user.wms_account_id != null ? parseInt(user.wms_account_id) || 0 : 0,
|
// 真实账号仍按 account_id 去重;无账号时按姓名去重。
|
||||||
account_name: user.real_name || '',
|
const seenPackerKeys = new Set();
|
||||||
seq: idx,
|
const packerRecords = [];
|
||||||
}));
|
for (const user of users) {
|
||||||
await ctx.model.PackTaskPacker.bulkCreate(packerRecords);
|
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(',');
|
operatorText = users.map(u => u.real_name || '').filter(Boolean).join(',');
|
||||||
// 回写 operator_text 到打包任务
|
// 回写 operator_text 到打包任务
|
||||||
await packTask.update({ operator_text: operatorText });
|
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