fix: 品牌增加批量添加接口

This commit is contained in:
2024-11-15 22:45:38 +08:00
parent 963d019d1b
commit 274304ec5b
3 changed files with 63 additions and 0 deletions

View File

@@ -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<WarehouseGoodsBrandDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
@PostMapping("/batchAdd")
public Boolean batchAdd(@RequestBody BatchAddRequest request) {
this.service.batchAdd(request);
return true;
}
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.warehouseGoodsBrand.dto;
import lombok.Data;
import java.util.*;
@Data
public class BatchAddRequest {
private List<String> names;
}

View File

@@ -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<String> set =
request.getNames().stream()
.distinct()
.map(StringUtils::trim)
.filter(StringUtils::isNotBlank)
.collect(Collectors.toSet());
List<String> 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);
});
}
}
}
}