From 03e2fe84c0298f09a8e1e34fb93c8221f5c7b989 Mon Sep 17 00:00:00 2001 From: gaowdong Date: Mon, 21 Apr 2025 15:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A4=E6=98=93=E8=AE=B0=E5=BD=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AccTradeServiceImpl.java | 4 + .../impl/AccWalletInfoServiceImpl.java | 90 ++++++++++++------- .../mapper/account/AccTradeMapper.xml | 3 +- 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccTradeServiceImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccTradeServiceImpl.java index 5a8548a..ceef5c4 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccTradeServiceImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccTradeServiceImpl.java @@ -15,6 +15,8 @@ import com.bonus.common.houqin.utils.id.Id; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -63,6 +65,7 @@ public class AccTradeServiceImpl implements IAccTradeService { * @return 结果 */ @Override + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) public int insertAccTrade(AccTrade accTrade) { accTrade.setCreateTime(DateUtils.getNowDate()); return accTradeMapper.insertAccTrade(accTrade); @@ -88,6 +91,7 @@ public class AccTradeServiceImpl implements IAccTradeService { * @return 结果 */ @Override + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) public int updateAccTrade(AccTrade accTrade) { accTrade.setUpdateTime(DateUtils.getNowDate()); try { diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccWalletInfoServiceImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccWalletInfoServiceImpl.java index 66b4068..96b069e 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccWalletInfoServiceImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/account/service/impl/AccWalletInfoServiceImpl.java @@ -37,6 +37,7 @@ import com.bonus.common.houqin.utils.id.Id; import com.bonus.common.security.utils.SecurityUtils; import com.bonus.system.api.RemoteUserService; import com.bonus.system.api.domain.SysUser; +import org.apache.commons.lang3.StringUtils; import org.checkerframework.checker.units.qual.A; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -242,7 +243,16 @@ public class AccWalletInfoServiceImpl implements IAccWalletInfoService { private void acWalletBalanceOperation(WalletBalanceOperation operation) { + Long tradeId = null; try{ + List walletInfoList = selectAccWalletInfoByUserId(operation.getUserId()); + BigDecimal accBalTotal = walletInfoList.stream().map(AccWalletInfo::getWalletBal) + .filter(ObjectUtil::isNotNull).reduce(BigDecimal.ZERO, BigDecimal::add); + tradeId = insertAccTradeRecode(operation, accBalTotal); + if(WalletBalanceOperationEnum.getEnum(operation.getOperationType()) == null) { + throw new ServiceException("钱包操作类型不存在"); + } + switch (WalletBalanceOperationEnum.getEnum(operation.getOperationType())) { case ADD_BAL: addAccWalletInfo(operation); @@ -253,38 +263,7 @@ public class AccWalletInfoServiceImpl implements IAccWalletInfoService { default: throw new ServiceException("钱包操作类型错误"); } - List walletInfoList = selectAccWalletInfoByUserId(operation.getUserId()); - BigDecimal accBalTotal = walletInfoList.stream().map(AccWalletInfo::getWalletBal) - .filter(ObjectUtil::isNotNull).reduce(BigDecimal.ZERO, BigDecimal::add); - AjaxResult userResult = remoteUserService.getInfo(operation.getUserId() , SecurityConstants.INNER); - SysUser sysUser = null; - if(Objects.nonNull(userResult)) { - sysUser = JSONObject.parseObject(JSON.toJSONString(userResult.get(AjaxResult.DATA_TAG)), SysUser.class); - } - long tradeId = Id.next(); - LocalDateTime tradeTime = LocalDateTime.now(); - AccTrade accTrade = new AccTrade(); - accTrade.setTradeId(tradeId); - accTrade.setTradeTime(tradeTime); - accTrade.setTradeType(operation.getTradeType()); - accTrade.setAmount(operation.getAmount()); - accTrade.setActualAmount(operation.getAmount()); - accTrade.setWalletBalTotal(accBalTotal); - accTrade.setAccAllBal(accBalTotal); - accTrade.setPayState(PayStateEnum.PAY_SUCC.getKey()); - accTrade.setTradeState(AccTradeStateEnum.TAKE_EFFECT.getKey()); - accTrade.setUserId(operation.getUserId()); - accTrade.setPayChannel(operation.getPayChannel()); - accTrade.setPayType(operation.getPayType()); - accTrade.setCreateBy(SecurityUtils.getUsername()); - if(Objects.nonNull(sysUser)) { - accTrade.setDeptId(sysUser.getDeptId()); - } - if(Objects.nonNull(operation.getOrderNo())) { - accTrade.setOrderNo(operation.getOrderNo().toString()); - } - this.accTradeService.insertAccTrade(accTrade); - + walletInfoList = selectAccWalletInfoByUserId(operation.getUserId()); AccTradeWalletDetail accTradeWalletDetail = new AccTradeWalletDetail(); accTradeWalletDetail.setTradeId(tradeId); accTradeWalletDetail.setUserId(operation.getUserId()); @@ -298,7 +277,22 @@ public class AccWalletInfoServiceImpl implements IAccWalletInfoService { accTradeWalletDetail.setTradeTime(DateUtils.getNowDate()); accTradeWalletDetail.setCreateBy(SecurityUtils.getUsername()); this.accTradeWalletDetailService.insertAccTradeWalletDetail(accTradeWalletDetail); + AccTrade accTrade = new AccTrade(); + accTrade.setTradeId(tradeId); + accTrade.setPayState(PayStateEnum.PAY_SUCC.getKey()); + accTrade.setTradeState(AccTradeStateEnum.TAKE_EFFECT.getKey()); + accTrade.setUpdateBy(SecurityUtils.getUsername()); + this.accTradeService.updateAccTrade(accTrade); }catch (Exception ex) { + if(Objects.nonNull(tradeId)) { + AccTrade accTrade = new AccTrade(); + accTrade.setTradeId(tradeId); + accTrade.setPayState(PayStateEnum.PAY_FAIL.getKey()); + accTrade.setTradeState(AccTradeStateEnum.CANCELED.getKey()); + accTrade.setUpdateBy(SecurityUtils.getUsername()); + accTrade.setFailReason(StringUtils.substring(ex.getMessage(), 0, 100)); + this.accTradeService.updateAccTrade(accTrade); + } log.error("修改钱包失败", ex); throw new ServiceException("修改钱包失败"); } @@ -313,4 +307,36 @@ public class AccWalletInfoServiceImpl implements IAccWalletInfoService { accWalletInfoMapper.reduceAccWalletInfo(operation.getAmount(), operation.getUserId(), operation.getWalletId(), SecurityUtils.getUserId().toString(), DateUtils.toLocalDateTime(new Date())); } + + private long insertAccTradeRecode(WalletBalanceOperation operation, BigDecimal accBalTotal) { + AjaxResult userResult = remoteUserService.getInfo(operation.getUserId() , SecurityConstants.INNER); + SysUser sysUser = null; + if(Objects.nonNull(userResult)) { + sysUser = JSONObject.parseObject(JSON.toJSONString(userResult.get(AjaxResult.DATA_TAG)), SysUser.class); + } + long tradeId = Id.next(); + LocalDateTime tradeTime = LocalDateTime.now(); + AccTrade accTrade = new AccTrade(); + accTrade.setTradeId(tradeId); + accTrade.setTradeTime(tradeTime); + accTrade.setTradeType(operation.getTradeType()); + accTrade.setAmount(operation.getAmount()); + accTrade.setActualAmount(operation.getAmount()); + accTrade.setWalletBalTotal(accBalTotal); + accTrade.setAccAllBal(accBalTotal); + accTrade.setPayState(PayStateEnum.PAY_INPROCESS.getKey()); + accTrade.setTradeState(AccTradeStateEnum.CREATE.getKey()); + accTrade.setUserId(operation.getUserId()); + accTrade.setPayChannel(operation.getPayChannel()); + accTrade.setPayType(operation.getPayType()); + accTrade.setCreateBy(SecurityUtils.getUsername()); + if(Objects.nonNull(sysUser)) { + accTrade.setDeptId(sysUser.getDeptId()); + } + if(Objects.nonNull(operation.getOrderNo())) { + accTrade.setOrderNo(operation.getOrderNo().toString()); + } + this.accTradeService.insertAccTrade(accTrade); + return tradeId; + } } diff --git a/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/account/AccTradeMapper.xml b/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/account/AccTradeMapper.xml index 9b02dbb..906fa4f 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/account/AccTradeMapper.xml +++ b/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/account/AccTradeMapper.xml @@ -284,7 +284,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" inner join acc_trade_wallet_detail atwd on ate.trade_id = atwd.trade_id where - ate.order_no = #{orderNo} + ate.pay_state = 3 + and ate.order_no = #{orderNo} and ate.trade_type = #{accTradeType}