fix: custom_barcode 唯一约束改为仓库级别,修复 batchUpdateSpaceInfo 等条码检查跨仓库问题

This commit is contained in:
liufumingren 2026-07-10 18:10:11 +08:00
parent a1f5c5d532
commit fddd142427
5 changed files with 53 additions and 23 deletions

View File

@ -209,7 +209,7 @@ module.exports = app => {
unique: true,
fields: [ 'warehouse_id', 'space_code', 'deleted_at' ],
},
{ name: 'uk_custom_barcode', unique: true, fields: [ 'custom_barcode' ] },
{ name: 'uk_warehouse_custom_barcode', unique: true, fields: [ 'warehouse_id', 'custom_barcode', 'deleted_at' ] },
{ fields: [ 'warehouse_id', 'zone_id' ] },
{ fields: [ 'location_type_id' ] },
{ fields: [ 'space_status' ] },

View File

@ -145,7 +145,7 @@ class warehouseService extends MainService {
if (!barcode) {
return this.fail('条码不能为空');
}
const data = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), barcode);
const data = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), barcode, this.ctx.user.store_id);
return this.success('获取成功', { data, code: 200 });
} catch (error) {
return this.fail(error.message);
@ -409,7 +409,7 @@ class warehouseService extends MainService {
throw new Error('仓位不存在');
}
if (data.custom_barcode) {
const { isUnique } = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), data.custom_barcode);
const { isUnique } = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), data.custom_barcode, ctx.user.store_id);
if (!isUnique) {
throw new Error('自定义条码已存在');
}
@ -651,6 +651,7 @@ class warehouseService extends MainService {
if (requestedBarcodes.length > 0) {
const conflictSpace = await ctx.model.LocationSpace.findOne({
where: {
warehouse_id: warehouseId,
deleted_at: null,
id: {
[Op.notIn]: targetIds,

View File

@ -48,10 +48,13 @@ class LocationSpaceService extends Service {
ctx.throw(400, '仓位编码已存在');
}
// 检查条码唯一性
// 检查条码唯一性(仓库内唯一)
if (data.custom_barcode) {
const barcodeExists = await ctx.model.LocationSpace.findOne({
where: { custom_barcode: data.custom_barcode },
where: {
warehouse_id: data.warehouse_id,
custom_barcode: data.custom_barcode,
},
});
if (barcodeExists) {
@ -103,10 +106,11 @@ class LocationSpaceService extends Service {
ctx.throw(400, `以下仓位编码已存在: ${codes}`);
}
// 检查条码重复
// 检查条码重复(仓库内唯一)
if (barcodes.length > 0) {
const duplicateBarcodes = await ctx.model.LocationSpace.findAll({
where: {
warehouse_id: warehouseId,
custom_barcode: { [ctx.app.Sequelize.Op.in]: barcodes },
},
attributes: [ 'custom_barcode' ],
@ -294,10 +298,11 @@ class LocationSpaceService extends Service {
}
}
// 如果修改条码,检查唯一性
// 如果修改条码,检查唯一性(仓库内唯一)
if (data.custom_barcode && data.custom_barcode !== locationSpace.custom_barcode) {
const exists = await ctx.model.LocationSpace.findOne({
where: {
warehouse_id: locationSpace.warehouse_id,
custom_barcode: data.custom_barcode,
id: { [ctx.app.Sequelize.Op.ne]: id },
},

View File

@ -1274,27 +1274,31 @@ class LocationZoneService extends Service {
throw error;
}
}
async checkBarcodeUnique(id, barcode) {
async checkBarcodeUnique(id, barcode, warehouseId) {
const { ctx, app } = this;
const { Op } = this.app.Sequelize || app.Sequelize;
try {
// 检查条码是否被其他仓位使用(排除当前仓位)
// 检查条码是否被其他仓位使用(排除当前仓位,仓库内唯一)
const whereConditions = [
{
id: {
[Op.ne]: id, // 排除当前仓位ID
},
},
{
[Op.or]: [
{ custom_barcode: barcode }, // 检查自定义条码
{ space_code: barcode }, // 检查仓位编码
],
},
{ deleted_at: null }, // 未删除的记录
];
if (warehouseId) {
whereConditions.push({ warehouse_id: warehouseId });
}
const space = await ctx.model.LocationSpace.findOne({
where: {
[Op.and]: [
{
id: {
[Op.ne]: id, // 排除当前仓位ID
},
},
{
[Op.or]: [
{ custom_barcode: barcode }, // 检查自定义条码
{ space_code: barcode }, // 检查仓位编码
],
},
{ deleted_at: null }, // 未删除的记录
],
[Op.and]: whereConditions,
},
attributes: [ 'id' ],
});

View File

@ -0,0 +1,20 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
// 将 custom_barcode 唯一索引从全局改为仓库级别
await queryInterface.removeIndex('wms_location_space', 'uk_custom_barcode');
await queryInterface.addIndex('wms_location_space', ['warehouse_id', 'custom_barcode', 'deleted_at'], {
name: 'uk_warehouse_custom_barcode',
unique: true,
});
},
async down(queryInterface, Sequelize) {
await queryInterface.removeIndex('wms_location_space', 'uk_warehouse_custom_barcode');
await queryInterface.addIndex('wms_location_space', ['custom_barcode'], {
name: 'uk_custom_barcode',
unique: true,
});
},
};