Commit 53ea2934 by “Kongxiangkun”

增加积分增加并发锁

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