新版仿ok交易所-后端
zyy
2026-03-02 73f071f8526142f91ebf3732e61fd5dfb6c9b6eb
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
268
269
package com.yami.trading.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.contract.domain.ContractOrder;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.*;
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.dao.CapitaltWalletMapper;
import com.yami.trading.service.CapitaltWalletService;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.contract.ContractOrderService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.user.PurchaseRecordService;
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.math.RoundingMode;
import java.util.Date;
import java.util.List;
 
/**
 * @program: trading-order-master
 * @description: 合约账户
 * @create: 2025-01-08 17:59
 **/
@Service
@Slf4j
public class CapitaltWalletServiceImpl extends ServiceImpl<CapitaltWalletMapper, CapitaltWallet> implements CapitaltWalletService {
 
    @Autowired
    WalletService walletService;
 
    @Autowired
    private ContractOrderService contractOrderService;
 
    @Autowired
    MoneyLogService moneyLogService;
 
    @Autowired
    ItemService itemService;
 
    @Autowired
    PurchaseRecordService purchaseRecordService;
 
    @Autowired
    DataService dataService;
 
    @Override
    public CapitaltWallet getUserIdWallet(String userId) {
        CapitaltWallet capitaltWallet = getOne(new LambdaQueryWrapper<>(CapitaltWallet.class)
                .eq(CapitaltWallet::getUserId,userId).last(" limit 1"));
        if (capitaltWallet != null) {
            return capitaltWallet;
        } else {
            capitaltWallet = new CapitaltWallet();
            capitaltWallet.setUserId(userId);
            save(capitaltWallet);
            return capitaltWallet;
        }
    }
 
    /**
     *capital  资金
     *contract 合约
     * @param userId 用户id
     * @param deductAccount 划转账户
     * @param receiveAccount  接收账户
     * @param moneyRevise   划转金额
     * @return
     */
    @Override
    @Transactional
    public Result updateWallt(String userId, String deductAccount, String receiveAccount, BigDecimal moneyRevise) {
        // 获取合约账户(contract)
        Wallet wallet = walletService.saveWalletByPartyId(userId);
 
        // 获取资金账户(capital)
        CapitaltWallet capitaltWallet = getOne(new LambdaQueryWrapper<>(CapitaltWallet.class)
                .eq(CapitaltWallet::getUserId, userId).last(" limit 1 "));
 
        // 判断划转账户类型
        if (deductAccount.equals("capital") && receiveAccount.equals("contract")) {
            // 从资金账户(capital)到合约账户(contract)的划转
            if (capitaltWallet != null && capitaltWallet.getMoney().compareTo(moneyRevise) >= 0) {
                // 执行从资金账户到合约账户的划转操作
                capitaltWallet.setMoney(capitaltWallet.getMoney().subtract(moneyRevise));  // 减少资金账户余额
                //wallet.setMoney(wallet.getMoney().add(moneyRevise));  // 增加合约账户余额
                // 更新账户余额
                //walletService.updateById(wallet);  // 保存合约账户的更新
                walletService.updateToBeCovered(wallet, moneyRevise, 1);
                updateById(capitaltWallet);  // 保存资金账户的更新
                return Result.succeed();
            } else {
                throw new YamiShopBindException("Insufficient balance in the fund account");
            }
        } else if (deductAccount.equals("contract") && receiveAccount.equals("capital")) {
 
            LambdaQueryWrapper<ContractOrder> wrapper = new LambdaQueryWrapper<ContractOrder>();
            wrapper.eq(ContractOrder::getPartyId, userId);
            wrapper.eq(ContractOrder::getLocationType, 1);
            wrapper.eq(ContractOrder::getState,"submitted");
            List<ContractOrder> list = contractOrderService.list(wrapper);
            if(CollectionUtil.isNotEmpty(list)){
                throw new YamiShopBindException("Currently holding the full position. Transfer is not supported!");
            }
 
            // 从合约账户(contract)到资金账户(capital)的划转
            if (wallet != null && wallet.getMoney().compareTo(moneyRevise) >= 0) {
                // 执行从合约账户到资金账户的划转操作
                wallet.setMoney(wallet.getMoney().subtract(moneyRevise));  // 减少合约账户余额
                capitaltWallet.setMoney(capitaltWallet.getMoney().add(moneyRevise));  // 增加资金账户余额
                // 更新账户余额
                walletService.updateById(wallet);  // 保存合约账户的更新
                updateById(capitaltWallet);  // 保存资金账户的更新
                return Result.succeed();
            } else {
                throw new YamiShopBindException("Insufficient balance in the contract account");
            }
        } else {
            // 如果划转账户和接收账户不符合预期,返回错误信息
            throw new YamiShopBindException("Unsupported account transfer types");
        }
    }
 
    /**
     * @return
     */
    @Override
    @Transactional
    public Result updateCapitaltWallt(String userId, BigDecimal moneyRevise,  int accountType, String coinType) {
        if (accountType == 1 && moneyRevise.compareTo(BigDecimal.ZERO) <= 0) { //充值
            throw new YamiShopBindException("请输入大于0的数量");
        }
        if (accountType == 2) {
            moneyRevise = moneyRevise.negate();
        }
        if ("usdt".equals(coinType)) {
            double amount1 = moneyRevise.doubleValue();
            CapitaltWallet capitaltWallet = getUserIdWallet(userId);
            if (capitaltWallet == null) {
                throw new YamiShopBindException("账号缺少资金数据");
            }
            double amount_before = capitaltWallet.getMoney().doubleValue();
            if (accountType == 2 && capitaltWallet.getMoney().subtract(moneyRevise).compareTo(BigDecimal.ZERO) < 0) {
                throw new YamiShopBindException("账号资金不足");
            }
            update(capitaltWallet, amount1);
 
            // 保存资金日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmountBefore(new BigDecimal(amount_before));
            moneyLog.setAmount(new BigDecimal(amount1));
            moneyLog.setAmountAfter(BigDecimal.valueOf(Arith.add(amount_before, amount1)));
            moneyLog.setLog("后台手动充值/扣款");
            moneyLog.setUserId(userId);
            moneyLog.setWalletType(Constants.WALLET);
            moneyLog.setContentType(Constants.MONEYLOG_CONTENT_RECHARGE);
            moneyLog.setCreateTime(new Date());
            moneyLogService.save(moneyLog);
        } else {
            Item item;
            if (coinType.equalsIgnoreCase("clkusdt")) {
                item = itemService.getOne(new LambdaQueryWrapper<Item>().eq(Item::getName, "CLK/USDT"));
                coinType = item.getSymbol();
                //充值入ico持仓
                String volume = moneyRevise.toString();
                List<Realtime> realtimes = this.dataService.realtime(coinType);
                double close;
                if (realtimes != null) {
                    close = realtimes.get(0).getClose().doubleValue();
                } else {
                    throw new YamiShopBindException("参数错误");
                }
                PurchaseRecord purchaseRecord = purchaseRecordService.getOne(new LambdaQueryWrapper<>(PurchaseRecord.class)
                        .eq(PurchaseRecord::getUserId, userId));
 
                BigDecimal amount = new BigDecimal(volume).multiply(new BigDecimal(close));
                if(ObjectUtil.isEmpty(purchaseRecord)){
                    if (accountType == 1) {
                        PurchaseRecord record = new PurchaseRecord();
                        record.setUserId(userId);
                        record.setPurchaseQuantity(new BigDecimal(volume));
                        record.setPurchasePrice(new BigDecimal(close));
                        record.setTotalAmount(amount);
                        purchaseRecordService.save(record);
                    }
                } else {
                    if (accountType == 1) {
                        purchaseRecord.setPurchaseQuantity(purchaseRecord.getPurchaseQuantity().add(new BigDecimal(volume)));
                        purchaseRecord.setTotalAmount(purchaseRecord.getTotalAmount().add(amount));
                        purchaseRecord.setPurchasePrice(purchaseRecord.getTotalAmount().divide(purchaseRecord.getPurchaseQuantity(), 4,RoundingMode.HALF_UP));
                        purchaseRecordService.updateById(purchaseRecord);
                    } else {
                        purchaseRecord.setPurchaseQuantity(purchaseRecord.getPurchaseQuantity().add(new BigDecimal(volume)));
                        if (purchaseRecord.getPurchaseQuantity().compareTo(BigDecimal.ZERO) < 0) {
                            throw new YamiShopBindException("持仓数量不足");
                        }
                        purchaseRecord.setTotalAmount(purchaseRecord.getTotalAmount().add(amount));
                        //purchaseRecord.setTotalAmount(purchaseRecord.getPurchaseQuantity().multiply(purchaseRecord.getPurchasePrice()));
                        purchaseRecord.setPurchasePrice(purchaseRecord.getTotalAmount().divide(purchaseRecord.getPurchaseQuantity(), 4,RoundingMode.HALF_UP));
                        purchaseRecordService.updateById(purchaseRecord);
                    }
 
                }
            } else {
                item = itemService.findBySymbol(coinType);
            }
            if (item == null) {
                throw new YamiShopBindException("暂不支持的币种");
            }
            WalletExtend walletExtend = new WalletExtend();
            walletExtend = walletService.saveExtendByPara(userId, coinType);
 
            double volume = moneyRevise.doubleValue();
 
            double amount_before = walletExtend.getAmount();
 
            if (accountType == 2 && Arith.sub(amount_before, volume) < 0) {
                throw new YamiShopBindException("账号资金不足");
            }
 
            // walletExtend = walletService.saveWalletExtendByParaAndUpdate(String.valueOf(recharge.getPartyId()), recharge.getSymbol(), volume);
            walletService.updateExtend(walletExtend.getPartyId().toString(), walletExtend.getWallettype(), volume);
 
            // 保存资金日志
            MoneyLog moneyLog = new MoneyLog();
            moneyLog.setCategory(Constants.MONEYLOG_CATEGORY_COIN);
            moneyLog.setAmountBefore(new BigDecimal(amount_before));
            moneyLog.setAmount(new BigDecimal(volume));
            moneyLog.setAmountAfter(new BigDecimal(Arith.add(amount_before, volume)));
 
            moneyLog.setLog("后台手动充值/扣款");
            moneyLog.setUserId(userId);
            moneyLog.setWalletType(coinType);
            moneyLog.setContentType(Constants.MONEYLOG_CONTENT_RECHARGE);
            moneyLog.setCreateTime(new Date());
            moneyLogService.save(moneyLog);
        }
        return Result.succeed();
    }
 
    //赠送ico
    @Override
    public Result giveIco(String partyId, BigDecimal moneyRevise) {
 
        return Result.succeed();
    }
 
    @Override
    public void update(CapitaltWallet capitaltWallet, double amount1) {
        capitaltWallet.setMoney(new BigDecimal(Arith.add(capitaltWallet.getMoney().doubleValue(), amount1)));
        if (!updateById(capitaltWallet)) {
            throw new YamiShopBindException("操作钱包失败!");
        }
    }
}