|
@@ -10,12 +10,14 @@ import com.yonge.cooleshow.biz.dal.entity.UserAccountRecord;
|
|
|
import com.yonge.cooleshow.biz.dal.entity.UserOrder;
|
|
|
import com.yonge.cooleshow.biz.dal.enums.InOrOutEnum;
|
|
|
import com.yonge.cooleshow.biz.dal.service.UserOrderService;
|
|
|
+import com.yonge.cooleshow.biz.dal.vo.UserAccountRecordVo;
|
|
|
import com.yonge.cooleshow.biz.dal.vo.res.OrderPayRes;
|
|
|
import com.yonge.cooleshow.common.enums.CacheNameEnum;
|
|
|
import com.yonge.cooleshow.biz.dal.enums.FrozenTypeEnum;
|
|
|
import com.yonge.cooleshow.biz.dal.service.UserAccountRecordService;
|
|
|
import com.yonge.cooleshow.biz.dal.vo.res.AccountTotal;
|
|
|
import com.yonge.cooleshow.common.entity.HttpResponseResult;
|
|
|
+import com.yonge.cooleshow.common.enums.PostStatusEnum;
|
|
|
import com.yonge.toolset.base.exception.BizException;
|
|
|
import com.yonge.toolset.payment.util.DistributedLock;
|
|
|
import org.redisson.api.RLock;
|
|
@@ -64,27 +66,121 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountDao, UserAcco
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public HttpResponseResult<UserAccountRecord> accountChange(UserAccountRecordDto accountRecordDto) {
|
|
|
- if (null == accountRecordDto.getUserId() || null == accountRecordDto.getTransAmount()
|
|
|
- || null == accountRecordDto.getInOrOut() || null == accountRecordDto.getBizType()) {
|
|
|
- return HttpResponseResult.failed("缺少参数");
|
|
|
+ public HttpResponseResult<UserAccountRecord> accountRecord(UserAccountRecordDto accountRecordDto) {
|
|
|
+ if (null == accountRecordDto.getInOrOut()
|
|
|
+ || null == accountRecordDto.getPostStatus()
|
|
|
+ || (!PostStatusEnum.WAIT.equals(accountRecordDto.getPostStatus()) && !PostStatusEnum.FROZEN.equals(accountRecordDto.getPostStatus()))
|
|
|
+ ) {
|
|
|
+ throw new BizException("记录入账-入账状态异常: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- HttpResponseResult<UserAccountRecord> res = DistributedLock.of(redissonClient)
|
|
|
- .runIfLockToFunction(CacheNameEnum.LOCK_CHANGE_ACCOUNT.getRedisKey(accountRecordDto.getUserId())
|
|
|
- , this::doAccountChange, accountRecordDto, 10L);
|
|
|
- if (null != res) {
|
|
|
- return res;
|
|
|
- } else {
|
|
|
- return HttpResponseResult.failed("账户变更失败");
|
|
|
+ if (null == accountRecordDto.getTransAmount() || null == accountRecordDto.getBizId()
|
|
|
+ || null == accountRecordDto.getBizType()) {
|
|
|
+ throw new BizException("记录入账-缺少入账参数: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
+ }
|
|
|
+ if (BigDecimal.ZERO.compareTo(accountRecordDto.getTransAmount()) < 0) {
|
|
|
+ throw new BizException("记录入账-变动金额不能为负数: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpResponseResult<UserAccountRecord> res = DistributedLock.of(redissonClient)
|
|
|
+ .runIfLockToFunction(CacheNameEnum.LOCK_RECORD_ACCOUNT.getRedisKey(accountRecordDto.getAccountId())
|
|
|
+ , this::doAccountRecord, accountRecordDto, 10L);
|
|
|
+ if (null != res) {
|
|
|
+ return res;
|
|
|
+ } else {
|
|
|
+ throw new BizException("记录入账-插入账户记录失败: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ HttpResponseResult<UserAccountRecord> doAccountRecord(UserAccountRecordDto accountRecordDto) {
|
|
|
+ //收入要校验金额
|
|
|
+ if (InOrOutEnum.IN.equals(accountRecordDto.getInOrOut())) {
|
|
|
+ UserOrder userOrder = orderService.getOne(Wrappers.<UserOrder>lambdaQuery()
|
|
|
+ .eq(UserOrder::getOrderNo, accountRecordDto.getOrderNo()));
|
|
|
+ if (null == userOrder) {
|
|
|
+ throw new BizException("记录入账-未找到入账对应订单信息: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
+ }
|
|
|
+ //收入判断订单金额,所有收入不能大于订单用户付款
|
|
|
+ BigDecimal totalTransAmount = baseMapper.totalTransAmount(accountRecordDto);
|
|
|
+ if (null == totalTransAmount) {
|
|
|
+ totalTransAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ //金额校验失败
|
|
|
+ if (null == userOrder || totalTransAmount.add(accountRecordDto.getTransAmount()).compareTo(userOrder.getActualPrice()) > 0) {
|
|
|
+ log.error("记录入账-订单入账总金额大于购买金额: param is {}" + JSONObject.toJSONString(accountRecordDto));
|
|
|
+ accountRecordDto.setErrFlag(1);
|
|
|
+ accountRecordDto.setErrMsg("账户变更异常,订单入账总金额大于购买金额,订单号:" + accountRecordDto.getOrderNo());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //支出 需要判断所有支出金额不能大于账户余额
|
|
|
+ UserAccountVo detail = baseMapper.detail(accountRecordDto.getAccountId());
|
|
|
+ if (detail.getAmountUsable().compareTo(accountRecordDto.getTransAmount()) < 0) {
|
|
|
+ throw new BizException("记录入账-账户余额不足: param is {}", JSONObject.toJSONString(accountRecordDto));
|
|
|
}
|
|
|
- } catch (BizException e) {
|
|
|
- return HttpResponseResult.failed(e.getMessage());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return HttpResponseResult.failed("付款失败");
|
|
|
}
|
|
|
+ //如果是冻结入账,需要修改主账户冻结金额和可用余额
|
|
|
+ if (PostStatusEnum.FROZEN.equals(accountRecordDto.getPostStatus()) && accountRecordDto.getErrFlag() == 0) {
|
|
|
+ baseMapper.frozenChangeAccount(accountRecordDto.getAccountId(), accountRecordDto.getTransAmount(), accountRecordDto.getInOrOut().getCode());
|
|
|
+ }
|
|
|
+ //插入账户变更记录
|
|
|
+ userAccountRecordService.save(accountRecordDto);
|
|
|
+ return HttpResponseResult.succeed(accountRecordDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpResponseResult<UserAccountRecord> accountChange(Long recordId, PostStatusEnum postStatus) {
|
|
|
+ if (null == postStatus || null == recordId
|
|
|
+ || (!PostStatusEnum.RECORDED.equals(postStatus) && !PostStatusEnum.CANCEL.equals(postStatus))) {
|
|
|
+ throw new BizException("入账状态变更, 参数异常: recordId is {} postStatus is {}", recordId, postStatus.getCode());
|
|
|
+ }
|
|
|
+ UserAccountRecord param = new UserAccountRecord();
|
|
|
+ param.setId(recordId);
|
|
|
+ param.setPostStatus(postStatus);
|
|
|
+ HttpResponseResult<UserAccountRecord> res = DistributedLock.of(redissonClient)
|
|
|
+ .runIfLockToFunction(CacheNameEnum.LOCK_CHANGE_ACCOUNT.getRedisKey(recordId)
|
|
|
+ , this::doAccountChange, param, 10L);
|
|
|
+ if (null != res) {
|
|
|
+ return res;
|
|
|
+ } else {
|
|
|
+ throw new BizException("记录变更-账户记录变更失败: param is {}", JSONObject.toJSONString(param));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public HttpResponseResult<UserAccountRecord> doAccountChange(UserAccountRecord param) {
|
|
|
+ UserAccountRecordVo detail = userAccountRecordService.detail(param.getId());
|
|
|
+ if (null == detail) {
|
|
|
+ throw new BizException("入账状态变更, 未找到记录信息: recordId is {} postStatus is {}", param.getId(), param.getPostStatus().getCode());
|
|
|
+ }
|
|
|
+ detail.setPostStatus(param.getPostStatus());
|
|
|
+ //待入账
|
|
|
+ if (PostStatusEnum.WAIT.equals(detail.getPostStatus())) {
|
|
|
+ if (PostStatusEnum.RECORDED.equals(param.getPostStatus())) {
|
|
|
+ //正常入账
|
|
|
+ baseMapper.changeAccount(detail.getAccountId(), detail.getTransAmount(), detail.getInOrOut().getCode());
|
|
|
+ } else if (PostStatusEnum.CANCEL.equals(param.getPostStatus())) {
|
|
|
+ //取消入账
|
|
|
+ }
|
|
|
+ } else if (PostStatusEnum.FROZEN.equals(detail.getPostStatus())) {
|
|
|
+ if (PostStatusEnum.RECORDED.equals(param.getPostStatus())) {
|
|
|
+ //冻结后正常入账
|
|
|
+ baseMapper.frozenDeductChangeAccount(detail.getAccountId(), detail.getTransAmount(), detail.getInOrOut().getCode());
|
|
|
+ } else if (PostStatusEnum.CANCEL.equals(param.getPostStatus())) {
|
|
|
+ //冻结后取消入账
|
|
|
+ baseMapper.frozenBackChangeAccount(detail.getAccountId(), detail.getTransAmount(), detail.getInOrOut().getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ UserAccountVo accountVo = detail(detail.getAccountId());
|
|
|
+ if (accountVo.getAmountTotal().doubleValue() < 0
|
|
|
+ || accountVo.getAmountUsable().doubleValue() < 0 || accountVo.getAmountFrozen().doubleValue() < 0) {
|
|
|
+ throw new BizException("账户变更失败");
|
|
|
+ }
|
|
|
+ //插入账户变更记录
|
|
|
+ detail.setAccountBalance(accountVo.getAmountUsable());
|
|
|
+ userAccountRecordService.updateById(detail);
|
|
|
+ return HttpResponseResult.succeed(detail);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -134,48 +230,5 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountDao, UserAcco
|
|
|
return HttpResponseResult.succeed(total);
|
|
|
}
|
|
|
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- HttpResponseResult<UserAccountRecord> doAccountChange(UserAccountRecordDto accountRecordDto) {
|
|
|
- //收入要校验金额
|
|
|
- if (InOrOutEnum.IN.equals(accountRecordDto.getInOrOut())) {
|
|
|
- UserOrder userOrder = orderService.getOne(Wrappers.<UserOrder>lambdaQuery()
|
|
|
- .eq(UserOrder::getOrderNo, accountRecordDto.getOrderNo()));
|
|
|
-
|
|
|
- BigDecimal totalTransAmount = baseMapper.totalTransAmount(accountRecordDto);
|
|
|
- if (null == totalTransAmount) {
|
|
|
- totalTransAmount = BigDecimal.ZERO;
|
|
|
- }
|
|
|
- //金额校验失败,入账为冻结金额
|
|
|
- if (null == userOrder || totalTransAmount.add(accountRecordDto.getTransAmount()).compareTo(userOrder.getActualPrice()) > 0) {
|
|
|
- log.error("账户变更失败,param is " + JSONObject.toJSONString(accountRecordDto));
|
|
|
- accountRecordDto.setErrFlag(1);
|
|
|
- accountRecordDto.setErrMsg("账户变更异常,订单入账总金额大于购买金额,订单号:" + accountRecordDto.getOrderNo());
|
|
|
- }
|
|
|
- }
|
|
|
- if (accountRecordDto.getErrFlag() == 0) {
|
|
|
- if (FrozenTypeEnum.FROZEN.equals(accountRecordDto.getFrozenType())) {
|
|
|
- baseMapper.frozenChangeAccount(accountRecordDto.getUserId(), accountRecordDto.getTransAmount(), accountRecordDto.getInOrOut().getCode());
|
|
|
- } else if (FrozenTypeEnum.FROZEN_DEDUCT.equals(accountRecordDto.getFrozenType())) {
|
|
|
- baseMapper.frozenDeductChangeAccount(accountRecordDto.getUserId(), accountRecordDto.getTransAmount(), accountRecordDto.getInOrOut().getCode());
|
|
|
- } else if (FrozenTypeEnum.FROZEN_BACK.equals(accountRecordDto.getFrozenType())) {
|
|
|
- baseMapper.frozenBackChangeAccount(accountRecordDto.getUserId(), accountRecordDto.getTransAmount(), accountRecordDto.getInOrOut().getCode());
|
|
|
- } else {
|
|
|
- baseMapper.changeAccount(accountRecordDto.getUserId(), accountRecordDto.getTransAmount(), accountRecordDto.getInOrOut().getCode());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- UserAccountVo detail = detail(accountRecordDto.getUserId());
|
|
|
- if (detail.getAmountTotal().doubleValue() < 0
|
|
|
- || detail.getAmountUsable().doubleValue() < 0 || detail.getAmountFrozen().doubleValue() < 0) {
|
|
|
- throw new BizException("账户变更失败");
|
|
|
- }
|
|
|
|
|
|
- if (accountRecordDto.getSaveRecord()) {
|
|
|
- //插入账户变更记录
|
|
|
- accountRecordDto.setAccountId(accountRecordDto.getUserId());
|
|
|
- accountRecordDto.setAccountBalance(detail.getAmountUsable());
|
|
|
- userAccountRecordService.save(accountRecordDto);
|
|
|
- }
|
|
|
- return HttpResponseResult.succeed(accountRecordDto);
|
|
|
- }
|
|
|
}
|