Commit c01573f7 authored by zhangfeng's avatar zhangfeng

bugfix-175: 礼品ID命名规则修改

parent 67345aaa
package cn.iocoder.yudao.module.reward.controller.admin.job;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.reward.util.RewardGenCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 礼品编号刷新任务
* @author zhaobiyan
*/
@Component
@Slf4j
public class RewardCodeFlushTask implements JobHandler {
@Resource
private RewardGenCodeUtils rewardGenCodeUtils;
@Override
public String execute(String param) throws Exception {
rewardGenCodeUtils.historyRewardCodeFlush();
return "success";
}
}
\ No newline at end of file
......@@ -7,11 +7,14 @@ import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.reward.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.module.reward.vo.reward.RewardQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 礼品 Mapper
*
* @author 系统管理员
*/
@Mapper
......@@ -19,18 +22,18 @@ public interface RewardMapper extends AbstractMapper<RewardDO> {
@Override
default PageResult<RewardDO> selectPage(PageVO page, Object object) {
if (object instanceof RewardQueryVO) {
RewardQueryVO vo = (RewardQueryVO)object;
RewardQueryVO vo = (RewardQueryVO) object;
return selectPage(page, new LambdaQuery<RewardDO>()
.eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.orderByDesc(RewardDO::getId));
}
......@@ -40,21 +43,25 @@ public interface RewardMapper extends AbstractMapper<RewardDO> {
@Override
default List<RewardDO> selectList(Object object) {
if (object instanceof RewardQueryVO) {
RewardQueryVO vo = (RewardQueryVO)object;
RewardQueryVO vo = (RewardQueryVO) object;
return selectList(new LambdaQuery<RewardDO>()
.eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.orderByDesc(RewardDO::getId));
}
return null;
}
@ResultType(String.class)
@Select({"select code from ecw_reward order by code desc limit 1"})
String getCurrentMaxRewardCode();
}
......@@ -22,11 +22,11 @@ import cn.iocoder.yudao.module.reward.enums.RewardPickMethedEnum;
import cn.iocoder.yudao.module.reward.enums.RewardStatusEnum;
import cn.iocoder.yudao.module.reward.vo.reward.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.reward.util.RewardGenCodeUtils;
import javax.annotation.Resource;
import java.time.Instant;
......@@ -51,6 +51,8 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
private NodeApi nodeApi;
@Resource
private MemberUserApi memberUserApi;
@Resource
private RewardGenCodeUtils rewardGenCodeUtils;
@Override
public List<Long> create(RewardCreateReqVO createReqVO) {
......@@ -65,14 +67,9 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
for (RewardCreateReqVO.NodeAndPoints nodeId : createReqVO.getNodeIds()) {
rewardDO.setNodeId(nodeId.getNodeId());
rewardDO.setPointsRequire(nodeId.getPoints());
rewardDO.setCode(generateRewardCode());
//如果插入失败,重新生成code再次插入
try {
rewardMapper.insert(rewardDO);
} catch (Exception e) {
rewardDO.setCode(generateRewardCode());
rewardMapper.insert(rewardDO);
}
rewardDO.setCode(rewardGenCodeUtils.generateRewardCode());
rewardMapper.insert(rewardDO);
ids.add(rewardDO.getId());
rewardDO.setId(null);
}
......@@ -267,7 +264,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
throw exception(REWARD_NOT_EXISTS);
}
//重新生成礼品ID
reward.setCode(generateRewardCode());
reward.setCode(rewardGenCodeUtils.generateRewardCode());
//设置为未启用
reward.setStatus(RewardStatusEnum.DISABLED.getValue());
//已兑换次数清零
......@@ -277,12 +274,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
reward.setCreateTime(null);
reward.setUpdateTime(null);
reward.setDeleted(null);
try {
rewardMapper.insert(reward);
} catch (Exception e) {
reward.setCode(generateRewardCode());
rewardMapper.insert(reward);
}
rewardMapper.insert(reward);
return reward.getId();
}
......@@ -407,13 +399,6 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
}
}
/**
* 生成礼品ID
*/
private String generateRewardCode() {
return RandomStringUtils.randomAlphanumeric(12).toUpperCase();
}
/**
* 校验礼品是否过期并修改礼品状态
*/
......
package cn.iocoder.yudao.module.reward.util;
import cn.iocoder.yudao.framework.redis.helper.RedisHelper;
import cn.iocoder.yudao.module.reward.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.module.reward.dal.mysql.reward.RewardMapper;
import com.alibaba.excel.util.DateUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.GET_LOCK_FAILED;
/**
* 根据规则生成各种code
*/
@Component
public class RewardGenCodeUtils {
@Resource
private RewardMapper rewardMapper;
@Resource
private RedisHelper redisHelper;
@Resource
private RedissonClient redissonClient;
/**
* 生成礼品ID
*/
public String generateRewardCode() {
// 礼品ID命名规则:G+年+5位序列号,例如G2400005
String key = "rewardCode:max:number";
StringBuilder code = new StringBuilder();
code.append("G");
code.append(DateUtils.format(new Date(), "yy"));
Long codeNum;
if (redisHelper.hasKey(key)) {
codeNum = redisHelper.incrBy(key, 1);
} else {
RLock lock = redissonClient.getLock("next:reward:code:lock");
try {
boolean lockSuccess = lock.tryLock(2, 2, TimeUnit.SECONDS);
if (!lockSuccess) {
throw exception(GET_LOCK_FAILED);
}
String currentMaxRewardCode = rewardMapper.getCurrentMaxRewardCode();
codeNum = Long.parseLong(currentMaxRewardCode.substring(3)) + 1;
redisHelper.set(key, String.valueOf(codeNum),10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
throw exception(GET_LOCK_FAILED);
} finally {
lock.unlock();
}
}
// 获得5位序列号,不足位前面补0
code.append(String.format("%05d", codeNum));
return code.toString();
}
public void historyRewardCodeFlush() {
long start = 1L;
redisHelper.delete("rewardCode:max:number");
List<RewardDO> rewardDOS = rewardMapper.selectList();
for (RewardDO rewardDO : rewardDOS) {
StringBuilder code = new StringBuilder();
code.append("G");
code.append(DateUtils.format(new Date(), "yy"));
code.append(String.format("%05d", start));
rewardDO.setCode(code.toString());
rewardMapper.updateById(rewardDO);
start += 1;
}
}
}
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