From 1a18fbb03b043dd8248cd15c72dcb517b7e78d10 Mon Sep 17 00:00:00 2001 From: "kuang.yife" Date: Mon, 28 Oct 2024 16:45:10 +0800 Subject: [PATCH] fix bug --- .../CategoryInsuranceRelationServiceImpl.java | 57 ++++++++++++++++--- .../mapper/goods/DeptGoodsCategoryMapper.xml | 5 +- .../mapper/order/OrderMasterMapper.xml | 2 +- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/ghy-goods/src/main/java/com/ghy/goods/service/impl/CategoryInsuranceRelationServiceImpl.java b/ghy-goods/src/main/java/com/ghy/goods/service/impl/CategoryInsuranceRelationServiceImpl.java index f31903b8..bc6d025a 100644 --- a/ghy-goods/src/main/java/com/ghy/goods/service/impl/CategoryInsuranceRelationServiceImpl.java +++ b/ghy-goods/src/main/java/com/ghy/goods/service/impl/CategoryInsuranceRelationServiceImpl.java @@ -1,11 +1,18 @@ package com.ghy.goods.service.impl; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import cn.hutool.core.collection.CollectionUtil; import com.ghy.common.utils.DateUtils; +import com.ghy.goods.domain.DeptCategoryInsuranceRelation; +import com.ghy.goods.domain.DeptGoodsCategory; +import com.ghy.goods.service.DeptGoodsCategoryService; +import com.ghy.goods.service.IDeptCategoryInsuranceRelationService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,6 +21,8 @@ import com.ghy.goods.domain.CategoryInsuranceRelation; import com.ghy.goods.service.ICategoryInsuranceRelationService; import com.ghy.common.core.text.Convert; +import javax.annotation.Resource; + /** * 总类目保险关联关系Service业务层处理 * @@ -26,6 +35,12 @@ public class CategoryInsuranceRelationServiceImpl implements ICategoryInsuranceR @Autowired private CategoryInsuranceRelationMapper categoryInsuranceRelationMapper; + @Resource + private IDeptCategoryInsuranceRelationService deptCategoryInsuranceRelationService; + + @Resource + private DeptGoodsCategoryService deptGoodsCategoryService; + /** * 查询总类目保险关联关系 * @@ -105,20 +120,46 @@ public class CategoryInsuranceRelationServiceImpl implements ICategoryInsuranceR CategoryInsuranceRelation param = new CategoryInsuranceRelation(); param.setGoodsCategoryId(categoryId); List oldRecord = categoryInsuranceRelationMapper.selectCategoryInsuranceRelationList(param); + Map insuranceRelationMap = oldRecord.stream().collect(Collectors.toMap(CategoryInsuranceRelation::getInsuranceId, Function.identity(), (x1, x2)->x1)); + // 已存在的id + List oldIds = oldRecord.stream().map(CategoryInsuranceRelation::getInsuranceId).collect(Collectors.toList()); + // 新的 + List newIds = new ArrayList<>(); + if(StringUtils.isNotEmpty(insuranceIds)){ + for (String id : insuranceIds.split(",")) { + newIds.add(Long.valueOf(id)); + } + } + // 删除旧的 + List delOlds = oldIds.stream().filter(x->!newIds.contains(x)).collect(Collectors.toList()); + // 增加新的 + List addNews = newIds.stream().filter(x->!oldIds.contains(x)).collect(Collectors.toList()); + // 删除历史记录 - if(CollectionUtil.isNotEmpty(oldRecord)){ - String [] arr = new String[oldRecord.size()]; - for (int index = 0; index < oldRecord.size(); index ++){ - arr[index] = oldRecord.get(index).getId().toString(); + if(CollectionUtil.isNotEmpty(delOlds)){ + String [] arr = new String[delOlds.size()]; + for (int index = 0; index < delOlds.size(); index ++){ + arr[index] = insuranceRelationMap.get(delOlds.get(index)).getId().toString(); + // 删除关联的平台类目的 + // 查找类目 + DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(categoryId); + if(deptGoodsCategory != null){ + DeptCategoryInsuranceRelation deptCategoryInsuranceRelation = new DeptCategoryInsuranceRelation(); + deptCategoryInsuranceRelation.setDeptCategoryId(deptGoodsCategory.getDeptGoodsCategoryId()); + deptCategoryInsuranceRelation.setInsuranceId(delOlds.get(index)); + List delList = deptCategoryInsuranceRelationService.selectDeptCategoryInsuranceRelationList(deptCategoryInsuranceRelation); + delList.forEach(model->{ + deptCategoryInsuranceRelationService.deleteDeptCategoryInsuranceRelationById(model.getId()); + }); + } } categoryInsuranceRelationMapper.deleteCategoryInsuranceRelationByIds(arr); } // 增加新记录 - if(StringUtils.isNotEmpty(insuranceIds)){ - String [] insuranceStr = insuranceIds.split(","); - for (String str : insuranceStr) { + if(CollectionUtil.isNotEmpty(addNews)){ + for (Long id : addNews) { CategoryInsuranceRelation model = new CategoryInsuranceRelation(); - model.setInsuranceId(Long.valueOf(str)); + model.setInsuranceId(id); model.setGoodsCategoryId(categoryId); categoryInsuranceRelationMapper.insertCategoryInsuranceRelation(model); } diff --git a/ghy-goods/src/main/resources/mapper/goods/DeptGoodsCategoryMapper.xml b/ghy-goods/src/main/resources/mapper/goods/DeptGoodsCategoryMapper.xml index c261f934..ad6f31db 100644 --- a/ghy-goods/src/main/resources/mapper/goods/DeptGoodsCategoryMapper.xml +++ b/ghy-goods/src/main/resources/mapper/goods/DeptGoodsCategoryMapper.xml @@ -190,6 +190,9 @@ AND gc.goods_category_name LIKE concat('%', #{goodsCategoryName}, '%') + + AND gc.goods_category_id = #{goodsCategoryId} + AND gc.status = #{status} @@ -213,7 +216,7 @@