From c7a6a0559873963320c4995dfd9df5ecc3222c18 Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Wed, 6 Aug 2025 11:25:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=99=BA=E6=85=A7=E8=B6=85=E5=B8=82):?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E9=80=82?= =?UTF-8?q?=E9=85=8D,=E5=A2=9E=E5=8A=A0=E8=A6=86=E7=9B=96=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/erpspu/SmErpSpuImportNewDto.java | 19 + .../service/impl/SmErpSpuServiceImpl.java | 340 ++++++++++++++++-- 2 files changed, 333 insertions(+), 26 deletions(-) diff --git a/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/dto/erpspu/SmErpSpuImportNewDto.java b/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/dto/erpspu/SmErpSpuImportNewDto.java index 2966d0c1d..f41b08681 100644 --- a/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/dto/erpspu/SmErpSpuImportNewDto.java +++ b/pjcloud-supermarket/pjcloud-supermarket-api/src/main/java/com/bsd/pjcloud/supermarket/dto/erpspu/SmErpSpuImportNewDto.java @@ -36,6 +36,9 @@ public class SmErpSpuImportNewDto { @ExcelProperty("装数") private Integer packageNum; // 内装数量 + @ExcelProperty("口味") + private String taste; // 口味 + @ExcelProperty("进价") private Double purchasePrice; // 进价 @@ -63,6 +66,22 @@ public class SmErpSpuImportNewDto { @ExcelProperty("商品状态") private String status; // 商品状态(0-正常,1-停售) + // 辅助条码字段 + @ExcelProperty("辅助条码1") + private String auxiliaryBarcode1; // 辅助条码1 + + @ExcelProperty("辅助条码2") + private String auxiliaryBarcode2; // 辅助条码2 + + @ExcelProperty("辅助条码3") + private String auxiliaryBarcode3; // 辅助条码3 + + @ExcelProperty("辅助条码4") + private String auxiliaryBarcode4; // 辅助条码4 + + @ExcelProperty("辅助条码5") + private String auxiliaryBarcode5; // 辅助条码5 + // 注释品牌字段,暂时不使用 // @ExcelProperty("品牌") // private String brand; // 品牌 diff --git a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java index c1f46860c..4044bbdc7 100644 --- a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java +++ b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/service/impl/SmErpSpuServiceImpl.java @@ -932,6 +932,245 @@ public class SmErpSpuServiceImpl extends ServiceImpl errorReasons = new ArrayList<>(); + + try { + // 1. 基础数据校验 + if (StringUtils.isBlank(dto.getProductCode())) { + errorReasons.add("货号不能为空"); + } + if (StringUtils.isBlank(dto.getProductName())) { + errorReasons.add("商品名称不能为空"); + } + if (StringUtils.isBlank(dto.getUnit())) { + errorReasons.add("单位不能为空"); + } + if (dto.getPackageNum() == null || dto.getPackageNum() <= 0) { + errorReasons.add("装数必须大于0"); + } + + if (!errorReasons.isEmpty()) { + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + continue; + } + + // 2. 处理分类 + Long categoryId = null; + if (StringUtils.isNotBlank(dto.getCategoryName())) { + categoryId = resolveCategory(dto.getCategoryName(), errorReasons, rowNumber, dto.getProductCode(), + SecurityUtils.getUser().getDeptId(), null); + if (categoryId == null) { + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + continue; + } + } + + // 3. 处理货商 + Long supplierId = null; + if (StringUtils.isNotBlank(dto.getSupplierName())) { + supplierId = resolveSupplierId(dto.getSupplierName(), errorReasons, rowNumber, dto.getProductCode()); + if (supplierId == null) { + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + continue; + } + } + + // 4. 检查商品是否已存在 + LambdaQueryWrapper existingSpuWrapper = new LambdaQueryWrapper<>(); + existingSpuWrapper.eq(SmErpSpuEntity::getProductCode, dto.getProductCode()); + SmErpSpuEntity existingSpu = this.getOne(existingSpuWrapper); + + SmErpSpuEntity spu; + if (existingSpu != null) { + // 商品已存在,记录警告但不更新(保护线上数据) + errorReasons.add("商品已存在,跳过导入以避免覆盖线上数据。货号:" + dto.getProductCode() + ",商品名称:" + existingSpu.getProductName()); + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + continue; + + // 如果需要更新模式,可以取消注释下面的代码 + /* + spu = existingSpu; + spu.setProductName(dto.getProductName()); + spu.setSpec(dto.getSpec()); + spu.setOrigin(dto.getOrigin()); + spu.setBarCode(dto.getBarCode()); + spu.setPurchasePrice(dto.getPurchasePrice() != null ? + BigDecimal.valueOf(dto.getPurchasePrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setRetailPrice(dto.getRetailPrice() != null ? + BigDecimal.valueOf(dto.getRetailPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setPromotionPrice(dto.getPromotionPrice() != null ? + BigDecimal.valueOf(dto.getPromotionPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setCostPrice(dto.getCostPrice() != null ? + BigDecimal.valueOf(dto.getCostPrice()).multiply(BigDecimal.valueOf(100)).doubleValue() : 0.0); + spu.setProductNature("电子称称重商品".equals(dto.getProductNature()) ? "2" : "1"); + spu.setStatus("停销".equals(dto.getStatus()) ? "1" : "0"); + if (categoryId != null) { + spu.setCategoryId(categoryId); + } + if (supplierId != null) { + spu.setSupplierId(supplierId); + } + this.updateById(spu); + */ + } else { + // 创建新商品 + spu = new SmErpSpuEntity(); + spu.setProductCode(dto.getProductCode()); + spu.setProductName(dto.getProductName()); + spu.setSpec(dto.getSpec()); + spu.setOrigin(dto.getOrigin()); + spu.setBarCode(dto.getBarCode()); + spu.setPurchasePrice(dto.getPurchasePrice() != null ? + BigDecimal.valueOf(dto.getPurchasePrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setRetailPrice(dto.getRetailPrice() != null ? + BigDecimal.valueOf(dto.getRetailPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setPromotionPrice(dto.getPromotionPrice() != null ? + BigDecimal.valueOf(dto.getPromotionPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + spu.setCostPrice(dto.getCostPrice() != null ? + BigDecimal.valueOf(dto.getCostPrice()).multiply(BigDecimal.valueOf(100)).doubleValue() : 0.0); + spu.setProductNature("电子称称重商品".equals(dto.getProductNature()) ? "2" : "1"); + spu.setStatus("停销".equals(dto.getStatus()) ? "1" : "0"); + spu.setDeptId(SecurityUtils.getUser().getDeptId()); + if (categoryId != null) { + spu.setCategoryId(categoryId); + } + if (supplierId != null) { + spu.setSupplierId(supplierId); + } + this.save(spu); + } + + // 5. 处理包装单位 + SmErpUnitEntity unit = new SmErpUnitEntity(); + unit.setSpuId(spu.getId()); + unit.setUnit(dto.getUnit()); + unit.setPackageNum(dto.getPackageNum()); + unit.setIsBase(true); // 默认为基础单位 + unit.setBarCode(dto.getBarCode()); // 包装单位的条码来自主条码 + unit.setTaste(dto.getTaste()); // 设置口味 + unit.setPurchasePrice(dto.getPurchasePrice() != null ? + BigDecimal.valueOf(dto.getPurchasePrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + unit.setRetailPrice(dto.getRetailPrice() != null ? + BigDecimal.valueOf(dto.getRetailPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + unit.setPromotionPrice(dto.getPromotionPrice() != null ? + BigDecimal.valueOf(dto.getPromotionPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + unit.setCostPrice(dto.getCostPrice() != null ? + BigDecimal.valueOf(dto.getCostPrice()).multiply(BigDecimal.valueOf(100)).intValue() : 0); + + // 检查单位是否已存在(根据单位名称和口味) + LambdaQueryWrapper existingUnitWrapper = new LambdaQueryWrapper<>(); + existingUnitWrapper.eq(SmErpUnitEntity::getSpuId, spu.getId()) + .eq(SmErpUnitEntity::getUnit, dto.getUnit()) + .eq(SmErpUnitEntity::getTaste, dto.getTaste()); + SmErpUnitEntity existingUnit = smErpUnitService.getOne(existingUnitWrapper); + + if (existingUnit != null) { + // 包装单位已存在,记录警告但不更新(保护线上数据) + errorReasons.add("包装单位已存在,跳过导入以避免覆盖线上数据。单位:" + dto.getUnit() + ",口味:" + dto.getTaste()); + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + continue; + + // 如果需要更新模式,可以取消注释下面的代码 + /* + existingUnit.setPackageNum(dto.getPackageNum()); + existingUnit.setBarCode(dto.getBarCode()); // 更新条码 + existingUnit.setPurchasePrice(unit.getPurchasePrice()); + existingUnit.setRetailPrice(unit.getRetailPrice()); + existingUnit.setPromotionPrice(unit.getPromotionPrice()); + existingUnit.setCostPrice(unit.getCostPrice()); + smErpUnitService.updateById(existingUnit); + */ + } else { + smErpUnitService.save(unit); + } + + // 6. 处理辅助条码(多个辅助条码) + List auxiliaryBarcodes = new ArrayList<>(); + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode1())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode1()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode2())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode2()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode3())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode3()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode4())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode4()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode5())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode5()); + } + + // 为每个辅助条码创建记录 + for (String auxiliaryBarcode : auxiliaryBarcodes) { + // 检查辅助条码是否已存在 + LambdaQueryWrapper existingBarcodeWrapper = new LambdaQueryWrapper<>(); + existingBarcodeWrapper.eq(SmErpAuxiliaryCodeEntity::getSpuId, spu.getId()) + .eq(SmErpAuxiliaryCodeEntity::getBarCode, auxiliaryBarcode); + SmErpAuxiliaryCodeEntity existingBarcode = smErpAuxiliaryCodeService.getOne(existingBarcodeWrapper); + + if (existingBarcode != null) { + // 辅助条码已存在,记录警告但不更新(保护线上数据) + errorReasons.add("辅助条码已存在,跳过导入以避免覆盖线上数据。条码:" + auxiliaryBarcode); + continue; + } else { + SmErpAuxiliaryCodeEntity auxiliaryCode = new SmErpAuxiliaryCodeEntity(); + auxiliaryCode.setSpuId(spu.getId()); + auxiliaryCode.setBarCode(auxiliaryBarcode); + auxiliaryCode.setUnitId(existingUnit != null ? existingUnit.getId() : unit.getId()); + smErpAuxiliaryCodeService.save(auxiliaryCode); + } + } + + // 7. 处理库存设置 + SmErpStockEntity stock = new SmErpStockEntity(); + stock.setErpSpuId(spu.getId()); + stock.setInventoryUpperLimit(dto.getInventoryUpperLimit() != null ? dto.getInventoryUpperLimit() : 1000); + stock.setInventoryLowerLimit(dto.getInventoryLowerLimit() != null ? dto.getInventoryLowerLimit() : 10); + stock.setBaseStock(dto.getStock() != null ? dto.getStock() : 0.0); + stock.setCurrentStock(dto.getStock() != null ? dto.getStock() : 0.0); + + // 检查库存记录是否已存在 + LambdaQueryWrapper existingStockWrapper = new LambdaQueryWrapper<>(); + existingStockWrapper.eq(SmErpStockEntity::getErpSpuId, spu.getId()); + SmErpStockEntity existingStock = smErpStockService.getOne(existingStockWrapper); + + if (existingStock != null) { + // 库存记录已存在,记录警告但不更新(保护线上数据) + errorReasons.add("库存记录已存在,跳过导入以避免覆盖线上数据"); + continue; + } else { + smErpStockService.save(stock); + } + + successList.add(spu); + successCount++; + + } catch (Exception e) { + errorReasons.add("处理失败: " + e.getMessage()); + errorList.add(createErrorInfo(rowNumber, dto.getProductCode(), errorReasons)); + } + } + + return buildImportResult(totalCount, successCount, errorList); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public R importErpSpuNewUpdate(List smErpSpuImportNewDtos, BindingResult bindingResult) { + if (CollectionUtils.isEmpty(smErpSpuImportNewDtos)) { + return R.failed("导入数据不能为空"); + } + + List> errorList = new ArrayList<>(); + List successList = new ArrayList<>(); + int totalCount = smErpSpuImportNewDtos.size(); + int successCount = 0; + for (int i = 0; i < smErpSpuImportNewDtos.size(); i++) { int rowNumber = i + 1; SmErpSpuImportNewDto dto = smErpSpuImportNewDtos.get(i); @@ -999,8 +1238,8 @@ public class SmErpSpuServiceImpl extends ServiceImpl existingUnitWrapper = new LambdaQueryWrapper<>(); existingUnitWrapper.eq(SmErpUnitEntity::getSpuId, spu.getId()) - .eq(SmErpUnitEntity::getUnit, dto.getUnit()); + .eq(SmErpUnitEntity::getUnit, dto.getUnit()) + .eq(SmErpUnitEntity::getTaste, dto.getTaste()); SmErpUnitEntity existingUnit = smErpUnitService.getOne(existingUnitWrapper); if (existingUnit != null) { + // 更新现有包装单位 existingUnit.setPackageNum(dto.getPackageNum()); + existingUnit.setBarCode(dto.getBarCode()); // 更新条码 existingUnit.setPurchasePrice(unit.getPurchasePrice()); existingUnit.setRetailPrice(unit.getRetailPrice()); existingUnit.setPromotionPrice(unit.getPromotionPrice()); @@ -1068,23 +1312,41 @@ public class SmErpSpuServiceImpl extends ServiceImpl auxiliaryBarcodes = new ArrayList<>(); + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode1())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode1()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode2())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode2()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode3())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode3()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode4())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode4()); + } + if (StringUtils.isNotBlank(dto.getAuxiliaryBarcode5())) { + auxiliaryBarcodes.add(dto.getAuxiliaryBarcode5()); + } + // 为每个辅助条码创建记录 + for (String auxiliaryBarcode : auxiliaryBarcodes) { // 检查辅助条码是否已存在 LambdaQueryWrapper existingBarcodeWrapper = new LambdaQueryWrapper<>(); existingBarcodeWrapper.eq(SmErpAuxiliaryCodeEntity::getSpuId, spu.getId()) - .eq(SmErpAuxiliaryCodeEntity::getBarCode, dto.getBarCode()); + .eq(SmErpAuxiliaryCodeEntity::getBarCode, auxiliaryBarcode); SmErpAuxiliaryCodeEntity existingBarcode = smErpAuxiliaryCodeService.getOne(existingBarcodeWrapper); if (existingBarcode != null) { + // 更新现有辅助条码 existingBarcode.setUnitId(existingUnit != null ? existingUnit.getId() : unit.getId()); smErpAuxiliaryCodeService.updateById(existingBarcode); } else { + SmErpAuxiliaryCodeEntity auxiliaryCode = new SmErpAuxiliaryCodeEntity(); + auxiliaryCode.setSpuId(spu.getId()); + auxiliaryCode.setBarCode(auxiliaryBarcode); + auxiliaryCode.setUnitId(existingUnit != null ? existingUnit.getId() : unit.getId()); smErpAuxiliaryCodeService.save(auxiliaryCode); } } @@ -1103,6 +1365,7 @@ public class SmErpSpuServiceImpl extends ServiceImpl unitAuxiliaryCodes = auxiliaryCodes.stream() + .filter(code -> unit.getId().equals(code.getUnitId())) + .collect(Collectors.toList()); + + // 设置辅助条码(最多5个) + if (unitAuxiliaryCodes.size() > 0) { + dto.setAuxiliaryBarcode1(unitAuxiliaryCodes.size() > 0 ? unitAuxiliaryCodes.get(0).getBarCode() : null); + } + if (unitAuxiliaryCodes.size() > 1) { + dto.setAuxiliaryBarcode2(unitAuxiliaryCodes.get(1).getBarCode()); + } + if (unitAuxiliaryCodes.size() > 2) { + dto.setAuxiliaryBarcode3(unitAuxiliaryCodes.get(2).getBarCode()); + } + if (unitAuxiliaryCodes.size() > 3) { + dto.setAuxiliaryBarcode4(unitAuxiliaryCodes.get(3).getBarCode()); + } + if (unitAuxiliaryCodes.size() > 4) { + dto.setAuxiliaryBarcode5(unitAuxiliaryCodes.get(4).getBarCode()); + } + exportList.add(dto); } } @@ -1264,12 +1552,12 @@ public class SmErpSpuServiceImpl extends ServiceImpl smErpSupplierEntityLambdaQueryWrapper = new LambdaQueryWrapper<>(); smErpSupplierEntityLambdaQueryWrapper.eq(SmErpSupplierEntity::getSupplierName, supplierName); // 1. 查询现有供应商(名称唯一) - SmErpSupplierEntity existSupplier = smErpSupplierMapper.selectListByScope(smErpSupplierEntityLambdaQueryWrapper + List existSuppliers = smErpSupplierMapper.selectListByScope(smErpSupplierEntityLambdaQueryWrapper ,DataScope.of() - ).get(0); - - if (existSupplier != null) { - return existSupplier.getSupplierId(); + ); + + if (CollectionUtils.isNotEmpty(existSuppliers)) { + return existSuppliers.get(0).getSupplierId(); } else { errorReasons.add("供应商不存在,请先导入'" + supplierName + "'该供应商"); return null; @@ -1292,11 +1580,11 @@ public class SmErpSpuServiceImpl extends ServiceImpl conflictSuppliers = smErpSupplierMapper.selectListByScope( new LambdaQueryWrapper() .eq(SmErpSupplierEntity::getSupplierName, supplierName),DataScope.of() - ).get(0); - return conflictSupplier != null ? conflictSupplier.getSupplierId() : null; + ); + return CollectionUtils.isNotEmpty(conflictSuppliers) ? conflictSuppliers.get(0).getSupplierId() : null; } catch (Exception e) { errorReasons.add("供应商处理异常: " + e.getMessage());