From a37210e77b883fbffc67050eb890cd6a0a3c7014 Mon Sep 17 00:00:00 2001 From: 陶杰 <1378534974@qq.com> Date: 星期五, 01 十一月 2024 09:53:45 +0800 Subject: [PATCH] 1.提现类型统一 --- src/main/java/com/mzl/flower/service/impl/wallet/WalletBillRecordServiceImpl.java | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 218 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/impl/wallet/WalletBillRecordServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/wallet/WalletBillRecordServiceImpl.java index 13ffa86..0420cbe 100644 --- a/src/main/java/com/mzl/flower/service/impl/wallet/WalletBillRecordServiceImpl.java +++ b/src/main/java/com/mzl/flower/service/impl/wallet/WalletBillRecordServiceImpl.java @@ -1,19 +1,43 @@ package com.mzl.flower.service.impl.wallet; -import com.mzl.flower.config.exception.ValidationException; +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.security.SecurityUtils; import com.mzl.flower.constant.Constants; +import com.mzl.flower.constant.LockConstants; import com.mzl.flower.dto.request.wallet.CreateWalletBillRecordDTO; -import com.mzl.flower.dto.response.supplier.SupplierDTO; +import com.mzl.flower.dto.request.wallet.QueryWalletBillRecordDTO; +import com.mzl.flower.dto.response.wallet.WalletBillRecordVO; +import com.mzl.flower.dto.request.wallet.QueryWalletBillDTO; +import com.mzl.flower.entity.payment.Transfer; +import com.mzl.flower.entity.payment.TransferDetail; 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.payment.TransferDetailMapper; +import com.mzl.flower.mapper.payment.TransferMapper; import com.mzl.flower.mapper.wallet.WalletBillRecordMapper; -import com.mzl.flower.service.supplier.SupplierService; +import com.mzl.flower.mapper.wallet.WalletMapper; +import com.mzl.flower.mapper.wallet.WalletWithdrawRecordMapper; import com.mzl.flower.service.wallet.WalletBillRecordService; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mzl.flower.service.wallet.WalletService; -import org.springframework.beans.factory.annotation.Autowired; +import com.mzl.flower.utils.ExcelExportUtil; +import com.mzl.flower.utils.UUIDGenerator; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; /** * <p> @@ -23,13 +47,202 @@ * @author @TaoJie * @since 2024-10-22 */ + @Service +@Transactional public class WalletBillRecordServiceImpl extends ServiceImpl<WalletBillRecordMapper, WalletBillRecordDO> implements WalletBillRecordService { + @Resource + private TransferMapper transferMapper; + + @Resource + private TransferDetailMapper transferDetailMapper; + + @Resource + private WalletBillRecordMapper walletBillRecordMapper; + + @Resource + private WalletService walletService; + + @Resource + private WalletMapper walletMapper; + + @Resource + private WalletWithdrawRecordMapper walletWithdrawRecordMapper; + @Resource + RedissonClient redissonClient; + + @Resource + WalletBillRecordService walletBillRecordService; @Override public void create(CreateWalletBillRecordDTO dto) { } + + public void updateTransferStatus(String transferId) { + Transfer t = transferMapper.selectById(transferId); + if ("FINISHED".equals(t.getStatus())) { + WalletBillRecordDO walletBillRecordDO = walletBillRecordMapper.selectOne(new QueryWrapper<WalletBillRecordDO>().eq("transfer_id", transferId)); + if (walletBillRecordDO == null) { + return; + } + String transferDetailId = walletBillRecordDO.getTransferDetailId(); + TransferDetail td = transferDetailMapper.selectById(transferDetailId); + if (td == null) { + log.warn("未找到对应明细"); + return; + } + String dStatus = td.getStatus(); + if ("SUCCESS".equals(dStatus)) { + walletBillRecordDO.setTransferState(Constants.SETTLEMENT_STATUS.COMPLETED.name()); + walletBillRecordDO.update("sys"); + walletBillRecordMapper.updateById(walletBillRecordDO); + //更新钱包 + WalletDO walletDO = walletService.getOrCreateBySupplierId(walletBillRecordDO.getWalletId()); + 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)) { + WalletBillRecordDO walletBillRecordDO2 = new WalletBillRecordDO(); + walletBillRecordDO2.setId(UUIDGenerator.getUUID()); + walletBillRecordDO2.setSupplierId(walletDO.getSupplierId()); + walletBillRecordDO2.setWalletId(walletDO.getId()); + walletBillRecordDO2.setType(Constants.BILL_CHANGE_TYPE.withdraw.name()); + // 发起提现 + walletBillRecordDO2.setWithdrawType(Constants.BILL_WITHDRAW_TYPE.success.name()); + walletBillRecordDO2.setMethod(Constants.BILL_CHANGE_METHOD.reduce.name()); + walletBillRecordDO2.setOriginalAmount(walletDO.getWithdrawableAmount()); + walletBillRecordDO2.setChangeAmount(BigDecimal.ZERO); + walletBillRecordDO2.setBalance(walletDO.getWithdrawableAmount()); + walletBillRecordDO2.create(SecurityUtils.getUserId()); + walletBillRecordDO.setId(UUIDGenerator.getUUID()); + // 保存账单明细 + walletBillRecordService.save(walletBillRecordDO); + //提现中金额:减少提现中金额 + walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().subtract(walletBillRecordDO.getChangeAmount())); + //已提现金额:增加已提现金额 + walletDO.setWithdrawnAmount(walletDO.getWithdrawnAmount().add(walletBillRecordDO.getChangeAmount())); + walletMapper.updateById(walletDO); + } + } finally { + lock.unlock(); + } + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + //回写提现状态 + if (StringUtils.isEmpty(walletBillRecordDO.getWithdrawRecordId())) { + WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletBillRecordDO.getWithdrawRecordId()); + withdrawRecordDO.setWithdrawState(Constants.BILL_WITHDRAW_TYPE.success.name()); + walletBillRecordDO.setRemark("账户资金提现完成"); + walletWithdrawRecordMapper.updateById(withdrawRecordDO); + } + + } else if ("FAIL".equals(dStatus)) { + walletBillRecordDO.setTransferState(Constants.SETTLEMENT_STATUS.FAILED.name()); + walletBillRecordDO.update("sys"); + walletBillRecordMapper.updateById(walletBillRecordDO); + WalletDO walletDO = walletService.getOrCreateBySupplierId(walletBillRecordDO.getWalletId()); + 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 walletBillRecordDO2 = new WalletBillRecordDO(); + walletBillRecordDO2.setId(UUIDGenerator.getUUID()); + walletBillRecordDO2.setSupplierId(walletDO.getSupplierId()); + walletBillRecordDO2.setWalletId(walletDO.getId()); + walletBillRecordDO2.setType(Constants.BILL_CHANGE_TYPE.withdraw.name()); + // 发起提现 + walletBillRecordDO2.setWithdrawType(Constants.BILL_WITHDRAW_TYPE.fail.name()); + walletBillRecordDO2.setMethod(Constants.BILL_CHANGE_METHOD.add.name()); + walletBillRecordDO2.setOriginalAmount(walletDO.getWithdrawableAmount()); + walletBillRecordDO2.setChangeAmount(walletBillRecordDO.getActualTransferAmount()); + walletBillRecordDO2.setBalance(walletDO.getWithdrawableAmount().add(walletBillRecordDO.getActualTransferAmount())); + walletBillRecordDO2.create(SecurityUtils.getUserId()); + walletBillRecordDO.setId(UUIDGenerator.getUUID()); + // 保存账单明细 + walletBillRecordService.save(walletBillRecordDO); + //提现中金额:减少提现中金额 + walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().subtract(walletBillRecordDO.getActualTransferAmount())); + //可提现金额:增加可提现金额 + walletDO.setWithdrawableAmount(walletDO.getWithdrawableAmount().add(walletBillRecordDO.getActualTransferAmount())); + walletMapper.updateById(walletDO); + } + } finally { + lock.unlock(); + } + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + //回写提现状态 + if (StringUtils.isEmpty(walletBillRecordDO.getWithdrawRecordId())) { + WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletBillRecordDO.getWithdrawRecordId()); + withdrawRecordDO.setWithdrawState(Constants.BILL_WITHDRAW_TYPE.fail.name()); + walletBillRecordDO.setRemark("账户资金提现失败"); + walletWithdrawRecordMapper.updateById(withdrawRecordDO); + } + } + } + } + + @Override + public Page<WalletBillRecordVO> queryPage(QueryWalletBillRecordDTO queryWalletBillRecordDTO, Page page) { + List<WalletBillRecordVO> list = walletBillRecordMapper.queryPage(queryWalletBillRecordDTO, page); + page.setRecords(list); + return page; + } + + @Override + public void exportSupplierFinanceList(HttpServletResponse response, QueryWalletBillRecordDTO queryWalletBillRecordDTO) { + + List<WalletBillRecordVO> list = walletBillRecordMapper.queryWalletBillRecordList(queryWalletBillRecordDTO); + String[] rowsName = new String[]{"序号","供应商信息", "变动类型", "原金额", "变动金额", "现余额", "提交时间", "审核时间", "备注"}; + List<Object[]> dataList = new ArrayList<>(); + int sn = 1; + for (WalletBillRecordVO o : list) { + Object[] objs = new Object[rowsName.length]; + int a = 0; + objs[a++] = sn; // 序号 + objs[a++] = o.getSupplierName()+":"+o.getSupplierId(); // 供应商信息 + objs[a++] = o.getTypeName(); // 变动类型 + objs[a++] = o.getOriginalAmount(); // 原金额 + objs[a++] = o.getChangeAmount(); // 变动金额 + objs[a++] = o.getBalance(); //现余额 + objs[a++] = o.getCreateTime();//提交时间 + objs[a++] = o.getApproveTime(); // 审核时间 + objs[a++] = o.getRemark(); //备注 + dataList.add(objs); + sn++; + } + + ExcelExportUtil excelExportUtil = new ExcelExportUtil("供应商财务", rowsName, dataList, response); + try { + response.addHeader("filename", URLEncoder.encode("供应商财务.xls", "UTF-8")); + response.addHeader("Access-Control-Expose-Headers", "filename"); + excelExportUtil.export(); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + @Override + public Page<WalletBillRecordVO> getPage(Page page, QueryWalletBillDTO dto) { + List<WalletBillRecordVO> list = walletBillRecordMapper.getPage(page,dto); + page.setRecords(list); + return page; + } + + @Override + public Page<WalletBillRecordVO> getPageByDesc(Page page, QueryWalletBillDTO dto) { + List<WalletBillRecordVO> list = walletBillRecordMapper.getPageByDesc(page,dto); + page.setRecords(list); + return page; + } } -- Gitblit v1.9.3