From f681a6a7852eb7c868ea539155861729c3e99da1 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 19 May 2022 17:59:07 +0800
Subject: [PATCH] finish return bonus

---
 src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java |  119 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 99 insertions(+), 20 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
index a47d48c..3780ec4 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java
@@ -16,6 +16,8 @@
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -42,6 +44,7 @@
     private final IApiMallMemberWalletService walletService;
     private final IMallMoneyFlowService moneyFlowService;
     private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
+    private final MallMoneyFlowMapper mallMoneyFlowMapper;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -53,7 +56,7 @@
         }
 
         Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
-        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountForDate(profitDate, null);
+        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D");
         if (totalIncome.compareTo(BigDecimal.ZERO) == 0) {
             return;
         }
@@ -71,10 +74,8 @@
             for (MallGoods goods : goodsList) {
                 BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit);
 
-                try {
-                    walletService.reduce(goodsProfit, member.getId(), "score");
-                } catch (Exception e) {
-                    log.info("静态分红异常:{}, {}", goodsProfit, member.getId());
+                int reduce = walletService.reduce(goodsProfit, member.getId(), "score");
+                if (reduce == 2) {
                     break;
                 }
 
@@ -132,12 +133,11 @@
                     continue;
                 }
 
-                try {
-                    walletService.reduce(direct, parentMember.getId(), "score");
-                } catch (Exception e) {
-                    log.info("隔代推荐奖异常:{}, {}", direct, parentMember.getId());
+                int reduce =  walletService.reduce(direct, parentMember.getId(), "score");
+                if (reduce == 2) {
                     continue;
                 }
+
                 walletService.add(direct, parentMember.getId(), "commission");
 
                 moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
@@ -149,12 +149,11 @@
                 direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
             }
 
-            try {
-                walletService.reduce(dynamicProfit, member.getId(), "score");
-            } catch (Exception e) {
-                log.info("直推奖励异常:{}, {}", dynamicProfit, member.getId());
+            int reduce = walletService.reduce(dynamicProfit, member.getId(), "score");
+            if (reduce == 2) {
                 continue;
             }
+
             walletService.add(dynamicProfit, member.getId(), "commission");
 
             moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
@@ -164,7 +163,7 @@
 
     @Override
     public void agentProfit(Date profitDate) {
-        log.info("#####==代理分红==#####");
+        log.info("#####==代理分红==start==#####");
         if (profitDate == null) {
             profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
         }
@@ -191,25 +190,105 @@
             BigDecimal profitProp = jsonObject.getBigDecimal("profitProp");
             BigDecimal income = preProfit.multiply(profitProp);
 
-            try {
-                walletService.reduce(income, agentMember.getId(), "score");
-            } catch (Exception e) {
-                log.info("直推奖励异常:{}, {}", income, agentMember.getId());
+            int reduce = walletService.reduce(income, agentMember.getId(), "score");
+            if (reduce == 2) {
                 continue;
             }
+
             walletService.add(income, agentMember.getId(), "commission");
 
             moneyFlowService.addMoneyFlow(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
             moneyFlowService.addMoneyFlow(agentMember.getId(), income.negate(), MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
         }
+
+        log.info("#####==代理分红==end==#####");
     }
 
     @Override
     public void rankProfit() {
         log.info("######==排名分红==####");
+        Page<MallMember> page = new Page<>(1, 5);
+        MallMember query = new MallMember();
+        query.setQuery("2");
+        Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
+        query.setCreatedTime(profitDate);
+        IPage<MallMember> rankListInPage = mallMemberMapper.selectRankListInPage(page, query);
+
+        List<MallMember> rankList = rankListInPage.getRecords();
+        if (CollUtil.isEmpty(rankList)) {
+            return;
+        }
+
+        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M");
+
+        List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD");
+        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode());
+
+
+        BigDecimal rankBonusTotal = new BigDecimal(dic.getValue()).divide(totalIncome, 2, RoundingMode.HALF_UP);
+        BigDecimal preBonus = rankBonusTotal.divide(BigDecimal.valueOf(100),2 , RoundingMode.HALF_UP);
+
+        int i = 0;
+        for (MallMember member : rankList) {
+            DataDictionaryCustom rank = rankAward.get(i);
+            i++;
+            BigDecimal bonus = preBonus.multiply(new BigDecimal(rank.getValue()));
+            int reduce = walletService.reduce(bonus, member.getId(), "score");
+            if (reduce == 2) {
+                continue;
+            }
+
+            walletService.add(bonus, member.getId(), "commission");
+
+            moneyFlowService.addMoneyFlow(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
+            moneyFlowService.addMoneyFlow(member.getId(), bonus.negate(), MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue());
+        }
     }
 
-    public static void main(String[] args) {
-        System.out.println(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1));
+    /**
+     * 拿推荐人收益(代理,直推,排名)的10%加权平分给下面直推
+     */
+    @Override
+    public void thankfulProfit() {
+        log.info("######==感恩奖==start==####");
+        List<MallMember> mallMembers = mallMemberMapper.selectMemberListHasChild();
+        if (CollUtil.isEmpty(mallMembers)) {
+            return;
+        }
+
+        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.THANKFUL_BONUS.getType(), DataDictionaryEnum.THANKFUL_BONUS.getCode());
+        // 感恩奖比例
+        BigDecimal prop = new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
+
+        Date date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
+        for (MallMember mallMember : mallMembers) {
+            BigDecimal income = mallMoneyFlowMapper.selectThankfulCommission(date, mallMember.getId());
+            List<MallMember> children = mallMemberMapper.selectMemberDirectForHasLevel(mallMember.getInviteId());
+            if (CollUtil.isEmpty(children)) {
+                continue;
+            }
+
+            BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN);
+
+
+            int reduce = walletService.reduce(preProfit.negate(), mallMember.getId(), "commission");
+            if (reduce == 2) {
+                continue;
+            }
+
+            moneyFlowService.addMoneyFlow(mallMember.getId(), income.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue());
+
+            for (MallMember child : children) {
+                int reduce1 = walletService.reduce(preProfit.negate(), child.getId(), "score");
+                if (reduce1 == 2) {
+                    continue;
+                }
+
+                walletService.add(preProfit, child.getId(), "commission");
+                moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue());
+                moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue());
+            }
+        }
+        log.info("######==感恩奖==end==####");
     }
 }

--
Gitblit v1.9.1