Commit 53ea2934 by “Kongxiangkun”

增加积分增加并发锁

parent b152f992
......@@ -17,6 +17,7 @@ import com.yizhi.application.orm.id.IdGenerator;
import com.yizhi.application.service.PointDetailsService;
import com.yizhi.application.service.PointService;
import com.yizhi.application.service.PointUserService;
import com.yizhi.core.application.cache.RedisCache;
import com.yizhi.core.application.context.ContextHolder;
import com.yizhi.point.application.vo.PointDetailListVO;
import com.yizhi.point.application.vo.PointUserExchangeVO;
......@@ -57,6 +58,8 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements
private PointProductMapper pointProductMapper;
@Autowired
private PointDetailsMapper pointDetailsMapper;
@Autowired
private RedisCache redisCache;
@Override
public Point pointList(Long companyId, Long siteId) {
......@@ -97,64 +100,77 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements
@Transactional(rollbackFor = Exception.class)
public void addPoint(Long accountId, String type, String sourceId){
if(accountId == null || StringUtils.isBlank(type)){
log.error("增加积分入参异常 accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
return;
}
log.info("增加积分入参: accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
Long companyId = ContextHolder.get().getCompanyId();
Long orgId = ContextHolder.get().getOrgId();
Long siteId = ContextHolder.get().getSiteId();
String accountName = ContextHolder.get().getAccountName();
//执行积分奖励策略进行相应的业务校验
PointTypeStrategy strategy = getPointByType(type, accountId, sourceId);
log.info("获取赠送积分策略:{}", JSONUtil.toJsonStr(strategy));
if(strategy != null) {
PointUser pu = new PointUser();
pu.setUserId(accountId);
EntityWrapper<PointUser> wrapper = new EntityWrapper<PointUser>(pu);
pu = pointUserService.selectOne(wrapper);
Long id = idGenerator.generate();
if (pu == null) {
pu = new PointUser();
pu.setId(id);
pu.setUserId(accountId);
pu.setTotalPoint(strategy.getPoint());
pu.setState(1);
pu.setCreateById(accountId);
pu.setCreateByName(accountName);
pu.setCreateTime(new Date());
String redisKey = "add_point_locked_user:" + accountId;
try {
if(redisCache.setIfAbsent(redisKey, "1")) {
if (accountId == null || StringUtils.isBlank(type)) {
log.error("增加积分入参异常 accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
redisCache.delete(redisKey);
return;
}
log.info("增加积分入参: accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
Long companyId = ContextHolder.get().getCompanyId();
Long orgId = ContextHolder.get().getOrgId();
Long siteId = ContextHolder.get().getSiteId();
String accountName = ContextHolder.get().getAccountName();
//执行积分奖励策略进行相应的业务校验
PointTypeStrategy strategy = getPointByType(type, accountId, sourceId);
log.info("获取赠送积分策略:{}", JSONUtil.toJsonStr(strategy));
if (strategy != null) {
PointUser pu = new PointUser();
pu.setUserId(accountId);
EntityWrapper<PointUser> wrapper = new EntityWrapper<PointUser>(pu);
pu = pointUserService.selectOne(wrapper);
Long id = idGenerator.generate();
if (pu == null) {
pu = new PointUser();
pu.setId(id);
pu.setUserId(accountId);
pu.setTotalPoint(strategy.getPoint());
pu.setState(1);
pu.setCreateById(accountId);
pu.setCreateByName(accountName);
pu.setCreateTime(new Date());
} else {
pu.setTotalPoint(pu.getTotalPoint() + strategy.getPoint());
pu.setUpdateById(accountId);
pu.setUpdateByName(accountName);
pu.setUpdateTime(new Date());
}
pu.setOrgId(orgId);
pu.setSiteId(siteId);
pu.setCompanyId(companyId);
boolean b = pointUserService.insertOrUpdate(pu);
if (b) {
PointDetails pd = new PointDetails();
pd.setId(idGenerator.generate());
pd.setTime(new Date());
pd.setChangeBefore(pu.getTotalPoint());
pd.setPoint(strategy.getPoint());
pd.setChangeAfter(pu.getTotalPoint() + strategy.getPoint());
pd.setMultiple(1);
pd.setAccountId(accountId);
pd.setLearnName(strategy.getPointTypeEnum().getDesc());
pd.setLearnSource(strategy.getLearnSource());
pd.setLearnType(strategy.getPointTypeEnum().getKey());
pd.setState(1);
pd.setCreateById(accountId);
pd.setCreateByName(accountName);
pd.setCreateTime(new Date());
pd.setOrgId(orgId);
pd.setCompanyId(companyId);
pd.setSiteId(siteId);
pointDetailsService.insert(pd);
}
}
redisCache.delete(redisKey);
} else {
pu.setTotalPoint(pu.getTotalPoint() + strategy.getPoint());
pu.setUpdateById(accountId);
pu.setUpdateByName(accountName);
pu.setUpdateTime(new Date());
}
pu.setOrgId(orgId);
pu.setSiteId(siteId);
pu.setCompanyId(companyId);
boolean b = pointUserService.insertOrUpdate(pu);
if(b) {
PointDetails pd = new PointDetails();
pd.setId(idGenerator.generate());
pd.setTime(new Date());
pd.setChangeBefore(pu.getTotalPoint());
pd.setPoint(strategy.getPoint());
pd.setChangeAfter(pu.getTotalPoint() + strategy.getPoint());
pd.setMultiple(1);
pd.setAccountId(accountId);
pd.setLearnName(strategy.getPointTypeEnum().getDesc());
pd.setLearnSource(strategy.getLearnSource());
pd.setLearnType(strategy.getPointTypeEnum().getKey());
pd.setState(1);
pd.setCreateById(accountId);
pd.setCreateByName(accountName);
pd.setCreateTime(new Date());
pd.setOrgId(orgId);
pd.setCompanyId(companyId);
pd.setSiteId(siteId);
pointDetailsService.insert(pd);
log.info("同一用户并发触发增加积分, 用户:{}" + accountId);
}
} catch (Exception e) {
log.error("增加积分发生异常:{}, 用户:{}, type:{}", e.getMessage(), accountId, type);
} finally {
redisCache.delete(redisKey);
}
}
......
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