refactor(coal): 优化商品搜索和仓储单据服务

- 在 GoodsSearchService 中增加了对搜索失败情况的处理,抛出 BizException 异常
- 在 WarehouseReceiptService 中优化了空内容的处理逻辑,为每个元素添加默认值
- 调整了代码格式,提高了可读性
This commit is contained in:
2025-02-16 17:00:05 +08:00
parent 9c313946df
commit 3eb8ee154e
2 changed files with 45 additions and 6 deletions

View File

@@ -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);

View File

@@ -609,7 +609,7 @@ public class WarehouseReceiptService
</#if>
<#if endTime??>
and re.receipt_date <= :endTime
</#if>
</#if>
<#if reason??>
and re.reason like '%' || :reason || '%'
</#if>
@@ -815,6 +815,36 @@ from (
.toList();
}
if (CollectionUtils.isEmpty(content)) {
content =
ans.stream()
.map(
x -> {
if (x instanceof Map) {
HashMap<Object, Object> 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<WarehouseReceiptEntity>)
(root, query, criteriaBuilder) ->
criteriaBuilder.and(
root.get("detail")
.get("goods")
.get("id")
.in(ids)));
root.get("detail").get("goods").get("id").in(ids)));
}
}