From 6c823dd44dbde79f008001a2a11e7bf9bc6bf8cc Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期三, 04 十二月 2024 18:15:44 +0800
Subject: [PATCH] fix:合伙人列表操作日志

---
 src/main/java/com/mzl/flower/service/impl/wallet/WalletWithdrawRecordServiceImpl.java |  357 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 339 insertions(+), 18 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 d4793f1..1b6b228 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
@@ -1,62 +1,383 @@
 package com.mzl.flower.service.impl.wallet;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.mzl.flower.config.PyamentV3Configurer;
 import com.mzl.flower.config.exception.ValidationException;
 import com.mzl.flower.config.security.SecurityUtils;
 import com.mzl.flower.constant.Constants;
+import com.mzl.flower.constant.LockConstants;
+import com.mzl.flower.dto.request.payment.TransferDetailReqDTO;
+import com.mzl.flower.dto.request.payment.TransferReqDTO;
 import com.mzl.flower.dto.request.wallet.CreateWalletWithdrawRecordDTO;
+import com.mzl.flower.dto.request.wallet.QueryWalletWithdrawDTO;
+import com.mzl.flower.dto.request.wallet.WalletWithdrawRecordDTO;
 import com.mzl.flower.dto.response.supplier.SupplierDTO;
+import com.mzl.flower.dto.response.wallet.WalletWithdrawRecordVO;
+import com.mzl.flower.entity.supplier.Supplier;
+import com.mzl.flower.entity.system.UserWechat;
+import com.mzl.flower.entity.wallet.WalletBillRecordDO;
 import com.mzl.flower.entity.wallet.WalletDO;
 import com.mzl.flower.entity.wallet.WalletWithdrawRecordDO;
+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;
+import com.mzl.flower.service.wallet.WalletBillRecordService;
 import com.mzl.flower.service.wallet.WalletService;
 import com.mzl.flower.service.wallet.WalletWithdrawRecordService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.mzl.flower.utils.UUIDGenerator;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springframework.beans.BeanUtils;
 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;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * <p>
- *  服务实现类
+ * 服务实现类
  * </p>
  *
  * @author @TaoJie
  * @since 2024-10-22
  */
+@Slf4j
 @Service
+@Transactional
+@RequiredArgsConstructor
 public class WalletWithdrawRecordServiceImpl extends ServiceImpl<WalletWithdrawRecordMapper, WalletWithdrawRecordDO> implements WalletWithdrawRecordService {
-    @Autowired
-    private WalletService walletService;
+
+    private final WalletWithdrawRecordMapper walletWithdrawRecordMapper;
+    private final WalletService walletService;
+    private final SupplierService supplierService;
+    private final SupplierMapper supplierMapper;
+    private final UserWechatMapper wechatMapper;
+    private final UserPaymentV3Service paymentV3Service;
+
+    private final WalletBillRecordMapper walletBillRecordMapper;
+
+    private final WalletBillRecordService walletBillRecordService;
+
+    private final WalletMapper walletMapper;
+
 
     @Autowired
-    private SupplierService supplierService;
+    RedissonClient redissonClient;
 
     @Transactional
     @Override
     public boolean create(CreateWalletWithdrawRecordDTO dto) {
-        SupplierDTO supplierDTO=supplierService.getCurrentSupplier();
-        if(null==supplierDTO){
+
+        SupplierDTO supplierDTO = supplierService.getCurrentSupplier();
+        if (null == supplierDTO) {
             throw new ValidationException("供应商不能为空");
         }
 
-        final WalletDO walletDO = walletService.getBySupplierId(supplierDTO.getId());
-        if(null==walletDO){
+        WalletDO walletDO = walletService.getCurrentSupplier();
+        if (null == walletDO) {
             throw new ValidationException("钱包不能为空");
         }
         // 查看钱包的金额是不是大于要提现的金额
-        if(null!=walletDO.getWithdrawableAmount() && null!=dto.getAmount()
-                && walletDO.getWithdrawableAmount().compareTo(dto.getAmount())<0){
+        if (null != walletDO.getWithdrawableAmount() && null != dto.getAmount() && walletDO.getWithdrawableAmount().compareTo(dto.getAmount()) < 0) {
             throw new ValidationException("钱包金额不足");
         }
-        WalletWithdrawRecordDO withdrawRecordDO=new WalletWithdrawRecordDO();
-        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.create(SecurityUtils.getUserId());
 
-        return save(withdrawRecordDO);
+        RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
+        // 获取锁,最多等待 10 秒,锁自动释放时间 30 秒
+        try {
+            if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
+                try {
 
+                    // 加锁了之后需要二次查询,防止未加锁之前 someone else 已经修改了
+                    walletDO = walletService.getCurrentSupplier();
+                    if (null == walletDO) {
+                        throw new ValidationException("钱包不能为空");
+                    }
+                    // 查看钱包的金额是不是大于要提现的金额
+                    if (null != walletDO.getWithdrawableAmount() && null != dto.getAmount() && walletDO.getWithdrawableAmount().compareTo(dto.getAmount()) < 0) {
+                        throw new ValidationException("钱包金额不足");
+                    }
+
+                    // 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.BILL_WITHDRAW_TYPE.start.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.setId(UUIDGenerator.getUUID());
+                    walletBillRecordDO.setSupplierId(supplierDTO.getId());
+                    walletBillRecordDO.setWalletId(walletDO.getId());
+                    walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name());
+                    // 发起提现
+                    walletBillRecordDO.setWithdrawType(Constants.BILL_WITHDRAW_TYPE.start.name());
+                    walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.reduce.name());
+                    walletBillRecordDO.setOriginalAmount(walletDO.getWithdrawableAmount());
+                    walletBillRecordDO.setChangeAmount(dto.getAmount());
+                    walletBillRecordDO.setBalance(walletDO.getWithdrawableAmount().subtract(dto.getAmount()));
+                    walletBillRecordDO.create(SecurityUtils.getUserId());
+                    walletBillRecordDO.setWithdrawRecordId(withdrawRecordDO.getId());
+                    walletBillRecordDO.setRemark("提现中");
+
+                    // 保存账单明细
+                    walletBillRecordService.save(walletBillRecordDO);
+
+
+
+                    // 3. 钱包更新
+                    // 钱包可提现的钱等于当前钱包可提现的钱-提现的钱
+                    walletDO.setWithdrawableAmount(walletDO.getWithdrawableAmount().subtract(dto.getAmount()));
+                    // 提现中的钱等于原有可提现的钱+提现的钱
+                    walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().add(dto.getAmount()));
+                    walletDO.update(SecurityUtils.getUserId());
+
+                    // 更新钱包
+                    return walletService.updateById(walletDO);
+
+                } finally {
+                    lock.unlock();
+                }
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+
+        return false;
+    }
+
+    @Override
+    public Page<WalletWithdrawRecordVO> queryPage(QueryWalletWithdrawDTO queryWalletDTO, Page page) {
+        try {
+            if (!StringUtils.isEmpty(queryWalletDTO.getSupplierId())) {
+                Long supplierId = Long.parseLong(queryWalletDTO.getSupplierId());
+            }
+        } catch (Exception e) {
+            throw new ValidationException("供应商ID格式不正确");
+        }
+        List<WalletWithdrawRecordVO> list = walletWithdrawRecordMapper.queryPage(queryWalletDTO, page);
+        page.setRecords(list);
+        return page;
+    }
+
+    @Override
+    public void updateWallet(WalletWithdrawRecordDTO walletWithdrawRecordDTO) {
+
+        WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletWithdrawRecordDTO.getId());
+        final String approveState = withdrawRecordDO.getApproveState();
+        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("不通过理由不能为空");
+            }
+        }
+
+        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.BILL_WITHDRAW_TYPE.fail.name());
+        }
+        walletWithdrawRecordMapper.updateById(withdrawRecordDO);
+
+        //审批通过之后写转账信息
+        if (Constants.WALLET_APPROVE_STATE.APPROVE.name().equals(walletWithdrawRecordDTO.getApproveState())) {
+            RLock recordLock = redissonClient.getLock(String.format(LockConstants.WALLET_WITHDRAW_RECORD_KEY, walletWithdrawRecordDTO.getId()));
+            try {
+                if (recordLock.tryLock(10, 30, TimeUnit.SECONDS)) {
+                    try {
+                        if(!approveState.equals(Constants.WALLET_APPROVE_STATE.WAITING.name())){
+                            String msg =  "重复通过";
+                            throw new ValidationException(msg);
+                        }
+                        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)) {
+                                        String name = "";
+                                        name = "供应商" + s.getName();
+                                        UserWechat wechat = wechatMapper.selectOne(new QueryWrapper<UserWechat>().eq("user_id", s.getUserId()));
+
+                                        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 + "钱包提现";
+                                        String appId = PyamentV3Configurer.supplier_app_id;
+                                        TransferReqDTO transferReqDTO = new TransferReqDTO();
+                                        transferReqDTO.setId(UUIDGenerator.getUUID());
+                                        transferReqDTO.setAppId(appId);
+                                        transferReqDTO.setName(day + "日" + remarks);
+                                        transferReqDTO.setRemarks(remarks);
+
+                                        //保存账单信息
+//                                        WalletBillRecordDO walletBillRecordDO = new WalletBillRecordDO();
+//                                        walletBillRecordDO.setSupplierId(s.getId());
+//                                        walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name());
+//                                        walletBillRecordDO.setWithdrawType(Constants.BILL_WITHDRAW_TYPE.success.name());
+//                                        walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.reduce.name());
+//                                        walletBillRecordDO.setTransferId(transferReqDTO.getId());
+//                                        walletBillRecordDO.setWithdrawRecordId(withdrawRecordDO.getId());
+//                                        walletBillRecordDO.create();
+//                                        walletBillRecordDO.setOriginalAmount(walletDO.getWithdrawableAmount());
+//                                        walletBillRecordDO.setChangeAmount(BigDecimal.ZERO);
+//                                        walletBillRecordDO.setBalance(walletDO.getWithdrawableAmount());
+                                        WalletBillRecordDO walletBillRecordDO = walletBillRecordService.getBillRecordByWithdrawRecordId(withdrawRecordDO.getId());
+                                        if(ObjectUtils.isEmpty(walletBillRecordDO)){
+                                            throw new ValidationException("提现没有对应的账单信息");
+                                        }
+                                        walletBillRecordDO.setTransferId(transferReqDTO.getId());
+                                        //提现金额
+                                        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());
+                                            dr.setOpenId(wechat.getOpenId());
+                                            dr.setRemarks(remarks);
+                                            if (withdrawAmount.doubleValue() >= 2000) {
+                                                dr.setUserName(wechat.getRealName());
+                                            }
+
+                                            List<TransferDetailReqDTO> details = new ArrayList<>();
+                                            details.add(dr);
+                                            walletBillRecordDO.setTransferDetailId(dr.getId());
+                                            log.info("发起转账");
+                                            transferReqDTO.setDetails(details);
+                                            try {
+                                                //发起转账
+                                                 paymentV3Service.doBatchTransfer(transferReqDTO, SecurityUtils.getUserId());
+                                            } catch (Exception e) {
+                                                throw new ValidationException(e.getMessage());
+                                            }
+                                        }
+
+                                        //记录转账状态,定时任务定时获取状态并更新钱包交易记录表信息
+                                        walletBillRecordDO.setRemark("微信商户转账中");
+                                        walletBillRecordDO.setActualTransferAmount(withdrawAmount);
+                                        walletBillRecordDO.setApproveBy(SecurityUtils.getUserId());
+                                        walletBillRecordDO.setApproveTime(LocalDateTime.now());
+                                        walletBillRecordMapper.updateById(walletBillRecordDO);
+                                        //不需要更新结算单了,此时提现的金额和计算单上的金额不一致
+                                    }
+                                } finally {
+                                    lock.unlock();
+                                }
+                            }
+                        } catch (InterruptedException e) {
+                            throw new RuntimeException(e);
+                        }
+                    } finally {
+                        recordLock.unlock();
+                    }
+                }
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        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)) {
+                            // 2.新增一条账单明细
+                            WalletBillRecordDO walletBillRecordDO = new WalletBillRecordDO();
+                            walletBillRecordDO.setId(UUIDGenerator.getUUID());
+                            walletBillRecordDO.setSupplierId(walletDO.getSupplierId());
+                            walletBillRecordDO.setWalletId(walletDO.getId());
+                            walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name());
+                            // 发起提现
+                            walletBillRecordDO.setWithdrawType(Constants.BILL_WITHDRAW_TYPE.fail.name());
+                            walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.add.name());
+                            walletBillRecordDO.setOriginalAmount(walletDO.getWithdrawableAmount());
+                            walletBillRecordDO.setChangeAmount(withdrawRecordDO.getAmount());
+                            walletBillRecordDO.setBalance(walletDO.getWithdrawableAmount().add(withdrawRecordDO.getAmount()));
+                            walletBillRecordDO.create(SecurityUtils.getUserId());
+                            walletBillRecordDO.setId(UUIDGenerator.getUUID());
+                            walletBillRecordDO.setRemark("平台审核拒绝,余额返还钱包");
+                            walletBillRecordDO.setApproveBy("sys");
+                            walletBillRecordDO.setApproveTime(LocalDateTime.now());
+                            // 保存账单明细
+                            walletBillRecordService.save(walletBillRecordDO);
+
+                            //提现中金额:审核失败体现中金额扣减
+                            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) {
+        if (dateTime == null || format == null) {
+            return null;
+        }
+        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format).withZone(ZoneId.of("Asia/Shanghai"));
+        return dateTimeFormatter.format(dateTime);
     }
 }
+

--
Gitblit v1.9.3