From 4f0449594ac069d569d477e800ed77de1a339836 Mon Sep 17 00:00:00 2001
From: Hentua <wangdoubleone@gmail.com>
Date: Fri, 21 Apr 2023 17:56:47 +0800
Subject: [PATCH] 添加全网分红逻辑

---
 src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java              |    2 
 src/main/java/cc/mrbird/febs/common/enumerates/DataDictionaryEnum.java      |    7 ++
 src/main/java/cc/mrbird/febs/mall/mapper/MallSystemSettingMapper.java       |    7 ++
 src/main/java/cc/mrbird/febs/mall/service/impl/MemberProfitServiceImpl.java |   70 +++++++++++++++++++++++
 src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java                     |    2 
 src/main/resources/mapper/modules/MallMemberMapper.xml                      |   23 +++++++
 src/main/java/cc/mrbird/febs/mall/entity/MallSystemSetting.java             |   24 ++++++++
 src/main/java/cc/mrbird/febs/mall/service/IMemberProfitService.java         |    2 
 8 files changed, 135 insertions(+), 2 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/common/enumerates/DataDictionaryEnum.java b/src/main/java/cc/mrbird/febs/common/enumerates/DataDictionaryEnum.java
index d1ccca5..4eb99d8 100644
--- a/src/main/java/cc/mrbird/febs/common/enumerates/DataDictionaryEnum.java
+++ b/src/main/java/cc/mrbird/febs/common/enumerates/DataDictionaryEnum.java
@@ -81,7 +81,12 @@
     /**
      * 美业直推分红设置
      */
-    DIRECT_BONUS_SETTING("DIRECT_BONUS_SETTING", "");
+    DIRECT_BONUS_SETTING("DIRECT_BONUS_SETTING", ""),
+
+    INTERNET_LEVEL_BONUS("INTERNET_LEVEL_BONUS", ""),
+
+    ALL_INTERNET_BONUS("MEIYE_BONUS_TYPE", "ALL_BONUS"),
+    ALL_INTERNET_BONUS_RELEASE("MEIYE_BONUS_TYPE", "BONUS_RELEASE");
     private String type;
 
     private String code;
diff --git a/src/main/java/cc/mrbird/febs/mall/entity/MallSystemSetting.java b/src/main/java/cc/mrbird/febs/mall/entity/MallSystemSetting.java
new file mode 100644
index 0000000..357b52c
--- /dev/null
+++ b/src/main/java/cc/mrbird/febs/mall/entity/MallSystemSetting.java
@@ -0,0 +1,24 @@
+package cc.mrbird.febs.mall.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+/**
+ * @author wzy
+ * @date 2023-04-21
+ **/
+@Data
+@TableName("mall_system_setting")
+public class MallSystemSetting {
+
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    private BigDecimal allBonus;
+
+    private BigDecimal agentBonus;
+}
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 dbf4fa1..f0c7f9f 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallMemberMapper.java
@@ -103,4 +103,6 @@
     List<MallMember> selectByRole(@Param("type") int type, @Param("state")int state);
 
     List<MallMember> selectParentMemberList(@Param("list") List<String> inviteIds, @Param("refererId") String refererId, @Param("limit") Integer limit);
+
+    List<MallMember> selectMemberAfterLevelList(String level);
 }
diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/MallSystemSettingMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/MallSystemSettingMapper.java
new file mode 100644
index 0000000..6e323b2
--- /dev/null
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallSystemSettingMapper.java
@@ -0,0 +1,7 @@
+package cc.mrbird.febs.mall.mapper;
+
+import cc.mrbird.febs.mall.entity.MallSystemSetting;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+public interface MallSystemSettingMapper extends BaseMapper<MallSystemSetting> {
+}
diff --git a/src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java b/src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java
index 9c61526..e56cc42 100644
--- a/src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java
+++ b/src/main/java/cc/mrbird/febs/mall/quartz/ProfitJob.java
@@ -42,7 +42,7 @@
      */
     @Scheduled(cron = "0 30 0 * * ?")
     public void profitJob() {
-        memberProfitService.agentProfit(null);
+        memberProfitService.agentProfit();
     }
 
     /**
diff --git a/src/main/java/cc/mrbird/febs/mall/service/IMemberProfitService.java b/src/main/java/cc/mrbird/febs/mall/service/IMemberProfitService.java
index 8c98896..a63f64f 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/IMemberProfitService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/IMemberProfitService.java
@@ -30,4 +30,6 @@
     void scorePool();
 
     void selaHalfVoucher(String price);
+
+    void allInternetProfit();
 }
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 b3fe593..350ebad 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
@@ -61,6 +61,8 @@
     private final MallScoreAchieveReleaseMapper mallScoreAchieveReleaseMapper;
     private final MallScoreVoucherMapper mallScoreVoucherMapper;
 
+    private final MallSystemSettingMapper mallSystemSettingMapper;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void staticProfit(Date profitDate) {
@@ -628,4 +630,72 @@
         return achieveRelease;
     }
 
+    @Override
+    public void allInternetProfit() {
+        log.info("###全网分红执行--start--###");
+        MallSystemSetting systemSetting = mallSystemSettingMapper.selectById(1L);
+        if (systemSetting == null) {
+            log.info("没有系统配置");
+            return;
+        }
+
+        BigDecimal hundred = BigDecimal.valueOf(100);
+        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.ALL_INTERNET_BONUS_RELEASE.getType(), DataDictionaryEnum.ALL_INTERNET_BONUS_RELEASE.getCode());
+        if (dic == null || StrUtil.isBlank(dic.getValue()) || Integer.parseInt(dic.getValue()) == 0) {
+            log.info("不进行全网分红");
+            return;
+        }
+
+        // 全网分红
+        BigDecimal waitToBonus = systemSetting.getAllBonus().multiply(new BigDecimal(dic.getValue()).divide(hundred, 2, RoundingMode.HALF_UP));
+        List<MallMember> mallMembers = mallMemberMapper.selectMemberAfterLevelList(MemberLevelEnums.FIRST_LEVEL_Y.getCode());
+        if (CollUtil.isEmpty(mallMembers) || waitToBonus.compareTo(BigDecimal.ZERO) < 1) {
+            log.info("待分红金额不足或会员不足");
+            return;
+        }
+
+        List<DataDictionaryCustom> dicList = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.INTERNET_LEVEL_BONUS.getType());
+        if (CollUtil.isEmpty(dicList)) {
+            return;
+        }
+
+        Map<String, BigDecimal> levelBonusMap = new HashMap<>();
+        dicList.forEach(item -> {
+            BigDecimal levelRatio = new BigDecimal(item.getValue()).divide(hundred, 2, RoundingMode.HALF_UP);
+            levelBonusMap.put(item.getCode(), waitToBonus.multiply(levelRatio));
+        });
+
+        Map<String, List<MallMember>> levelMemberMap = new HashMap<>();
+        // TODO 并发处理
+        mallMembers.forEach(item -> {
+            List<MallMember> memberList = levelMemberMap.get(item.getLevel());
+            if (CollUtil.isEmpty(memberList)) {
+                memberList = new ArrayList<>();
+            }
+
+            memberList.add(item);
+            levelMemberMap.put(item.getLevel(), memberList);
+        });
+
+        if (levelMemberMap.isEmpty()) {
+            return;
+        }
+
+        for (Map.Entry<String, List<MallMember>> entry : levelMemberMap.entrySet()) {
+            BigDecimal levelBonus = levelBonusMap.get(entry.getKey());
+            List<MallMember> memberList = entry.getValue();
+            
+            if (CollUtil.isEmpty(memberList) || levelBonus.compareTo(BigDecimal.ZERO) < 1) {
+                continue;
+            }
+
+            BigDecimal perBonus = levelBonus.divide(BigDecimal.valueOf(memberList.size()), 2, RoundingMode.HALF_UP);
+            memberList.forEach(item -> {
+                changeScoreAndCommission(item.getId(), perBonus, 2, null);
+            });
+        }
+
+
+        log.info("###全网分红执行--end--###");
+    }
 }
diff --git a/src/main/resources/mapper/modules/MallMemberMapper.xml b/src/main/resources/mapper/modules/MallMemberMapper.xml
index e7fbe16..6e75bff 100644
--- a/src/main/resources/mapper/modules/MallMemberMapper.xml
+++ b/src/main/resources/mapper/modules/MallMemberMapper.xml
@@ -457,4 +457,27 @@
             limit #{limit}
         </if>
     </select>
+
+    <select id="selectMemberAfterLevelList" resultType="cc.mrbird.febs.mall.entity.MallMember">
+        SELECT
+            m.*, b.
+            VALUE
+
+        FROM
+            mall_member m
+                INNER JOIN data_dictionary_custom b ON m.LEVEL = b. CODE
+        WHERE 1=1
+          AND b.type = 'AGENT_LEVEL'
+          AND b.VALUE > (
+            SELECT
+                VALUE
+            FROM
+                data_dictionary_custom a
+            WHERE
+                a. CODE = #{level}
+              AND a.type = 'AGENT_LEVEL'
+        )
+        GROUP BY
+            m.id
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.1