【代码优化】商城: 完善积分商城

This commit is contained in:
puhui999 2024-09-22 23:33:21 +08:00
parent e99a6e4ec8
commit 67705e7232
7 changed files with 84 additions and 50 deletions

View File

@ -4,6 +4,9 @@ import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/**
* 商品 SPU API 接口
@ -21,6 +24,16 @@ public interface ProductSpuApi {
*/
List<ProductSpuRespDTO> getSpuList(Collection<Long> ids);
/**
* 批量查询 SPU MAP
*
* @param ids SPU 编号列表
* @return SPU MAP
*/
default Map<Long, ProductSpuRespDTO> getSpusMap(Collection<Long> ids) {
return convertMap(getSpuList(ids), ProductSpuRespDTO::getId);
}
/**
* 批量查询 SPU 数组并且校验是否 SPU 是否有效
*

View File

@ -1,31 +1,35 @@
package cn.iocoder.yudao.module.promotion.controller.admin.point;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO;
import cn.iocoder.yudao.module.promotion.service.point.PointActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@Tag(name = "管理后台 - 积分商城活动")
@RestController
@ -35,6 +39,8 @@ public class PointActivityController {
@Resource
private PointActivityService pointActivityService;
@Resource
private ProductSpuApi productSpuApi;
@PostMapping("/create")
@Operation(summary = "创建积分商城活动")
@ -75,7 +81,14 @@ public class PointActivityController {
@PreAuthorize("@ss.hasPermission('promotion:point-activity:query')")
public CommonResult<PointActivityRespVO> getPointActivity(@RequestParam("id") Long id) {
PointActivityDO pointActivity = pointActivityService.getPointActivity(id);
return success(BeanUtils.toBean(pointActivity, PointActivityRespVO.class));
if (pointActivity == null) {
return success(null);
}
List<PointProductDO> products = pointActivityService.getPointProductListByActivityIds(Collections.singletonList(id));
PointActivityRespVO respVO = BeanUtils.toBean(pointActivity, PointActivityRespVO.class);
respVO.setProducts(BeanUtils.toBean(products, PointProductRespVO.class));
return success(respVO);
}
@GetMapping("/page")
@ -83,20 +96,24 @@ public class PointActivityController {
@PreAuthorize("@ss.hasPermission('promotion:point-activity:query')")
public CommonResult<PageResult<PointActivityRespVO>> getPointActivityPage(@Valid PointActivityPageReqVO pageReqVO) {
PageResult<PointActivityDO> pageResult = pointActivityService.getPointActivityPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, PointActivityRespVO.class));
if (CollUtil.isEmpty(pageResult.getList())) {
return success(PageResult.empty(pageResult.getTotal()));
}
@GetMapping("/export-excel")
@Operation(summary = "导出积分商城活动 Excel")
@PreAuthorize("@ss.hasPermission('promotion:point-activity:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportPointActivityExcel(@Valid PointActivityPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<PointActivityDO> list = pointActivityService.getPointActivityPage(pageReqVO).getList();
// 导出 Excel
ExcelUtils.write(response, "积分商城活动.xls", "数据", PointActivityRespVO.class,
BeanUtils.toBean(list, PointActivityRespVO.class));
// 拼接数据
List<PointProductDO> products = pointActivityService.getPointProductListByActivityIds(
convertSet(pageResult.getList(), PointActivityDO::getId));
Map<Long, List<PointProductDO>> productsMap = convertMultiMap(products, PointProductDO::getActivityId);
Map<Long, ProductSpuRespDTO> spuMap = productSpuApi.getSpusMap(
convertSet(pageResult.getList(), PointActivityDO::getSpuId));
PageResult<PointActivityRespVO> result = BeanUtils.toBean(pageResult, PointActivityRespVO.class);
result.getList().forEach(activity -> {
activity.setProducts(BeanUtils.toBean(productsMap.get(activity.getId()), PointProductRespVO.class));
MapUtils.findAndThen(spuMap, activity.getSpuId(),
spu -> activity.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl()).setMarketPrice(spu.getMarketPrice()));
});
return success(result);
}
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductSaveReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductRespVO;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
@ -39,7 +39,7 @@ public class PointActivityRespVO {
private LocalDateTime createTime;
@Schema(description = "积分商城商品", requiredMode = Schema.RequiredMode.REQUIRED)
private List<PointProductSaveReqVO> products;
private List<PointProductRespVO> products;
// ========== 商品字段 ==========

View File

@ -18,10 +18,6 @@ public class PointActivitySaveReqVO {
@NotNull(message = "积分商城活动商品不能为空")
private Long spuId;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@NotNull(message = "活动状态不能为空")
private Integer status;
@Schema(description = "备注", example = "你说的对")
private String remark;

View File

@ -1,55 +1,39 @@
package cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 积分商城商品 Response VO")
@Data
@ExcelIgnoreUnannotated
public class PointProductRespVO {
@Schema(description = "积分商城商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "31718")
@ExcelProperty("积分商城商品编号")
private Long id;
@Schema(description = "积分商城活动 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "29388")
@ExcelProperty("积分商城活动 id")
private Long activityId;
@Schema(description = "商品 SPU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "8112")
@ExcelProperty("商品 SPU 编号")
private Long spuId;
@Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2736")
@ExcelProperty("商品 SKU 编号")
private Long skuId;
@Schema(description = "可兑换数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "3926")
@ExcelProperty("可兑换数量")
private Integer maxCount;
private Integer count;
@Schema(description = "兑换积分", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("兑换积分")
private Integer point;
@Schema(description = "兑换金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "15860")
@ExcelProperty("兑换金额,单位:分")
private Integer price;
@Schema(description = "兑换类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@ExcelProperty("兑换类型")
private Integer type;
@Schema(description = "积分商城商品库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
private Integer stock;
@Schema(description = "积分商城商品状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@ExcelProperty("积分商城商品状态")
private Integer activityStatus;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@ -4,8 +4,12 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO;
import jakarta.validation.Valid;
import java.util.Collection;
import java.util.List;
/**
* 积分商城活动 Service 接口
*
@ -58,4 +62,12 @@ public interface PointActivityService {
*/
PageResult<PointActivityDO> getPointActivityPage(PointActivityPageReqVO pageReqVO);
/**
* 获得活动商品
*
* @param activityIds 活动编号
* @return 获得活动商品
*/
List<PointProductDO> getPointProductListByActivityIds(Collection<Long> activityIds);
}

View File

@ -17,8 +17,10 @@ import cn.iocoder.yudao.module.promotion.dal.mysql.point.PointActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.point.PointProductMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -51,7 +53,14 @@ public class PointActivityServiceImpl implements PointActivityService {
@Resource
private ProductSkuApi productSkuApi;
private static List<PointProductDO> buildPointProductDO(PointActivityDO pointActivity, List<PointProductSaveReqVO> products) {
return BeanUtils.toBean(products, PointProductDO.class, product ->
product.setSpuId(pointActivity.getSpuId()).setActivityId(pointActivity.getId())
.setActivityStatus(pointActivity.getStatus()));
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long createPointActivity(PointActivitySaveReqVO createReqVO) {
// 1.1 校验商品是否存在
validateProductExists(createReqVO.getSpuId(), createReqVO.getProducts());
@ -69,12 +78,8 @@ public class PointActivityServiceImpl implements PointActivityService {
return pointActivity.getId();
}
private static List<PointProductDO> buildPointProductDO(PointActivityDO pointActivity, List<PointProductSaveReqVO> products) {
return BeanUtils.toBean(products, PointProductDO.class, product ->
product.setActivityId(pointActivity.getId()).setActivityStatus(pointActivity.getStatus()));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updatePointActivity(PointActivitySaveReqVO updateReqVO) {
// 1.1 校验存在
PointActivityDO activity = validatePointActivityExists(updateReqVO.getId());
@ -98,6 +103,7 @@ public class PointActivityServiceImpl implements PointActivityService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void closePointActivity(Long id) {
// 校验存在
PointActivityDO pointActivity = validatePointActivityExists(id);
@ -143,6 +149,7 @@ public class PointActivityServiceImpl implements PointActivityService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deletePointActivity(Long id) {
// 校验存在
PointActivityDO pointActivity = validatePointActivityExists(id);
@ -227,4 +234,9 @@ public class PointActivityServiceImpl implements PointActivityService {
return pointActivityMapper.selectPage(pageReqVO);
}
@Override
public List<PointProductDO> getPointProductListByActivityIds(Collection<Long> activityIds) {
return pointProductMapper.selectListByActivityId(activityIds);
}
}