From 3cc5cf1925fa57ebe0a5a2174cd44ae803a8114f Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Wed, 20 Aug 2025 13:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=A3=E5=91=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SmErpTasteStockController.java | 17 +- .../service/impl/SmErpSpuServiceImpl.java | 156 +++++++++++------- 2 files changed, 102 insertions(+), 71 deletions(-) diff --git a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/controller/SmErpTasteStockController.java b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/controller/SmErpTasteStockController.java index 42df1634e..79698ed06 100644 --- a/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/controller/SmErpTasteStockController.java +++ b/pjcloud-supermarket/pjcloud-supermarket-biz/src/main/java/com/bsd/pjcloud/supermarket/controller/SmErpTasteStockController.java @@ -45,7 +45,8 @@ public class SmErpTasteStockController { */ @Operation(summary = "分页查询" , description = "分页查询" ) @GetMapping("/page" ) - @PreAuthorize("@pms.hasPermission('supermarket_smErpTasteStock_view')" ) +// @PreAuthorize("@pms.hasPermission('supermarket_smErpTasteStock_view')" ) + @PreAuthorize("@pms.hasPermission('supermarket_supermarketSpu_view')" ) public R getSmErpTasteStockPage(@ParameterObject Page page, @ParameterObject SmErpTasteStockEntity smErpTasteStock) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); return R.ok(smErpTasteStockService.page(page, wrapper)); @@ -59,7 +60,7 @@ public class SmErpTasteStockController { */ @Operation(summary = "通过id查询" , description = "通过id查询" ) @GetMapping("/{id}" ) - @PreAuthorize("@pms.hasPermission('supermarket_smErpTasteStock_view')" ) + @PreAuthorize("@pms.hasPermission('supermarket_supermarketSpu_view')" ) public R getById(@PathVariable("id" ) Long id) { return R.ok(smErpTasteStockService.getById(id)); } @@ -104,16 +105,4 @@ public class SmErpTasteStockController { } - /** - * 导出excel 表格 - * @param smErpTasteStock 查询条件 - * @param ids 导出指定ID - * @return excel 文件流 - */ - @ResponseExcel - @GetMapping("/export") - @PreAuthorize("@pms.hasPermission('supermarket_smErpTasteStock_export')" ) - public List export(SmErpTasteStockEntity smErpTasteStock,Long[] ids) { - return smErpTasteStockService.list(Wrappers.lambdaQuery(smErpTasteStock).in(ArrayUtil.isNotEmpty(ids), SmErpTasteStockEntity::getId, ids)); - } } \ No newline at end of file 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 1b5946bf7..fa0739df0 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 @@ -162,7 +162,32 @@ public class SmErpSpuServiceImpl extends ServiceImpl supermarketUnitEntity.setSpuId(supermarketSpu.getId())); + // 建立口味名称到ID的映射 + final Map tasteNameToIdMap; + if (CollectionUtils.isNotEmpty(tasteList)) { + tasteNameToIdMap = tasteList.stream() + .collect(Collectors.toMap( + taste -> taste.getTasteName() != null ? taste.getTasteName() : "", + SmErpTasteStockEntity::getId, + (existing, replacement) -> existing + )); + } else { + tasteNameToIdMap = new HashMap<>(); + } + + // 在循环中直接设置SPU ID和口味ID,然后保存 + smErpUnitEntityList.forEach(supermarketUnitEntity -> { + supermarketUnitEntity.setSpuId(supermarketSpu.getId()); + + // 根据口味名称设置口味ID + if (supermarketUnitEntity.getTaste() != null && !supermarketUnitEntity.getTaste().trim().isEmpty() && !tasteNameToIdMap.isEmpty()) { + Long tasteId = tasteNameToIdMap.get(supermarketUnitEntity.getTaste().trim()); + if (tasteId != null) { + supermarketUnitEntity.setTasteId(tasteId); + } + } + }); + smErpUnitService.saveBatch(smErpUnitEntityList); } else { return R.failed("包装类型不能为空"); @@ -207,6 +232,39 @@ public class SmErpSpuServiceImpl extends ServiceImpl tasteList = supermarketSpu.getSmErpTasteStockEntityList(); + if (CollectionUtils.isNotEmpty(tasteList)) { + // 分离新增和更新的口味 + List insertTastes = tasteList.stream() + .filter(taste -> taste.getId() == null) + .collect(Collectors.toList()); + + List updateTastes = tasteList.stream() + .filter(taste -> taste.getId() != null) + .collect(Collectors.toList()); + + // 处理新增口味 + if (CollectionUtils.isNotEmpty(insertTastes)) { + insertTastes.forEach(taste -> { + taste.setSpuId(supermarketSpu.getId()); + // 设置默认库存为0 + if (taste.getStock() == null) { + taste.setStock(0.0); + } + }); + smErpTasteStockService.saveBatch(insertTastes); + } + + // 处理更新口味 + if (CollectionUtils.isNotEmpty(updateTastes)) { + updateTastes.forEach(taste -> { + taste.setSpuId(supermarketSpu.getId()); + }); + smErpTasteStockService.updateBatchById(updateTastes); + } + } + //新增包装类型 List smErpUnitEntityList = supermarketSpu.getSmErpUnitEntityList(); List insertUnits= new ArrayList<>(); @@ -222,12 +280,50 @@ public class SmErpSpuServiceImpl extends ServiceImpl tasteNameToIdMap; + if (CollectionUtils.isNotEmpty(tasteList)) { + tasteNameToIdMap = tasteList.stream() + .collect(Collectors.toMap( + taste -> taste.getTasteName() != null ? taste.getTasteName() : "", + SmErpTasteStockEntity::getId, + (existing, replacement) -> existing + )); + } else { + tasteNameToIdMap = new HashMap<>(); + } + //id为null新增 insertUnits = smErpUnitEntityList.stream() .filter(supermarketUnitEntity -> supermarketUnitEntity.getId() == null).collect(Collectors.toList()); - insertUnits.forEach(supermarketUnitEntity -> supermarketUnitEntity.setSpuId(supermarketSpu.getId())); - updateUnits = smErpUnitEntityList.stream() + + // 为新增的包装单位设置SPU ID和口味ID + insertUnits.forEach(supermarketUnitEntity -> { + supermarketUnitEntity.setSpuId(supermarketSpu.getId()); + + // 根据口味名称设置口味ID + if (supermarketUnitEntity.getTaste() != null && !supermarketUnitEntity.getTaste().trim().isEmpty() && !tasteNameToIdMap.isEmpty()) { + Long tasteId = tasteNameToIdMap.get(supermarketUnitEntity.getTaste().trim()); + if (tasteId != null) { + supermarketUnitEntity.setTasteId(tasteId); + } + } + }); + + updateUnits = smErpUnitEntityList.stream() .filter(supermarketUnitEntity -> supermarketUnitEntity.getId() != null).collect(Collectors.toList()); + + // 为更新的包装单位设置口味ID + updateUnits.forEach(supermarketUnitEntity -> { + // 根据口味名称设置口味ID + if (supermarketUnitEntity.getTaste() != null && !supermarketUnitEntity.getTaste().trim().isEmpty() && !tasteNameToIdMap.isEmpty()) { + Long tasteId = tasteNameToIdMap.get(supermarketUnitEntity.getTaste().trim()); + if (tasteId != null) { + supermarketUnitEntity.setTasteId(tasteId); + } + } + }); + smErpUnitService.updateBatchById(updateUnits); smErpUnitService.saveBatch(insertUnits); //判断是否修改了基础包装单位,如果修改了基础包装单位的类型,其他的要变成false @@ -361,60 +457,6 @@ public class SmErpSpuServiceImpl extends ServiceImpl tasteList = supermarketSpu.getSmErpTasteStockEntityList(); - if (CollectionUtils.isNotEmpty(tasteList)) { - // 分离新增和更新的口味 - List insertTastes = tasteList.stream() - .filter(taste -> taste.getId() == null) - .collect(Collectors.toList()); - - List updateTastes = tasteList.stream() - .filter(taste -> taste.getId() != null) - .collect(Collectors.toList()); - - // 处理新增口味 - if (CollectionUtils.isNotEmpty(insertTastes)) { - insertTastes.forEach(taste -> { - taste.setSpuId(supermarketSpu.getId()); - // 设置默认库存为0 - if (taste.getStock() == null) { - taste.setStock(0.0); - } - }); - smErpTasteStockService.saveBatch(insertTastes); - } - - // 处理更新口味 - if (CollectionUtils.isNotEmpty(updateTastes)) { - updateTastes.forEach(taste -> { - taste.setSpuId(supermarketSpu.getId()); - }); - smErpTasteStockService.updateBatchById(updateTastes); - } - - // 获取现有所有口味ID - List existingTasteIds = smErpTasteStockService.list( - new LambdaQueryWrapper() - .eq(SmErpTasteStockEntity::getSpuId, supermarketSpu.getId()) - ).stream().map(SmErpTasteStockEntity::getId).collect(Collectors.toList()); - - // 收集本次提交的口味ID(排除新增的) - List submittedTasteIds = tasteList.stream() - .map(SmErpTasteStockEntity::getId) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - // 计算需要删除的口味ID - List toDeleteTasteIds = existingTasteIds.stream() - .filter(id -> !submittedTasteIds.contains(id)) - .collect(Collectors.toList()); - - // 批量删除失效口味 - if (!toDeleteTasteIds.isEmpty()) { - smErpTasteStockService.removeByIds(toDeleteTasteIds); - } - } return R.ok(); }