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; ...@@ -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.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.module.reward.vo.reward.RewardQueryVO; import cn.iocoder.yudao.module.reward.vo.reward.RewardQueryVO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import java.util.List; import java.util.List;
/** /**
* 礼品 Mapper * 礼品 Mapper
*
* @author 系统管理员 * @author 系统管理员
*/ */
@Mapper @Mapper
...@@ -19,18 +22,18 @@ public interface RewardMapper extends AbstractMapper<RewardDO> { ...@@ -19,18 +22,18 @@ public interface RewardMapper extends AbstractMapper<RewardDO> {
@Override @Override
default PageResult<RewardDO> selectPage(PageVO page, Object object) { default PageResult<RewardDO> selectPage(PageVO page, Object object) {
if (object instanceof RewardQueryVO) { if (object instanceof RewardQueryVO) {
RewardQueryVO vo = (RewardQueryVO)object; RewardQueryVO vo = (RewardQueryVO) object;
return selectPage(page, new LambdaQuery<RewardDO>() return selectPage(page, new LambdaQuery<RewardDO>()
.eqIfPresent(RewardDO::getCode, vo.getCode()) .eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle()) .eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle()) .eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle()) .eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire()) .eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId()) .eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain()) .eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod()) .eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus()) .eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime()) .betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.orderByDesc(RewardDO::getId)); .orderByDesc(RewardDO::getId));
} }
...@@ -40,21 +43,25 @@ public interface RewardMapper extends AbstractMapper<RewardDO> { ...@@ -40,21 +43,25 @@ public interface RewardMapper extends AbstractMapper<RewardDO> {
@Override @Override
default List<RewardDO> selectList(Object object) { default List<RewardDO> selectList(Object object) {
if (object instanceof RewardQueryVO) { if (object instanceof RewardQueryVO) {
RewardQueryVO vo = (RewardQueryVO)object; RewardQueryVO vo = (RewardQueryVO) object;
return selectList(new LambdaQuery<RewardDO>() return selectList(new LambdaQuery<RewardDO>()
.eqIfPresent(RewardDO::getCode, vo.getCode()) .eqIfPresent(RewardDO::getCode, vo.getCode())
.eqIfPresent(RewardDO::getTitleZh, vo.getTitle()) .eqIfPresent(RewardDO::getTitleZh, vo.getTitle())
.eqIfPresent(RewardDO::getTitleEn, vo.getTitle()) .eqIfPresent(RewardDO::getTitleEn, vo.getTitle())
.eqIfPresent(RewardDO::getTitleFr, vo.getTitle()) .eqIfPresent(RewardDO::getTitleFr, vo.getTitle())
.eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire()) .eqIfPresent(RewardDO::getPointsRequire, vo.getPointsRequire())
.eqIfPresent(RewardDO::getNodeId, vo.getNodeId()) .eqIfPresent(RewardDO::getNodeId, vo.getNodeId())
.eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain()) .eqIfPresent(RewardDO::getQuantityRemain, vo.getQuantityRemain())
.eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod()) .eqIfPresent(RewardDO::getPickMethod, vo.getPickMethod())
.eqIfPresent(RewardDO::getStatus, vo.getStatus()) .eqIfPresent(RewardDO::getStatus, vo.getStatus())
.betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime()) .betweenIfPresent(RewardDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
.orderByDesc(RewardDO::getId)); .orderByDesc(RewardDO::getId));
} }
return null; 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; ...@@ -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.enums.RewardStatusEnum;
import cn.iocoder.yudao.module.reward.vo.reward.*; import cn.iocoder.yudao.module.reward.vo.reward.*;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.module.reward.util.RewardGenCodeUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Instant; import java.time.Instant;
...@@ -51,6 +51,8 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i ...@@ -51,6 +51,8 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
private NodeApi nodeApi; private NodeApi nodeApi;
@Resource @Resource
private MemberUserApi memberUserApi; private MemberUserApi memberUserApi;
@Resource
private RewardGenCodeUtils rewardGenCodeUtils;
@Override @Override
public List<Long> create(RewardCreateReqVO createReqVO) { public List<Long> create(RewardCreateReqVO createReqVO) {
...@@ -65,14 +67,9 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i ...@@ -65,14 +67,9 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
for (RewardCreateReqVO.NodeAndPoints nodeId : createReqVO.getNodeIds()) { for (RewardCreateReqVO.NodeAndPoints nodeId : createReqVO.getNodeIds()) {
rewardDO.setNodeId(nodeId.getNodeId()); rewardDO.setNodeId(nodeId.getNodeId());
rewardDO.setPointsRequire(nodeId.getPoints()); rewardDO.setPointsRequire(nodeId.getPoints());
rewardDO.setCode(generateRewardCode()); rewardDO.setCode(rewardGenCodeUtils.generateRewardCode());
//如果插入失败,重新生成code再次插入
try { rewardMapper.insert(rewardDO);
rewardMapper.insert(rewardDO);
} catch (Exception e) {
rewardDO.setCode(generateRewardCode());
rewardMapper.insert(rewardDO);
}
ids.add(rewardDO.getId()); ids.add(rewardDO.getId());
rewardDO.setId(null); rewardDO.setId(null);
} }
...@@ -267,7 +264,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i ...@@ -267,7 +264,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
throw exception(REWARD_NOT_EXISTS); throw exception(REWARD_NOT_EXISTS);
} }
//重新生成礼品ID //重新生成礼品ID
reward.setCode(generateRewardCode()); reward.setCode(rewardGenCodeUtils.generateRewardCode());
//设置为未启用 //设置为未启用
reward.setStatus(RewardStatusEnum.DISABLED.getValue()); reward.setStatus(RewardStatusEnum.DISABLED.getValue());
//已兑换次数清零 //已兑换次数清零
...@@ -277,12 +274,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i ...@@ -277,12 +274,7 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i
reward.setCreateTime(null); reward.setCreateTime(null);
reward.setUpdateTime(null); reward.setUpdateTime(null);
reward.setDeleted(null); reward.setDeleted(null);
try { rewardMapper.insert(reward);
rewardMapper.insert(reward);
} catch (Exception e) {
reward.setCode(generateRewardCode());
rewardMapper.insert(reward);
}
return reward.getId(); return reward.getId();
} }
...@@ -407,13 +399,6 @@ public class RewardServiceImpl extends AbstractService<RewardMapper, RewardDO> i ...@@ -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