Helius
2021-05-13 1b44a9a25a51324dba8e8640cb405440f2cf7d48
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
package com.xcong.excoin.modules.yunding.service.Impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.xcong.excoin.modules.coin.dao.MemberAccountMoneyChangeDao;
import com.xcong.excoin.modules.member.dao.MemberDao;
import com.xcong.excoin.modules.member.dao.MemberWalletCoinDao;
import com.xcong.excoin.modules.member.entity.MemberEntity;
import com.xcong.excoin.modules.member.entity.MemberWalletCoinEntity;
import com.xcong.excoin.modules.yunding.dao.YdOrderDao;
import com.xcong.excoin.modules.yunding.dao.YdProductDao;
import com.xcong.excoin.modules.yunding.entity.YdBasicLevelSettingEntity;
import com.xcong.excoin.modules.yunding.entity.YdOrderEntity;
import com.xcong.excoin.modules.yunding.entity.YdProductEntity;
import com.xcong.excoin.modules.yunding.service.XchProfitService;
import com.xcong.excoin.utils.LogRecordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class XchProfitServiceImpl implements XchProfitService {
 
    @Autowired
    private MemberDao memberDao;
 
    @Autowired
    private YdOrderDao ydOrderDao;
 
    @Autowired
    private MemberWalletCoinDao memberWalletCoinDao;
 
    @Autowired
    private YdProductDao ydProductDao;
 
    @Override
    public void autoBeAgent(Long id) {
 
    }
 
    @Override
    public void agentUsdtProfitDistributor() {
        List<YdOrderEntity> orders = ydOrderDao.selectNeedReturnOrders();
 
        if (CollUtil.isNotEmpty(orders)) {
            for (YdOrderEntity order : orders) {
                usdtProfitDistributor(order);
            }
        }
    }
 
    @Override
    public void usdtProfitDistributorByOrderId(Long id) {
        YdOrderEntity ydOrderEntity = ydOrderDao.selectById(id);
        usdtProfitDistributor(ydOrderEntity);
    }
 
    private void usdtProfitDistributor(YdOrderEntity order) {
        MemberEntity memberEntity = memberDao.selectById(order.getMemberId());
        List<String> inviteIds = StrUtil.split(memberEntity.getRefererIds(), ',');
        List<MemberEntity> agents = memberDao.selectYdParentAgent(inviteIds);
 
        if (CollUtil.isNotEmpty(agents)) {
            Map<Long, BigDecimal> returnRatio = buildReturnRatioObj(agents, 1);
 
            for (Map.Entry<Long, BigDecimal> entry : returnRatio.entrySet()) {
                String conent = "USDT返利";
                BigDecimal amount = order.getAmount().multiply(entry.getValue());
                LogRecordUtils.insertMemberAccountMoneyChangeWithId(entry.getKey(), conent, amount, "USDT", 1, 6, order.getId());
                MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(entry.getKey(), "USDT");
 
                memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), amount, BigDecimal.ZERO, 0);
            }
 
            YdOrderEntity updateOrder = new YdOrderEntity();
            updateOrder.setReturnState(2);
            updateOrder.setId(order.getId());
            ydOrderDao.updateById(updateOrder);
        }
    }
 
    private Map<Long, BigDecimal> buildReturnRatioObj(List<MemberEntity> agents, int type) {
        Map<Long, BigDecimal> returnRatio = new HashMap<Long, BigDecimal>();
        Long lastId = null;
        for (MemberEntity agent : agents) {
            YdBasicLevelSettingEntity settingEntity = agent.getYdBasicLevelSettingEntity();
 
            BigDecimal ratio;
            if (type == 1) {
                ratio = settingEntity.getUsdtRatio();
            } else {
                ratio = settingEntity.getXchRatio();
            }
            if(settingEntity.getLevel() == 1) {
                if (CollUtil.isNotEmpty(returnRatio)) {
                    BigDecimal lastRatio = returnRatio.get(lastId);
                    returnRatio.put(agent.getId(), ratio.subtract(lastRatio));
                } else {
                    returnRatio.put(agent.getId(), ratio);
                }
                break;
            }
 
            if (CollUtil.isEmpty(returnRatio)) {
                lastId = agent.getId();
                returnRatio.put(agent.getId(), ratio);
            }
        }
 
        return returnRatio;
    }
 
    @Override
    public void xchProfitDistributor(BigDecimal totalProfit) {
        Date currentDate = new Date();
        List<YdProductEntity> products = ydProductDao.selectList(null);
        if (CollUtil.isNotEmpty(products)) {
            BigDecimal totalCount = BigDecimal.ZERO;
            for (YdProductEntity product : products) {
                BigDecimal count = product.getTotalT();
                if ("P".equals(product.getProUnit())) {
                    count = count.multiply(BigDecimal.valueOf(1024));
                }
 
                totalCount = totalCount.add(count);
            }
 
            // 单位XCH收益
            BigDecimal unitProfit = totalProfit.divide(totalCount, 8, BigDecimal.ROUND_DOWN);
 
            List<YdOrderEntity> orders = ydOrderDao.selectAllValidOrders();
            if (CollUtil.isNotEmpty(orders)) {
                for (YdOrderEntity order : orders) {
                    long between = DateUtil.between(order.getWorkTime(), currentDate, DateUnit.DAY);
                    if (between < 1) {
                        continue;
                    }
 
                    MemberEntity memberEntity = memberDao.selectById(order.getMemberId());
                    List<String> inviteIds = StrUtil.split(memberEntity.getRefererIds(), ',');
                    List<MemberEntity> agents = memberDao.selectYdParentAgent(inviteIds);
 
                    BigDecimal count = BigDecimal.valueOf(order.getQuantity());
                    if ("P".equals(order.getYdProductEntity().getProUnit())) {
                        count = count.multiply(BigDecimal.valueOf(1024));
                    }
                    BigDecimal orderProfit = count.multiply(unitProfit);
                    BigDecimal remainProfit = orderProfit;
                    if (CollUtil.isNotEmpty(agents)) {
                        Map<Long, BigDecimal> returnRatio = buildReturnRatioObj(agents, 2);
 
                        for (Map.Entry<Long, BigDecimal> entry : returnRatio.entrySet()) {
                            String content = "XCH收益返利";
                            BigDecimal amount = entry.getValue().multiply(orderProfit);
 
                            LogRecordUtils.insertMemberAccountMoneyChangeWithId(entry.getKey(), content, amount, "XCH", 1, 5, order.getId());
                            MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(entry.getKey(), "XCH");
 
                            memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), amount, BigDecimal.ZERO, 0);
                            remainProfit = remainProfit.subtract(amount);
                        }
                    }
 
                    String content = "XCH收益";
                    LogRecordUtils.insertMemberAccountMoneyChangeWithId(order.getMemberId(), content, remainProfit, "XCH", 1, 4, order.getId());
                    MemberWalletCoinEntity walletCoin = memberWalletCoinDao.selectWalletCoinBymIdAndCode(order.getMemberId(), "XCH");
 
                    memberWalletCoinDao.updateBlockBalance(walletCoin.getId(), remainProfit, BigDecimal.ZERO, 0);
                }
            }
        }
    }
}