From 9507ab3e93d6a38b136fca4677851fe8645c191a Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 17 Jun 2022 14:14:41 +0800
Subject: [PATCH] fix

---
 src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java              |    2 +
 src/test/java/cc/mrbird/febs/ProfitTest.java                                |    4 ++
 src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java |   61 +++++++++++++++---------------
 src/main/resources/mapper/modules/MallMemberMapper.xml                      |    5 ++
 4 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java
index b48d370..5c4787a 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java
@@ -83,4 +83,6 @@
     List<MallMember> selectMemberDirectForHasLevel(@Param("inviteId") String inviteId);
 
     List<MallMember> selectDirectorsOrStoreMaster(@Param("type") Integer type);
+
+    List<MallMember> selectMemberWithLevel(String level);
 }
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 f087359..c2b6583 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
@@ -45,7 +45,7 @@
     private final IMallMoneyFlowService moneyFlowService;
     private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
     private final MallMoneyFlowMapper mallMoneyFlowMapper;
-    private final IMallAchieveService mallAchieveService;
+    private final MallAchieveRecordMapper mallAchieveRecordMapper;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -58,10 +58,10 @@
 
         Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
         // 套餐业绩
-        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
+//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
         // 普通商品业绩
-        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
-        BigDecimal totalIncome = tcIncome.add(normalIncome);
+//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
+        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
 
         if (totalIncome.compareTo(BigDecimal.ZERO) == 0) {
             return;
@@ -251,42 +251,43 @@
             profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
         }
         // 套餐业绩
-        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
+//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
         // 普通商品业绩
-        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
-        BigDecimal totalIncome = tcIncome.add(normalIncome);
+//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
+        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
 
         DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.AGENT_BONUS.getType(), DataDictionaryEnum.AGENT_BONUS.getCode());
-        BigDecimal profit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP);
-        BigDecimal preProfit = profit.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
+        BigDecimal profit = totalIncome.multiply(new BigDecimal(dic.getValue()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP));
 
         List<MallMember> agentMembers = mallMemberMapper.selectByIdAndNoLevel(null, AgentLevelEnum.FIRST_LEVEL.name());
         if (CollUtil.isEmpty(agentMembers)) {
             return;
         }
 
-        for (MallMember agentMember : agentMembers) {
-            DataDictionaryCustom agentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(AppContants.AGENT_LEVEL_REQUIRE, agentMember.getLevel());
-
-            if (agentDic == null) {
-                continue;
-            }
-
+        List<DataDictionaryCustom> dics = dataDictionaryCustomMapper.selectDicByType(AppContants.AGENT_LEVEL_REQUIRE);
+        for (DataDictionaryCustom agentDic : dics) {
             JSONObject jsonObject = JSONObject.parseObject(agentDic.getValue());
-            BigDecimal profitProp = jsonObject.getBigDecimal("profitProp");
-            BigDecimal income = preProfit.multiply(profitProp);
+            BigDecimal profitProp = jsonObject.getBigDecimal("profitProp").divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
 
-            int reduce = walletService.reduce(income, agentMember.getId(), "score");
-            if (reduce == 2) {
+            List<MallMember> agentMemberList = mallMemberMapper.selectMemberWithLevel(agentDic.getCode());
+            if (CollUtil.isEmpty(agentMemberList)) {
                 continue;
             }
 
-            walletService.add(income, agentMember.getId(), "commission");
+            BigDecimal income = profit.multiply(profitProp).divide(BigDecimal.valueOf(agentMemberList.size()), 2, RoundingMode.HALF_UP);
 
-            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());
+            for (MallMember agentMember : agentMemberList) {
+                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==#####");
     }
 
@@ -306,10 +307,10 @@
         }
 
         // 套餐业绩
-        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2);
+//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 2);
         // 普通商品业绩
-        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1);
-        BigDecimal totalIncome = tcIncome.add(normalIncome);
+//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M", 1);
+        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
 
         List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD");
         DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode());
@@ -395,10 +396,10 @@
             profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
         }
         // 套餐业绩
-        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
+//        BigDecimal tcIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 2);
         // 普通商品业绩
-        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
-        BigDecimal totalIncome = tcIncome.add(normalIncome);
+//        BigDecimal normalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D", 1);
+        BigDecimal totalIncome = mallAchieveRecordMapper.selectAchieveTotal("D", profitDate);
 
         // 董事
         DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DIRECTOR_BONUS.getType(), DataDictionaryEnum.DIRECTOR_BONUS.getCode());
diff --git a/src/main/resources/mapper/modules/MallMemberMapper.xml b/src/main/resources/mapper/modules/MallMemberMapper.xml
index 973bf34..a70bd00 100644
--- a/src/main/resources/mapper/modules/MallMemberMapper.xml
+++ b/src/main/resources/mapper/modules/MallMemberMapper.xml
@@ -395,4 +395,9 @@
             </if>
         </where>
     </select>
+
+    <select id="selectMemberWithLevel" resultType="cc.mrbird.febs.mall.entity.MallMember">
+        select * from mall_member
+        where level=#{level}
+    </select>
 </mapper>
\ No newline at end of file
diff --git a/src/test/java/cc/mrbird/febs/ProfitTest.java b/src/test/java/cc/mrbird/febs/ProfitTest.java
index ce90bcd..39005fd 100644
--- a/src/test/java/cc/mrbird/febs/ProfitTest.java
+++ b/src/test/java/cc/mrbird/febs/ProfitTest.java
@@ -35,6 +35,10 @@
     public void dynamicProfit() {
         memberProfitService.dynamicProfit(16L);
     }
+    @Test
+    public void agentProfit() {
+        memberProfitService.agentProfit(null);
+    }
 
 
     @Test

--
Gitblit v1.9.1