From 9b51a95fa8bc9b3a86108c6900e17fc55f8db467 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 19 May 2025 17:02:07 +0800
Subject: [PATCH] refactor(vip): 优化积分和返佣计算逻辑

---
 src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java |   83 +++++++++++++++++++++++++++++------------
 1 files changed, 59 insertions(+), 24 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
index 8ed0b3d..2372a63 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
@@ -3,6 +3,7 @@
 import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
 import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
 import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
+import cc.mrbird.febs.common.enumerates.ScoreFlowTypeEnum;
 import cc.mrbird.febs.common.exception.FebsException;
 import cc.mrbird.febs.common.utils.LoginUserUtil;
 import cc.mrbird.febs.common.utils.MallUtils;
@@ -14,15 +15,24 @@
 import cc.mrbird.febs.mall.service.IMallMemberWithdrawService;
 import cc.mrbird.febs.mall.service.MallMemberService;
 import cc.mrbird.febs.mall.vo.CashOutSettingVo;
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.SecureUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Date;
+import java.util.List;
 
 /**
  * @author wzy
@@ -41,6 +51,7 @@
     private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void withdrawal(WithdrawalDto withdrawalDto) {
         Long memberId = LoginUserUtil.getLoginUser().getId();
         MallMember mallMember = mallMemberService.getById(memberId);
@@ -52,43 +63,67 @@
             throw new FebsException("支付密码错误");
         }
 
-        if (withdrawalDto.getAmount().compareTo(BigDecimal.valueOf(100)) < 0) {
-            throw new FebsException("最小提现金额为100");
-        }
-
-        MallMemberPayment payment = mallMemberPaymentMapper.selectByMemberId(memberId);
-        if (payment == null) {
-            throw new FebsException("未设置收款方式");
-        }
-
-        BigDecimal profit = mallMoneyFlowMapper.selectProfitByDateAndMemberId(memberId);
-        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
-        if (profit != null) {
-            // 可提现
-            BigDecimal canMoney = wallet.getCommission().subtract(profit);
-
-            if(withdrawalDto.getAmount().compareTo(canMoney) > 0) {
-                throw new FebsException("提现金额不足");
-            }
-        }
 
         CashOutSettingVo cashOutSettingVo = new CashOutSettingVo();
         DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.CASHOUT_SETTING.getType(), DataDictionaryEnum.CASHOUT_SETTING.getCode());
         if (dic != null) {
             cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class);
         }
+        if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) {
+            throw new FebsException("最小提现金额为"+cashOutSettingVo.getMinCashOut()+"元");
+        }
+        DateTime dateTime = DateUtil.beginOfDay(new Date());
+        List<MallMemberWithdraw> mallMemberWithdraws = this.baseMapper.selectList(
+                new LambdaQueryWrapper<MallMemberWithdraw>()
+                        .ge(MallMemberWithdraw::getCreatedTime, dateTime)
+        );
+        if(CollUtil.isNotEmpty(mallMemberWithdraws)){
+            //stream流操作mallMemberWithdraws,获取amount的总和
+            BigDecimal sum = mallMemberWithdraws.stream().map(MallMemberWithdraw::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
+            BigDecimal sumTotal = sum.add(withdrawalDto.getAmount());
+            BigDecimal allCashOut = cashOutSettingVo.getAllCashOut();
+            if(sumTotal.compareTo(allCashOut) > 0){
+                BigDecimal subtract = allCashOut.subtract(sum).setScale(2, RoundingMode.HALF_DOWN);
+                throw new FebsException("今日还能提现"+subtract);
+            }
+        }
 
-        walletService.reduce(withdrawalDto.getAmount(), memberId, "commission");
-        String orderNo = MallUtils.getOrderNum("W");
+        String openId = mallMember.getOpenId();
+        if(StrUtil.isEmpty(openId)){
+            throw new FebsException("没有绑定微信");
+        }
 
+        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
+        if (ObjectUtil.isNotEmpty(wallet)) {
+            // 可提现
+            BigDecimal canMoney = wallet.getBalance();
+            if(withdrawalDto.getAmount().compareTo(canMoney) > 0) {
+                throw new FebsException("提现金额不足");
+            }
+        }
+
+        walletService.reduce(withdrawalDto.getAmount(), memberId, "balance");
+//        String withdrawalNo = "TX_"+MallUtils.getOrderNum();
         MallMemberWithdraw withdraw = new MallMemberWithdraw();
-        withdraw.setWithdrawNo(orderNo);
+//        withdraw.setWithdrawNo(withdrawalNo);
         withdraw.setMemberId(memberId);
         withdraw.setAmount(withdrawalDto.getAmount());
         withdraw.setStatus(1);
-        withdraw.setAmountFee(cashOutSettingVo.getServiceFee().multiply(withdrawalDto.getAmount()));
         this.baseMapper.insert(withdraw);
+        String withdrawalNo = "TX_"+withdraw.getId().toString();
+        withdraw.setWithdrawNo(withdrawalNo);
+        this.baseMapper.updateById(withdraw);
 
-        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.COMMISSION.getValue());
+        mallMemberService.addMoneyFlow(
+                memberId,
+                withdrawalDto.getAmount().negate(),
+                ScoreFlowTypeEnum.WITHDRAWAL.getValue(),
+                withdrawalNo,
+                StrUtil.format(ScoreFlowTypeEnum.WITHDRAWAL.getDesc(),withdrawalDto.getAmount()),
+                StrUtil.format(ScoreFlowTypeEnum.WITHDRAWAL.getDesc(),withdrawalDto.getAmount()),
+                null,
+                1,
+                FlowTypeEnum.BALANCE.getValue()
+        );
     }
 }

--
Gitblit v1.9.1