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

View File

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

View File

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

View File

@ -20,15 +20,6 @@ public class BargainActivityPageReqVO extends PageParam {
@Schema(description = "砍价名称", example = "赵六") @Schema(description = "砍价名称", example = "赵六")
private String name; 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 = "开始时间") @Schema(description = "开始时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] startTime; private LocalDateTime[] startTime;
@ -37,29 +28,7 @@ public class BargainActivityPageReqVO extends PageParam {
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] endTime; 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") @Schema(description = "活动状态0开启 1关闭", example = "0")
private Integer status; 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.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@ -17,8 +16,6 @@ import java.util.List;
@ToString(callSuper = true) @ToString(callSuper = true)
public class BargainActivityRespVO extends BargainActivityBaseVO { public class BargainActivityRespVO extends BargainActivityBaseVO {
// TODO @puhui999example 补全
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "618大促") @Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "618大促")
private String spuName; private String spuName;
@ -31,7 +28,7 @@ public class BargainActivityRespVO extends BargainActivityBaseVO {
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime; private LocalDateTime createTime;
@Schema(description = "砍价成功数量", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "砍价成功数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "888")
private Integer successCount; private Integer successCount;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0") @Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@ -39,7 +36,6 @@ public class BargainActivityRespVO extends BargainActivityBaseVO {
private Integer status; private Integer status;
@Schema(description = "砍价商品", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "砍价商品", requiredMode = Schema.RequiredMode.REQUIRED)
@Valid
private List<BargainProductRespVO> products; private List<BargainProductRespVO> products;
} }

View File

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

View File

@ -16,19 +16,19 @@ public class BargainProductBaseVO {
@NotNull(message = "商品 spuId 不能为空") @NotNull(message = "商品 spuId 不能为空")
private Long 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 不能为空") @NotNull(message = "商品 skuId 不能为空")
private Long skuId; private Long skuId;
@Schema(description = "砍价起始价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "23") @Schema(description = "砍价起始价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "33")
@NotNull(message = "砍价起始价格不能为空") @NotNull(message = "砍价起始价格不能为空")
private Integer bargainFirstPrice; private Integer bargainFirstPrice;
@Schema(description = "砍价底价", requiredMode = Schema.RequiredMode.REQUIRED, example = "23") @Schema(description = "砍价底价", requiredMode = Schema.RequiredMode.REQUIRED, example = "22")
@NotNull(message = "砍价底价不能为空") @NotNull(message = "砍价底价不能为空")
private Integer bargainPrice; private Integer bargainPrice;
@Schema(description = "活动库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "23") @Schema(description = "活动库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "11")
@NotNull(message = "活动库存不能为空") @NotNull(message = "活动库存不能为空")
private Integer stock; 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.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.BargainProductBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.product.BargainProductRespVO; 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.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainProductDO; import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainProductDO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
@ -40,8 +39,9 @@ public interface BargainActivityConvert {
BargainProductRespVO convert(BargainProductDO bean); BargainProductRespVO convert(BargainProductDO bean);
default BargainActivityRespVO convert(BargainActivityDO bean, List<BargainProductDO> productDOs) { default BargainActivityRespVO convert(BargainActivityDO activity,
return convert(bean).setProducts(convertList2(productDOs)); List<BargainProductDO> products) {
return convert(activity).setProducts(convertList2(products));
} }
List<BargainActivityRespVO> convertList(List<BargainActivityDO> list); List<BargainActivityRespVO> convertList(List<BargainActivityDO> list);
@ -51,39 +51,37 @@ public interface BargainActivityConvert {
default PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page, default PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page,
List<BargainProductDO> productList, List<BargainProductDO> productList,
List<ProductSpuRespDTO> spuList) { List<ProductSpuRespDTO> spuList) {
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
PageResult<BargainActivityRespVO> pageResult = convertPage(page); PageResult<BargainActivityRespVO> pageResult = convertPage(page);
// 拼接商品
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
pageResult.getList().forEach(item -> { pageResult.getList().forEach(item -> {
MapUtils.findAndThen(spuMap, item.getSpuId(), spu -> {
item.setSpuName(spu.getName());
item.setPicUrl(spu.getPicUrl());
});
item.setProducts(convertList2(productList)); item.setProducts(convertList2(productList));
MapUtils.findAndThen(spuMap, item.getSpuId(),
spu -> item.setSpuName(spu.getName()).setPicUrl(spu.getPicUrl()));
}); });
return pageResult; return pageResult;
} }
List<BargainProductRespVO> convertList2(List<BargainProductDO> productDOs); List<BargainProductRespVO> convertList2(List<BargainProductDO> productDOs);
// TODO @puhui999参数改成 activityproduct 会不会干净一点哈
@Mappings({ @Mappings({
@Mapping(target = "id", ignore = true), @Mapping(target = "id", ignore = true),
@Mapping(target = "activityId", source = "activityDO.id"), @Mapping(target = "activityId", source = "activity.id"),
@Mapping(target = "spuId", source = "activityDO.spuId"), @Mapping(target = "spuId", source = "activity.spuId"),
@Mapping(target = "skuId", source = "vo.skuId"), @Mapping(target = "skuId", source = "product.skuId"),
@Mapping(target = "bargainFirstPrice", source = "vo.bargainFirstPrice"), @Mapping(target = "bargainFirstPrice", source = "product.bargainFirstPrice"),
@Mapping(target = "bargainPrice", source = "vo.bargainPrice"), @Mapping(target = "bargainPrice", source = "product.bargainPrice"),
@Mapping(target = "stock", source = "vo.stock"), @Mapping(target = "stock", source = "product.stock"),
@Mapping(target = "activityStartTime", source = "activityDO.startTime"), @Mapping(target = "activityStartTime", source = "activity.startTime"),
@Mapping(target = "activityEndTime", source = "activityDO.endTime") @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) { default List<BargainProductDO> convertList(List<? extends BargainProductBaseVO> products, BargainActivityDO activityDO) {
return CollectionUtils.convertList(products, item -> convert(activityDO, item).setActivityStatus(activityDO.getStatus())); 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) { List<BargainProductDO> products, BargainActivityDO activity) {
Map<Long, Long> productMap = convertMap(products, BargainProductDO::getSkuId, BargainProductDO::getId); Map<Long, Long> productMap = convertMap(products, BargainProductDO::getSkuId, BargainProductDO::getId);
return CollectionUtils.convertList(updateProductVOs, updateProductVO -> convert(activity, updateProductVO) return CollectionUtils.convertList(updateProductVOs, updateProductVO -> convert(activity, updateProductVO)

View File

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

View File

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

View File

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

View File

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