From 3eb8ee154ef982382b145d95e548185864200e37 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Sun, 16 Feb 2025 17:00:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(coal):=20=E4=BC=98=E5=8C=96=E5=95=86?= =?UTF-8?q?=E5=93=81=E6=90=9C=E7=B4=A2=E5=92=8C=E4=BB=93=E5=82=A8=E5=8D=95?= =?UTF-8?q?=E6=8D=AE=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 GoodsSearchService 中增加了对搜索失败情况的处理,抛出 BizException 异常 - 在 WarehouseReceiptService 中优化了空内容的处理逻辑,为每个元素添加默认值 - 调整了代码格式,提高了可读性 --- .../service/GoodsSearchService.java | 14 ++++++- .../service/WarehouseReceiptService.java | 37 ++++++++++++++++--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java b/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java index 1deddd3c..dd449276 100644 --- a/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java +++ b/src/main/java/cn/lihongjie/coal/goodsSearch/service/GoodsSearchService.java @@ -1,5 +1,6 @@ package cn.lihongjie.coal.goodsSearch.service; +import cn.lihongjie.coal.exception.BizException; import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchRequest; import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchResponse; @@ -31,8 +32,10 @@ import java.nio.charset.StandardCharsets; public class GoodsSearchService { @Autowired ObjectMapper objectMapper; + @Value("${goods.search.ak}") private String ak; + @Value("${goods.search.sk}") private String sk; @@ -47,11 +50,20 @@ public class GoodsSearchService { } @SneakyThrows - @Cacheable(cacheNames = "goodsSearch", key = "#request.code", condition = "#request.code != null", unless = "#result.flag.equals('true')") + @Cacheable( + cacheNames = "goodsSearch", + key = "#request.code", + condition = "#request.code != null", + unless = "#result.flag.equals('true')") public GoodsSearchResponse search(GoodsSearchRequest request) { ApiResponse response = Client.getInstance().search(request.getCode()); + if (response.getCode() != 200) { + log.warn("failed to search goods {} {} ", request.getCode(), response.getMessage()); + throw new BizException(response.getMessage()); + } + String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8); ObjectNode jsonNodes = objectMapper.readValue(bodyStr, ObjectNode.class); diff --git a/src/main/java/cn/lihongjie/coal/warehouseReceipt/service/WarehouseReceiptService.java b/src/main/java/cn/lihongjie/coal/warehouseReceipt/service/WarehouseReceiptService.java index fc461cfa..6fe9cb05 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseReceipt/service/WarehouseReceiptService.java +++ b/src/main/java/cn/lihongjie/coal/warehouseReceipt/service/WarehouseReceiptService.java @@ -609,7 +609,7 @@ public class WarehouseReceiptService <#if endTime??> and re.receipt_date <= :endTime - + <#if reason??> and re.reason like '%' || :reason || '%' @@ -815,6 +815,36 @@ from ( .toList(); } + if (CollectionUtils.isEmpty(content)) { + content = + ans.stream() + .map( + x -> { + if (x instanceof Map) { + + HashMap map = new HashMap<>(); + + map.putAll((Map) x); + + map.put("prevNumber", 0.0); + map.put("prevAmount", 0.0); + map.put("prevPrice", 0.0); + map.put("currNumber", 0.0); + map.put("currAmount", 0.0); + map.put("currPrice", 0.0); + map.put("type0Number", 0.0); + map.put("type1Number", 0.0); + map.put("type0Amount", 0.0); + map.put("type1Amount", 0.0); + map.put("type0Price", 0.0); + map.put("type1Price", 0.0); + return map; + } + return null; + }) + .toList(); + } + for (Object o : content) { if (o instanceof Map prevMap) { @@ -902,9 +932,6 @@ from ( (Specification) (root, query, criteriaBuilder) -> criteriaBuilder.and( - root.get("detail") - .get("goods") - .get("id") - .in(ids))); + root.get("detail").get("goods").get("id").in(ids))); } }