code review:砍价的代码

This commit is contained in:
YunaiV 2023-08-13 00:49:35 +08:00
parent ba31d5f6fb
commit e159b2e500
15 changed files with 81 additions and 144 deletions

View File

@ -1,8 +1,10 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.bargain.vo.activity.BargainActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityRespVO;
@ -20,8 +22,8 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static cn.hutool.core.collection.CollectionUtil.newArrayList;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -37,7 +39,7 @@ public class BargainActivityController {
private BargainActivityService activityService;
@Resource
private ProductSpuApi spuApi;
private ProductSpuApi productSpuApi;
@PostMapping("/create")
@Operation(summary = "创建砍价活动")
@ -80,12 +82,17 @@ public class BargainActivityController {
@Valid BargainActivityPageReqVO pageVO) {
// 查询砍价活动
PageResult<BargainActivityDO> pageResult = activityService.getBargainActivityPage(pageVO);
if (CollUtil.isEmpty(pageResult.getList())) {
return success(PageResult.empty(pageResult.getTotal()));
}
// 拼接数据
Set<Long> activityIds = convertSet(pageResult.getList(), BargainActivityDO::getId);
Set<Long> spuIds = convertSet(pageResult.getList(), BargainActivityDO::getSpuId);
return success(BargainActivityConvert.INSTANCE.convertPage(pageResult,
activityService.getBargainProductsByActivityIds(activityIds),
spuApi.getSpuList(spuIds)));
// List<BargainProductDO> products = activityService.getBargainProductsByActivityIds(
// convertSet(pageResult.getList(), BargainActivityDO::getId));
List<BargainProductDO> products = Collections.emptyList();
List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(
convertSet(pageResult.getList(), BargainActivityDO::getSpuId));
return success(BargainActivityConvert.INSTANCE.convertPage(pageResult, products, spus));
}
}

View File

@ -22,7 +22,7 @@ public class BargainActivityBaseVO {
@NotNull(message = "砍价名称不能为空")
private String name;
@Schema(description = "商品 SPU 编号,关联 ProductSpuDO 的 id", example = "[1,2,3]")
@Schema(description = "商品 SPU 编号", example = "1")
@NotNull(message = "砍价商品不能为空")
private Long spuId;

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductBaseVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -17,6 +17,6 @@ public class BargainActivityCreateReqVO extends BargainActivityBaseVO {
@Schema(description = "砍价商品", requiredMode = Schema.RequiredMode.REQUIRED)
@Valid
private List<BargainProductCreateReqVO> products;
private List<BargainProductBaseVO> products;
}

View File

@ -20,15 +20,6 @@ public class BargainActivityPageReqVO extends PageParam {
@Schema(description = "砍价名称", example = "赵六")
private String name;
@Schema(description = "商品 SPU 编号关联 ProductSpuDO 的 id", example = "14016")
private Long spuId;
@Schema(description = "总限购数量", example = "16218")
private Integer totalLimitCount;
@Schema(description = "单次限购数量", example = "28265")
private Integer singleLimitCount;
@Schema(description = "开始时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] startTime;
@ -37,29 +28,7 @@ public class BargainActivityPageReqVO extends PageParam {
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] endTime;
@Schema(description = "开团人数")
private Integer userSize;
@Schema(description = "开团组数")
private Integer totalNum;
@Schema(description = "成团组数")
private Integer successNum;
@Schema(description = "参与人数", example = "25222")
private Integer orderUserCount;
@Schema(description = "虚拟成团")
private Integer virtualGroup;
@Schema(description = "活动状态0开启 1关闭", example = "0")
private Integer status;
@Schema(description = "限制时长(小时)")
private Integer limitDuration;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -6,7 +6,6 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
@ -17,8 +16,6 @@ import java.util.List;
@ToString(callSuper = true)
public class BargainActivityRespVO extends BargainActivityBaseVO {
// TODO @puhui999example 补全
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "618大促")
private String spuName;
@ -31,7 +28,7 @@ public class BargainActivityRespVO extends BargainActivityBaseVO {
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
@Schema(description = "砍价成功数量", requiredMode = Schema.RequiredMode.REQUIRED)
@Schema(description = "砍价成功数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "888")
private Integer successCount;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@ -39,7 +36,6 @@ public class BargainActivityRespVO extends BargainActivityBaseVO {
private Integer status;
@Schema(description = "砍价商品", requiredMode = Schema.RequiredMode.REQUIRED)
@Valid
private List<BargainProductRespVO> products;
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductBaseVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -22,6 +22,6 @@ public class BargainActivityUpdateReqVO extends BargainActivityBaseVO {
@Schema(description = "砍价商品", requiredMode = Schema.RequiredMode.REQUIRED)
@Valid
private List<BargainProductUpdateReqVO> products;
private List<BargainProductBaseVO> products;
}

View File

@ -16,19 +16,19 @@ public class BargainProductBaseVO {
@NotNull(message = "商品 spuId 不能为空")
private Long spuId;
@Schema(description = "商品 skuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@Schema(description = "商品 skuId", requiredMode = Schema.RequiredMode.REQUIRED, example = "44")
@NotNull(message = "商品 skuId 不能为空")
private Long skuId;
@Schema(description = "砍价起始价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@Schema(description = "砍价起始价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "33")
@NotNull(message = "砍价起始价格不能为空")
private Integer bargainFirstPrice;
@Schema(description = "砍价底价", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@Schema(description = "砍价底价", requiredMode = Schema.RequiredMode.REQUIRED, example = "22")
@NotNull(message = "砍价底价不能为空")
private Integer bargainPrice;
@Schema(description = "活动库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@Schema(description = "活动库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
@NotNull(message = "活动库存不能为空")
private Integer stock;

View File

@ -1,15 +0,0 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
// TODO @puhui999是不是应该把 BargainProductCreateReqVO BargainProductUpdateReqVO 合并在一起哈就是一个 SaveReqVO
@Schema(description = "管理后台 - 砍价商品创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class BargainProductCreateReqVO extends BargainProductBaseVO {
}

View File

@ -1,14 +0,0 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Schema(description = "管理后台 - 砍价商品更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class BargainProductUpdateReqVO extends BargainProductBaseVO {
}

View File

@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.Ba
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductUpdateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainProductDO;
import org.mapstruct.Mapper;
@ -40,8 +39,9 @@ public interface BargainActivityConvert {
BargainProductRespVO convert(BargainProductDO bean);
default BargainActivityRespVO convert(BargainActivityDO bean, List<BargainProductDO> productDOs) {
return convert(bean).setProducts(convertList2(productDOs));
default BargainActivityRespVO convert(BargainActivityDO activity,
List<BargainProductDO> products) {
return convert(activity).setProducts(convertList2(products));
}
List<BargainActivityRespVO> convertList(List<BargainActivityDO> list);
@ -51,39 +51,37 @@ public interface BargainActivityConvert {
default PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page,
List<BargainProductDO> productList,
List<ProductSpuRespDTO> spuList) {
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
PageResult<BargainActivityRespVO> pageResult = convertPage(page);
// 拼接商品
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
pageResult.getList().forEach(item -> {
MapUtils.findAndThen(spuMap, item.getSpuId(), spu -> {
item.setSpuName(spu.getName());
item.setPicUrl(spu.getPicUrl());
});
item.setProducts(convertList2(productList));
MapUtils.findAndThen(spuMap, item.getSpuId(),
spu -> item.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl()));
});
return pageResult;
}
List<BargainProductRespVO> convertList2(List<BargainProductDO> productDOs);
// TODO @puhui999参数改成 activityproduct 会不会干净一点哈
@Mappings({
@Mapping(target = "id", ignore = true),
@Mapping(target = "activityId", source = "activityDO.id"),
@Mapping(target = "spuId", source = "activityDO.spuId"),
@Mapping(target = "skuId", source = "vo.skuId"),
@Mapping(target = "bargainFirstPrice", source = "vo.bargainFirstPrice"),
@Mapping(target = "bargainPrice", source = "vo.bargainPrice"),
@Mapping(target = "stock", source = "vo.stock"),
@Mapping(target = "activityStartTime", source = "activityDO.startTime"),
@Mapping(target = "activityEndTime", source = "activityDO.endTime")
@Mapping(target = "activityId", source = "activity.id"),
@Mapping(target = "spuId", source = "activity.spuId"),
@Mapping(target = "skuId", source = "product.skuId"),
@Mapping(target = "bargainFirstPrice", source = "product.bargainFirstPrice"),
@Mapping(target = "bargainPrice", source = "product.bargainPrice"),
@Mapping(target = "stock", source = "product.stock"),
@Mapping(target = "activityStartTime", source = "activity.startTime"),
@Mapping(target = "activityEndTime", source = "activity.endTime")
})
BargainProductDO convert(BargainActivityDO activityDO, BargainProductBaseVO vo);
BargainProductDO convert(BargainActivityDO activity, BargainProductBaseVO product);
default List<BargainProductDO> convertList(List<? extends BargainProductBaseVO> products, BargainActivityDO activityDO) {
return CollectionUtils.convertList(products, item -> convert(activityDO, item).setActivityStatus(activityDO.getStatus()));
}
default List<BargainProductDO> convertList(List<BargainProductUpdateReqVO> updateProductVOs,
default List<BargainProductDO> convertList(List<BargainProductBaseVO> updateProductVOs,
List<BargainProductDO> products, BargainActivityDO activity) {
Map<Long, Long> productMap = convertMap(products, BargainProductDO::getSkuId, BargainProductDO::getId);
return CollectionUtils.convertList(updateProductVOs, updateProductVO -> convert(activity, updateProductVO)

View File

@ -41,8 +41,9 @@ public interface CombinationActivityConvert {
CombinationProductRespVO convert(CombinationProductDO bean);
default CombinationActivityRespVO convert(CombinationActivityDO bean, List<CombinationProductDO> productDOs) {
return convert(bean).setProducts(convertList2(productDOs));
default CombinationActivityRespVO convert(CombinationActivityDO activity,
List<CombinationProductDO> products) {
return convert(activity).setProducts(convertList2(products));
}
List<CombinationActivityRespVO> convertList(List<CombinationActivityDO> list);

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.bargain;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
@ -28,7 +29,6 @@ public class BargainActivityDO extends BaseDO {
*/
@TableId
private Long id;
/**
* 砍价活动名称
*/
@ -38,7 +38,6 @@ public class BargainActivityDO extends BaseDO {
* 活动开始时间
*/
private LocalDateTime startTime;
/**
* 活动结束时间
*/
@ -46,6 +45,8 @@ public class BargainActivityDO extends BaseDO {
/**
* 活动状态
*
* 枚举 {@link CommonStatusEnum}
*/
private Integer status;
@ -53,40 +54,36 @@ public class BargainActivityDO extends BaseDO {
* 商品 SPU 编号
*/
private Long spuId;
/**
* 达到该人数才能砍到低价
*/
private Integer userSize;
/**
* 最大帮砍次数
*/
private Integer bargainCount;
/**
* 总限购数量
*/
private Integer totalLimitCount;
/**
* 砍价库存
*/
private Integer stock;
/**
* 达到该人数才能砍到低价
*/
private Integer userSize;
/**
* 最大帮砍次数
*/
private Integer bargainCount;
/**
* 总限购数量
*/
private Integer totalLimitCount;
/**
* 用户每次砍价的最小金额单位
*/
private Integer randomMinPrice;
/**
* 用户每次砍价的最大金额单位
*/
private Integer randomMaxPrice;
/**
* 砍价成功数量
*/
private Integer successCount;
// TODO @puhui999 BargainProductDO 字段融合过来
}

View File

@ -13,6 +13,7 @@ import java.time.LocalDateTime;
*
* @author HUIHUI
*/
@Deprecated // 应该融合到 BargainActivityDO
@TableName("promotion_bargain_product")
@KeySequence("promotion_bargain_product_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data

View File

@ -59,9 +59,9 @@ public interface BargainActivityService {
/**
* 获得砍价活动商品列表
*
* @param ids 砍价活动 ids
* @param activityIds 砍价活动 ids
* @return 砍价活动的商品列表
*/
List<BargainProductDO> getBargainProductsByActivityIds(Collection<Long> ids);
List<BargainProductDO> getBargainProductsByActivityIds(Collection<Long> activityIds);
}

View File

@ -13,14 +13,12 @@ import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.activity.BargainActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductBaseVO;
import cn.iocoder.yudao.module.promotion.convert.bargain.BargainActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainProductDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainProductMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@ -43,14 +41,13 @@ import static cn.iocoder.yudao.module.promotion.util.PromotionUtils.validateProd
*/
@Service
@Validated
public class BargainServiceImpl implements BargainActivityService, BargainRecordService {
public class BargainActivityServiceImpl implements BargainActivityService {
@Resource
private BargainActivityMapper bargainActivityMapper;
@Resource
private BargainRecordMapper recordMapper;
@Resource
private BargainProductMapper bargainProductMapper;
@Resource
private ProductSpuApi productSpuApi;
@Resource
@ -64,21 +61,21 @@ public class BargainServiceImpl implements BargainActivityService, BargainRecord
// 获取所选 spu下的所有 sku
List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollectionUtil.newArrayList(createReqVO.getSpuId()));
// 校验商品 sku 是否存在
validateProductSkuAllExists(skus, createReqVO.getProducts(), BargainProductCreateReqVO::getSkuId);
validateProductSkuAllExists(skus, createReqVO.getProducts(), BargainProductBaseVO::getSkuId);
// 插入砍价活动
BargainActivityDO activityDO = BargainActivityConvert.INSTANCE.convert(createReqVO);
BargainActivityDO activity = BargainActivityConvert.INSTANCE.convert(createReqVO);
// TODO 营销相关属性初始化 砍价成功更新相关属性
activityDO.setSuccessCount(0);
activity.setSuccessCount(0);
// 活动总库存
activityDO.setStock(getSumValue(createReqVO.getProducts(), BargainProductCreateReqVO::getStock, Integer::sum));
activityDO.setStatus(CommonStatusEnum.ENABLE.getStatus());
bargainActivityMapper.insert(activityDO);
activity.setStock(getSumValue(createReqVO.getProducts(), BargainProductBaseVO::getStock, Integer::sum));
activity.setStatus(CommonStatusEnum.ENABLE.getStatus());
bargainActivityMapper.insert(activity);
// 插入商品
List<BargainProductDO> productDOs = BargainActivityConvert.INSTANCE.convertList(createReqVO.getProducts(), activityDO);
List<BargainProductDO> productDOs = BargainActivityConvert.INSTANCE.convertList(createReqVO.getProducts(), activity);
bargainProductMapper.insertBatch(productDOs);
// 返回
return activityDO.getId();
return activity.getId();
}
private void validateProductBargainConflict(Long spuId, Long activityId) {
@ -113,12 +110,12 @@ public class BargainServiceImpl implements BargainActivityService, BargainRecord
// 获取所选 spu下的所有 sku
List<ProductSkuRespDTO> skus = productSkuApi.getSkuListBySpuId(CollectionUtil.newArrayList(updateReqVO.getSpuId()));
// 校验商品 sku 是否存在
validateProductSkuAllExists(skus, updateReqVO.getProducts(), BargainProductUpdateReqVO::getSkuId);
validateProductSkuAllExists(skus, updateReqVO.getProducts(), BargainProductBaseVO::getSkuId);
// 更新
BargainActivityDO updateObj = BargainActivityConvert.INSTANCE.convert(updateReqVO);
// 更新活动库存
updateObj.setStock(getSumValue(updateReqVO.getProducts(), BargainProductUpdateReqVO::getStock, Integer::sum));
updateObj.setStock(getSumValue(updateReqVO.getProducts(), BargainProductBaseVO::getStock, Integer::sum));
bargainActivityMapper.updateById(updateObj);
// 更新商品
updateBargainProduct(updateObj, updateReqVO.getProducts());
@ -130,7 +127,7 @@ public class BargainServiceImpl implements BargainActivityService, BargainRecord
* @param updateObj 更新的活动
* @param products 商品配置
*/
private void updateBargainProduct(BargainActivityDO updateObj, List<BargainProductUpdateReqVO> products) {
private void updateBargainProduct(BargainActivityDO updateObj, List<BargainProductBaseVO> products) {
// 默认全部新增
List<BargainProductDO> defaultNewList = BargainActivityConvert.INSTANCE.convertList(products, updateObj);
// 数据库中的老数据
@ -181,7 +178,7 @@ public class BargainServiceImpl implements BargainActivityService, BargainRecord
@Override
public BargainActivityDO getBargainActivity(Long id) {
return validateBargainActivityExists(id);
return bargainActivityMapper.selectById(id);
}
@Override
@ -190,8 +187,8 @@ public class BargainServiceImpl implements BargainActivityService, BargainRecord
}
@Override
public List<BargainProductDO> getBargainProductsByActivityIds(Collection<Long> ids) {
return bargainProductMapper.selectListByActivityIds(ids);
public List<BargainProductDO> getBargainProductsByActivityIds(Collection<Long> activityIds) {
return bargainProductMapper.selectListByActivityIds(activityIds);
}
}