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,8 +100,12 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements ...@@ -97,8 +100,12 @@ 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;
try {
if(redisCache.setIfAbsent(redisKey, "1")) {
if (accountId == null || StringUtils.isBlank(type)) {
log.error("增加积分入参异常 accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId); log.error("增加积分入参异常 accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
redisCache.delete(redisKey);
return; return;
} }
log.info("增加积分入参: accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId); log.info("增加积分入参: accountId:{}, type:{}, sourceId:{}", accountId, type, sourceId);
...@@ -109,7 +116,7 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements ...@@ -109,7 +116,7 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements
//执行积分奖励策略进行相应的业务校验 //执行积分奖励策略进行相应的业务校验
PointTypeStrategy strategy = getPointByType(type, accountId, sourceId); PointTypeStrategy strategy = getPointByType(type, accountId, sourceId);
log.info("获取赠送积分策略:{}", JSONUtil.toJsonStr(strategy)); log.info("获取赠送积分策略:{}", JSONUtil.toJsonStr(strategy));
if(strategy != null) { if (strategy != null) {
PointUser pu = new PointUser(); PointUser pu = new PointUser();
pu.setUserId(accountId); pu.setUserId(accountId);
EntityWrapper<PointUser> wrapper = new EntityWrapper<PointUser>(pu); EntityWrapper<PointUser> wrapper = new EntityWrapper<PointUser>(pu);
...@@ -134,7 +141,7 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements ...@@ -134,7 +141,7 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements
pu.setSiteId(siteId); pu.setSiteId(siteId);
pu.setCompanyId(companyId); pu.setCompanyId(companyId);
boolean b = pointUserService.insertOrUpdate(pu); boolean b = pointUserService.insertOrUpdate(pu);
if(b) { if (b) {
PointDetails pd = new PointDetails(); PointDetails pd = new PointDetails();
pd.setId(idGenerator.generate()); pd.setId(idGenerator.generate());
pd.setTime(new Date()); pd.setTime(new Date());
...@@ -156,6 +163,15 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements ...@@ -156,6 +163,15 @@ public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements
pointDetailsService.insert(pd); pointDetailsService.insert(pd);
} }
} }
redisCache.delete(redisKey);
} else {
log.info("同一用户并发触发增加积分, 用户:{}" + accountId);
}
} catch (Exception e) {
log.error("增加积分发生异常:{}, 用户:{}, type:{}", e.getMessage(), accountId, type);
} finally {
redisCache.delete(redisKey);
}
} }
public PointTypeStrategy getPointByType(String type, Long accountId, String sourceId){ public PointTypeStrategy getPointByType(String type, Long accountId, String sourceId){
......
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