修复商品更新删除问题
This commit is contained in:
1 parent
21af312edd
commit
ff3433dd48
1 file changed
+46
-512
+46
-512
@@ -195,6 +195,29 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
|
||||
.set(SmErpUnitEntity::getIsBase, false);
|
||||
smErpUnitService.update(updateWrapper);
|
||||
}
|
||||
// 删除失效单位
|
||||
// 获取现有所有包装单位ID
|
||||
List<Long> existingUnitIds = smErpUnitService.list(
|
||||
new LambdaQueryWrapper<SmErpUnitEntity>()
|
||||
.eq(SmErpUnitEntity::getSpuId, supermarketSpu.getId())
|
||||
).stream().map(SmErpUnitEntity::getId).collect(Collectors.toList());
|
||||
|
||||
// 收集本次提交的ID(排除新增的)
|
||||
List<Long> submittedIds = smErpUnitEntityList.stream()
|
||||
.map(SmErpUnitEntity::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 计算需要删除的ID
|
||||
List<Long> toDeleteIds = existingUnitIds.stream()
|
||||
.filter(id -> !submittedIds.contains(id))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量删除失效单位
|
||||
if (!toDeleteIds.isEmpty()) {
|
||||
smErpUnitService.removeByIds(toDeleteIds);
|
||||
}
|
||||
|
||||
} else {
|
||||
return R.failed("包装类型不能为空");
|
||||
}
|
||||
@@ -206,9 +229,32 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
|
||||
insertCollect.stream().forEach(supermarketAuxiliaryCodeEntity -> supermarketAuxiliaryCodeEntity.setSpuId(supermarketSpu.getId()));
|
||||
smErpAuxiliaryCodeService.saveBatch(insertCollect);
|
||||
smErpAuxiliaryCodeService.updateBatchById(updateCollect);
|
||||
|
||||
// 获取现有所有条码ID
|
||||
List<Long> existingCodeIds = smErpAuxiliaryCodeService.list(
|
||||
new LambdaQueryWrapper<SmErpAuxiliaryCodeEntity>()
|
||||
.eq(SmErpAuxiliaryCodeEntity::getSpuId, supermarketSpu.getId())
|
||||
).stream().map(SmErpAuxiliaryCodeEntity::getId).collect(Collectors.toList());
|
||||
|
||||
// 收集本次提交的ID(排除新增的)
|
||||
List<Long> submittedCodeIds = smErpAuxiliaryCodeEntityList.stream()
|
||||
.map(SmErpAuxiliaryCodeEntity::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 计算需要删除的ID
|
||||
List<Long> toDeleteCodeIds = existingCodeIds.stream()
|
||||
.filter(id -> !submittedCodeIds.contains(id))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量删除失效条码
|
||||
if (!toDeleteCodeIds.isEmpty()) {
|
||||
smErpAuxiliaryCodeService.removeByIds(toDeleteCodeIds);
|
||||
}
|
||||
} else {
|
||||
return R.failed("辅助条码不能为空");
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@@ -441,518 +487,6 @@ public class SmErpSpuServiceImpl extends ServiceImpl<SmErpSpuMapper, SmErpSpuEnt
|
||||
return smErpSpuEntities;
|
||||
}
|
||||
|
||||
/* @Override
|
||||
@Transactional
|
||||
public R importErpSpu(List<SmErpSpuImportDto> smErpSpuImportDtos, BindingResult bindingResult) {
|
||||
if (CollectionUtils.isEmpty(smErpSpuImportDtos)) {
|
||||
return R.failed("导入数据不能为空");
|
||||
}
|
||||
List<Map<String, Object>> errorList = new ArrayList<>();
|
||||
List<SmErpSupplierEntity> successList = new ArrayList<>();
|
||||
for (int i = 0; i < smErpSpuImportDtos.size(); i++) {
|
||||
int rowNumber = i + 1;
|
||||
SmErpSpuImportDto dto = smErpSpuImportDtos.get(i);
|
||||
List<String> errorReasons = new ArrayList<>();
|
||||
// 数据校验阶段
|
||||
List<String> validationErrors = ErpSpuValidator.validate(dto, rowNumber);
|
||||
String productCode = dto.getProductCode();
|
||||
if (!validationErrors.isEmpty()) {
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, validationErrors));
|
||||
continue;
|
||||
}
|
||||
//新增商品
|
||||
//1.先查询当前货号是否存在,存在即更新,不存在即新增
|
||||
List<SmErpSpuEntity> smErpSpuEntities = baseMapper.selectListByScope(new LambdaQueryWrapper<SmErpSpuEntity>().
|
||||
eq(SmErpSpuEntity::getProductCode, productCode), DataScope.of());
|
||||
if (CollectionUtils.isEmpty(smErpSpuEntities)) {
|
||||
//新增
|
||||
SmErpSpuEntity smErpSpuEntity = BeanUtils.toBean(dto, SmErpSpuEntity.class);
|
||||
if (dto.getCommodityNature().equals("电子称称重商品")){
|
||||
smErpSpuEntity.setProductNature("2");
|
||||
}else {
|
||||
smErpSpuEntity.setProductNature("1");
|
||||
}
|
||||
//先查询分类是否有,没有自动新增
|
||||
SmErpCategoryEntity smErpCategoryEntity = smErpCategoryMapper.selectOne
|
||||
(new LambdaQueryWrapper<SmErpCategoryEntity>().eq(SmErpCategoryEntity::getName, dto.getClassification()));
|
||||
if (smErpCategoryEntity == null) {
|
||||
//新增分类
|
||||
SmErpCategoryEntity smErpCategoryEntity1 = new SmErpCategoryEntity();
|
||||
smErpCategoryEntity1.setName(dto.getClassification());
|
||||
smErpCategoryEntity1.setStatus("0");
|
||||
smErpCategoryMapper.insert(smErpCategoryEntity1);
|
||||
smErpSpuEntity.setCategoryId(smErpCategoryEntity1.getId());
|
||||
}else {
|
||||
smErpSpuEntity.setCategoryId(smErpCategoryEntity.getId());
|
||||
}
|
||||
baseMapper.insert(smErpSpuEntity);
|
||||
//新增包装单位
|
||||
SmErpUnitEntity smErpUnitEntity = BeanUtils.toBean(dto, SmErpUnitEntity.class);
|
||||
smErpUnitEntity.setIsBase(true);
|
||||
smErpUnitEntity.setSpuId(smErpSpuEntity.getId());
|
||||
smErpUnitService.save(smErpUnitEntity);
|
||||
//新增库存
|
||||
SmErpStockEntity smErpStockEntity = new SmErpStockEntity();
|
||||
smErpStockEntity.setCurrentStock(dto.getStock());
|
||||
smErpStockEntity.setBaseStock(dto.getStock());
|
||||
smErpStockEntity.setInventoryLowerLimit(dto.getInventoryLowerLimit());
|
||||
smErpStockEntity.setInventoryUpperLimit(dto.getInventoryUpperLimit());
|
||||
smErpStockEntity.setErpSpuId(smErpSpuEntity.getId());
|
||||
smErpStockMapper.insert(smErpStockEntity);
|
||||
//新增辅助条码
|
||||
SmErpAuxiliaryCodeEntity smErpAuxiliaryCodeEntity = new SmErpAuxiliaryCodeEntity();
|
||||
smErpAuxiliaryCodeEntity.setBarCode(dto.getBarCode());
|
||||
smErpAuxiliaryCodeEntity.setSpuId(smErpSpuEntity.getId());
|
||||
smErpAuxiliaryCodeEntity.setUnitId(smErpUnitEntity.getId());
|
||||
smErpAuxiliaryCodeService.save(smErpAuxiliaryCodeEntity);
|
||||
}else {
|
||||
//查到相同的货号,更新
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return null;
|
||||
}*/
|
||||
/*
|
||||
@Override
|
||||
@Transactional
|
||||
public R importErpSpu(List<SmErpSpuImportDto> smErpSpuImportDtos, BindingResult bindingResult) {
|
||||
if (CollectionUtils.isEmpty(smErpSpuImportDtos)) {
|
||||
return R.failed("导入数据不能为空");
|
||||
}
|
||||
|
||||
List<Map<String, Object>> errorList = new ArrayList<>();
|
||||
List<SmErpSpuEntity> successList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < smErpSpuImportDtos.size(); i++) {
|
||||
int rowNumber = i + 1;
|
||||
SmErpSpuImportDto dto = smErpSpuImportDtos.get(i);
|
||||
List<String> errorReasons = new ArrayList<>();
|
||||
String productCode = dto.getProductCode();
|
||||
|
||||
try {
|
||||
// 数据校验
|
||||
List<String> validationErrors = ErpSpuValidator.validate(dto, rowNumber);
|
||||
if (!validationErrors.isEmpty()) {
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, validationErrors));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 解析商品分类
|
||||
Long categoryId = resolveCategory(dto.getClassification(), errorReasons);
|
||||
if (categoryId == null) {
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查询现有商品
|
||||
SmErpSpuEntity existingSpu = baseMapper.selectOne(new LambdaQueryWrapper<SmErpSpuEntity>()
|
||||
.eq(SmErpSpuEntity::getProductCode, productCode));
|
||||
|
||||
if (existingSpu == null) {
|
||||
// 新增逻辑
|
||||
handleNewSpu(dto, categoryId, errorReasons, rowNumber, productCode, errorList);
|
||||
} else {
|
||||
// 更新逻辑
|
||||
handleExistingSpu(existingSpu, dto, categoryId, errorReasons, rowNumber, productCode, errorList);
|
||||
}
|
||||
|
||||
// 记录成功数据
|
||||
if (errorReasons.isEmpty()) {
|
||||
successList.add(baseMapper.selectById(existingSpu != null ? existingSpu.getId() : null));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("系统异常: " + e.getMessage());
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
}
|
||||
}
|
||||
|
||||
return buildImportResult(smErpSpuImportDtos.size(), successList.size(), errorList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 处理分类(公共方法)
|
||||
private Long resolveCategory(String categoryName, List<String> errorReasons) {
|
||||
try {
|
||||
SmErpCategoryEntity category = smErpCategoryMapper.selectOne(
|
||||
new LambdaQueryWrapper<SmErpCategoryEntity>()
|
||||
.eq(SmErpCategoryEntity::getName, categoryName)
|
||||
);
|
||||
|
||||
if (category != null) return category.getId();
|
||||
|
||||
// 新建分类
|
||||
SmErpCategoryEntity newCategory = new SmErpCategoryEntity();
|
||||
newCategory.setName(categoryName);
|
||||
newCategory.setStatus("0"); // 假设0为启用状态
|
||||
if (smErpCategoryMapper.insert(newCategory) > 0) {
|
||||
return newCategory.getId();
|
||||
}
|
||||
errorReasons.add("分类创建失败");
|
||||
} catch (DuplicateKeyException e) {
|
||||
// 处理并发创建场景
|
||||
return smErpCategoryMapper.selectOne(
|
||||
new LambdaQueryWrapper<SmErpCategoryEntity>()
|
||||
.eq(SmErpCategoryEntity::getName, categoryName)
|
||||
).getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 处理新增商品
|
||||
private void handleNewSpu(SmErpSpuImportDto dto, Long categoryId, List<String> errorReasons,
|
||||
int rowNumber, String productCode, List<Map<String, Object>> errorList) {
|
||||
try {
|
||||
SmErpSpuEntity newSpu = BeanUtils.toBean(dto, SmErpSpuEntity.class);
|
||||
newSpu.setProductNature(dto.getCommodityNature().equals("电子称称重商品") ? "2" : "1");
|
||||
newSpu.setCategoryId(categoryId);
|
||||
|
||||
if (baseMapper.insert(newSpu) <= 0) {
|
||||
errorReasons.add("商品创建失败");
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理关联数据
|
||||
handleRelatedData(newSpu, dto, errorReasons, rowNumber, productCode, errorList);
|
||||
} catch (DuplicateKeyException e) {
|
||||
errorReasons.add("货号已存在");
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
}
|
||||
}
|
||||
|
||||
// 处理更新商品
|
||||
private void handleExistingSpu(SmErpSpuEntity existingSpu, SmErpSpuImportDto dto, Long categoryId,
|
||||
List<String> errorReasons, int rowNumber, String productCode,
|
||||
List<Map<String, Object>> errorList) {
|
||||
try {
|
||||
// 更新主数据
|
||||
org.springframework.beans.BeanUtils.copyProperties(dto, existingSpu);
|
||||
existingSpu.setProductNature(dto.getCommodityNature().equals("电子称称重商品") ? "2" : "1");
|
||||
existingSpu.setCategoryId(categoryId);
|
||||
|
||||
if (baseMapper.updateById(existingSpu) <= 0) {
|
||||
errorReasons.add("商品更新失败");
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理关联数据
|
||||
handleRelatedData(existingSpu, dto, errorReasons, rowNumber, productCode, errorList);
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("更新异常: " + e.getMessage());
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
}
|
||||
}
|
||||
|
||||
// 处理关联数据(单位、库存、条码)
|
||||
private void handleRelatedData(SmErpSpuEntity spu, SmErpSpuImportDto dto, List<String> errorReasons,
|
||||
int rowNumber, String productCode, List<Map<String, Object>> errorList) {
|
||||
// 处理包装单位
|
||||
SmErpUnitEntity unit = smErpUnitService.getOne(
|
||||
new LambdaQueryWrapper<SmErpUnitEntity>()
|
||||
.eq(SmErpUnitEntity::getSpuId, spu.getId())
|
||||
);
|
||||
|
||||
try {
|
||||
if (unit != null) {
|
||||
// 更新单位
|
||||
org.springframework.beans.BeanUtils.copyProperties(dto, unit);
|
||||
smErpUnitService.updateById(unit);
|
||||
} else {
|
||||
// 新增单位
|
||||
unit = BeanUtils.toBean(dto, SmErpUnitEntity.class);
|
||||
unit.setSpuId(spu.getId());
|
||||
unit.setIsBase(true);
|
||||
smErpUnitService.save(unit);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("包装单位处理失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 处理库存
|
||||
SmErpStockEntity stock = smErpStockMapper.selectOne(
|
||||
new LambdaQueryWrapper<SmErpStockEntity>()
|
||||
.eq(SmErpStockEntity::getErpSpuId, spu.getId())
|
||||
);
|
||||
|
||||
try {
|
||||
if (stock != null) {
|
||||
// 更新库存
|
||||
stock.setCurrentStock(dto.getStock());
|
||||
stock.setBaseStock(dto.getStock());
|
||||
stock.setInventoryLowerLimit(dto.getInventoryLowerLimit());
|
||||
stock.setInventoryUpperLimit(dto.getInventoryUpperLimit());
|
||||
smErpStockMapper.updateById(stock);
|
||||
} else {
|
||||
// 新增库存
|
||||
stock = BeanUtils.toBean(dto, SmErpStockEntity.class);
|
||||
stock.setErpSpuId(spu.getId());
|
||||
smErpStockMapper.insert(stock);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("库存处理失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 处理辅助条码
|
||||
SmErpAuxiliaryCodeEntity code = smErpAuxiliaryCodeService.getOne(
|
||||
new LambdaQueryWrapper<SmErpAuxiliaryCodeEntity>()
|
||||
.eq(SmErpAuxiliaryCodeEntity::getSpuId, spu.getId())
|
||||
);
|
||||
|
||||
try {
|
||||
if (code != null) {
|
||||
// 更新条码
|
||||
code.setBarCode(dto.getBarCode());
|
||||
code.setUnitId(unit.getId());
|
||||
smErpAuxiliaryCodeService.updateById(code);
|
||||
} else {
|
||||
// 新增条码
|
||||
code = new SmErpAuxiliaryCodeEntity();
|
||||
code.setBarCode(dto.getBarCode());
|
||||
code.setSpuId(spu.getId());
|
||||
code.setUnitId(unit.getId());
|
||||
smErpAuxiliaryCodeService.save(code);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("条码处理失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 记录错误
|
||||
if (!errorReasons.isEmpty()) {
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 构建返回结果(与供应商导入保持一致)
|
||||
private R buildImportResult(int total, int successCount, List<Map<String, Object>> errorList) {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("total", total);
|
||||
result.put("successCount", successCount);
|
||||
result.put("errorCount", errorList.size());
|
||||
|
||||
if (!errorList.isEmpty()) {
|
||||
result.put("errors", errorList);
|
||||
return R.failed(result,"部分数据导入失败");
|
||||
}
|
||||
return R.ok( result,"成功导入所有" + successCount + "条数据");
|
||||
}
|
||||
|
||||
// 错误信息构建方法
|
||||
private Map<String, Object> createErrorInfo(int row, String name, List<String> reasons) {
|
||||
return new HashMap<String, Object>() {{
|
||||
put("row", row);
|
||||
put("supplierName", name);
|
||||
put("reasons", reasons);
|
||||
}};
|
||||
}
|
||||
*/
|
||||
|
||||
// private R buildImportResult(int total, int successCount, List<Map<String, Object>> errorList) {
|
||||
// Map<String, Object> result = new LinkedHashMap<>();
|
||||
// result.put("total", total);
|
||||
// result.put("successCount", successCount);
|
||||
// result.put("errorCount", errorList.size());
|
||||
//
|
||||
// if (!errorList.isEmpty()) {
|
||||
// result.put("errors", errorList);
|
||||
// return R.failed(result,"部分数据导入失败");
|
||||
// }
|
||||
// return R.ok( result,"成功导入所有" + successCount + "条数据");
|
||||
// }
|
||||
// // 错误信息构建方法
|
||||
// private Map<String, Object> createErrorInfo(int row, String name, List<String> reasons) {
|
||||
// return new HashMap<String, Object>() {{
|
||||
// put("row", row);
|
||||
// put("supplierName", name);
|
||||
// put("reasons", reasons);
|
||||
// }};
|
||||
// }
|
||||
/* @Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public R importErpSpu(List<SmErpSpuImportDto> smErpSpuImportDtos, BindingResult bindingResult) {
|
||||
if (CollectionUtils.isEmpty(smErpSpuImportDtos)) {
|
||||
return R.failed("导入数据不能为空");
|
||||
}
|
||||
|
||||
List<Map<String, Object>> errorList = new ArrayList<>();
|
||||
List<SmErpSpuEntity> successList = new ArrayList<>();
|
||||
|
||||
// 批量处理分类
|
||||
Map<String, Long> categoryMap = resolveCategories(smErpSpuImportDtos, errorList);
|
||||
|
||||
// 批量处理商品
|
||||
List<SmErpSpuEntity> spuBatch = new ArrayList<>();
|
||||
List<SmErpUnitEntity> unitBatch = new ArrayList<>();
|
||||
List<SmErpStockEntity> stockBatch = new ArrayList<>();
|
||||
List<SmErpAuxiliaryCodeEntity> codeBatch = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < smErpSpuImportDtos.size(); i++) {
|
||||
int rowNumber = i + 1;
|
||||
SmErpSpuImportDto dto = smErpSpuImportDtos.get(i);
|
||||
List<String> errorReasons = new ArrayList<>();
|
||||
String productCode = dto.getProductCode();
|
||||
|
||||
try {
|
||||
// 数据校验
|
||||
List<String> validationErrors = ErpSpuValidator.validate(dto, rowNumber);
|
||||
if (!validationErrors.isEmpty()) {
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, validationErrors));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取分类ID
|
||||
Long categoryId = categoryMap.get(dto.getClassification());
|
||||
if (categoryId == null) {
|
||||
errorReasons.add("分类不存在且创建失败");
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
continue;
|
||||
}
|
||||
|
||||
// 构造商品实体
|
||||
SmErpSpuEntity spu = BeanUtils.toBean(dto, SmErpSpuEntity.class);
|
||||
spu.setProductNature("电子称称重商品".equals(dto.getCommodityNature()) ? "2" : "1");
|
||||
spu.setCategoryId(categoryId);
|
||||
spuBatch.add(spu);
|
||||
|
||||
// 构造关联数据
|
||||
buildRelatedData(dto, spu, unitBatch, stockBatch, codeBatch);
|
||||
|
||||
successList.add(spu);
|
||||
} catch (Exception e) {
|
||||
errorReasons.add("系统异常: " + e.getMessage());
|
||||
errorList.add(createErrorInfo(rowNumber, productCode, errorReasons));
|
||||
}
|
||||
}
|
||||
|
||||
// 批量数据库操作
|
||||
try {
|
||||
// 批量插入/更新商品
|
||||
if (!spuBatch.isEmpty()) {
|
||||
saveOrUpdateBatchSpu(spuBatch);
|
||||
}
|
||||
|
||||
// 批量处理关联数据
|
||||
if (!unitBatch.isEmpty()) {
|
||||
smErpUnitService.saveOrUpdateBatch(unitBatch);
|
||||
}
|
||||
if (!stockBatch.isEmpty()) {
|
||||
smErpStockService.saveOrUpdateBatch(stockBatch);
|
||||
}
|
||||
if (!codeBatch.isEmpty()) {
|
||||
smErpAuxiliaryCodeService.saveOrUpdateBatch(codeBatch);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("批量操作失败", e);
|
||||
}
|
||||
|
||||
return buildImportResult(smErpSpuImportDtos.size(), successList.size(), errorList);
|
||||
}
|
||||
|
||||
// 批量处理分类
|
||||
private Map<String, Long> resolveCategories(List<SmErpSpuImportDto> dtos, List<Map<String, Object>> errorList) {
|
||||
// 获取所有分类名称
|
||||
Set<String> categoryNames = dtos.stream()
|
||||
.map(SmErpSpuImportDto::getClassification)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询已有分类
|
||||
List<SmErpCategoryEntity> existCategories = smErpCategoryMapper.selectList(
|
||||
new LambdaQueryWrapper<SmErpCategoryEntity>()
|
||||
.in(SmErpCategoryEntity::getName, categoryNames));
|
||||
|
||||
Map<String, Long> categoryMap = existCategories.stream()
|
||||
.collect(Collectors.toMap(SmErpCategoryEntity::getName, SmErpCategoryEntity::getId));
|
||||
|
||||
// 找出需要新增的分类
|
||||
List<SmErpCategoryEntity> newCategories = categoryNames.stream()
|
||||
.filter(name -> !categoryMap.containsKey(name))
|
||||
.map(name -> {
|
||||
SmErpCategoryEntity category = new SmErpCategoryEntity();
|
||||
category.setName(name);
|
||||
category.setStatus("0");
|
||||
return category;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量插入新分类
|
||||
if (!newCategories.isEmpty()) {
|
||||
smCategoryService.saveBatch(newCategories);
|
||||
newCategories.forEach(cat -> categoryMap.put(cat.getName(), cat.getId()));
|
||||
}
|
||||
|
||||
return categoryMap;
|
||||
}
|
||||
|
||||
// 批量保存或更新商品
|
||||
private void saveOrUpdateBatchSpu(List<SmErpSpuEntity> spuList) {
|
||||
// 按货号分组
|
||||
Map<String, List<SmErpSpuEntity>> spuMap = spuList.stream()
|
||||
.collect(Collectors.groupingBy(SmErpSpuEntity::getProductCode));
|
||||
|
||||
// 批量查询已存在的商品
|
||||
List<SmErpSpuEntity> existSpus = baseMapper.selectList(
|
||||
new LambdaQueryWrapper<SmErpSpuEntity>()
|
||||
.in(SmErpSpuEntity::getProductCode, spuMap.keySet()));
|
||||
|
||||
// 分离新增和更新数据
|
||||
List<SmErpSpuEntity> toInsert = new ArrayList<>();
|
||||
List<SmErpSpuEntity> toUpdate = new ArrayList<>();
|
||||
|
||||
spuList.forEach(spu -> {
|
||||
Optional<SmErpSpuEntity> exist = existSpus.stream()
|
||||
.filter(e -> e.getProductCode().equals(spu.getProductCode()))
|
||||
.findFirst();
|
||||
|
||||
if (exist.isPresent()) {
|
||||
SmErpSpuEntity entity = exist.get();
|
||||
org.springframework.beans.BeanUtils.copyProperties(spu, entity);
|
||||
toUpdate.add(entity);
|
||||
} else {
|
||||
toInsert.add(spu);
|
||||
}
|
||||
});
|
||||
|
||||
// 批量操作
|
||||
if (!toInsert.isEmpty()) {
|
||||
this.saveBatch((toInsert));
|
||||
}
|
||||
if (!toUpdate.isEmpty()) {
|
||||
this.updateBatchById(toUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
// 构建关联数据
|
||||
private void buildRelatedData(SmErpSpuImportDto dto, SmErpSpuEntity spu,
|
||||
List<SmErpUnitEntity> unitBatch,
|
||||
List<SmErpStockEntity> stockBatch,
|
||||
List<SmErpAuxiliaryCodeEntity> codeBatch) {
|
||||
// 包装单位
|
||||
SmErpUnitEntity unit = BeanUtils.toBean(dto, SmErpUnitEntity.class);
|
||||
unit.setSpuId(spu.getId());
|
||||
unit.setIsBase(true);
|
||||
unitBatch.add(unit);
|
||||
|
||||
// 库存
|
||||
SmErpStockEntity stock = BeanUtils.toBean(dto, SmErpStockEntity.class);
|
||||
stock.setErpSpuId(spu.getId());
|
||||
stock.setCurrentStock(dto.getStock());
|
||||
stock.setBaseStock(dto.getStock());
|
||||
stockBatch.add(stock);
|
||||
|
||||
// 辅助条码
|
||||
SmErpAuxiliaryCodeEntity code = new SmErpAuxiliaryCodeEntity();
|
||||
code.setBarCode(dto.getBarCode());
|
||||
code.setSpuId(spu.getId());
|
||||
code.setUnitId(unit.getId());
|
||||
codeBatch.add(code);
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user