新版仿ok交易所-后端
1
zj
18 hours ago 640ccb9229224642515527daf87f308a7aa9bdf4
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.yami.trading.service.ico;
 
 
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.ico.domain.Ico;
import com.yami.trading.bean.ico.domain.UserSubscription;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.MoneyLog;
import com.yami.trading.bean.model.User;
import com.yami.trading.bean.model.Wallet;
import com.yami.trading.bean.model.WalletExtend;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.C2cLock;
import com.yami.trading.dao.ico.IcoMapper;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.user.UserService;
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.Date;
 
/**
 * 新币Service
 
 */
@Service
@Transactional
@Slf4j
public class IcoService extends ServiceImpl<IcoMapper, Ico> {
 
    @Autowired
    UserService userService;
 
    @Autowired
    WalletService walletService;
 
    @Autowired
    UserSubscriptionService userSubscriptionService;
 
    @Autowired
    MoneyLogService moneyLogService;
 
    /**
     * 新币申购
     */
    public Result<String> subscribe(UserSubscription model) {
        if (model == null || model.getIcoProjectId() == null) {
            throw new YamiShopBindException("Invalid parameters");
        }
        String partyId = model.getUserId();
        Ico ico = this.getById(model.getIcoProjectId());
        if (ico == null) {
            throw new YamiShopBindException("New coin does not exist");
        }
 
        User party = userService.getById(partyId);
        if (!party.isEnabled()) {
            throw new YamiShopBindException("User is locked");
        }
        if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) {
            throw new YamiShopBindException("Permission denied");
        }
 
        if (model.getSubscribeNums() == null || model.getSubscribeNums() == 0) {
            throw new YamiShopBindException("Application quantity cannot be empty");
        }
        //购买金额
        BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getSubscribeNums()));
        if(amount.compareTo(ico.getMinContribution()) < 0 ){
            throw new YamiShopBindException("Minimum investment: " + ico.getMinContribution());
        }
        if(amount.compareTo(ico.getMaxContribution()) > 0 ){
            throw new YamiShopBindException("Maximum investment: " + ico.getMaxContribution());
        }
        Date currentDate = new Date();
        if(currentDate.before(ico.getStartDate())){
            throw new YamiShopBindException("Not yet on sale");
        }
        if(currentDate.after(ico.getEndDate())){
            throw new YamiShopBindException("Already ended");
        }
 
        model.setStatus(1);
        model.setUserId(partyId);
        if (model.getSubscriptionType() == null) { //默认自主申购
            model.setSubscriptionType(1);
        }
 
        Wallet wallet = walletService.saveWalletByPartyId(partyId);
        if(wallet.getAmountToBeCovered().compareTo(BigDecimal.ZERO) > 0){
            throw new YamiShopBindException("Please pay outstanding supplementary funds first");
        }
 
        //需要先支付
        if (ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
            //手续费
            BigDecimal fee = ico.getUnitFee().multiply(new BigDecimal(model.getSubscribeNums()));
            BigDecimal totalAmount = amount.add(fee);
            if (totalAmount.compareTo(wallet.getMoney()) > 0) {
                throw new YamiShopBindException("Insufficient balance");
            }
            BigDecimal before = wallet.getMoney();
            walletService.update(partyId, totalAmount.negate().doubleValue());
 
            // 保存 资金日志
            MoneyLog moneylog = new MoneyLog();
            moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
            moneylog.setAmountBefore(before);
            moneylog.setAmount(totalAmount.negate());
            moneylog.setAmountAfter(before.subtract(totalAmount));
            moneylog.setLog("新币购买,申购金额[" + amount + "]" + "手续费金额[" + fee + "]");
            moneylog.setUserId(partyId);
            moneylog.setSymbol(ico.getSymbol());
            moneylog.setWalletType(ico.getSymbol());
            moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
            moneyLogService.save(moneylog);
        }
        userSubscriptionService.save(model);
        return Result.ok("申请成功" );
    }
 
 
    public Result <String> updateRecord(UserSubscription model) {
        try {
            if( model.getId() == null || model.getStatus() == null){
                throw new YamiShopBindException("Please enter parameters");
            }
            UserSubscription userSubscription = userSubscriptionService.getById(model.getId());
            if (userSubscription == null) {
                throw new YamiShopBindException("Record does not exist");
            }
            if (model.getStatus() == 3|| model.getStatus() == 2) {
                userSubscription.setEndTime(DateTime.now());
            }
            if (userSubscription.getStatus() == 5) {
                throw new YamiShopBindException("It's been converted");
            }
            if(userSubscription.getStatus() == 3 || userSubscription.getStatus() == 2){
                throw new YamiShopBindException("Subscription status cannot be changed");
            }
            Ico ico = this.getById(userSubscription.getIcoProjectId());
            String userId = userSubscription.getUserId();
            if (model.getStatus() == 3 || model.getStatus() == 5) {
                if(model.getBallotNumber() == null || model.getBallotNumber() == 0){
                    throw new YamiShopBindException("Allotted quantity cannot be less than 0");
                }
                if(model.getBallotNumber() > userSubscription.getSubscribeNums()){
                    throw new YamiShopBindException("Configured allotted quantity cannot exceed application quantity");
                }
            }
            userSubscription.setStatus(model.getStatus());
            userSubscription.setBallotNumber(model.getBallotNumber());
 
            //资金账户
            Wallet wallet = walletService.saveWalletByPartyId(userId);
            BigDecimal before = wallet.getMoney();
            if (model.getStatus() == 2) {   //未中签
                //预支付退回全部资金
                if(ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
                    BigDecimal refundPrice = ico.getUnitAmount().multiply(new BigDecimal(userSubscription.getSubscribeNums()));
                    BigDecimal fee = ico.getUnitFee().multiply(new BigDecimal(userSubscription.getSubscribeNums()));
                    BigDecimal totalAmount = refundPrice.add(fee);
                    wallet = walletService.updateToBeCovered(wallet, totalAmount, 1);
                    //保存 资金日志
                    MoneyLog moneylog = new MoneyLog();
                    moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
                    moneylog.setAmountBefore(before);
                    moneylog.setAmount(totalAmount);
                    moneylog.setAmountAfter(wallet.getMoney());
                    moneylog.setLog("新币未中签,退回金额[" + refundPrice + "]" + "退回手续费[" + fee + "]");
                    moneylog.setUserId(userId);
                    moneylog.setSymbol(ico.getSymbol());
                    moneylog.setWalletType(ico.getSymbol());
                    moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_RT_BUY);
                    moneyLogService.save(moneylog);
                }
            } else if (model.getStatus() == 3) { //中签
                //已经预支付
                if(ico.getIsPayDown() !=null && ico.getIsPayDown() == 1) {
                    long applyNumber = userSubscription.getSubscribeNums() - model.getBallotNumber();
                    if(applyNumber > 0) {
                        //退回资金
                        BigDecimal refundPrice = ico.getUnitAmount().multiply(new BigDecimal(applyNumber));
                        BigDecimal fee = ico.getUnitFee().multiply(new BigDecimal(applyNumber));
                        BigDecimal totalAmount = refundPrice.add(fee);
                        wallet = walletService.updateToBeCovered(wallet, totalAmount, 1);
 
                        //保存 资金日志
                        MoneyLog moneylog = new MoneyLog();
                        moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
                        moneylog.setAmountBefore(before);
                        moneylog.setAmount(totalAmount);
                        moneylog.setAmountAfter(wallet.getMoney());
                        moneylog.setLog("新币购买,退回金额[" + refundPrice + "]" + "退回手续费[" + fee + "]");
                        moneylog.setUserId(userId);
                        moneylog.setSymbol(ico.getSymbol());
                        moneylog.setWalletType(ico.getSymbol());
                        moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_RT_BUY);
                        moneyLogService.save(moneylog);
                    }
                } else {
                    BigDecimal amount = ico.getUnitAmount().multiply(new BigDecimal(model.getBallotNumber()));
                    //手续费
                    BigDecimal fee = ico.getUnitFee().multiply(new BigDecimal(model.getBallotNumber()));
                    BigDecimal totalAmount = amount.add(fee);
                    wallet = walletService.updateToBeCovered(wallet, totalAmount, 2);
                    //保存 资金日志
                    MoneyLog moneylog = new MoneyLog();
                    moneylog.setCategory(Constants.MONEYLOG_CONTENT_NEW_COIN);
                    moneylog.setAmountBefore(before);
                    moneylog.setAmount(totalAmount.negate());
                    moneylog.setAmountAfter(wallet.getMoney());
                    moneylog.setLog("新币购买,申购金额[" + amount + "]" + "手续费金额[" + fee + "]");
                    moneylog.setUserId(userId);
                    moneylog.setSymbol(ico.getSymbol());
                    moneylog.setWalletType(ico.getSymbol());
                    moneylog.setContentType(Constants.MONEYLOG_CONTENT_NEW_COIN_BUY);
                    moneyLogService.save(moneylog);
                }
                userSubscription.setStatus(5);
            }
            if (model.getStatus() == 3 || model.getStatus() == 5) {  //转入账户
                //获取币账户
                WalletExtend walletExtend = walletService.saveExtendByPara(userId, ico.getSymbol());
                walletService.updateExtend(userId, walletExtend.getWallettype(), model.getBallotNumber());
            }
            userSubscriptionService.saveOrUpdate(userSubscription);
            return Result.ok ( "操作成功" );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    public Item icoToItem(Item item, Ico ico) {
        item.setName(ico.getName());
        item.setSymbol(ico.getSymbol());
        item.setSymbolData(ico.getSymbolData());
        item.setPips(ico.getPips());
        item.setPipsAmount(ico.getPipsAmount());
        item.setAdjustmentValue(ico.getAdjustmentValue());
        item.setUnitAmount(ico.getUnitAmount());
        item.setUnitFee(ico.getUnitFee());
        item.setMarket("");
        item.setDecimals(ico.getDecimals());
        item.setMultiple(ico.getMultiple());
        item.setBorrowingRate(ico.getBorrowingRate());
        item.setSymbolFullName(ico.getName());
        item.setType(Item.cryptos);
        item.setCategory(Item.cryptos);
        item.setShowStatus("1");
        item.setTradeStatus("1");
        item.setQuoteCurrency(ico.getCurrency());
        item.setCurrencyType(1);  //新币
        item.setStatus(1);
        item.setTradeType(ico.getIsContractTrading().toString());
        return item;
    }
}