幸运礼物-mq异步结算-收益和账单拆开事务
This commit is contained in:
@@ -70,7 +70,7 @@ public class SuperLuckyIncomeAllotService {
|
||||
|
||||
public void send(long senderUid, Long receiverUid, Long roomUid, BigDecimal income, BillObjTypeEnum billObjTypeEnum,
|
||||
int giftId, int everyGiftNum, long giftTotalGoldNum, Date sendGiftTime) {
|
||||
userPurseService.addGold(receiverUid, income.doubleValue(), billObjTypeEnum,
|
||||
userPurseService.addGoldWithTx(receiverUid, income.doubleValue(), billObjTypeEnum,
|
||||
(userPurse)-> billRecordService.insertGiftSendBillRecord(receiverUid, senderUid, roomUid, null, billObjTypeEnum, income.doubleValue(),
|
||||
giftId, everyGiftNum, giftTotalGoldNum, sendGiftTime, userPurse));
|
||||
|
||||
|
@@ -261,6 +261,73 @@ public class UserPurseService extends ServiceImpl<UserPurseMapper,UserPurse> {
|
||||
return after;
|
||||
}
|
||||
|
||||
//@Transactional(rollbackFor = Exception.class, transactionManager = "mybatisplusTransactionManager")
|
||||
public UserPurse addGoldWithTx(Long uid, Double goldNum, BillObjTypeEnum objTypeEnum, Consumer<UserPurse> billConsumer) {
|
||||
if (goldNum <= 0d) {
|
||||
throw new ServiceException(BusiStatus.AMOUNT_PARAM_ERROR);
|
||||
}
|
||||
String desc = objTypeEnum.getDesc();
|
||||
UserPurse after = withLock(uid, RedisKey.lock_user_gold, userPurse -> {
|
||||
log.info("addGold 操作前,buss:{},userPurse:{}", desc, gson.toJson(userPurse));
|
||||
int ret = baseMapper.updateAddGolds(uid,goldNum);
|
||||
boolean result = SqlHelper.retBool(ret);
|
||||
if(!result) {
|
||||
throw new ServiceException(BusiStatus.SERVERBUSY);
|
||||
}
|
||||
userPurse.setGolds(DoubleUtil.add(goldNum, userPurse.getGolds()));
|
||||
log.info("addGold 操作后,buss:{},userPurse:{}", desc, gson.toJson(userPurse));
|
||||
return userPurse;
|
||||
});
|
||||
|
||||
if (null != billConsumer) {
|
||||
billConsumer.accept(after);
|
||||
} else {
|
||||
billRecordService.insertGeneralBillRecord(uid, objTypeEnum, goldNum, after);
|
||||
}
|
||||
|
||||
return after;
|
||||
}
|
||||
|
||||
//@Transactional(rollbackFor = Exception.class, transactionManager = "mybatisplusTransactionManager")
|
||||
public UserPurse batchAddGoldWithoutTx(Long uid, Map<Double, Integer> countMap, BillObjTypeEnum objTypeEnum, Consumer<UserPurse> billConsumer) {
|
||||
if (CollectionUtils.isEmpty(countMap)
|
||||
|| countMap.entrySet().stream()
|
||||
.anyMatch(entry->entry.getKey().compareTo(0d) <= 0 || entry.getValue().compareTo(0) <= 0)) {
|
||||
throw new ServiceException(BusiStatus.AMOUNT_PARAM_ERROR);
|
||||
}
|
||||
BigDecimal totalGoldNum = countMap.entrySet().stream()
|
||||
.map(entry->BigDecimal.valueOf(entry.getKey()).multiply(BigDecimal.valueOf(entry.getValue())))
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
if (totalGoldNum.compareTo(BigDecimal.ZERO) <= 0){
|
||||
throw new ServiceException(BusiStatus.AMOUNT_PARAM_ERROR);
|
||||
}
|
||||
String desc = objTypeEnum.getDesc();
|
||||
UserPurse after = withLock(uid, RedisKey.lock_user_gold, userPurse -> {
|
||||
log.info("addGold 操作前,buss:{},userPurse:{}", desc, gson.toJson(userPurse));
|
||||
int ret = baseMapper.updateAddGolds(uid, totalGoldNum.doubleValue());
|
||||
boolean result = SqlHelper.retBool(ret);
|
||||
if(!result) {
|
||||
throw new ServiceException(BusiStatus.SERVERBUSY);
|
||||
}
|
||||
userPurse.setGolds(DoubleUtil.add(totalGoldNum.doubleValue(), userPurse.getGolds()));
|
||||
log.info("addGold 操作后,buss:{},userPurse:{}", desc, gson.toJson(userPurse));
|
||||
return userPurse;
|
||||
});
|
||||
|
||||
for (Map.Entry<Double, Integer> entry : countMap.entrySet()){
|
||||
Double goldNum = entry.getKey();
|
||||
for (int i = 0; i < entry.getValue(); i++) {
|
||||
if (null != billConsumer) {
|
||||
billConsumer.accept(after);
|
||||
} else {
|
||||
billRecordService.insertGeneralBillRecord(uid, objTypeEnum, goldNum, after);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return after;
|
||||
}
|
||||
|
||||
@Frozen
|
||||
@Transactional(rollbackFor = Exception.class, transactionManager = "mybatisplusTransactionManager")
|
||||
public UserPurse subGuildUsd(Long uid, BigDecimal guildUsdNum, BillObjTypeEnum objTypeEnum,
|
||||
|
Reference in New Issue
Block a user