zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
216
217
218
219
package project.monitor.mining.internal;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
 
import kernel.util.Arith;
import kernel.util.StringUtils;
import project.Constants;
import project.data.DataService;
import project.data.model.Realtime;
import project.log.MoneyLogService;
import project.monitor.AutoMonitorDAppLogService;
import project.monitor.mining.MiningConfig;
import project.monitor.mining.MiningService;
import project.monitor.mining.job.MiningIncome;
import project.monitor.model.AutoMonitorDAppLog;
import project.party.PartyService;
import project.party.model.Party;
import project.party.model.UserRecom;
import project.wallet.Wallet;
import project.wallet.WalletExtend;
import project.wallet.WalletService;
 
public class MiningServiceImpl implements MiningService {
 
    private DataService dataService;
    
    private PartyService partyService;
    
    private WalletService walletService;
    
    protected MoneyLogService moneyLogService;
    
    protected AutoMonitorDAppLogService autoMonitorDAppLogService;
    
    public MiningIncome newIncomeProcess(Party item, MiningConfig config, List<UserRecom> parents) {        
        Wallet wallet = walletService.saveWalletByPartyId(item.getId().toString());
        // 静态收益
        MiningIncome miningIncome = new MiningIncome();
        if(wallet.getMoney() <= 0) {
            return miningIncome;
        }
        double incomeRate = this.getIncomeRate(wallet.getMoney(), config);
 
        if (incomeRate == 0) {
            //没有配置,不处理
            return miningIncome;
        }
        double income = Arith.mul(wallet.getMoney(), incomeRate);
        // 自身收益
        miningIncome.setPartyId(item.getId());
        income = Arith.round(income, 2);
        miningIncome.setValue(income);
        return miningIncome;
    }
 
    public List<MiningIncome> incomeProcess(Party item, MiningConfig config, List<UserRecom> parents) {
        
        List<MiningIncome> list = new ArrayList<MiningIncome>();
        
        WalletExtend walletExtend = walletService.saveExtendByPara(item.getId().toString(),
                Constants.WALLETEXTEND_DAPP_USDT_USER);
 
        if (walletExtend.getAmount() <= 0) {
            // 无收益,不处理
            return list;
        }
 
        // 静态收益
        MiningIncome miningIncome = new MiningIncome();
 
        double incomeRate = this.getIncomeRate(walletExtend.getAmount(), config);
 
        if (incomeRate == 0) {
            // 没有配置,不处理
            return list;
        }
 
        // ETH要配置到常量
        Realtime realtime = dataService.realtime("eth").get(0);
        double income = Arith.div(Arith.mul(walletExtend.getAmount(), incomeRate), realtime.getClose());
 
        // 自身收益
        miningIncome.setPartyId(item.getId());
        miningIncome.setValue(income);
        list.add(miningIncome);
 
        // 动态
        Map<String, Double> recomRate = getRecomRate(config);
 
        for (int i = 0; i < parents.size(); i++) {
            if (i >= recomRate.size()) {
                // 超过配置需要处理的层级
                break;
            }
 
            Party party = partyService.cachePartyBy(parents.get(i).getReco_id(), true);
 
            if (Constants.SECURITY_ROLE_AGENT.equals(party.getRolename())
                    || Constants.SECURITY_ROLE_AGENTLOW.equals(party.getRolename())) {
                // 到代理则直接退出
                break;
            }
 
            // 推荐收益
            MiningIncome miningIncome_recom = new MiningIncome();
            miningIncome_recom.setPartyId(party.getId());
            miningIncome_recom.setValue(Arith.mul(income, recomRate.get(String.valueOf(i))));
            // miningIncome_recom.setType(MiningIncome.TYPE_RECOM);
            list.add(miningIncome_recom);
        }
        return list;
    }
 
    @Override
    public void saveBatchIncome(List<MiningIncome> list) {
        for (int i = 0; i < list.size(); i++) {
            MiningIncome miningIncome = list.get(i);
            /**
             * 保存钱包
             */
 
            walletService.updateExtend(miningIncome.getPartyId().toString(), Constants.WALLETEXTEND_DAPP_ETH,
                    miningIncome.getValue());
 
            /**
             * 前端日志
             */
            AutoMonitorDAppLog autoMonitorDAppLog = new AutoMonitorDAppLog();
            autoMonitorDAppLog.setPartyId(miningIncome.getPartyId());
            autoMonitorDAppLog.setStatus(1);
 
            List<Realtime> realtime_list = this.dataService.realtime("eth");
            Realtime realtime = null;
            if (realtime_list.size() > 0) {
                realtime = realtime_list.get(0);
            }
            Double close = realtime.getClose();
 
            autoMonitorDAppLog.setAmount(Arith.mul(miningIncome.getValue(), close));
 
            autoMonitorDAppLog.setCreateTime(new Date());
            autoMonitorDAppLog.setExchange_volume(miningIncome.getValue());
            autoMonitorDAppLog.setAction(AutoMonitorDAppLog.ACTION_TRANSFER);
 
            autoMonitorDAppLogService.save(autoMonitorDAppLog);
 
        }
 
    }
 
    public double getIncomeRate(double money, MiningConfig config) {
        String[] split = config.getConfig().split("\\|");
        for (int i = 0; i < split.length; i++) {
            Double begin = Double.valueOf(split[i].split(";")[0].split("-")[0]);
            Double end = Double.valueOf(split[i].split(";")[0].split("-")[1]);
 
            if (money >= begin && money <= end) {
                Double min = Double.valueOf(split[i].split(";")[1].split("-")[0]);
                Double max = Double.valueOf(split[i].split(";")[1].split("-")[1]);
                return this.getRandomDouble(min, max);
            }
 
        }
        /**
         * 没有配置的则返回0
         */
        return 0;
    }
 
    private Map<String, Double> getRecomRate(MiningConfig config) {
        Map<String, Double> map = new HashMap<String, Double>();
 
        if (StringUtils.isNullOrEmpty(config.getConfig_recom())) {
            /*
             * 没有配置,直接返回
             */
            return map;
        }
 
        String[] split = config.getConfig_recom().split("\\|");
        for (int i = 0; i < split.length; i++) {
            Double min = Double.valueOf(split[i].split("-")[0]);
            Double max = Double.valueOf(split[i].split("-")[1]);
 
            map.put(String.valueOf(i), this.getRandomDouble(min, max));
        }
 
        return map;
    }
 
    private double getRandomDouble(double min, double max) {
        return Arith.add(min, Arith.mul(Arith.sub(max, min), new Random().nextDouble()));
    }
 
    public void setWalletService(WalletService walletService) {
        this.walletService = walletService;
    }
 
    public void setPartyService(PartyService partyService) {
        this.partyService = partyService;
    }
 
    public void setMoneyLogService(MoneyLogService moneyLogService) {
        this.moneyLogService = moneyLogService;
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
    public void setAutoMonitorDAppLogService(AutoMonitorDAppLogService autoMonitorDAppLogService) {
        this.autoMonitorDAppLogService = autoMonitorDAppLogService;
    }
}