Commit 9979cd5e authored by zhangfeng's avatar zhangfeng

feature-reward:完善接口逻辑

parent 9f576514
......@@ -8,5 +8,14 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
public interface ErrorCodeConstants {
ErrorCode REWARD_NOT_EXISTS = new ErrorCode(1010011001, "礼品不存在");
ErrorCode REWARD_ENDTIME_ERROR = new ErrorCode(1001011002, "礼品结束时间不能早于当前时间");
ErrorCode REWARD_STATUS_NOT_ALLOW_DELETE = new ErrorCode(1001011003, "只有未启用可删除");
ErrorCode REWARD_STATUS_NOT_ALLOW_ClOSE = new ErrorCode(1001011004, "未启用礼品不可关闭");
ErrorCode REWARD_STATUS_CHANGE_ERROR = new ErrorCode(1001011005, "礼品状态操作不符合规则");
ErrorCode REWARD_STATUS_NOT_ALLOW_DELAY = new ErrorCode(1001011006, "只允许延期启用状态礼品");
ErrorCode REWARD_STATUS_NOT_ALLOW_CREATE = new ErrorCode(1001011007, "创建的礼品状态只能是启用或未启用");
ErrorCode REWARD_START_OR_END_TIME_NOT_ALLOW_CREATE = new ErrorCode(1001011008, "活动时间不合法");
ErrorCode REWARD_PICK_METHOD_NOT_ALLOW_CREATE = new ErrorCode(1001011009, "领取方式不合法");
ErrorCode REWARD_STATUS_NOT_ALLOW_UPDATE = new ErrorCode(1001011010, "礼物状态不允许编辑");
ErrorCode REWARD_STATUS_NOT_ALLOW_ENABLE = new ErrorCode(1001011011, "礼物不能启用");
}
package cn.iocoder.yudao.module.reward.service.reward;
import java.time.Instant;
import java.util.*;
import javax.annotation.Resource;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.module.reward.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.reward.vo.reward.*;
import cn.iocoder.yudao.module.reward.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.reward.convert.reward.RewardConvert;
import cn.iocoder.yudao.module.reward.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.module.reward.dal.mysql.reward.RewardMapper;
import cn.iocoder.yudao.module.reward.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.reward.vo.reward.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.reward.enums.ErrorCodeConstants.*;
import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;
/**
* 礼品 Service 实现类
......@@ -35,10 +39,33 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
@Override
public Long create(RewardCreateReqVO createReqVO) {
//活动开始时间不能比活动结束时间晚,活动结束时间不能比当前时间早
if (ObjectUtils.allNotNull(createReqVO.getStartTime(), createReqVO.getEndTime())) {
if (createReqVO.getStartTime().after((createReqVO.getEndTime())) || createReqVO.getEndTime().before(Date.from(Instant.now()))) {
throw exception(REWARD_START_OR_END_TIME_NOT_ALLOW_CREATE);
}
} else if (ObjectUtils.allNull(createReqVO.getStartTime(), createReqVO.getEndTime())) {
} else {
throw exception(REWARD_START_OR_END_TIME_NOT_ALLOW_CREATE);
}
// 创建的礼品状态只能是启用或未启用
if (createReqVO.getStatus() != 1 && createReqVO.getStatus() != 2) {
throw exception(REWARD_STATUS_NOT_ALLOW_CREATE);
}
//领取方式只能是1,2,3
if (createReqVO.getPickMethod() != 1 && createReqVO.getPickMethod() != 2 && createReqVO.getPickMethod() != 3) {
throw exception(REWARD_PICK_METHOD_NOT_ALLOW_CREATE);
}
// 插入
RewardDO rewardDO = RewardConvert.INSTANCE.convert(createReqVO);
rewardDO.setCode(generateRewardCode());
rewardMapper.insert(rewardDO);
//如果插入失败,重新生成code再次插入
try {
rewardMapper.insert(rewardDO);
} catch (Exception e) {
rewardDO.setCode(generateRewardCode());
rewardMapper.insert(rewardDO);
}
// 返回
return rewardDO.getId();
}
......@@ -46,16 +73,48 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
@Override
public void update(RewardUpdateReqVO updateReqVO) {
// 校验存在
this.validateExists(updateReqVO.getId());
RewardDO rewardDO = rewardMapper.selectById(updateReqVO.getId());
if (rewardDO == null) {
throw exception(REWARD_NOT_EXISTS);
}
RewardDO updateObj = new RewardDO();
//未启用可编辑所有内容
if (rewardDO.getStatus() == 2) {
if (ObjectUtils.allNotNull(updateReqVO.getStartTime(), updateReqVO.getEndTime())) {
if (updateReqVO.getStartTime().after((updateReqVO.getEndTime())) || updateReqVO.getEndTime().before(Date.from(Instant.now()))) {
throw exception(REWARD_START_OR_END_TIME_NOT_ALLOW_CREATE);
}
} else if (ObjectUtils.allNull(updateReqVO.getStartTime(), updateReqVO.getEndTime())) {
} else {
throw exception(REWARD_START_OR_END_TIME_NOT_ALLOW_CREATE);
}
//领取方式只能是1,2,3
if (updateReqVO.getPickMethod() != 1 && updateReqVO.getPickMethod() != 2 && updateReqVO.getPickMethod() != 3) {
throw exception(REWARD_PICK_METHOD_NOT_ALLOW_CREATE);
}
updateObj = RewardConvert.INSTANCE.convert(updateReqVO);
} else if (rewardDO.getStatus() == 1) { //已启用可编辑剩余数量和备注
updateObj.setQuantityRemain(updateReqVO.getQuantityRemain());
updateObj.setRemarkEn(updateReqVO.getRemarkEn());
updateObj.setRemarkFr(updateReqVO.getRemarkFr());
updateObj.setRemarkZh(updateReqVO.getRemarkZh());
} else {
throw exception(REWARD_STATUS_NOT_ALLOW_UPDATE);
}
// 更新
RewardDO updateObj = RewardConvert.INSTANCE.convert(updateReqVO);
rewardMapper.updateById(updateObj);
}
@Override
public void delete(Long id) {
// 校验存在
this.validateExists(id);
RewardDO rewardDO = rewardMapper.selectById(id);
if (rewardDO == null) {
throw exception(REWARD_NOT_EXISTS);
}
//只有未启用可以删除
if (rewardDO.getStatus() != 2) {
throw exception(REWARD_STATUS_NOT_ALLOW_DELETE);
}
// 删除
rewardMapper.deleteById(id);
}
......@@ -68,12 +127,20 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
@Override
public RewardDO get(Long id) {
return rewardMapper.selectById(id);
RewardDO rewardDO = rewardMapper.selectById(id);
//校验是否过期
if (rewardDO != null) {
validateExpire(rewardDO);
}
return rewardDO;
}
@Override
public List<RewardDO> getList(Collection<Long> ids) {
return rewardMapper.selectBatchIds(ids);
List<RewardDO> rewardDOS = rewardMapper.selectBatchIds(ids);
//校验是否过期
rewardDOS.forEach(this::validateExpire);
return rewardDOS;
}
@Override
......@@ -121,7 +188,10 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
//创建时间
lambdaQuery.betweenIfPresent(RewardDO::getCreateTime, query.getBeginCreateTime(), query.getEndCreateTime())
.orderByDesc(RewardDO::getId);
return rewardMapper.selectPage(page, lambdaQuery);
PageResult<RewardDO> rewardDOPageResult = rewardMapper.selectPage(page, lambdaQuery);
//校验是否过期
rewardDOPageResult.getList().forEach(this::validateExpire);
return rewardDOPageResult;
}
@Override
......@@ -144,17 +214,29 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
reward.setCreateTime(null);
reward.setUpdateTime(null);
reward.setDeleted(null);
rewardMapper.insert(reward);
try {
rewardMapper.insert(reward);
} catch (Exception e) {
reward.setCode(generateRewardCode());
rewardMapper.insert(reward);
}
return reward.getId();
}
@Override
public void delayReward(RewardDelayReqVO delayVO) {
this.validateExists(delayVO.getId());
RewardDO reward = rewardMapper.selectById(delayVO.getId());
if (reward == null) {
throw exception(REWARD_NOT_EXISTS);
}
//只允许延期已启用状态礼品
if (reward.getStatus() != 1) {
throw exception(REWARD_STATUS_NOT_ALLOW_DELAY);
}
Instant now = Instant.now();
//结束时间不能小于当前时间
if (!delayVO.getEndTime().toInstant().isAfter(now)) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.REWARD_ENDTIME_ERROR);
throw exception(REWARD_ENDTIME_ERROR);
}
rewardMapper.updateById(RewardConvert.INSTANCE.convertDelayReqVO(delayVO));
}
......@@ -162,12 +244,42 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
@Override
public void updateStatus(RewardStatusReqVO statusVO) {
this.validateExists(statusVO.getId());
rewardMapper.updateById(RewardConvert.INSTANCE.convertStatusReqVO(statusVO));
RewardDO rewardDO = rewardMapper.selectById(statusVO.getId());
Integer oldStatus = rewardDO.getStatus();
Integer newStatus = statusVO.getStatus();
switch (newStatus) {
case 3://关闭:未启用情况下,不可进行关闭;点击关闭需要有二次弹窗提示;关闭后状态为已关闭,不支持兑换
if (oldStatus == 2) {
throw exception(REWARD_STATUS_NOT_ALLOW_ClOSE);
} else {
rewardMapper.updateById(RewardConvert.INSTANCE.convertStatusReqVO(statusVO));
}
break;
case 1://启用:有效期有效且未启用可进行点击启用,需要有二次确认弹窗
if (oldStatus == 2) {
if (rewardDO.getEndTime().toInstant().isBefore(Instant.now())) {
throw exception(REWARD_STATUS_NOT_ALLOW_ENABLE);
}
rewardMapper.updateById(RewardConvert.INSTANCE.convertStatusReqVO(statusVO));
}
break;
default:
throw exception(REWARD_STATUS_CHANGE_ERROR);
}
}
//生成礼品ID
private String generateRewardCode() {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
return uuid.substring(0, 12).toUpperCase();
return RandomStringUtils.randomAlphanumeric(12).toUpperCase();
}
//校验礼品是否过期并修改礼品状态
private void validateExpire(RewardDO rewardDO) {
if (rewardDO.getStatus() == 1 && rewardDO.getEndTime().toInstant().isBefore(Instant.now())) {
RewardDO expireReward = new RewardDO();
expireReward.setId(rewardDO.getId());
expireReward.setStatus(4);
rewardMapper.updateById(expireReward);
}
}
}
......@@ -15,12 +15,15 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_
public class RewardCreateReqVO {
@ApiModelProperty(value = "中文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleZh;
@ApiModelProperty(value = "英文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleEn;
@ApiModelProperty(value = "法文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleFr;
@ApiModelProperty(value = "中文礼品图片")
......@@ -34,6 +37,7 @@ public class RewardCreateReqVO {
@ApiModelProperty(value = "兑换所需积分", required = true)
@NotNull(message = "兑换所需积分不能为空")
@Min(value = 0)
private Integer pointsRequire;
@ApiModelProperty(value = "兑换网点", required = true)
......@@ -42,6 +46,7 @@ public class RewardCreateReqVO {
@ApiModelProperty(value = "剩余数量", required = true)
@NotNull(message = "剩余数量不能为空")
@Min(value = 0)
private Integer quantityRemain;
@ApiModelProperty(value = "活动开始时间")
......@@ -58,6 +63,7 @@ public class RewardCreateReqVO {
@ApiModelProperty(value = "允许兑换次数", required = true)
@NotNull(message = "允许兑换次数不能为空")
@Min(value = 0)
private Integer allowCount;
@ApiModelProperty(value = "中文备注")
......
package cn.iocoder.yudao.module.reward.vo.reward;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel("管理后台 - 礼品更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class RewardUpdateReqVO extends RewardBaseVO {
public class RewardUpdateReqVO {
@ApiModelProperty(value = "", required = true)
@NotNull(message = "不能为空")
private Long id;
@ApiModelProperty(value = "中文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleZh;
@ApiModelProperty(value = "英文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleEn;
@ApiModelProperty(value = "法文名称")
@Size(max = 50, message = "名称长度不能超过50")
private String titleFr;
@ApiModelProperty(value = "中文礼品图片")
private String imgZh;
@ApiModelProperty(value = "英文礼品图片")
private String imgEn;
@ApiModelProperty(value = "法文礼品图片")
private String imgFr;
@ApiModelProperty(value = "兑换所需积分")
@Min(value = 0)
private Integer pointsRequire;
@ApiModelProperty(value = "兑换网点")
private Integer nodeId;
@ApiModelProperty(value = "剩余数量")
@Min(value = 0)
private Integer quantityRemain;
@ApiModelProperty(value = "活动开始时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private Date startTime;
@ApiModelProperty(value = "活动结束时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private Date endTime;
@ApiModelProperty(value = "领取方式(1上门领取,2包邮到家,3邮寄到付)")
private Integer pickMethod;
@ApiModelProperty(value = "允许兑换次数")
@Min(value = 0)
private Integer allowCount;
@ApiModelProperty(value = "中文备注")
@Size(max = 500, message = "中文备注长度不能超过500")
private String remarkZh;
@ApiModelProperty(value = "英文备注")
@Size(max = 500, message = "英文备注长度不能超过500")
private String remarkEn;
@ApiModelProperty(value = "法文备注")
@Size(max = 500, message = "法文备注长度不能超过500")
private String remarkFr;
}
import cn.hutool.core.lang.UUID;
import org.apache.commons.lang3.RandomStringUtils;
public class GenRewardCodeTest {
public static void main(String[] args) {
String format = String.format("%012d", 2);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
System.out.println(format);
System.out.println(uuid);
String string = RandomStringUtils.randomAlphanumeric(12).toUpperCase();
System.out.println(string);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment