|
|
|
@ -1,22 +1,21 @@
|
|
|
|
|
package com.logpm.basicdata.excel; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
|
import com.logpm.basicdata.entity.BasicdataBrandEntity; |
|
|
|
|
import com.logpm.basicdata.entity.BasicdataCategoryEntity; |
|
|
|
|
import com.logpm.basicdata.entity.BasicdataFactoryCategoryEntity; |
|
|
|
|
import com.logpm.basicdata.service.IBasicdataBrandService; |
|
|
|
|
import com.logpm.basicdata.service.IBasicdataCategoryService; |
|
|
|
|
import com.logpm.basicdata.service.IBasicdataFactoryCategoryService; |
|
|
|
|
import com.logpm.basicdata.entity.*; |
|
|
|
|
import com.logpm.basicdata.service.*; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import org.springblade.common.exception.CustomerException; |
|
|
|
|
import org.springblade.core.excel.support.ExcelImporter; |
|
|
|
|
import org.springblade.core.log.exception.ServiceException; |
|
|
|
|
import org.springblade.core.tool.utils.BeanUtil; |
|
|
|
|
import org.springblade.core.tool.utils.ObjectUtil; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
@AllArgsConstructor |
|
|
|
|
public class BasicdataFactoryCategoryImporter implements ExcelImporter<BasicdataFactoryCategoryExcel> { |
|
|
|
@ -24,6 +23,8 @@ public class BasicdataFactoryCategoryImporter implements ExcelImporter<Basicdata
|
|
|
|
|
private final IBasicdataFactoryCategoryService basicdataFactoryCategoryService; |
|
|
|
|
private final IBasicdataBrandService basicdataBrandService; |
|
|
|
|
private final IBasicdataCategoryService basicdataCategoryService; |
|
|
|
|
private final IBasicdataMainCategoryService basicdataMainCategoryService; |
|
|
|
|
private final IBasicdataMainService mainService; |
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = CustomerException.class) |
|
|
|
|
@Override |
|
|
|
@ -37,16 +38,41 @@ public class BasicdataFactoryCategoryImporter implements ExcelImporter<Basicdata
|
|
|
|
|
LambdaQueryWrapper<BasicdataCategoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
lambdaQueryWrapper.eq(BasicdataCategoryEntity::getIsDeleted, 0); |
|
|
|
|
List<BasicdataCategoryEntity> basicdataCategoryEntities = basicdataCategoryService.list(lambdaQueryWrapper); |
|
|
|
|
//把basicdataCategoryEntities转成以name为Key的Map
|
|
|
|
|
Map<String, BasicdataCategoryEntity> categoryMap = basicdataCategoryEntities.stream() |
|
|
|
|
.collect(Collectors.toMap(BasicdataCategoryEntity::getName, entity -> entity)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<BasicdataMainCategoryEntity> mainCategoryQueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
mainCategoryQueryWrapper.eq(BasicdataMainCategoryEntity::getIsDeleted, 0); |
|
|
|
|
List<BasicdataMainCategoryEntity> basicdataMainCategoryEntities = basicdataMainCategoryService.list(mainCategoryQueryWrapper); |
|
|
|
|
//把basicdataMainCategoryEntities通过factoryCategoryId进行分组
|
|
|
|
|
Map<Long, List<BasicdataMainCategoryEntity>> groupedByFactoryCategoryId = basicdataMainCategoryEntities.stream() |
|
|
|
|
.collect(Collectors.groupingBy(BasicdataMainCategoryEntity::getFactoryCategoryId)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<BasicdataMainEntity> mainqueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
mainqueryWrapper.eq(BasicdataMainEntity::getIsDeleted, 0); |
|
|
|
|
List<BasicdataMainEntity> mainlist = mainService.list(mainqueryWrapper); |
|
|
|
|
//把mainlist转成以name为Key的Map
|
|
|
|
|
Map<String, BasicdataMainEntity> mainMap = mainlist.stream() |
|
|
|
|
.collect(Collectors.toMap(BasicdataMainEntity::getName, entity -> entity)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<BasicdataFactoryCategoryEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>(); |
|
|
|
|
lambdaQueryWrapper1.eq(BasicdataFactoryCategoryEntity::getIsDeleted, 0); |
|
|
|
|
List<BasicdataFactoryCategoryEntity> allBasicdataFactoryCategoryEntities = basicdataFactoryCategoryService.list(lambdaQueryWrapper1); |
|
|
|
|
//todo 这里的优化 是最好能进行品牌分组 获取对应的品牌数据 来进行匹配 这样可以减少循环次数
|
|
|
|
|
//把allBasicdataFactoryCategoryEntities转成以id为key的map
|
|
|
|
|
Map<Long, BasicdataFactoryCategoryEntity> allBasicdataFactoryCategoryEntityMap = allBasicdataFactoryCategoryEntities.stream() |
|
|
|
|
.collect(Collectors.toMap(BasicdataFactoryCategoryEntity::getId, entity -> entity)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<BasicdataFactoryCategoryExcel> newAddList = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
List<BasicdataFactoryCategoryEntity> basicdataFactoryCategoryEntities = new ArrayList<>(); |
|
|
|
|
for (BasicdataFactoryCategoryExcel datum : data) { |
|
|
|
|
List<BasicdataMainCategoryEntity> newMainCategoryEntities = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
data.forEach(datum -> { |
|
|
|
|
|
|
|
|
|
boolean isBrandFound = list.stream() |
|
|
|
|
.anyMatch(entity -> entity.getBrandName().equals(datum.getBrand())); |
|
|
|
@ -65,21 +91,123 @@ public class BasicdataFactoryCategoryImporter implements ExcelImporter<Basicdata
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 需要判断目前增加的三方物料在系统中是否已经存在
|
|
|
|
|
checkAllBasicdataFactoryCategoryEntities(allBasicdataFactoryCategoryEntities, datum); |
|
|
|
|
Long factoryCategoryId = checkAllBasicdataFactoryCategoryEntities(allBasicdataFactoryCategoryEntities, datum); |
|
|
|
|
|
|
|
|
|
if(Objects.isNull(factoryCategoryId)){ |
|
|
|
|
//如果没有id,需要存入三方品类信息存入和三方品类的主体映射需要存入
|
|
|
|
|
datum.setFactoryCategoryId(factoryCategoryId); |
|
|
|
|
|
|
|
|
|
newAddList.add(datum); |
|
|
|
|
|
|
|
|
|
}else{ |
|
|
|
|
String mainName = datum.getMainName(); |
|
|
|
|
String mainCategory1 = datum.getMainCategory(); |
|
|
|
|
List<BasicdataMainCategoryEntity> mainCategoryEntityList = groupedByFactoryCategoryId.get(factoryCategoryId); |
|
|
|
|
//把mainCategoryEntityList转成以mainName为Key的Map
|
|
|
|
|
Map<String, BasicdataMainCategoryEntity> mainCategoryEntityMap = mainCategoryEntityList.stream() |
|
|
|
|
.collect(Collectors.toMap(BasicdataMainCategoryEntity::getMainName, entity -> entity)); |
|
|
|
|
|
|
|
|
|
BasicdataMainCategoryEntity mainCategory = mainCategoryEntityMap.get(mainName); |
|
|
|
|
if (Objects.isNull(mainCategory)){ |
|
|
|
|
mainCategory = new BasicdataMainCategoryEntity(); |
|
|
|
|
mainCategory.setFactoryCategoryId(factoryCategoryId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mainCategory.setMainName(mainName); |
|
|
|
|
BasicdataMainEntity basicdataMainEntity = mainMap.get(mainName); |
|
|
|
|
if(!Objects.isNull(basicdataMainEntity)){ |
|
|
|
|
mainCategory.setMainId(basicdataMainEntity.getId()); |
|
|
|
|
} |
|
|
|
|
BasicdataCategoryEntity basicdataCategoryEntity = categoryMap.get(mainCategory1); |
|
|
|
|
if(!Objects.isNull(basicdataCategoryEntity)){ |
|
|
|
|
mainCategory.setCategoryId(basicdataCategoryEntity.getId()); |
|
|
|
|
} |
|
|
|
|
mainCategory.setCategory(mainCategory1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newMainCategoryEntities.add(mainCategory); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(CollUtil.isNotEmpty(newAddList)){ |
|
|
|
|
|
|
|
|
|
//把newAddList通过buildEqualString2()方法返回值进行分组
|
|
|
|
|
Map<String, List<BasicdataFactoryCategoryExcel>> groupedByBuildEqualString2 = newAddList.stream() |
|
|
|
|
.collect(Collectors.groupingBy(this::buildEqualString2)); |
|
|
|
|
|
|
|
|
|
groupedByBuildEqualString2.keySet().forEach(key -> { |
|
|
|
|
List<BasicdataFactoryCategoryExcel> basicdataFactoryCategoryExcels = groupedByBuildEqualString2.get(key); |
|
|
|
|
if(CollUtil.isNotEmpty(basicdataFactoryCategoryExcels)) { |
|
|
|
|
BasicdataFactoryCategoryExcel basicdataFactoryCategoryExcel = basicdataFactoryCategoryExcels.get(0); |
|
|
|
|
BasicdataFactoryCategoryEntity basicdataFactoryCategoryEntity = BeanUtil.copy(basicdataFactoryCategoryExcel, BasicdataFactoryCategoryEntity.class); |
|
|
|
|
|
|
|
|
|
if (ObjectUtil.isNotEmpty(basicdataFactoryCategoryEntity)) { |
|
|
|
|
assert basicdataFactoryCategoryEntity != null; |
|
|
|
|
basicdataFactoryCategoryEntity.setBrandId(list.stream().filter(entity -> entity.getBrandName().equals(basicdataFactoryCategoryEntity.getBrand())).findFirst().get().getId()); |
|
|
|
|
basicdataFactoryCategoryEntity.setCategoryId(categoryMap.get(basicdataFactoryCategoryExcel.getCategory()).getId()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
basicdataFactoryCategoryEntities.add(basicdataFactoryCategoryEntity); |
|
|
|
|
|
|
|
|
|
BasicdataFactoryCategoryEntity basicdataFactoryCategoryEntity = BeanUtil.copy(datum, BasicdataFactoryCategoryEntity.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (ObjectUtil.isNotEmpty(basicdataFactoryCategoryEntity)) { |
|
|
|
|
assert basicdataFactoryCategoryEntity != null; |
|
|
|
|
basicdataFactoryCategoryEntity.setBrandId(list.stream().filter(entity -> entity.getBrandName().equals(basicdataFactoryCategoryEntity.getBrand())).findFirst().get().getId()); |
|
|
|
|
basicdataFactoryCategoryEntity.setCategoryId(basicdataCategoryEntities.stream().filter(entity -> entity.getName().equals(basicdataFactoryCategoryEntity.getCategory())).findFirst().get().getId()); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if(CollUtil.isNotEmpty(basicdataFactoryCategoryEntities)){ |
|
|
|
|
basicdataFactoryCategoryService.saveBatch(basicdataFactoryCategoryEntities); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
basicdataFactoryCategoryEntities.add(basicdataFactoryCategoryEntity); |
|
|
|
|
basicdataFactoryCategoryEntities.forEach(basicdataFactoryCategoryEntity -> { |
|
|
|
|
Long factoryCategoryEntityId = basicdataFactoryCategoryEntity.getId(); |
|
|
|
|
String equestString = buildEqualString(basicdataFactoryCategoryEntity); |
|
|
|
|
List<BasicdataFactoryCategoryExcel> basicdataFactoryCategoryExcels = groupedByBuildEqualString2.get(equestString); |
|
|
|
|
if(CollUtil.isNotEmpty(basicdataFactoryCategoryExcels)){ |
|
|
|
|
basicdataFactoryCategoryExcels.forEach(basicdataFactoryCategoryExcel -> { |
|
|
|
|
String mainName = basicdataFactoryCategoryExcel.getMainName(); |
|
|
|
|
String mainCategory1 = basicdataFactoryCategoryExcel.getMainCategory(); |
|
|
|
|
List<BasicdataMainCategoryEntity> mainCategoryEntityList = groupedByFactoryCategoryId.get(factoryCategoryEntityId); |
|
|
|
|
//把mainCategoryEntityList转成以mainName为Key的Map
|
|
|
|
|
Map<String, BasicdataMainCategoryEntity> mainCategoryEntityMap = mainCategoryEntityList.stream() |
|
|
|
|
.collect(Collectors.toMap(BasicdataMainCategoryEntity::getMainName, entity -> entity)); |
|
|
|
|
|
|
|
|
|
BasicdataMainCategoryEntity mainCategory = mainCategoryEntityMap.get(mainName); |
|
|
|
|
if (Objects.isNull(mainCategory)){ |
|
|
|
|
mainCategory = new BasicdataMainCategoryEntity(); |
|
|
|
|
mainCategory.setFactoryCategoryId(factoryCategoryEntityId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mainCategory.setMainName(mainName); |
|
|
|
|
BasicdataMainEntity basicdataMainEntity = mainMap.get(mainName); |
|
|
|
|
if(!Objects.isNull(basicdataMainEntity)){ |
|
|
|
|
mainCategory.setMainId(basicdataMainEntity.getId()); |
|
|
|
|
} |
|
|
|
|
BasicdataCategoryEntity basicdataCategoryEntity = categoryMap.get(mainCategory1); |
|
|
|
|
if(!Objects.isNull(basicdataCategoryEntity)){ |
|
|
|
|
mainCategory.setCategoryId(basicdataCategoryEntity.getId()); |
|
|
|
|
} |
|
|
|
|
mainCategory.setCategory(mainCategory1); |
|
|
|
|
|
|
|
|
|
newMainCategoryEntities.add(mainCategory); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(CollUtil.isNotEmpty(newMainCategoryEntities)){ |
|
|
|
|
|
|
|
|
|
basicdataMainCategoryService.saveBatch(newMainCategoryEntities); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
basicdataFactoryCategoryService.saveBatch(basicdataFactoryCategoryEntities); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -89,16 +217,17 @@ public class BasicdataFactoryCategoryImporter implements ExcelImporter<Basicdata
|
|
|
|
|
* @param allBasicdataFactoryCategoryEntities |
|
|
|
|
* @param datum |
|
|
|
|
*/ |
|
|
|
|
private void checkAllBasicdataFactoryCategoryEntities(List<BasicdataFactoryCategoryEntity> allBasicdataFactoryCategoryEntities, BasicdataFactoryCategoryExcel datum) { |
|
|
|
|
private Long checkAllBasicdataFactoryCategoryEntities(List<BasicdataFactoryCategoryEntity> allBasicdataFactoryCategoryEntities, BasicdataFactoryCategoryExcel datum) { |
|
|
|
|
for (BasicdataFactoryCategoryEntity allBasicdataFactoryCategoryEntity : allBasicdataFactoryCategoryEntities) { |
|
|
|
|
String equalString = buildEqualString(allBasicdataFactoryCategoryEntity); |
|
|
|
|
String equalString2 = buildEqualString2(datum); |
|
|
|
|
if (equalString2.equals(equalString)) { |
|
|
|
|
throw new CustomerException("品牌:" + datum.getBrand() + " 品类:" + datum.getCategory() + "已存在相同的配置内容"); |
|
|
|
|
// throw new CustomerException("品牌:" + datum.getBrand() + " 品类:" + datum.getCategory() + "已存在相同的配置内容");
|
|
|
|
|
// datum.setFactoryCategoryId(allBasicdataFactoryCategoryEntity.getId());
|
|
|
|
|
return allBasicdataFactoryCategoryEntity.getId(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|