Commit 446951bd119f27959c9574c456e53c56f86b1c9b
1 parent
6a387387
fix:封装函数 convertCDUMap 作用:数据划分为需要新增的、还是删除的、还是更新的。
Showing
6 changed files
with
96 additions
and
51 deletions
pom.xml
| ... | ... | @@ -16,11 +16,11 @@ |
| 16 | 16 | <module>yudao-module-member</module> |
| 17 | 17 | <module>yudao-module-system</module> |
| 18 | 18 | <module>yudao-module-infra</module> |
| 19 | -<!-- <module>yudao-module-pay</module>--> | |
| 19 | + <module>yudao-module-pay</module> | |
| 20 | 20 | <!-- <module>yudao-module-bpm</module>--> |
| 21 | 21 | <!-- <module>yudao-module-report</module>--> |
| 22 | 22 | <!-- <module>yudao-module-mp</module>--> |
| 23 | -<!-- <module>yudao-module-mall</module>--> | |
| 23 | + <module>yudao-module-mall</module> | |
| 24 | 24 | <!-- 示例项目 --> |
| 25 | 25 | <module>yudao-example</module> |
| 26 | 26 | </modules> | ... | ... |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.common.util.collection; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 5 | +import cn.hutool.core.map.MapUtil; | |
| 5 | 6 | import com.google.common.collect.ImmutableMap; |
| 6 | 7 | |
| 7 | 8 | import java.util.*; |
| ... | ... | @@ -149,6 +150,41 @@ public class CollectionUtils { |
| 149 | 150 | return builder.build(); |
| 150 | 151 | } |
| 151 | 152 | |
| 153 | + /** | |
| 154 | + * 数据划分为需要新增的、还是删除的、还是更新的。 | |
| 155 | + * | |
| 156 | + * @param list1 需要处理的数据的相关编号列表 | |
| 157 | + * @param list2 数据库中存在的数据的相关编号列表 | |
| 158 | + * @param func 比较出哪些记录是新增,还是修改,还是删除 | |
| 159 | + * @return 包含需要新增、修改、删除的数据 Map 对象 | |
| 160 | + */ | |
| 161 | + public static <D> Map<String, List<D>> convertCDUMap(Collection<Long> list1, Collection<Long> list2, | |
| 162 | + Function<Map<String, List<Long>>, Map<String, List<D>>> func) { | |
| 163 | + HashMap<String, List<Long>> mapData = MapUtil.newHashMap(3); | |
| 164 | + if (CollUtil.isEmpty(list1)) { | |
| 165 | + return func.apply(mapData); | |
| 166 | + } | |
| 167 | + if (CollUtil.isEmpty(list2)) { | |
| 168 | + return func.apply(mapData); | |
| 169 | + } | |
| 170 | + // 后台存在的前端不存在的 | |
| 171 | + List<Long> d = CollectionUtils.filterList(list2, item -> !list1.contains(item)); | |
| 172 | + if (CollUtil.isNotEmpty(d)) { | |
| 173 | + mapData.put("delete", d); | |
| 174 | + } | |
| 175 | + // 前端存在的后端不存在的 | |
| 176 | + List<Long> c = CollectionUtils.filterList(list1, item -> !list2.contains(item)); | |
| 177 | + if (CollUtil.isNotEmpty(c)) { | |
| 178 | + mapData.put("create", c); | |
| 179 | + } | |
| 180 | + // 更新已存在的 | |
| 181 | + List<Long> u = CollectionUtils.filterList(list1, list2::contains); | |
| 182 | + if (CollUtil.isNotEmpty(u)) { | |
| 183 | + mapData.put("update", u); | |
| 184 | + } | |
| 185 | + return func.apply(mapData); | |
| 186 | + } | |
| 187 | + | |
| 152 | 188 | public static boolean containsAny(Collection<?> source, Collection<?> candidates) { |
| 153 | 189 | return org.springframework.util.CollectionUtils.containsAny(source, candidates); |
| 154 | 190 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/combination/CombinationActivityServiceImpl.java
| ... | ... | @@ -2,10 +2,12 @@ package cn.iocoder.yudao.module.promotion.service.combination; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 5 | +import cn.hutool.core.map.MapUtil; | |
| 5 | 6 | import cn.hutool.core.util.ObjectUtil; |
| 6 | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | 9 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 10 | +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | |
| 9 | 11 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; |
| 10 | 12 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; |
| 11 | 13 | import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; |
| ... | ... | @@ -31,9 +33,7 @@ import org.springframework.validation.annotation.Validated; |
| 31 | 33 | |
| 32 | 34 | import javax.annotation.Resource; |
| 33 | 35 | import java.time.LocalDateTime; |
| 34 | -import java.util.Collection; | |
| 35 | -import java.util.List; | |
| 36 | -import java.util.Set; | |
| 36 | +import java.util.*; | |
| 37 | 37 | |
| 38 | 38 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 39 | 39 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SPU_NOT_EXISTS; |
| ... | ... | @@ -67,7 +67,7 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 67 | 67 | // 获取所选 spu下的所有 sku |
| 68 | 68 | List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollectionUtil.newArrayList(createReqVO.getSpuId())); |
| 69 | 69 | // 校验商品 sku 是否存在 |
| 70 | - validateProductSkuExistence(skus, createReqVO.getProducts(), CombinationProductCreateReqVO::getSkuId); | |
| 70 | + validateProductSkuExistence(createReqVO.getProducts(), skus, CombinationProductCreateReqVO::getSkuId); | |
| 71 | 71 | |
| 72 | 72 | // TODO 艿艿 有个小问题:现在有活动时间和限制时长,活动时间的结束时间早于设置的限制时间怎么算状态比如: |
| 73 | 73 | // 活动时间 2023-08-05 15:00:00 - 2023-08-05 15:20:00 限制时长 2小时,那么活动时间结束就结束还是加时到满两小时 |
| ... | ... | @@ -119,7 +119,7 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 119 | 119 | // 获取所选 spu下的所有 sku |
| 120 | 120 | List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollectionUtil.newArrayList(updateReqVO.getSpuId())); |
| 121 | 121 | // 校验商品 sku 是否存在 |
| 122 | - validateProductSkuExistence(skus, updateReqVO.getProducts(), CombinationProductUpdateReqVO::getSkuId); | |
| 122 | + validateProductSkuExistence(updateReqVO.getProducts(), skus, CombinationProductUpdateReqVO::getSkuId); | |
| 123 | 123 | |
| 124 | 124 | // 更新 |
| 125 | 125 | CombinationActivityDO updateObj = CombinationActivityConvert.INSTANCE.convert(updateReqVO); |
| ... | ... | @@ -130,7 +130,6 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * 更新秒杀商品 |
| 133 | - * TODO 更新商品要不要封装成通用方法 | |
| 134 | 133 | * |
| 135 | 134 | * @param updateObj DO |
| 136 | 135 | * @param products 商品配置 |
| ... | ... | @@ -141,25 +140,28 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 141 | 140 | Set<Long> convertSet = CollectionUtils.convertSet(combinationProductDOs, CombinationProductDO::getSkuId); |
| 142 | 141 | // 前端传过来的活动商品 |
| 143 | 142 | Set<Long> convertSet1 = CollectionUtils.convertSet(products, CombinationProductUpdateReqVO::getSkuId); |
| 144 | - // 删除后台存在的前端不存在的商品 | |
| 145 | - List<Long> d = CollectionUtils.filterList(convertSet, item -> !convertSet1.contains(item)); | |
| 146 | - if (CollUtil.isNotEmpty(d)) { | |
| 147 | - combinationProductMapper.deleteBatchIds(d); | |
| 148 | - } | |
| 149 | - // 前端存在的后端不存在的商品 | |
| 150 | - List<Long> c = CollectionUtils.filterList(convertSet1, item -> !convertSet.contains(item)); | |
| 151 | - if (CollUtil.isNotEmpty(c)) { | |
| 152 | - List<CombinationProductUpdateReqVO> vos = CollectionUtils.filterList(products, item -> c.contains(item.getSkuId())); | |
| 153 | - List<CombinationProductDO> productDOs = CombinationActivityConvert.INSTANCE.convertList(updateObj, vos); | |
| 154 | - combinationProductMapper.insertBatch(productDOs); | |
| 155 | - } | |
| 156 | - // 更新已存在的商品 | |
| 157 | - List<Long> u = CollectionUtils.filterList(convertSet1, convertSet::contains); | |
| 158 | - if (CollUtil.isNotEmpty(u)) { | |
| 159 | - List<CombinationProductUpdateReqVO> vos = CollectionUtils.filterList(products, item -> u.contains(item.getSkuId())); | |
| 160 | - List<CombinationProductDO> productDOs = CombinationActivityConvert.INSTANCE.convertList1(updateObj, vos, combinationProductDOs); | |
| 161 | - combinationProductMapper.updateBatch(productDOs); | |
| 162 | - } | |
| 143 | + // 分化数据 | |
| 144 | + Map<String, List<CombinationProductDO>> data = CollectionUtils.convertCDUMap(convertSet1, convertSet, mapData -> { | |
| 145 | + HashMap<String, List<CombinationProductDO>> cdu = MapUtil.newHashMap(3); | |
| 146 | + MapUtils.findAndThen(mapData, "create", list -> { | |
| 147 | + cdu.put("create", CombinationActivityConvert.INSTANCE.convertList(updateObj, | |
| 148 | + CollectionUtils.filterList(products, item -> list.contains(item.getSkuId())))); | |
| 149 | + }); | |
| 150 | + MapUtils.findAndThen(mapData, "delete", list -> { | |
| 151 | + cdu.put("create", CollectionUtils.filterList(combinationProductDOs, item -> list.contains(item.getSkuId()))); | |
| 152 | + }); | |
| 153 | + MapUtils.findAndThen(mapData, "update", list -> { | |
| 154 | + cdu.put("update", CombinationActivityConvert.INSTANCE.convertList1(updateObj, | |
| 155 | + CollectionUtils.filterList(products, item -> list.contains(item.getSkuId())), combinationProductDOs)); | |
| 156 | + }); | |
| 157 | + return cdu; | |
| 158 | + }); | |
| 159 | + | |
| 160 | + // 执行增删改 | |
| 161 | + MapUtils.findAndThen(data, "create", item -> combinationProductMapper.insertBatch(item)); | |
| 162 | + MapUtils.findAndThen(data, "delete", item -> combinationProductMapper.deleteBatchIds( | |
| 163 | + CollectionUtils.convertSet(item, CombinationProductDO::getId))); | |
| 164 | + MapUtils.findAndThen(data, "update", item -> combinationProductMapper.updateBatch(item)); | |
| 163 | 165 | } |
| 164 | 166 | |
| 165 | 167 | @Override |
| ... | ... | @@ -274,6 +276,7 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic |
| 274 | 276 | } |
| 275 | 277 | |
| 276 | 278 | // TODO @puhui999:status 传入进来搞哈; |
| 279 | + | |
| 277 | 280 | /** |
| 278 | 281 | * APP 端获取开团记录 |
| 279 | 282 | * | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/seckill/seckillactivity/SeckillActivityServiceImpl.java
| ... | ... | @@ -64,7 +64,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { |
| 64 | 64 | List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollUtil.newArrayList(createReqVO.getSpuId())); |
| 65 | 65 | // 校验商品 sku 是否存在 |
| 66 | 66 | // TODO @puhui999:直接校验 sku 数量,是不是就完事啦,不需要校验的特别严谨哈; |
| 67 | - validateProductSkuExistence(skus, createReqVO.getProducts(), SeckillProductCreateReqVO::getSkuId); | |
| 67 | + validateProductSkuExistence(createReqVO.getProducts(), skus, SeckillProductCreateReqVO::getSkuId); | |
| 68 | 68 | |
| 69 | 69 | // 插入秒杀活动 |
| 70 | 70 | SeckillActivityDO activity = SeckillActivityConvert.INSTANCE.convert(createReqVO) |
| ... | ... | @@ -122,7 +122,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService { |
| 122 | 122 | // 获取所选 spu下的所有 sku |
| 123 | 123 | List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollUtil.newArrayList(updateReqVO.getSpuId())); |
| 124 | 124 | // 校验商品 sku 是否存在 |
| 125 | - validateProductSkuExistence(skus, updateReqVO.getProducts(), SeckillProductUpdateReqVO::getSkuId); | |
| 125 | + validateProductSkuExistence(updateReqVO.getProducts(), skus, SeckillProductUpdateReqVO::getSkuId); | |
| 126 | 126 | |
| 127 | 127 | // 更新活动 |
| 128 | 128 | SeckillActivityDO updateObj = SeckillActivityConvert.INSTANCE.convert(updateReqVO) | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/util/PromotionUtils.java
| ... | ... | @@ -31,8 +31,14 @@ public class PromotionUtils { |
| 31 | 31 | return LocalDateTimeUtils.beforeNow(endTime) ? CommonStatusEnum.DISABLE.getStatus() : CommonStatusEnum.ENABLE.getStatus(); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - // TODO @puhui999:写个注释哈。 | |
| 35 | - public static <T> void validateProductSkuExistence(List<ProductSkuRespDTO> skus, List<T> products, Function<T, Long> func) { | |
| 34 | + /** | |
| 35 | + * 校验商品 sku 是否存在 | |
| 36 | + * | |
| 37 | + * @param products 需要校验的商品 | |
| 38 | + * @param skus 数据库中的商品 skus | |
| 39 | + * @param func 获取需要校验的商品的 skuId | |
| 40 | + */ | |
| 41 | + public static <T> void validateProductSkuExistence(List<T> products, List<ProductSkuRespDTO> skus, Function<T, Long> func) { | |
| 36 | 42 | // 校验 sku 个数是否一致 |
| 37 | 43 | Set<Long> skuIdsSet = CollectionUtils.convertSet(products, func); |
| 38 | 44 | Set<Long> skuIdsSet1 = CollectionUtils.convertSet(skus, ProductSkuRespDTO::getId); | ... | ... |
yudao-server/pom.xml
| ... | ... | @@ -54,11 +54,11 @@ |
| 54 | 54 | <!-- <version>${revision}</version>--> |
| 55 | 55 | <!-- </dependency>--> |
| 56 | 56 | <!-- 支付服务。默认注释,保证编译速度 --> |
| 57 | -<!-- <dependency>--> | |
| 58 | -<!-- <groupId>cn.iocoder.boot</groupId>--> | |
| 59 | -<!-- <artifactId>yudao-module-pay-biz</artifactId>--> | |
| 60 | -<!-- <version>${revision}</version>--> | |
| 61 | -<!-- </dependency>--> | |
| 57 | + <dependency> | |
| 58 | + <groupId>cn.iocoder.boot</groupId> | |
| 59 | + <artifactId>yudao-module-pay-biz</artifactId> | |
| 60 | + <version>${revision}</version> | |
| 61 | + </dependency> | |
| 62 | 62 | |
| 63 | 63 | <!-- 微信公众号模块。默认注释,保证编译速度 --> |
| 64 | 64 | <!-- <dependency>--> |
| ... | ... | @@ -68,21 +68,21 @@ |
| 68 | 68 | <!-- </dependency>--> |
| 69 | 69 | |
| 70 | 70 | <!-- 商城相关模块。默认注释,保证编译速度 --> |
| 71 | -<!-- <dependency>--> | |
| 72 | -<!-- <groupId>cn.iocoder.boot</groupId>--> | |
| 73 | -<!-- <artifactId>yudao-module-promotion-biz</artifactId>--> | |
| 74 | -<!-- <version>${revision}</version>--> | |
| 75 | -<!-- </dependency>--> | |
| 76 | -<!-- <dependency>--> | |
| 77 | -<!-- <groupId>cn.iocoder.boot</groupId>--> | |
| 78 | -<!-- <artifactId>yudao-module-product-biz</artifactId>--> | |
| 79 | -<!-- <version>${revision}</version>--> | |
| 80 | -<!-- </dependency>--> | |
| 81 | -<!-- <dependency>--> | |
| 82 | -<!-- <groupId>cn.iocoder.boot</groupId>--> | |
| 83 | -<!-- <artifactId>yudao-module-trade-biz</artifactId>--> | |
| 84 | -<!-- <version>${revision}</version>--> | |
| 85 | -<!-- </dependency>--> | |
| 71 | + <dependency> | |
| 72 | + <groupId>cn.iocoder.boot</groupId> | |
| 73 | + <artifactId>yudao-module-promotion-biz</artifactId> | |
| 74 | + <version>${revision}</version> | |
| 75 | + </dependency> | |
| 76 | + <dependency> | |
| 77 | + <groupId>cn.iocoder.boot</groupId> | |
| 78 | + <artifactId>yudao-module-product-biz</artifactId> | |
| 79 | + <version>${revision}</version> | |
| 80 | + </dependency> | |
| 81 | + <dependency> | |
| 82 | + <groupId>cn.iocoder.boot</groupId> | |
| 83 | + <artifactId>yudao-module-trade-biz</artifactId> | |
| 84 | + <version>${revision}</version> | |
| 85 | + </dependency> | |
| 86 | 86 | |
| 87 | 87 | <!-- spring boot 配置所需依赖 --> |
| 88 | 88 | <dependency> | ... | ... |