Helius
2021-07-15 abbb3326355de35bcb535deba49d8da704d92d63
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
package com.xzx.gc.order.service;
 
import cn.hutool.core.util.StrUtil;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.entity.*;
import com.xzx.gc.order.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author wzy
 * @date 2021-07-14
 **/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class DistribService {
 
    @Autowired
    private UserHeadDetailsMapper userHeadDetailsMapper;
    @Autowired
    private UserHeadRelateMapper userHeadRelateMapper;
    @Autowired
    private JhyOrderMapper jhyOrderMapper;
    @Autowired
    private RedPaperRuleMapper redPaperRuleMapper;
    @Autowired
    private JhyOrderItemsMapper jhyOrderItemsMapper;
 
    public void distribRecord(Long orderId, String userId) {
        UserHeadRelate userHeadRelate = userHeadRelateMapper.selectRelateByUserId(userId);
        if (userHeadRelate == null) {
            return;
        }
 
        RedPaperRule redPaperRule = redPaperRuleMapper.selectDistribRule();
        if (0 == redPaperRule.getStatus()) {
            return;
        }
 
        Integer count = userHeadDetailsMapper.selectDetailsCount(userId);
 
        List<String> remark = new ArrayList<>();
        BigDecimal totalReturnScore = BigDecimal.ZERO;
        BigDecimal totalReturnCoin = BigDecimal.ZERO;
 
        BigDecimal totalScore = jhyOrderItemsMapper.selectOrderScoreByOrderId(orderId);
        JhyOrder order = jhyOrderMapper.selectByPrimaryKey(orderId);
        if (count == 0) {
            String value = getRuleValue(redPaperRule, CommonEnum.团长首单奖励.getValue());
 
            // 首单返利
            if (StrUtil.isNotBlank(value) && !"0".equals(value)) {
                BigDecimal money = new BigDecimal(value);
                totalReturnScore = totalReturnScore.add(money);
                remark.add("首单返利:" + money);
            }
        }
 
        if (count > 0) {
            String value = getRuleValue(redPaperRule, CommonEnum.首单后返利数量.getValue());
 
            if (StrUtil.isNotBlank(value) && !"0".equals(value)) {
                // 首单完成后,在value单内,返利积分或者环保币
                if (count - 1 <= Integer.parseInt(value)) {
                    String scoreStr = getRuleValue(redPaperRule, CommonEnum.返利固定积分.getValue());
                    String coinStr = getRuleValue(redPaperRule, CommonEnum.返利环保币比例.getValue());
                    // 根据积分返利固定积分
                    if (StrUtil.isNotBlank(scoreStr) && !"0".equals(scoreStr)) {
                        BigDecimal score = new BigDecimal(scoreStr);
                        totalReturnScore = totalReturnScore.add(score);
                        remark.add("首单完成后返固定积分:" + scoreStr);
                    }
 
                    // 根据订单总积分, 1:40比例,换算成环保币,返对应百分比
                    if (StrUtil.isNotBlank(coinStr) && !"0".equals(coinStr)) {
                        BigDecimal coinRatio = new BigDecimal(coinStr).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_DOWN);
                        BigDecimal totalCoin = totalScore.multiply(new BigDecimal(40));
 
                        BigDecimal returnCoin = totalCoin.multiply(coinRatio).setScale(2, BigDecimal.ROUND_DOWN);
                        totalReturnCoin = totalReturnCoin.add(returnCoin);
                        remark.add("首单完成后返比例环保币:" + returnCoin +"(" + coinRatio +")");
                    }
 
                    // 当用户进行订单数量,超过value单后,每完成一单返利指定积分
                } else {
                    String finishValue = getRuleValue(redPaperRule, CommonEnum.完成数量后返积分.getValue());
                    if (StrUtil.isNotBlank(finishValue) && !"0".equals(finishValue)) {
                        BigDecimal score = new BigDecimal(finishValue);
                        totalReturnScore = totalReturnScore.add(score);
                        remark.add("完成指定"+ Integer.parseInt(value) +"数量后每单返利:" + score);
                    }
                }
            }
        }
 
        UserHeadDetails userHeadDetails = new UserHeadDetails();
        userHeadDetails.setHeadUserId(userHeadRelate.getHeadUserId());
        userHeadDetails.setUserId(userId);
        userHeadDetails.setOrderNo(order.getOrderNo());
        userHeadDetails.setAmount(totalReturnCoin);
        userHeadDetails.setScore(totalReturnScore);
        userHeadDetails.setCreatedTime(new Date());
        userHeadDetailsMapper.insert(userHeadDetails);
 
        userHeadRelate.setAmount(userHeadRelate.getAmount().add(totalReturnCoin));
        userHeadRelate.setScore(userHeadDetails.getScore().add(totalReturnScore));
        userHeadRelateMapper.updateByPrimaryKey(userHeadRelate);
 
    }
 
    private String getRuleValue(RedPaperRule redPaperRule, String key) {
        List<String> rules = StrUtil.split(redPaperRule.getSharingProfitType(), ',');
        List<String> values = StrUtil.split(redPaperRule.getShareRatio(), ',');
 
        int i = rules.indexOf(key);
        return values.get(i);
    }
}