From 274304ec5b56ecdda2ec0b81ed28d387d67fc96f Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Fri, 15 Nov 2024 22:45:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=93=81=E7=89=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarehouseGoodsBrandController.java | 8 ++++ .../dto/BatchAddRequest.java | 11 +++++ .../service/WarehouseGoodsBrandService.java | 44 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/dto/BatchAddRequest.java diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/controller/WarehouseGoodsBrandController.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/controller/WarehouseGoodsBrandController.java index 00b31ff9..7aa1d8ed 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/controller/WarehouseGoodsBrandController.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/controller/WarehouseGoodsBrandController.java @@ -4,6 +4,7 @@ import cn.lihongjie.coal.annotation.OrgScope; import cn.lihongjie.coal.annotation.SysLog; import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.warehouseGoodsBrand.dto.BatchAddRequest; import cn.lihongjie.coal.warehouseGoodsBrand.dto.CreateWarehouseGoodsBrandDto; import cn.lihongjie.coal.warehouseGoodsBrand.dto.UpdateWarehouseGoodsBrandDto; import cn.lihongjie.coal.warehouseGoodsBrand.dto.WarehouseGoodsBrandDto; @@ -49,6 +50,13 @@ public class WarehouseGoodsBrandController { @PostMapping("/list") public Page list(@RequestBody CommonQuery request) { + return this.service.list(request); } + + @PostMapping("/batchAdd") + public Boolean batchAdd(@RequestBody BatchAddRequest request) { + this.service.batchAdd(request); + return true; + } } diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/dto/BatchAddRequest.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/dto/BatchAddRequest.java new file mode 100644 index 00000000..a0e4fca9 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/dto/BatchAddRequest.java @@ -0,0 +1,11 @@ +package cn.lihongjie.coal.warehouseGoodsBrand.dto; + +import lombok.Data; + +import java.util.*; + +@Data +public class BatchAddRequest { + + private List names; +} diff --git a/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/service/WarehouseGoodsBrandService.java b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/service/WarehouseGoodsBrandService.java index 53b481d4..a17037bf 100644 --- a/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/service/WarehouseGoodsBrandService.java +++ b/src/main/java/cn/lihongjie/coal/warehouseGoodsBrand/service/WarehouseGoodsBrandService.java @@ -2,7 +2,9 @@ package cn.lihongjie.coal.warehouseGoodsBrand.service; 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.warehouseGoodsBrand.dto.BatchAddRequest; import cn.lihongjie.coal.warehouseGoodsBrand.dto.CreateWarehouseGoodsBrandDto; import cn.lihongjie.coal.warehouseGoodsBrand.dto.UpdateWarehouseGoodsBrandDto; import cn.lihongjie.coal.warehouseGoodsBrand.dto.WarehouseGoodsBrandDto; @@ -10,8 +12,13 @@ import cn.lihongjie.coal.warehouseGoodsBrand.entity.WarehouseGoodsBrandEntity; import cn.lihongjie.coal.warehouseGoodsBrand.mapper.WarehouseGoodsBrandMapper; import cn.lihongjie.coal.warehouseGoodsBrand.repository.WarehouseGoodsBrandRepository; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; + import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Page; @@ -20,6 +27,10 @@ import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + @Service @Slf4j @Transactional @@ -68,4 +79,37 @@ public class WarehouseGoodsBrandService return page.map(this.mapper::toDto); } + + + @PersistenceContext EntityManager em; + @Autowired private CommonMapper commonMapper; + + public void batchAdd(BatchAddRequest request) { + + if (CollectionUtils.isNotEmpty(request.getNames())) { + + Set set = + request.getNames().stream() + .distinct() + .map(StringUtils::trim) + .filter(StringUtils::isNotBlank) + .collect(Collectors.toSet()); + + List exists = + em.createQuery( + "select distinct name from WarehouseGoodsBrandEntity", + String.class) + .getResultList(); + set.removeAll(exists); + if (CollectionUtils.isNotEmpty(set)) { + set.forEach( + name -> { + WarehouseGoodsBrandEntity entity = new WarehouseGoodsBrandEntity(); + entity.setName(name); + this.repository.save(entity); + }); + } + } + } + }