From fddd142427d0d420e89c6b2f0bb3a5f40dc289c7 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 10 Jul 2026 18:10:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20custom=5Fbarcode=20=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F=E6=94=B9=E4=B8=BA=E4=BB=93=E5=BA=93=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=EF=BC=8C=E4=BF=AE=E5=A4=8D=20batchUpdateSpaceInfo=20?= =?UTF-8?q?=E7=AD=89=E6=9D=A1=E7=A0=81=E6=A3=80=E6=9F=A5=E8=B7=A8=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/model/location_space.js | 2 +- app/service/systemMethods/warehouse.js | 5 +-- app/service/systems/location_space.js | 13 ++++--- app/service/systems/location_zone.js | 36 ++++++++++--------- ...-custom-barcode-unique-to-per-warehouse.js | 20 +++++++++++ 5 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 database/migrations/20260710000000-fix-custom-barcode-unique-to-per-warehouse.js diff --git a/app/model/location_space.js b/app/model/location_space.js index 875299a..1355d5b 100644 --- a/app/model/location_space.js +++ b/app/model/location_space.js @@ -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' ] }, diff --git a/app/service/systemMethods/warehouse.js b/app/service/systemMethods/warehouse.js index 7c5afc5..493a5e7 100644 --- a/app/service/systemMethods/warehouse.js +++ b/app/service/systemMethods/warehouse.js @@ -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, diff --git a/app/service/systems/location_space.js b/app/service/systems/location_space.js index 0a12e66..8cc4c1c 100644 --- a/app/service/systems/location_space.js +++ b/app/service/systems/location_space.js @@ -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 }, }, diff --git a/app/service/systems/location_zone.js b/app/service/systems/location_zone.js index 17fdd80..6d7515b 100644 --- a/app/service/systems/location_zone.js +++ b/app/service/systems/location_zone.js @@ -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' ], }); diff --git a/database/migrations/20260710000000-fix-custom-barcode-unique-to-per-warehouse.js b/database/migrations/20260710000000-fix-custom-barcode-unique-to-per-warehouse.js new file mode 100644 index 0000000..4f1004f --- /dev/null +++ b/database/migrations/20260710000000-fix-custom-barcode-unique-to-per-warehouse.js @@ -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, + }); + }, +};