|
@@ -2,6 +2,7 @@ package com.liangjian.dataplatform.member.service.impl;
|
|
|
|
|
|
import com.liangjian.dataplatform.member.dao.elasticsearch.UserPointLogRepository;
|
|
|
import com.liangjian.dataplatform.member.dao.secondary.WeixinUserInfoRepository;
|
|
|
+import com.liangjian.dataplatform.member.entity.genernator.SnowflakeIdWorker;
|
|
|
import com.liangjian.dataplatform.member.entity.po.elasticsearch.UserPointLog;
|
|
|
import com.liangjian.dataplatform.member.entity.po.secondary.WeixinUserInfo;
|
|
|
import com.liangjian.dataplatform.member.entity.pojo.BirdGirdFilter;
|
|
@@ -63,30 +64,47 @@ public class WeixinUserInfoQueryService implements IWeixinUserInfoQueryService,
|
|
|
|
|
|
@Override
|
|
|
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
|
|
|
- public synchronized boolean update(UserPointLog log) {
|
|
|
+ public boolean update(UserPointLog log) {
|
|
|
log.setId(null);
|
|
|
log.setCrtDate(new Date());
|
|
|
log.setLastUpdate(new Date());
|
|
|
+
|
|
|
+ boolean success = false;
|
|
|
try {
|
|
|
- Optional<WeixinUserInfo> opt = weixinUserInfoRepository.queryBySecretPhone(log.getSecretPhone());
|
|
|
- if(!opt.isPresent()) {
|
|
|
- throw new Exception("can't find user by secretPhone :" + log.getSecretPhone());
|
|
|
+ //使用乐观锁
|
|
|
+ for (int i = 0; i < 5 && !success; i++) {
|
|
|
+ Optional<WeixinUserInfo> opt = weixinUserInfoRepository.queryBySecretPhone(log.getSecretPhone());
|
|
|
+ if(!opt.isPresent()) {
|
|
|
+ throw new Exception("can't find user by secretPhone :" + log.getSecretPhone());
|
|
|
+ }
|
|
|
+ WeixinUserInfo userInfo = opt.get();
|
|
|
+ userInfo.setPointUpdateDate(new Date());
|
|
|
+ Long version = userInfo.getVersion();
|
|
|
+ log.setOrigin(userInfo.getPoint());
|
|
|
+ Integer point = null;
|
|
|
+ if(log.isIncrease()) {
|
|
|
+ point = userInfo.getPoint() + log.getValue();
|
|
|
+ } else {
|
|
|
+ point = userInfo.getPoint() - log.getValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ //weixinUserInfoRepository.save(userInfo);
|
|
|
+ int res = weixinUserInfoRepository.updateWeiUser(point, userInfo.getId(), version);
|
|
|
+ success = res == 1;
|
|
|
}
|
|
|
- WeixinUserInfo userInfo = opt.get();
|
|
|
- userInfo.setPointUpdateDate(new Date());
|
|
|
- if(log.isIncrease()) {
|
|
|
- userInfo.setPoint(userInfo.getPoint() + log.getValue());
|
|
|
+ if(success) {
|
|
|
+ Long workId = Long.valueOf(System.getenv("workerid"));
|
|
|
+ Long datacenterId = Long.valueOf(System.getenv("datacenterid"));
|
|
|
+ SnowflakeIdWorker worker = new SnowflakeIdWorker(workId,datacenterId);
|
|
|
+ log.setId(worker.nextId().toString());
|
|
|
+ userPointLogRepository.save(log);
|
|
|
+ return true;
|
|
|
} else {
|
|
|
- userInfo.setPoint(userInfo.getPoint() - log.getValue());
|
|
|
+ return false;
|
|
|
}
|
|
|
-
|
|
|
- weixinUserInfoRepository.save(userInfo);
|
|
|
- userPointLogRepository.save(log);
|
|
|
- return true;
|
|
|
} catch (Exception ex) {
|
|
|
ex.printStackTrace();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|