新版仿ok交易所-后端
1
zyy
2025-10-21 c67081686f866f0c00b767a60d38d63f19b393b9
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
package com.yami.trading.service.user.impl;
 
import cn.hutool.json.JSONUtil;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.CapitaltWallet;
import com.yami.trading.bean.model.Wallet;
import com.yami.trading.bean.model.WalletExtend;
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.StringUtils;
import com.yami.trading.service.CapitaltWalletService;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.user.UserRecomService;
import com.yami.trading.service.user.UserStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class UserStatisticsServiceImpl  implements UserStatisticsService {
 
 
    @Autowired
    UserRecomService userRecomService;
    @Autowired
    WalletService walletService;
    @Autowired
    CapitaltWalletService capitaltWalletService;
    @Autowired
    ItemService itemService;
 
    @Override
     public List<Map<String,Object>> getAssetsAll(String loginPartyId,String targetPartyId) {
        if (!StringUtils.isNullOrEmpty(loginPartyId)) {
//            List<String> children = this.userRecomService.findChildren(loginPartyId);
//            if (children.size() == 0) {
//                return new ArrayList<>();
//            }
//            if(!children.contains(targetPartyId)) throw new BusinessException("目标用户不属于登录人下级");
        }
        Map<String, Object> moneyAll = walletService.getMoneyAll(targetPartyId);
        Map<String, Object> nameMap = getNameMap();
        List<Map<String,Object>> result = new LinkedList<Map<String,Object>>();
        log.info(JSONUtil.toJsonPrettyStr(moneyAll)+"=============");
 
        log.info(JSONUtil.toJsonPrettyStr(nameMap)+"=============");
 
        for(Map.Entry<String, Object> entry :nameMap.entrySet()) {
            if("money_trader".equals(entry.getKey())) {
                continue;
            }
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("name", entry.getValue());
            data.put("value", moneyAll.get(entry.getKey()));
            result.add(data);
        }
        return result;
    }
 
 
    public Map<String,Object> getNameMap(){
        Map<String,Object> data = new LinkedHashMap<String, Object>();
 
        data.put("money_all_coin", "钱包资产折合[USDT]");
 
        data.put("money_miner", "矿机");
        data.put("money_finance", "理财");
        data.put("money_contract", "永续合约");
        data.put("money_futures", "交割合约");
        data.put("money_fund", "基金");
        data.put("money_coin", "币余额");
        data.put("money_wallet", "钱包USDT");
//        data.put("money_trader", "理财资产");
        data.put("money_ico", "ico");
        data.put("total", "总资产");
        return data;
    }
 
    @Override
    public List<Map<String,Object>> getWalletExtends(String loginPartyId, String targetPartyId) {
//        if (!StringUtils.isNullOrEmpty(loginPartyId)) {
//            List<String> children = this.userRecomService.findChildren(loginPartyId);
//            if (children.size() == 0) {
//                return new ArrayList<>();
//            }
//            if(!children.contains(targetPartyId)) throw new YamiShopBindException("目标用户不属于登录人下级");
//        }
        List<WalletExtend> findExtend = walletService.findExtend(targetPartyId);
        List<Map<String,Object>> result = new LinkedList<Map<String,Object>>();
        if(ObjectUtils.isNotEmpty(findExtend)) {
            for(WalletExtend we : findExtend) {
                if(ObjectUtils.isNotEmpty(we)) {
                    Map<String,Object> data = new HashMap<String,Object>();
                    if ("USDT_USER".equals(we.getWallettype())
                            || "ETH_DAPP".equals(we.getWallettype())
                            || "USDT_DAPP".equals(we.getWallettype())
                            || "ETH_USER".equals(we.getWallettype())) {
                        continue;
                    }
                    data.put("wallettype", we.getWallettype());
                    data.put("amount", new BigDecimal(we.getAmount()).setScale(8, RoundingMode.FLOOR).toPlainString());
                    data.put("lock_amount",null==we?0:new BigDecimal(we.getLockAmount()).setScale(8, RoundingMode.FLOOR).toPlainString() );
                    data.put("freeze_amount",null==we?0:new BigDecimal(we.getFreezeAmount()).setScale(8, RoundingMode.FLOOR).toPlainString() );
                    result.add(data);
                }
            }
        }
        Map<String,Object> data = new HashMap<String,Object>();
        Wallet wallet = walletService.saveWalletByPartyId(targetPartyId);
        data.put("wallettype", "usdt");
        data.put("amount",null==wallet?0:new BigDecimal(wallet.getMoney().doubleValue()).setScale(8, RoundingMode.FLOOR).toPlainString() );
        data.put("lock_amount",null==wallet?0:wallet.getLockMoney().setScale(8, RoundingMode.FLOOR).toPlainString() );
        data.put("freeze_amount",null==wallet?0:wallet.getFreezeMoney().setScale(8, RoundingMode.FLOOR).toPlainString() );
        result.add(0,data);
 
        return result;
    }
 
    @Override
    public Result getWalletExtends(String partyId) {
        Map<String, Object> mapRet = new LinkedHashMap<String, Object>();
        DecimalFormat df2 = new DecimalFormat("#.########");
        // 向下取整
        df2.setRoundingMode(RoundingMode.FLOOR);
 
        CapitaltWallet usdt = null;
        if (StringUtils.isNotEmpty(partyId)) {
            usdt = capitaltWalletService.getUserIdWallet(partyId);
        }
 
        // 其他币账户
        List<Item> list_it = this.itemService.cacheGetByMarket("");
        if (!list_it.isEmpty()) {
            list_it = list_it.stream().filter(i -> Item.cryptos.equalsIgnoreCase(i.getType())).collect(Collectors.toList());
        }
        List<String> list_symbol = new ArrayList<String>();
        // symbol为空,获取所有的
        for (int i = 0; i < list_it.size(); i++) {
 
            Item items = list_it.get(i);
            list_symbol.add(items.getSymbol());
        }
 
        List<Item> items = itemService.cacheGetAll();
        // 按id排序
        Collections.sort(items, new Comparator<Item>() {
            @Override
            public int compare(Item arg0, Item arg1) {
                return arg0.getUuid().toString().compareTo(arg1.getUuid().toString());
            }
        });
        Map<String, Item> itemMap = new HashMap<String, Item>();
        for (int i = 0; i < items.size(); i++) {
            itemMap.put(items.get(i).getSymbol(), items.get(i));
        }
        List<WalletExtend> walletExtends = null;
        log.info(list_symbol.toString() + "=============");
        if (StringUtils.isNotEmpty(partyId)) {
            walletExtends = this.walletService.findExtend(partyId, list_symbol);
        }
        if (null == walletExtends) {
            walletExtends = new ArrayList<WalletExtend>();
        }
        List<WalletExtend> walletExtendsRet = new ArrayList<WalletExtend>();
        int temp = 0;
        for (int i = 0; i < list_symbol.size(); i++) {
            for (int j = 0; j < walletExtends.size(); j++) {
                WalletExtend walletExtend = walletExtends.get(j);
                if (walletExtend.getWallettype().equals(list_symbol.get(i))) {
                    walletExtend.setAmount(Double.valueOf(df2.format(walletExtend.getAmount())));
                    walletExtend.setLockAmount(Double.valueOf(df2.format(walletExtend.getLockAmount())));
                    walletExtend.setFreezeAmount(Double.valueOf(df2.format(walletExtend.getFreezeAmount())));
                    walletExtend.setName(itemMap.get(list_symbol.get(i)).getName());
                    walletExtendsRet.add(walletExtend);
                    temp = 1;
                }
            }
            if (0 == temp) {
                WalletExtend walletExtend = new WalletExtend();
                if (StringUtils.isNotEmpty(partyId)) {
                    walletExtend.setPartyId(partyId);
                }
                walletExtend.setWallettype(list_symbol.get(i));
                walletExtend.setAmount(0);
                walletExtend.setLockAmount(0);
                walletExtend.setFreezeAmount(0);
                walletExtend.setName(itemMap.get(list_symbol.get(i)).getName());
                walletExtendsRet.add(walletExtend);
            }
            temp = 0;
        }
        String symbolsStr = "";
        for (int i = 0; i < list_symbol.size(); i++) {
            if (i != 0) {
                symbolsStr = symbolsStr + "," + list_symbol.get(i);
            } else {
                symbolsStr = list_symbol.get(i);
            }
        }
 
        List<Map<String, Object>> extendsList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < walletExtendsRet.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", walletExtendsRet.get(i).getName());
            String wallettype = walletExtendsRet.get(i).getWallettype();
            map.put("symbol", wallettype);
            map.put("symbol_data", itemMap.get(wallettype).getSymbolData());
            double volume = Arith.add(walletExtendsRet.get(i).getAmount(), walletExtendsRet.get(i).getFreezeAmount());
            map.put("volume", df2.format(volume));
 
            extendsList.add(map);
        }
 
 
        // 添加usdt到列表最前面
        Map<String, Object> mapUsdt = new HashMap<String, Object>();
        mapUsdt.put("name", "USDT/USDT");
        mapUsdt.put("symbol", "usdt");
        mapUsdt.put("symbol_data", "usdt");
        mapUsdt.put("volume", df2.format(usdt.getMoney().doubleValue()));
        extendsList.add(0, mapUsdt);
 
        // 分离 USDC 和 USDT,并保留其他数据
        Map<String, Object> usdcEntry = null;
        Map<String, Object> usdtEntry = null;
        List<Map<String, Object>> otherEntries = new ArrayList<>();
 
        for (Map<String, Object> entry : extendsList) {
            String symbolData = (String) entry.get("symbol_data");
            if ("usdc".equals(symbolData)) {
                usdcEntry = entry;
            } else if ("usdt".equals(symbolData)) {
                usdtEntry = entry;
            } else {
                otherEntries.add(entry);
            }
        }
 
        // 重新组合,USDC 第一,USDT 第二,其他保持原顺序
        List<Map<String, Object>> newExtendsList = new ArrayList<>();
        if (usdcEntry != null) newExtendsList.add(usdcEntry);
        if (usdtEntry != null) newExtendsList.add(usdtEntry);
        newExtendsList.addAll(otherEntries);
 
        mapRet.put("extends", newExtendsList);
 
        return Result.succeed(mapRet);
    }
}