mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-14 18:21:53 +08:00
promotion:增加满减送活动的排重校验、单元测试
This commit is contained in:
@@ -47,18 +47,18 @@ public class RewardActivityController {
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@ApiOperation("删除满减送活动")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('promotion:reward-activity:delete')")
|
||||
public CommonResult<Boolean> deleteRewardActivity(@RequestParam("id") Integer id) {
|
||||
public CommonResult<Boolean> deleteRewardActivity(@RequestParam("id") Long id) {
|
||||
rewardActivityService.deleteRewardActivity(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@ApiOperation("获得满减送活动")
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('promotion:reward-activity:query')")
|
||||
public CommonResult<RewardActivityRespVO> getRewardActivity(@RequestParam("id") Integer id) {
|
||||
public CommonResult<RewardActivityRespVO> getRewardActivity(@RequestParam("id") Long id) {
|
||||
RewardActivityDO rewardActivity = rewardActivityService.getRewardActivity(id);
|
||||
return success(RewardActivityConvert.INSTANCE.convert(rewardActivity));
|
||||
}
|
||||
|
@@ -1,10 +1,18 @@
|
||||
package cn.iocoder.yudao.module.promotion.controller.admin.reward.vo;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.AssertTrue;
|
||||
import javax.validation.constraints.Future;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -30,6 +38,7 @@ public class RewardActivityBaseVO {
|
||||
@ApiModelProperty(value = "结束时间", required = true)
|
||||
@NotNull(message = "结束时间不能为空")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
@Future(message = "结束时间必须大于当前时间")
|
||||
private Date endTime;
|
||||
|
||||
@ApiModelProperty(value = "备注", example = "biubiubiu")
|
||||
@@ -37,10 +46,12 @@ public class RewardActivityBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "条件类型", required = true, example = "1")
|
||||
@NotNull(message = "条件类型不能为空")
|
||||
@InEnum(value = PromotionConditionTypeEnum.class, message = "条件类型必须是 {value}")
|
||||
private Integer conditionType;
|
||||
|
||||
@ApiModelProperty(value = "商品范围", required = true, example = "1")
|
||||
@NotNull(message = "商品范围不能为空")
|
||||
@InEnum(value = PromotionConditionTypeEnum.class, message = "商品范围必须是 {value}")
|
||||
private Integer productScope;
|
||||
|
||||
@ApiModelProperty(value = "商品 SPU 编号的数组", example = "1,2,3")
|
||||
@@ -49,6 +60,7 @@ public class RewardActivityBaseVO {
|
||||
/**
|
||||
* 优惠规则的数组
|
||||
*/
|
||||
@Valid // 校验下子对象
|
||||
private List<Rule> rules;
|
||||
|
||||
@ApiModel("优惠规则")
|
||||
@@ -56,15 +68,18 @@ public class RewardActivityBaseVO {
|
||||
public static class Rule {
|
||||
|
||||
@ApiModelProperty(value = "优惠门槛", required = true, example = "100", notes = "1. 满 N 元,单位:分; 2. 满 N 件")
|
||||
@Min(value = 1L, message = "优惠门槛必须大于等于 1")
|
||||
private Integer limit;
|
||||
|
||||
@ApiModelProperty(value = "优惠价格", required = true, example = "100", notes = "单位:分")
|
||||
@Min(value = 1L, message = "优惠价格必须大于等于 1")
|
||||
private Integer discountPrice;
|
||||
|
||||
@ApiModelProperty(value = "是否包邮", required = true, example = "true")
|
||||
private Boolean freeDelivery;
|
||||
|
||||
@ApiModelProperty(value = "赠送的积分", required = true, example = "100")
|
||||
@Min(value = 1L, message = "赠送的积分必须大于等于 1")
|
||||
private Integer point;
|
||||
|
||||
@ApiModelProperty(value = "赠送的优惠劵编号的数组", example = "1,2,3")
|
||||
@@ -73,6 +88,12 @@ public class RewardActivityBaseVO {
|
||||
@ApiModelProperty(value = "赠送的优惠卷数量的数组", example = "1,2,3")
|
||||
private List<Integer> couponCounts;
|
||||
|
||||
@AssertTrue(message = "优惠劵和数量必须一一对应")
|
||||
@JsonIgnore
|
||||
public boolean isCouponCountsValid() {
|
||||
return CollUtil.size(couponCounts) == CollUtil.size(couponCounts);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -12,6 +12,6 @@ public class RewardActivityUpdateReqVO extends RewardActivityBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "活动编号", required = true, example = "1024")
|
||||
@NotNull(message = "活动编号不能为空")
|
||||
private Integer id;
|
||||
private Long id;
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.module.promotion.dal.dataobject.reward;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
|
||||
@@ -8,10 +10,11 @@ import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -68,19 +71,19 @@ public class RewardActivityDO extends BaseDO {
|
||||
/**
|
||||
* 商品 SPU 编号的数组
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
@TableField(typeHandler = LongListTypeHandler.class)
|
||||
private List<Long> productSpuIds;
|
||||
/**
|
||||
* 优惠规则的数组
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
@TableField(typeHandler = RuleTypeHandler.class)
|
||||
private List<Rule> rules;
|
||||
|
||||
/**
|
||||
* 优惠规则
|
||||
*/
|
||||
@Data
|
||||
public static class Rule {
|
||||
public static class Rule implements Serializable {
|
||||
|
||||
/**
|
||||
* 优惠门槛
|
||||
@@ -112,5 +115,19 @@ public class RewardActivityDO extends BaseDO {
|
||||
|
||||
}
|
||||
|
||||
// TODO @芋艿:可以找一些新的思路
|
||||
public static class RuleTypeHandler extends AbstractJsonTypeHandler<List<Rule>> {
|
||||
|
||||
@Override
|
||||
protected List<Rule> parse(String json) {
|
||||
return JsonUtils.parseArray(json, Rule.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String toJson(List<Rule> obj) {
|
||||
return JsonUtils.toJsonString(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -7,8 +7,6 @@ import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivi
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -39,7 +37,7 @@ public interface RewardActivityService {
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteRewardActivity(Integer id);
|
||||
void deleteRewardActivity(Long id);
|
||||
|
||||
/**
|
||||
* 获得满减送活动
|
||||
@@ -47,15 +45,7 @@ public interface RewardActivityService {
|
||||
* @param id 编号
|
||||
* @return 满减送活动
|
||||
*/
|
||||
RewardActivityDO getRewardActivity(Integer id);
|
||||
|
||||
/**
|
||||
* 获得满减送活动列表
|
||||
*
|
||||
* @param ids 编号
|
||||
* @return 满减送活动列表
|
||||
*/
|
||||
List<RewardActivityDO> getRewardActivityList(Collection<Integer> ids);
|
||||
RewardActivityDO getRewardActivity(Long id);
|
||||
|
||||
/**
|
||||
* 获得满减送活动分页
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.module.promotion.service.reward;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO;
|
||||
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO;
|
||||
@@ -7,6 +8,7 @@ import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivi
|
||||
import cn.iocoder.yudao.module.promotion.convert.reward.RewardActivityConvert;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.mysql.reward.RewardActivityMapper;
|
||||
import cn.iocoder.yudao.module.promotion.util.PromotionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@@ -18,6 +20,7 @@ import java.util.Set;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.REWARD_ACTIVITY_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.REWARD_ACTIVITY_SPU_CONFLICTS;
|
||||
|
||||
/**
|
||||
* 满减送活动 Service 实现类
|
||||
@@ -33,8 +36,12 @@ public class RewardActivityServiceImpl implements RewardActivityService {
|
||||
|
||||
@Override
|
||||
public Long createRewardActivity(RewardActivityCreateReqVO createReqVO) {
|
||||
// 校验商品是否冲突
|
||||
validateRewardActivitySpuConflicts(null, createReqVO.getProductSpuIds());
|
||||
|
||||
// 插入
|
||||
RewardActivityDO rewardActivity = RewardActivityConvert.INSTANCE.convert(createReqVO);
|
||||
RewardActivityDO rewardActivity = RewardActivityConvert.INSTANCE.convert(createReqVO)
|
||||
.setStatus(PromotionUtils.calculateActivityStatus(createReqVO.getStartTime(), createReqVO.getEndTime()));
|
||||
rewardActivityMapper.insert(rewardActivity);
|
||||
// 返回
|
||||
return rewardActivity.getId();
|
||||
@@ -43,34 +50,64 @@ public class RewardActivityServiceImpl implements RewardActivityService {
|
||||
@Override
|
||||
public void updateRewardActivity(RewardActivityUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
this.validateRewardActivityExists(updateReqVO.getId());
|
||||
validateRewardActivityExists(updateReqVO.getId());
|
||||
validateRewardActivitySpuConflicts(updateReqVO.getId(), updateReqVO.getProductSpuIds());
|
||||
|
||||
// 更新
|
||||
RewardActivityDO updateObj = RewardActivityConvert.INSTANCE.convert(updateReqVO);
|
||||
RewardActivityDO updateObj = RewardActivityConvert.INSTANCE.convert(updateReqVO)
|
||||
.setStatus(PromotionUtils.calculateActivityStatus(updateReqVO.getStartTime(), updateReqVO.getEndTime()));
|
||||
rewardActivityMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRewardActivity(Integer id) {
|
||||
public void deleteRewardActivity(Long id) {
|
||||
// 校验存在
|
||||
this.validateRewardActivityExists(id);
|
||||
validateRewardActivityExists(id);
|
||||
// 删除
|
||||
rewardActivityMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void validateRewardActivityExists(Integer id) {
|
||||
private void validateRewardActivityExists(Long id) {
|
||||
if (rewardActivityMapper.selectById(id) == null) {
|
||||
throw exception(REWARD_ACTIVITY_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RewardActivityDO getRewardActivity(Integer id) {
|
||||
return rewardActivityMapper.selectById(id);
|
||||
/**
|
||||
* 校验商品参加的活动是否冲突
|
||||
*
|
||||
* @param id 活动编号
|
||||
* @param spuIds 商品 SPU 编号数组
|
||||
*/
|
||||
private void validateRewardActivitySpuConflicts(Long id, Collection<Long> spuIds) {
|
||||
if (CollUtil.isEmpty(spuIds)) {
|
||||
return;
|
||||
}
|
||||
// 查询商品参加的活动
|
||||
List<RewardActivityDO> rewardActivityList = getRewardActivityListBySpuIds(spuIds);
|
||||
if (id != null) { // 排除活动
|
||||
rewardActivityList.removeIf(activity -> id.equals(activity.getId()));
|
||||
}
|
||||
// 如果非空,则说明冲突
|
||||
if (CollUtil.isNotEmpty(rewardActivityList)) {
|
||||
throw exception(REWARD_ACTIVITY_SPU_CONFLICTS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得商品参加的满减送活动的数组
|
||||
*
|
||||
* @param spuIds 商品 SPU 编号数组
|
||||
* @return 商品参加的满减送活动的数组
|
||||
*/
|
||||
private List<RewardActivityDO> getRewardActivityListBySpuIds(Collection<Long> spuIds) {
|
||||
List<RewardActivityDO> list = rewardActivityMapper.selectList();
|
||||
return CollUtil.filter(list, activity -> CollUtil.containsAny(activity.getProductSpuIds(), spuIds));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RewardActivityDO> getRewardActivityList(Collection<Integer> ids) {
|
||||
return rewardActivityMapper.selectBatchIds(ids);
|
||||
public RewardActivityDO getRewardActivity(Long id) {
|
||||
return rewardActivityMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,32 @@
|
||||
package cn.iocoder.yudao.module.promotion.util;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 活动工具类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class PromotionUtils {
|
||||
|
||||
/**
|
||||
* 根据时间,计算活动状态
|
||||
*
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 活动状态
|
||||
*/
|
||||
public static Integer calculateActivityStatus(Date startTime, Date endTime) {
|
||||
if (DateUtils.beforeNow(endTime)) {
|
||||
return PromotionActivityStatusEnum.END.getStatus();
|
||||
}
|
||||
if (DateUtils.afterNow(startTime)) {
|
||||
return PromotionActivityStatusEnum.WAIT.getStatus();
|
||||
}
|
||||
return PromotionActivityStatusEnum.RUN.getStatus();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user