From 920e045a7076bd37ac10f919b7bfe399dce80b30 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Thu, 5 Dec 2024 22:48:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8C=89=E7=85=A7id=E3=80=81=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E3=80=81=E5=95=86=E5=93=81=E6=9D=A1=E7=A0=81=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WarehouseGoodsController.java | 9 ++- .../service/WarehouseGoodsService.java | 57 ++++++++++++++++++- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoods/controller/WarehouseGoodsController.java b/src/main/java/cn/lihongjie/coal/warehouseGoods/controller/WarehouseGoodsController.java index 31c801fa..57c871c5 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoods/controller/WarehouseGoodsController.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoods/controller/WarehouseGoodsController.java @@ -53,5 +53,12 @@ public class WarehouseGoodsController { } - + @PostMapping("/searchByCode") + public WarehouseGoodsDto searchByCode(@RequestBody IdRequest request) { + return this.service.searchByCode(request); + } + + + + } diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoods/service/WarehouseGoodsService.java b/src/main/java/cn/lihongjie/coal/warehouseGoods/service/WarehouseGoodsService.java index 5b75196b..ff754446 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoods/service/WarehouseGoodsService.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoods/service/WarehouseGoodsService.java @@ -4,6 +4,7 @@ import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.base.mapper.CommonMapper; import cn.lihongjie.coal.base.service.BaseService; +import cn.lihongjie.coal.common.Ctx; import cn.lihongjie.coal.warehouseGoods.dto.CreateWarehouseGoodsDto; import cn.lihongjie.coal.warehouseGoods.dto.UpdateWarehouseGoodsDto; import cn.lihongjie.coal.warehouseGoods.dto.WarehouseGoodsDto; @@ -11,19 +12,30 @@ import cn.lihongjie.coal.warehouseGoods.entity.WarehouseGoodsEntity; import cn.lihongjie.coal.warehouseGoods.mapper.WarehouseGoodsMapper; import cn.lihongjie.coal.warehouseGoods.repository.WarehouseGoodsRepository; +import com.fasterxml.jackson.databind.ObjectMapper; + import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @Slf4j @Transactional @@ -73,10 +85,49 @@ public class WarehouseGoodsService return page.map(this.mapper::toDto); } - @Autowired - private CommonMapper commonMapper; + @Autowired ObjectMapper objectMapper; + @Autowired private CommonMapper commonMapper; - + @SneakyThrows + public WarehouseGoodsDto searchByCode(IdRequest request) { + String id = request.getId(); + + List all = + this.repository.findAll( + new Specification() { + @Override + public Predicate toPredicate( + Root root, + CriteriaQuery query, + CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.or( + criteriaBuilder.equal(root.get("id"), id), + criteriaBuilder.and( + criteriaBuilder.equal(root.get("code"), id), + criteriaBuilder.equal( + root.get("organizationId"), + Ctx.currentUser().getOrganizationId())), + criteriaBuilder.and( + criteriaBuilder.equal(root.get("barCode"), id), + criteriaBuilder.equal( + root.get("organizationId"), + Ctx.currentUser().getOrganizationId()))); + } + }); + + if (CollectionUtils.isEmpty(all)) { + return null; + } else if (all.size() > 1) { + log.warn( + "find more than one result by code: {} {}", + id, + objectMapper.writeValueAsString(all)); + + return mapper.toDto(all.get(0)); + } else { + return mapper.toDto(all.get(0)); + } + } }