promotion:增加满减送活动的排重校验、单元测试

pull/2/head
YunaiV 2022-11-05 09:57:45 +08:00
parent 74b6ff1eef
commit 79304b3a73
11 changed files with 228 additions and 88 deletions

View File

@ -29,5 +29,6 @@ public interface ErrorCodeConstants {
// ========== 满减送活动 1003006000 ========== // ========== 满减送活动 1003006000 ==========
ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1003006000, "满减送活动不存在"); ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1003006000, "满减送活动不存在");
ErrorCode REWARD_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1003006001, "商品({}) 已经参加满减送活动({})");
} }

View File

@ -47,18 +47,18 @@ public class RewardActivityController {
@DeleteMapping("/delete") @DeleteMapping("/delete")
@ApiOperation("删除满减送活动") @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')") @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); rewardActivityService.deleteRewardActivity(id);
return success(true); return success(true);
} }
@GetMapping("/get") @GetMapping("/get")
@ApiOperation("获得满减送活动") @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')") @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); RewardActivityDO rewardActivity = rewardActivityService.getRewardActivity(id);
return success(RewardActivityConvert.INSTANCE.convert(rewardActivity)); return success(RewardActivityConvert.INSTANCE.convert(rewardActivity));
} }

View File

@ -1,10 +1,18 @@
package cn.iocoder.yudao.module.promotion.controller.admin.reward.vo; 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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat; 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 javax.validation.constraints.NotNull;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -30,6 +38,7 @@ public class RewardActivityBaseVO {
@ApiModelProperty(value = "结束时间", required = true) @ApiModelProperty(value = "结束时间", required = true)
@NotNull(message = "结束时间不能为空") @NotNull(message = "结束时间不能为空")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@Future(message = "结束时间必须大于当前时间")
private Date endTime; private Date endTime;
@ApiModelProperty(value = "备注", example = "biubiubiu") @ApiModelProperty(value = "备注", example = "biubiubiu")
@ -37,10 +46,12 @@ public class RewardActivityBaseVO {
@ApiModelProperty(value = "条件类型", required = true, example = "1") @ApiModelProperty(value = "条件类型", required = true, example = "1")
@NotNull(message = "条件类型不能为空") @NotNull(message = "条件类型不能为空")
@InEnum(value = PromotionConditionTypeEnum.class, message = "条件类型必须是 {value}")
private Integer conditionType; private Integer conditionType;
@ApiModelProperty(value = "商品范围", required = true, example = "1") @ApiModelProperty(value = "商品范围", required = true, example = "1")
@NotNull(message = "商品范围不能为空") @NotNull(message = "商品范围不能为空")
@InEnum(value = PromotionConditionTypeEnum.class, message = "商品范围必须是 {value}")
private Integer productScope; private Integer productScope;
@ApiModelProperty(value = "商品 SPU 编号的数组", example = "1,2,3") @ApiModelProperty(value = "商品 SPU 编号的数组", example = "1,2,3")
@ -49,6 +60,7 @@ public class RewardActivityBaseVO {
/** /**
* *
*/ */
@Valid // 校验下子对象
private List<Rule> rules; private List<Rule> rules;
@ApiModel("优惠规则") @ApiModel("优惠规则")
@ -56,15 +68,18 @@ public class RewardActivityBaseVO {
public static class Rule { public static class Rule {
@ApiModelProperty(value = "优惠门槛", required = true, example = "100", notes = "1. 满 N 元,单位:分; 2. 满 N 件") @ApiModelProperty(value = "优惠门槛", required = true, example = "100", notes = "1. 满 N 元,单位:分; 2. 满 N 件")
@Min(value = 1L, message = "优惠门槛必须大于等于 1")
private Integer limit; private Integer limit;
@ApiModelProperty(value = "优惠价格", required = true, example = "100", notes = "单位:分") @ApiModelProperty(value = "优惠价格", required = true, example = "100", notes = "单位:分")
@Min(value = 1L, message = "优惠价格必须大于等于 1")
private Integer discountPrice; private Integer discountPrice;
@ApiModelProperty(value = "是否包邮", required = true, example = "true") @ApiModelProperty(value = "是否包邮", required = true, example = "true")
private Boolean freeDelivery; private Boolean freeDelivery;
@ApiModelProperty(value = "赠送的积分", required = true, example = "100") @ApiModelProperty(value = "赠送的积分", required = true, example = "100")
@Min(value = 1L, message = "赠送的积分必须大于等于 1")
private Integer point; private Integer point;
@ApiModelProperty(value = "赠送的优惠劵编号的数组", example = "1,2,3") @ApiModelProperty(value = "赠送的优惠劵编号的数组", example = "1,2,3")
@ -73,6 +88,12 @@ public class RewardActivityBaseVO {
@ApiModelProperty(value = "赠送的优惠卷数量的数组", example = "1,2,3") @ApiModelProperty(value = "赠送的优惠卷数量的数组", example = "1,2,3")
private List<Integer> couponCounts; private List<Integer> couponCounts;
@AssertTrue(message = "优惠劵和数量必须一一对应")
@JsonIgnore
public boolean isCouponCountsValid() {
return CollUtil.size(couponCounts) == CollUtil.size(couponCounts);
}
} }
} }

View File

@ -12,6 +12,6 @@ public class RewardActivityUpdateReqVO extends RewardActivityBaseVO {
@ApiModelProperty(value = "活动编号", required = true, example = "1024") @ApiModelProperty(value = "活动编号", required = true, example = "1024")
@NotNull(message = "活动编号不能为空") @NotNull(message = "活动编号不能为空")
private Integer id; private Long id;
} }

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.reward; 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.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.PromotionActivityStatusEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum; import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; 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.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; 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.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -68,19 +71,19 @@ public class RewardActivityDO extends BaseDO {
/** /**
* SPU * SPU
*/ */
@TableField(typeHandler = JacksonTypeHandler.class) @TableField(typeHandler = LongListTypeHandler.class)
private List<Long> productSpuIds; private List<Long> productSpuIds;
/** /**
* *
*/ */
@TableField(typeHandler = JacksonTypeHandler.class) @TableField(typeHandler = RuleTypeHandler.class)
private List<Rule> rules; private List<Rule> rules;
/** /**
* *
*/ */
@Data @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);
}
}
} }

View File

@ -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 cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -39,7 +37,7 @@ public interface RewardActivityService {
* *
* @param id * @param id
*/ */
void deleteRewardActivity(Integer id); void deleteRewardActivity(Long id);
/** /**
* *
@ -47,15 +45,7 @@ public interface RewardActivityService {
* @param id * @param id
* @return * @return
*/ */
RewardActivityDO getRewardActivity(Integer id); RewardActivityDO getRewardActivity(Long id);
/**
*
*
* @param ids
* @return
*/
List<RewardActivityDO> getRewardActivityList(Collection<Integer> ids);
/** /**
* *

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.service.reward; 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.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.RewardActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO; 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.convert.reward.RewardActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO; 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.dal.mysql.reward.RewardActivityMapper;
import cn.iocoder.yudao.module.promotion.util.PromotionUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; 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.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_NOT_EXISTS;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.REWARD_ACTIVITY_SPU_CONFLICTS;
/** /**
* Service * Service
@ -33,8 +36,12 @@ public class RewardActivityServiceImpl implements RewardActivityService {
@Override @Override
public Long createRewardActivity(RewardActivityCreateReqVO createReqVO) { 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); rewardActivityMapper.insert(rewardActivity);
// 返回 // 返回
return rewardActivity.getId(); return rewardActivity.getId();
@ -43,34 +50,64 @@ public class RewardActivityServiceImpl implements RewardActivityService {
@Override @Override
public void updateRewardActivity(RewardActivityUpdateReqVO updateReqVO) { 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); rewardActivityMapper.updateById(updateObj);
} }
@Override @Override
public void deleteRewardActivity(Integer id) { public void deleteRewardActivity(Long id) {
// 校验存在 // 校验存在
this.validateRewardActivityExists(id); validateRewardActivityExists(id);
// 删除 // 删除
rewardActivityMapper.deleteById(id); rewardActivityMapper.deleteById(id);
} }
private void validateRewardActivityExists(Integer id) { private void validateRewardActivityExists(Long id) {
if (rewardActivityMapper.selectById(id) == null) { if (rewardActivityMapper.selectById(id) == null) {
throw exception(REWARD_ACTIVITY_NOT_EXISTS); 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 @Override
public List<RewardActivityDO> getRewardActivityList(Collection<Integer> ids) { public RewardActivityDO getRewardActivity(Long id) {
return rewardActivityMapper.selectBatchIds(ids); return rewardActivityMapper.selectById(id);
} }
@Override @Override

View File

@ -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();
}
}

View File

@ -1,25 +1,29 @@
package cn.iocoder.yudao.module.promotion.service.rewardactivity; package cn.iocoder.yudao.module.promotion.service.reward;
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityServiceImpl; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.junit.jupiter.api.Disabled; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityUpdateReqVO;
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.enums.common.PromotionActivityStatusEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.addTime;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.*; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import cn.iocoder.yudao.module.promotion.dal.dataobject.rewardactivity.RewardActivityDO; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import cn.iocoder.yudao.module.promotion.dal.mysql.reward.RewardActivityMapper; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import org.springframework.context.annotation.Import; import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.REWARD_ACTIVITY_NOT_EXISTS;
import java.util.*;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
/** /**
@ -39,15 +43,24 @@ public class RewardActivityServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testCreateRewardActivity_success() { public void testCreateRewardActivity_success() {
// 准备参数 // 准备参数
RewardActivityCreateReqVO reqVO = randomPojo(RewardActivityCreateReqVO.class); RewardActivityCreateReqVO reqVO = randomPojo(RewardActivityCreateReqVO.class, o -> {
o.setConditionType(randomEle(PromotionConditionTypeEnum.values()).getType());
o.setProductScope(randomEle(PromotionProductScopeEnum.values()).getScope());
// 用于触发进行中的状态
o.setStartTime(addTime(Duration.ofDays(1))).setEndTime(addTime(Duration.ofDays(2)));
});
// 调用 // 调用
Integer rewardActivityId = rewardActivityService.createRewardActivity(reqVO); Long rewardActivityId = rewardActivityService.createRewardActivity(reqVO);
// 断言 // 断言
assertNotNull(rewardActivityId); assertNotNull(rewardActivityId);
// 校验记录的属性是否正确 // 校验记录的属性是否正确
RewardActivityDO rewardActivity = rewardActivityMapper.selectById(rewardActivityId); RewardActivityDO rewardActivity = rewardActivityMapper.selectById(rewardActivityId);
assertPojoEquals(reqVO, rewardActivity); assertPojoEquals(reqVO, rewardActivity, "rules");
assertEquals(rewardActivity.getStatus(), PromotionActivityStatusEnum.WAIT.getStatus());
for (int i = 0; i < reqVO.getRules().size(); i++) {
assertPojoEquals(reqVO.getRules().get(i), rewardActivity.getRules().get(i));
}
} }
@Test @Test
@ -58,13 +71,21 @@ public class RewardActivityServiceImplTest extends BaseDbUnitTest {
// 准备参数 // 准备参数
RewardActivityUpdateReqVO reqVO = randomPojo(RewardActivityUpdateReqVO.class, o -> { RewardActivityUpdateReqVO reqVO = randomPojo(RewardActivityUpdateReqVO.class, o -> {
o.setId(dbRewardActivity.getId()); // 设置更新的 ID o.setId(dbRewardActivity.getId()); // 设置更新的 ID
o.setConditionType(randomEle(PromotionConditionTypeEnum.values()).getType());
o.setProductScope(randomEle(PromotionProductScopeEnum.values()).getScope());
// 用于触发进行中的状态
o.setStartTime(addTime(Duration.ofDays(1))).setEndTime(addTime(Duration.ofDays(2)));
}); });
// 调用 // 调用
rewardActivityService.updateRewardActivity(reqVO); rewardActivityService.updateRewardActivity(reqVO);
// 校验是否更新正确 // 校验是否更新正确
RewardActivityDO rewardActivity = rewardActivityMapper.selectById(reqVO.getId()); // 获取最新的 RewardActivityDO rewardActivity = rewardActivityMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, rewardActivity); assertPojoEquals(reqVO, rewardActivity, "rules");
assertEquals(rewardActivity.getStatus(), PromotionActivityStatusEnum.WAIT.getStatus());
for (int i = 0; i < reqVO.getRules().size(); i++) {
assertPojoEquals(reqVO.getRules().get(i), rewardActivity.getRules().get(i));
}
} }
@Test @Test
@ -82,7 +103,7 @@ public class RewardActivityServiceImplTest extends BaseDbUnitTest {
RewardActivityDO dbRewardActivity = randomPojo(RewardActivityDO.class); RewardActivityDO dbRewardActivity = randomPojo(RewardActivityDO.class);
rewardActivityMapper.insert(dbRewardActivity);// @Sql: 先插入出一条存在的数据 rewardActivityMapper.insert(dbRewardActivity);// @Sql: 先插入出一条存在的数据
// 准备参数 // 准备参数
Integer id = dbRewardActivity.getId(); Long id = dbRewardActivity.getId();
// 调用 // 调用
rewardActivityService.deleteRewardActivity(id); rewardActivityService.deleteRewardActivity(id);
@ -93,61 +114,35 @@ public class RewardActivityServiceImplTest extends BaseDbUnitTest {
@Test @Test
public void testDeleteRewardActivity_notExists() { public void testDeleteRewardActivity_notExists() {
// 准备参数 // 准备参数
Integer id = randomIntegerId(); Long id = randomLongId();
// 调用, 并断言异常 // 调用, 并断言异常
assertServiceException(() -> rewardActivityService.deleteRewardActivity(id), REWARD_ACTIVITY_NOT_EXISTS); assertServiceException(() -> rewardActivityService.deleteRewardActivity(id), REWARD_ACTIVITY_NOT_EXISTS);
} }
@Test @Test
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
public void testGetRewardActivityPage() { public void testGetRewardActivityPage() {
// mock 数据 // mock 数据
RewardActivityDO dbRewardActivity = randomPojo(RewardActivityDO.class, o -> { // 等会查询到 RewardActivityDO dbRewardActivity = randomPojo(RewardActivityDO.class, o -> { // 等会查询到
o.setName(null); o.setName("芋艿");
o.setStatus(null); o.setStatus(PromotionActivityStatusEnum.CLOSE.getStatus());
}); });
rewardActivityMapper.insert(dbRewardActivity); rewardActivityMapper.insert(dbRewardActivity);
// 测试 name 不匹配 // 测试 name 不匹配
rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setName(null))); rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setName("土豆")));
// 测试 status 不匹配 // 测试 status 不匹配
rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setStatus(null))); rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setStatus(PromotionActivityStatusEnum.RUN.getStatus())));
// 准备参数 // 准备参数
RewardActivityPageReqVO reqVO = new RewardActivityPageReqVO(); RewardActivityPageReqVO reqVO = new RewardActivityPageReqVO();
reqVO.setName(null); reqVO.setName("芋艿");
reqVO.setStatus(null); reqVO.setStatus(PromotionActivityStatusEnum.CLOSE.getStatus());
// 调用 // 调用
PageResult<RewardActivityDO> pageResult = rewardActivityService.getRewardActivityPage(reqVO); PageResult<RewardActivityDO> pageResult = rewardActivityService.getRewardActivityPage(reqVO);
// 断言 // 断言
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbRewardActivity, pageResult.getList().get(0)); assertPojoEquals(dbRewardActivity, pageResult.getList().get(0), "rules");
}
@Test
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
public void testGetRewardActivityList() {
// mock 数据
RewardActivityDO dbRewardActivity = randomPojo(RewardActivityDO.class, o -> { // 等会查询到
o.setName(null);
o.setStatus(null);
});
rewardActivityMapper.insert(dbRewardActivity);
// 测试 name 不匹配
rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setName(null)));
// 测试 status 不匹配
rewardActivityMapper.insert(cloneIgnoreId(dbRewardActivity, o -> o.setStatus(null)));
// 准备参数
RewardActivityExportReqVO reqVO = new RewardActivityExportReqVO();
reqVO.setName(null);
reqVO.setStatus(null);
// 调用
List<RewardActivityDO> list = rewardActivityService.getRewardActivityList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbRewardActivity, list.get(0));
} }
} }

View File

@ -1,2 +1,4 @@
DELETE FROM "market_activity"; DELETE FROM "market_activity";
DELETE FROM "promotion_coupon_template"; DELETE FROM "promotion_coupon_template";
DELETE FROM "promotion_coupon";
DELETE FROM "promotion_reward_activity";

View File

@ -46,3 +46,48 @@ CREATE TABLE IF NOT EXISTS "promotion_coupon_template" (
"deleted" bit NOT NULL DEFAULT FALSE, "deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id") PRIMARY KEY ("id")
) COMMENT ''; ) COMMENT '';
CREATE TABLE IF NOT EXISTS "promotion_coupon" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"template_id" bigint NOT NULL,
"name" varchar NOT NULL,
"status" int NOT NULL,
"user_id" bigint NOT NULL,
"take_type" int NOT NULL,
"useprice" int NOT NULL,
"valid_start_time" datetime NOT NULL,
"valid_end_time" datetime NOT NULL,
"product_scope" int NOT NULL,
"product_spu_ids" varchar,
"discount_type" int NOT NULL,
"discount_percent" int,
"discount_price" int,
"discount_limit_price" int,
"use_order_id" bigint,
"use_time" datetime,
"creator" varchar DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '';
CREATE TABLE IF NOT EXISTS "promotion_reward_activity" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"name" varchar NOT NULL,
"status" int NOT NULL,
"start_time" datetime NOT NULL,
"end_time" datetime NOT NULL,
"remark" varchar,
"condition_type" int NOT NULL,
"product_scope" int NOT NULL,
"product_spu_ids" varchar,
"rules" varchar,
"creator" varchar DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '';