From 19f963e688b672c2dba9eb77aeea57b4eab62829 Mon Sep 17 00:00:00 2001 From: gongzuming <gongzuming> Date: 星期二, 29 十月 2024 11:34:14 +0800 Subject: [PATCH] 菜单优化 --- src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java | 90 ++++++++++++++++++++++++++++++++------------ 1 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java index f2653ae..46ccee8 100644 --- a/src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java +++ b/src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java @@ -23,6 +23,7 @@ import com.mzl.flower.mapper.supplier.SupplierMapper; import com.mzl.flower.mapper.system.UserWechatMapper; import com.mzl.flower.mapper.wallet.WalletBillRecordMapper; +import com.mzl.flower.mapper.wallet.WalletMapper; import com.mzl.flower.mapper.wallet.WalletWithdrawRecordMapper; import com.mzl.flower.service.payment.UserPaymentV3Service; import com.mzl.flower.service.supplier.SupplierService; @@ -38,6 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.math.BigDecimal; @@ -72,6 +74,8 @@ private final WalletBillRecordMapper walletBillRecordMapper; private final WalletBillRecordService walletBillRecordService; + + private final WalletMapper walletMapper; @Autowired @@ -113,29 +117,25 @@ // 1.新增一条余额提现记录 WalletWithdrawRecordDO withdrawRecordDO = new WalletWithdrawRecordDO(); + //原金额 + withdrawRecordDO.setOriginalAmount(walletDO.getWithdrawableAmount()); + // 变动金额 + withdrawRecordDO.setChangeAmount(dto.getAmount()); + // 余额 + withdrawRecordDO.setBalance(walletDO.getWithdrawableAmount().subtract(dto.getAmount())); withdrawRecordDO.setAmount(dto.getAmount()); withdrawRecordDO.setSupplierId(supplierDTO.getId()); withdrawRecordDO.setWithdrawState(Constants.WALLET_WITHDRAW_STATE.WAITING.name()); withdrawRecordDO.setMethod(Constants.WALLET_WITHDRAW_METHOD.WEIXIN.name()); + withdrawRecordDO.setWithdrawType(Constants.WALLET_WITHDRAW_TYPE.BALANCE.name()); + // 待审核状态 + withdrawRecordDO.setApproveState(Constants.WALLET_APPROVE_STATE.WAITING.name()); + withdrawRecordDO.create(SecurityUtils.getUserId()); + // 保存余额提现记录 save(withdrawRecordDO); - /* - // 2.新增一条账单明细 - WalletBillRecordDO walletBillRecordDO=new WalletBillRecordDO(); - walletBillRecordDO.setSupplierId(supplierDTO.getId()); - walletBillRecordDO.setWalletId(walletDO.getId()); - // 提现 - walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name()); - walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.add.name()); - walletBillRecordDO.setOriginalAmount(walletDO.getWithdrawableAmount()); - walletBillRecordDO.setChangeAmount(dto.getAmount()); - walletBillRecordDO.setBalance(walletDO.getWithdrawableAmount().subtract(dto.getAmount())); - - // 保存账单明细 - walletBillRecordService.save(walletBillRecordDO); -*/ // 3. 钱包更新 // 钱包可提现的钱等于当前钱包可提现的钱-提现的钱 @@ -167,36 +167,46 @@ @Override public void updateWallet(WalletWithdrawRecordDTO walletWithdrawRecordDTO) { + WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletWithdrawRecordDTO.getId()); if (withdrawRecordDO == null) { throw new ValidationException("提现记录信息不存在"); } + if (StringUtils.isEmpty(walletWithdrawRecordDTO.getApproveState())) { throw new ValidationException("审批状态不能为空"); } + if (Constants.WALLET_APPROVE_STATE.REJECT.name().equals(walletWithdrawRecordDTO.getApproveState())) { - if(StringUtils.isEmpty(walletWithdrawRecordDTO.getRejectReason())){ - throw new ValidationException("不通过理由不能为空"); - } + if (StringUtils.isEmpty(walletWithdrawRecordDTO.getRejectReason())) { + throw new ValidationException("不通过理由不能为空"); + } } + BeanUtils.copyProperties(walletWithdrawRecordDTO, withdrawRecordDO); withdrawRecordDO.update(SecurityUtils.getUserId()); withdrawRecordDO.setApproveState(walletWithdrawRecordDTO.getApproveState()); withdrawRecordDO.setApproveBy(SecurityUtils.getUserId()); withdrawRecordDO.setApproveTime(LocalDateTime.now()); + if(Constants.WALLET_APPROVE_STATE.REJECT.name().equals(walletWithdrawRecordDTO.getApproveState())){ + //如果拒绝直接提现失败 + withdrawRecordDO.setWithdrawState(Constants.WALLET_WITHDRAW_STATE.FAILURE.name()); + } walletWithdrawRecordMapper.updateById(withdrawRecordDO); //审批通过之后写转账信息 - if(Constants.WALLET_APPROVE_STATE.APPROVE.name().equals(walletWithdrawRecordDTO.getApproveState())){ + if (Constants.WALLET_APPROVE_STATE.APPROVE.name().equals(walletWithdrawRecordDTO.getApproveState())) { Supplier s = supplierMapper.selectById(withdrawRecordDO.getSupplierId()); String name = ""; name = "供应商" + s.getName(); UserWechat wechat = wechatMapper.selectOne(new QueryWrapper<UserWechat>().eq("user_id", s.getUserId())); - if(wechat == null){ - String msg = name + "未绑定账号无法结算"; + + if (wechat == null) { + String msg = name + "未绑定账号无法提现"; log.error(msg); throw new ValidationException(msg); } + LocalDateTime now = LocalDateTime.now(); String day = format(now, "yyyy-MM-dd"); String remarks = name + "钱包提现"; @@ -208,14 +218,17 @@ transferReqDTO.setRemarks(remarks); //保存账单信息 - WalletBillRecordDO walletBillRecordDO = new WalletBillRecordDO(); + WalletBillRecordDO walletBillRecordDO = new WalletBillRecordDO(); walletBillRecordDO.setSupplierId(s.getId()); walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name()); walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.reduce.name()); - walletBillRecordDO.setTransferId(transferReqDTO.getAppId()); + walletBillRecordDO.setTransferId(transferReqDTO.getId()); + walletBillRecordDO.setWithdrawRecordId(withdrawRecordDO.getId()); + walletBillRecordDO.create(); //提现金额 - BigDecimal withdrawAmount = withdrawRecordDO.getAmount(); - if(withdrawAmount.doubleValue() > 0) {//结算金额>0时才去转账 + BigDecimal withdrawAmount = withdrawRecordDO.getAmount(); + + if (withdrawAmount.doubleValue() > 0) {//结算金额>0时才去转账 TransferDetailReqDTO dr = new TransferDetailReqDTO(); dr.setId(UUIDGenerator.getUUID()); dr.setAmount(withdrawAmount.multiply(new BigDecimal(100)).longValue()); @@ -233,9 +246,36 @@ //发起转账 paymentV3Service.doBatchTransfer(transferReqDTO, SecurityUtils.getUserId()); } + + //记录转账状态,定时任务定时获取状态并更新钱包交易记录表信息 + walletBillRecordDO.setRemark("账户资金提现中"); walletBillRecordMapper.insert(walletBillRecordDO); //不需要更新结算单了,此时提现的金额和计算单上的金额不一致 + } + if (Constants.WALLET_APPROVE_STATE.REJECT.name().equals(walletWithdrawRecordDTO.getApproveState())) { + //更新钱包 + Supplier s = supplierMapper.selectById(withdrawRecordDO.getSupplierId()); + + WalletDO walletDO = walletService.getOrCreateBySupplierId(s.getId()); + RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId())); + try { + if (lock.tryLock(10, 30, TimeUnit.SECONDS)) { + try { + if (!ObjectUtils.isEmpty(walletDO)) { + //提现中金额:审核失败体现中金额扣减 + walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().subtract(withdrawRecordDO.getAmount())); + //可提现金额:审核失败可提现金额增加 + walletDO.setWithdrawableAmount(walletDO.getWithdrawableAmount().add(withdrawRecordDO.getAmount())); + walletMapper.updateById(walletDO); + } + } finally { + lock.unlock(); + } + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } } public String format(LocalDateTime dateTime, String format) { -- Gitblit v1.9.3