zj
2025-05-02 01830e44921b187b448d8cce9c9a46b9ad55af43
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
package project.monitor.pledge.internal;
 
import java.util.Date;
import java.util.Random;
 
import kernel.util.Arith;
import kernel.web.ApplicationUtil;
import project.Constants;
import project.data.DataService;
import project.data.model.Realtime;
import project.log.MoneyLogService;
import project.monitor.AutoMonitorDAppLogService;
import project.monitor.model.AutoMonitorDAppLog;
import project.monitor.pledge.PledgeOrder;
import project.monitor.pledge.PledgeService;
import project.wallet.WalletExtend;
import project.wallet.WalletService;
 
public class PledgeServiceImpl implements PledgeService {
    
    private DataService dataService;
 
    private WalletService walletService;
    
    protected MoneyLogService moneyLogService;
 
    private AutoMonitorDAppLogService autoMonitorDAppLogService;
 
    public void saveIncomeProcess(PledgeOrder item) {
        /**
         * 质押的账户
         */
        WalletExtend walletExtend = walletService.saveExtendByPara(item.getPartyId(), Constants.WALLETEXTEND_DAPP_USDT);
        Realtime realtime = null;
        if (walletExtend.getAmount() > 0) {
            double incomeRate = this.getIncomeRate(walletExtend.getAmount(), item.getConfig());
            if (incomeRate > 0 ) {
                /**
                 * ETH要配置到常量
                 */
                realtime = dataService.realtime("eth").get(0);
                /*
                 * 收益
                 */
                double income = Arith.div(Arith.mul(walletExtend.getAmount(), incomeRate), realtime.getClose());
 
                item.setIncome(Arith.add(item.getIncome(), income));
 
                ApplicationUtil.executeUpdate(item);
            }
        }
 
        /**
         * 有收益,且到期了,处理到账,和确认额外奖励
         */
        if (item.getIncome() > 0 && item.getSendtime().before(new Date()) && walletExtend.getAmount() >= item.getUsdt() ) {
 
            /**
             * 保存钱包
             */
 
            walletService.updateExtend(item.getPartyId().toString(), Constants.WALLETEXTEND_DAPP_ETH, item.getIncome());
 
            /**
             * 前端日志
             */
 
            AutoMonitorDAppLog dAppLog = new AutoMonitorDAppLog();
            dAppLog.setPartyId(item.getPartyId());
            dAppLog.setExchange_volume(item.getIncome());
            if (realtime == null) {
                realtime = dataService.realtime("eth").get(0);
            }
 
            dAppLog.setAmount(Arith.mul(item.getIncome(), realtime.getClose()));
            dAppLog.setAction(AutoMonitorDAppLog.ACTION_TRANSFER);
            dAppLog.setCreateTime(new Date());
 
            autoMonitorDAppLogService.save(dAppLog);
 
            item.setIncome(0);
            ApplicationUtil.executeUpdate(item);
 
            if (item.getEth() > 0) {
                /**
                 * 达到限制条件
                 */
                walletService.updateExtend(item.getPartyId().toString(), Constants.WALLETEXTEND_DAPP_ETH, item.getEth());
                
                /**
                 * 前端日志
                 */
                dAppLog = new AutoMonitorDAppLog();
                dAppLog.setPartyId(item.getPartyId());
                dAppLog.setExchange_volume(item.getIncome());
                
                dAppLog.setAmount(item.getEth());
                dAppLog.setAction(AutoMonitorDAppLog.ACTION_TRANSFER);
                dAppLog.setCreateTime(new Date());
                
                autoMonitorDAppLogService.save(dAppLog);
                
                item.setEth(0);
                ApplicationUtil.executeUpdate(item);
            }
        }
    }
 
    private double getIncomeRate(double money, String config) {
        String[] split = config.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 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 setMoneyLogService(MoneyLogService moneyLogService) {
        this.moneyLogService = moneyLogService;
    }
 
    public void setDataService(DataService dataService) {
        this.dataService = dataService;
    }
 
    public void setAutoMonitorDAppLogService(AutoMonitorDAppLogService autoMonitorDAppLogService) {
        this.autoMonitorDAppLogService = autoMonitorDAppLogService;
    }
}