Commit 2fb55f3649fa7e03347d30bdcaa49b08986c2d53
Committed by
Gitee
!1092 【代码优化】商城: 完善积分商城
Merge pull request !1092 from puhui999/develop
Showing
7 changed files
with
84 additions
and
50 deletions
yudao-module-mall/yudao-module-product-api/src/main/java/cn/iocoder/yudao/module/product/api/spu/ProductSpuApi.java
| ... | ... | @@ -4,6 +4,9 @@ import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; |
| 4 | 4 | |
| 5 | 5 | import java.util.Collection; |
| 6 | 6 | import java.util.List; |
| 7 | +import java.util.Map; | |
| 8 | + | |
| 9 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 7 | 10 | |
| 8 | 11 | /** |
| 9 | 12 | * 商品 SPU API 接口 |
| ... | ... | @@ -22,6 +25,16 @@ public interface ProductSpuApi { |
| 22 | 25 | List<ProductSpuRespDTO> getSpuList(Collection<Long> ids); |
| 23 | 26 | |
| 24 | 27 | /** |
| 28 | + * 批量查询 SPU MAP | |
| 29 | + * | |
| 30 | + * @param ids SPU 编号列表 | |
| 31 | + * @return SPU MAP | |
| 32 | + */ | |
| 33 | + default Map<Long, ProductSpuRespDTO> getSpusMap(Collection<Long> ids) { | |
| 34 | + return convertMap(getSpuList(ids), ProductSpuRespDTO::getId); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 25 | 38 | * 批量查询 SPU 数组,并且校验是否 SPU 是否有效。 |
| 26 | 39 | * |
| 27 | 40 | * 如下情况,视为无效: | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/point/PointActivityController.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.controller.admin.point; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | |
| 6 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | |
| 7 | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 8 | -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; | |
| 8 | +import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; | |
| 9 | +import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; | |
| 9 | 10 | import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO; |
| 10 | 11 | import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityRespVO; |
| 11 | 12 | import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO; |
| 13 | +import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductRespVO; | |
| 12 | 14 | import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO; |
| 15 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO; | |
| 13 | 16 | import cn.iocoder.yudao.module.promotion.service.point.PointActivityService; |
| 14 | 17 | import io.swagger.v3.oas.annotations.Operation; |
| 15 | 18 | import io.swagger.v3.oas.annotations.Parameter; |
| 16 | 19 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 17 | 20 | import jakarta.annotation.Resource; |
| 18 | -import jakarta.servlet.http.HttpServletResponse; | |
| 19 | 21 | import jakarta.validation.Valid; |
| 20 | 22 | import org.springframework.security.access.prepost.PreAuthorize; |
| 21 | 23 | import org.springframework.validation.annotation.Validated; |
| 22 | 24 | import org.springframework.web.bind.annotation.*; |
| 23 | 25 | |
| 24 | -import java.io.IOException; | |
| 26 | +import java.util.Collections; | |
| 25 | 27 | import java.util.List; |
| 28 | +import java.util.Map; | |
| 26 | 29 | |
| 27 | -import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; | |
| 28 | 30 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 31 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap; | |
| 32 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | |
| 29 | 33 | |
| 30 | 34 | @Tag(name = "管理后台 - 积分商城活动") |
| 31 | 35 | @RestController |
| ... | ... | @@ -35,6 +39,8 @@ public class PointActivityController { |
| 35 | 39 | |
| 36 | 40 | @Resource |
| 37 | 41 | private PointActivityService pointActivityService; |
| 42 | + @Resource | |
| 43 | + private ProductSpuApi productSpuApi; | |
| 38 | 44 | |
| 39 | 45 | @PostMapping("/create") |
| 40 | 46 | @Operation(summary = "创建积分商城活动") |
| ... | ... | @@ -75,7 +81,14 @@ public class PointActivityController { |
| 75 | 81 | @PreAuthorize("@ss.hasPermission('promotion:point-activity:query')") |
| 76 | 82 | public CommonResult<PointActivityRespVO> getPointActivity(@RequestParam("id") Long id) { |
| 77 | 83 | PointActivityDO pointActivity = pointActivityService.getPointActivity(id); |
| 78 | - return success(BeanUtils.toBean(pointActivity, PointActivityRespVO.class)); | |
| 84 | + if (pointActivity == null) { | |
| 85 | + return success(null); | |
| 86 | + } | |
| 87 | + | |
| 88 | + List<PointProductDO> products = pointActivityService.getPointProductListByActivityIds(Collections.singletonList(id)); | |
| 89 | + PointActivityRespVO respVO = BeanUtils.toBean(pointActivity, PointActivityRespVO.class); | |
| 90 | + respVO.setProducts(BeanUtils.toBean(products, PointProductRespVO.class)); | |
| 91 | + return success(respVO); | |
| 79 | 92 | } |
| 80 | 93 | |
| 81 | 94 | @GetMapping("/page") |
| ... | ... | @@ -83,20 +96,24 @@ public class PointActivityController { |
| 83 | 96 | @PreAuthorize("@ss.hasPermission('promotion:point-activity:query')") |
| 84 | 97 | public CommonResult<PageResult<PointActivityRespVO>> getPointActivityPage(@Valid PointActivityPageReqVO pageReqVO) { |
| 85 | 98 | PageResult<PointActivityDO> pageResult = pointActivityService.getPointActivityPage(pageReqVO); |
| 86 | - return success(BeanUtils.toBean(pageResult, PointActivityRespVO.class)); | |
| 87 | - } | |
| 99 | + if (CollUtil.isEmpty(pageResult.getList())) { | |
| 100 | + return success(PageResult.empty(pageResult.getTotal())); | |
| 101 | + } | |
| 102 | + | |
| 103 | + // 拼接数据 | |
| 104 | + List<PointProductDO> products = pointActivityService.getPointProductListByActivityIds( | |
| 105 | + convertSet(pageResult.getList(), PointActivityDO::getId)); | |
| 106 | + Map<Long, List<PointProductDO>> productsMap = convertMultiMap(products, PointProductDO::getActivityId); | |
| 107 | + Map<Long, ProductSpuRespDTO> spuMap = productSpuApi.getSpusMap( | |
| 108 | + convertSet(pageResult.getList(), PointActivityDO::getSpuId)); | |
| 109 | + PageResult<PointActivityRespVO> result = BeanUtils.toBean(pageResult, PointActivityRespVO.class); | |
| 110 | + result.getList().forEach(activity -> { | |
| 111 | + activity.setProducts(BeanUtils.toBean(productsMap.get(activity.getId()), PointProductRespVO.class)); | |
| 112 | + MapUtils.findAndThen(spuMap, activity.getSpuId(), | |
| 113 | + spu -> activity.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice())); | |
| 88 | 114 | |
| 89 | - @GetMapping("/export-excel") | |
| 90 | - @Operation(summary = "导出积分商城活动 Excel") | |
| 91 | - @PreAuthorize("@ss.hasPermission('promotion:point-activity:export')") | |
| 92 | - @ApiAccessLog(operateType = EXPORT) | |
| 93 | - public void exportPointActivityExcel(@Valid PointActivityPageReqVO pageReqVO, | |
| 94 | - HttpServletResponse response) throws IOException { | |
| 95 | - pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); | |
| 96 | - List<PointActivityDO> list = pointActivityService.getPointActivityPage(pageReqVO).getList(); | |
| 97 | - // 导出 Excel | |
| 98 | - ExcelUtils.write(response, "积分商城活动.xls", "数据", PointActivityRespVO.class, | |
| 99 | - BeanUtils.toBean(list, PointActivityRespVO.class)); | |
| 115 | + }); | |
| 116 | + return success(result); | |
| 100 | 117 | } |
| 101 | 118 | |
| 102 | 119 | } |
| 103 | 120 | \ No newline at end of file | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/point/vo/activity/PointActivityRespVO.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductSaveReqVO; | |
| 3 | +import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductRespVO; | |
| 4 | 4 | import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; |
| 5 | 5 | import com.alibaba.excel.annotation.ExcelProperty; |
| 6 | 6 | import io.swagger.v3.oas.annotations.media.Schema; |
| ... | ... | @@ -39,7 +39,7 @@ public class PointActivityRespVO { |
| 39 | 39 | private LocalDateTime createTime; |
| 40 | 40 | |
| 41 | 41 | @Schema(description = "积分商城商品", requiredMode = Schema.RequiredMode.REQUIRED) |
| 42 | - private List<PointProductSaveReqVO> products; | |
| 42 | + private List<PointProductRespVO> products; | |
| 43 | 43 | |
| 44 | 44 | // ========== 商品字段 ========== |
| 45 | 45 | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/point/vo/activity/PointActivitySaveReqVO.java
| ... | ... | @@ -18,10 +18,6 @@ public class PointActivitySaveReqVO { |
| 18 | 18 | @NotNull(message = "积分商城活动商品不能为空") |
| 19 | 19 | private Long spuId; |
| 20 | 20 | |
| 21 | - @Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") | |
| 22 | - @NotNull(message = "活动状态不能为空") | |
| 23 | - private Integer status; | |
| 24 | - | |
| 25 | 21 | @Schema(description = "备注", example = "你说的对") |
| 26 | 22 | private String remark; |
| 27 | 23 | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/point/vo/product/PointProductRespVO.java
| 1 | 1 | package cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product; |
| 2 | 2 | |
| 3 | 3 | import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; |
| 4 | -import com.alibaba.excel.annotation.ExcelProperty; | |
| 5 | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
| 6 | 5 | import lombok.Data; |
| 7 | 6 | |
| 8 | -import java.time.LocalDateTime; | |
| 9 | - | |
| 10 | 7 | @Schema(description = "管理后台 - 积分商城商品 Response VO") |
| 11 | 8 | @Data |
| 12 | 9 | @ExcelIgnoreUnannotated |
| 13 | 10 | public class PointProductRespVO { |
| 14 | 11 | |
| 15 | 12 | @Schema(description = "积分商城商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "31718") |
| 16 | - @ExcelProperty("积分商城商品编号") | |
| 17 | 13 | private Long id; |
| 18 | 14 | |
| 19 | 15 | @Schema(description = "积分商城活动 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "29388") |
| 20 | - @ExcelProperty("积分商城活动 id") | |
| 21 | 16 | private Long activityId; |
| 22 | 17 | |
| 23 | 18 | @Schema(description = "商品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "8112") |
| 24 | - @ExcelProperty("商品 SPU 编号") | |
| 25 | 19 | private Long spuId; |
| 26 | 20 | |
| 27 | 21 | @Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2736") |
| 28 | - @ExcelProperty("商品 SKU 编号") | |
| 29 | 22 | private Long skuId; |
| 30 | 23 | |
| 31 | 24 | @Schema(description = "可兑换数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "3926") |
| 32 | - @ExcelProperty("可兑换数量") | |
| 33 | - private Integer maxCount; | |
| 25 | + private Integer count; | |
| 34 | 26 | |
| 35 | 27 | @Schema(description = "兑换积分", requiredMode = Schema.RequiredMode.REQUIRED) |
| 36 | - @ExcelProperty("兑换积分") | |
| 37 | 28 | private Integer point; |
| 38 | 29 | |
| 39 | 30 | @Schema(description = "兑换金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "15860") |
| 40 | - @ExcelProperty("兑换金额,单位:分") | |
| 41 | 31 | private Integer price; |
| 42 | 32 | |
| 43 | - @Schema(description = "兑换类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") | |
| 44 | - @ExcelProperty("兑换类型") | |
| 45 | - private Integer type; | |
| 33 | + @Schema(description = "积分商城商品库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") | |
| 34 | + private Integer stock; | |
| 46 | 35 | |
| 47 | 36 | @Schema(description = "积分商城商品状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") |
| 48 | - @ExcelProperty("积分商城商品状态") | |
| 49 | 37 | private Integer activityStatus; |
| 50 | 38 | |
| 51 | - @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 52 | - @ExcelProperty("创建时间") | |
| 53 | - private LocalDateTime createTime; | |
| 54 | - | |
| 55 | 39 | } |
| 56 | 40 | \ No newline at end of file | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityService.java
| ... | ... | @@ -4,8 +4,12 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 4 | import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO; |
| 5 | 5 | import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO; |
| 6 | 6 | import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO; |
| 7 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO; | |
| 7 | 8 | import jakarta.validation.Valid; |
| 8 | 9 | |
| 10 | +import java.util.Collection; | |
| 11 | +import java.util.List; | |
| 12 | + | |
| 9 | 13 | /** |
| 10 | 14 | * 积分商城活动 Service 接口 |
| 11 | 15 | * |
| ... | ... | @@ -58,4 +62,12 @@ public interface PointActivityService { |
| 58 | 62 | */ |
| 59 | 63 | PageResult<PointActivityDO> getPointActivityPage(PointActivityPageReqVO pageReqVO); |
| 60 | 64 | |
| 65 | + /** | |
| 66 | + * 获得活动商品 | |
| 67 | + * | |
| 68 | + * @param activityIds 活动编号 | |
| 69 | + * @return 获得活动商品 | |
| 70 | + */ | |
| 71 | + List<PointProductDO> getPointProductListByActivityIds(Collection<Long> activityIds); | |
| 72 | + | |
| 61 | 73 | } |
| 62 | 74 | \ No newline at end of file | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/point/PointActivityServiceImpl.java
| ... | ... | @@ -17,8 +17,10 @@ import cn.iocoder.yudao.module.promotion.dal.mysql.point.PointActivityMapper; |
| 17 | 17 | import cn.iocoder.yudao.module.promotion.dal.mysql.point.PointProductMapper; |
| 18 | 18 | import jakarta.annotation.Resource; |
| 19 | 19 | import org.springframework.stereotype.Service; |
| 20 | +import org.springframework.transaction.annotation.Transactional; | |
| 20 | 21 | import org.springframework.validation.annotation.Validated; |
| 21 | 22 | |
| 23 | +import java.util.Collection; | |
| 22 | 24 | import java.util.List; |
| 23 | 25 | import java.util.Map; |
| 24 | 26 | |
| ... | ... | @@ -51,7 +53,14 @@ public class PointActivityServiceImpl implements PointActivityService { |
| 51 | 53 | @Resource |
| 52 | 54 | private ProductSkuApi productSkuApi; |
| 53 | 55 | |
| 56 | + private static List<PointProductDO> buildPointProductDO(PointActivityDO pointActivity, List<PointProductSaveReqVO> products) { | |
| 57 | + return BeanUtils.toBean(products, PointProductDO.class, product -> | |
| 58 | + product.setSpuId(pointActivity.getSpuId()).setActivityId(pointActivity.getId()) | |
| 59 | + .setActivityStatus(pointActivity.getStatus())); | |
| 60 | + } | |
| 61 | + | |
| 54 | 62 | @Override |
| 63 | + @Transactional(rollbackFor = Exception.class) | |
| 55 | 64 | public Long createPointActivity(PointActivitySaveReqVO createReqVO) { |
| 56 | 65 | // 1.1 校验商品是否存在 |
| 57 | 66 | validateProductExists(createReqVO.getSpuId(), createReqVO.getProducts()); |
| ... | ... | @@ -69,12 +78,8 @@ public class PointActivityServiceImpl implements PointActivityService { |
| 69 | 78 | return pointActivity.getId(); |
| 70 | 79 | } |
| 71 | 80 | |
| 72 | - private static List<PointProductDO> buildPointProductDO(PointActivityDO pointActivity, List<PointProductSaveReqVO> products) { | |
| 73 | - return BeanUtils.toBean(products, PointProductDO.class, product -> | |
| 74 | - product.setActivityId(pointActivity.getId()).setActivityStatus(pointActivity.getStatus())); | |
| 75 | - } | |
| 76 | - | |
| 77 | 81 | @Override |
| 82 | + @Transactional(rollbackFor = Exception.class) | |
| 78 | 83 | public void updatePointActivity(PointActivitySaveReqVO updateReqVO) { |
| 79 | 84 | // 1.1 校验存在 |
| 80 | 85 | PointActivityDO activity = validatePointActivityExists(updateReqVO.getId()); |
| ... | ... | @@ -98,6 +103,7 @@ public class PointActivityServiceImpl implements PointActivityService { |
| 98 | 103 | } |
| 99 | 104 | |
| 100 | 105 | @Override |
| 106 | + @Transactional(rollbackFor = Exception.class) | |
| 101 | 107 | public void closePointActivity(Long id) { |
| 102 | 108 | // 校验存在 |
| 103 | 109 | PointActivityDO pointActivity = validatePointActivityExists(id); |
| ... | ... | @@ -143,6 +149,7 @@ public class PointActivityServiceImpl implements PointActivityService { |
| 143 | 149 | } |
| 144 | 150 | |
| 145 | 151 | @Override |
| 152 | + @Transactional(rollbackFor = Exception.class) | |
| 146 | 153 | public void deletePointActivity(Long id) { |
| 147 | 154 | // 校验存在 |
| 148 | 155 | PointActivityDO pointActivity = validatePointActivityExists(id); |
| ... | ... | @@ -227,4 +234,9 @@ public class PointActivityServiceImpl implements PointActivityService { |
| 227 | 234 | return pointActivityMapper.selectPage(pageReqVO); |
| 228 | 235 | } |
| 229 | 236 | |
| 237 | + @Override | |
| 238 | + public List<PointProductDO> getPointProductListByActivityIds(Collection<Long> activityIds) { | |
| 239 | + return pointProductMapper.selectListByActivityId(activityIds); | |
| 240 | + } | |
| 241 | + | |
| 230 | 242 | } |
| 231 | 243 | \ No newline at end of file | ... | ... |