Helius
2022-05-19 54852835a9b1f9fe3574264fa9e2611f1d20e621
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.enumerates.AgentLevelEnum;
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.utils.AppContants;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.mall.service.IMemberProfitService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
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
 * @date 2022-05-10
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MemberProfitServiceImpl implements IMemberProfitService {
 
    private final MallMemberMapper mallMemberMapper;
    private final MallOrderInfoMapper mallOrderInfoMapper;
    private final MallOrderItemMapper mallOrderItemMapper;
    private final MallGoodsMapper mallGoodsMapper;
    private final IApiMallMemberWalletService walletService;
    private final IMallMoneyFlowService moneyFlowService;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void staticProfit() {
        log.info("#####静态分红开始运行:{}#####", new Date());
        List<MallMember> members = mallMemberMapper.selectList(null);
        if (CollUtil.isEmpty(members)) {
            return;
        }
 
        Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountForDate(profitDate, null);
        if (totalIncome.compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
 
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.STATIC_BONUS.getType(), DataDictionaryEnum.STATIC_BONUS.getCode());
        BigDecimal perProfit = totalIncome.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
 
        for (MallMember member : members) {
            List<MallGoods> goodsList = mallGoodsMapper.selectOrderGoodsList(member.getId(), profitDate);
 
            if (CollUtil.isEmpty(goodsList)) {
                continue;
            }
 
            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());
                    break;
                }
 
                walletService.add(goodsProfit, member.getId(), "commission");
                moneyFlowService.addMoneyFlow(member.getId(), goodsProfit, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(member.getId(), goodsProfit.negate(), MoneyFlowTypeEnum.STATIC_BONUS.getValue(), goods.getOrderNo(), FlowTypeEnum.SCORE.getValue());
            }
        }
    }
 
    /**
     * 直推20%,隔代收益为直推奖励金额的30%,a_b_c_d,d购买1000套餐,c得200,b得200*30%=60元,a得60*30%=18元。。。
     * 以此类推,结算到一元为止。
     *
     * @param orderId
     */
    @Override
    public void dynamicProfit(Long orderId) {
        log.info("######直推奖励, 订单ID:{}######", orderId);
        MallOrderInfo orderInfo = mallOrderInfoMapper.selectById(orderId);
        if (orderInfo.getOrderType() == 2) {
            log.info("积分订单无返利");
            return;
        }
 
        MallMember member = mallMemberMapper.selectById(orderInfo.getMemberId());
 
        if (StrUtil.isBlank(member.getReferrerId())) {
            return;
        }
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.DYNAMIC_BONUS.getType(), DataDictionaryEnum.DYNAMIC_BONUS.getCode());
        DataDictionaryCustom indrectDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.INDRECT_BONUS.getType(), DataDictionaryEnum.INDRECT_BONUS.getCode());
 
        String parent = member.getReferrerId();
        List<MallOrderItem> items = mallOrderInfoMapper.getMallOrderItemByOrderId(orderId);
        for (MallOrderItem item : items) {
            // 减去成本后算收益
            BigDecimal amount = item.getPrice().subtract(item.getCostPrice()).multiply(BigDecimal.valueOf(item.getCnt()));
 
            BigDecimal dynamicProfit = amount.divide(new BigDecimal(dic.getValue()), 2, RoundingMode.HALF_UP);
 
            List<String> parents = StrUtil.split(member.getReferrerIds(), ',');
            List<MallMember> members = mallMemberMapper.selectByInviteIds(parents);
            if (CollUtil.isEmpty(members)) {
                return;
            }
 
            // 隔代比例
            BigDecimal indrectDicProp = new BigDecimal(indrectDic.getValue());
 
            // 隔代推荐奖
            BigDecimal direct = dynamicProfit.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
            for (MallMember parentMember : members) {
                if (parent.equals(parentMember.getInviteId())) {
                    continue;
                }
 
                try {
                    walletService.reduce(direct, parentMember.getId(), "score");
                } catch (Exception e) {
                    log.info("隔代推荐奖异常:{}, {}", direct, parentMember.getId());
                    continue;
                }
                walletService.add(direct, parentMember.getId(), "commission");
 
                moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
                moneyFlowService.addMoneyFlow(parentMember.getId(), direct.negate(), MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
 
                if (direct.compareTo(BigDecimal.ONE) < 1) {
                    break;
                }
                direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP);
            }
 
            try {
                walletService.reduce(dynamicProfit, member.getId(), "score");
            } catch (Exception e) {
                log.info("直推奖励异常:{}, {}", dynamicProfit, member.getId());
                continue;
            }
            walletService.add(dynamicProfit, member.getId(), "commission");
 
            moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue());
            moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit.negate(), MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.SCORE.getValue());
        }
    }
 
    @Override
    public void agentProfit(Date profitDate) {
        log.info("#####==代理分红==#####");
        if (profitDate == null) {
            profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1);
        }
        BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D");
 
 
        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);
 
        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;
            }
 
            JSONObject jsonObject = JSONObject.parseObject(agentDic.getValue());
            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());
                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());
        }
    }
 
    @Override
    public void rankProfit() {
        log.info("######==排名分红==####");
    }
 
    public static void main(String[] args) {
        System.out.println(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1));
    }
}