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); + }); + } + } + } + }