新版仿ok交易所-后端
zyy
2025-09-06 1445a0ef76299a76ebc1ea61a334100fe71414c4
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.yami.trading.service.c2c.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.c2c.C2cAdvert;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.model.C2cTranslate;
import com.yami.trading.bean.rate.domain.ExchangeRate;
import com.yami.trading.common.constants.Constants;
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.dao.c2c.C2cAdvertMapper;
import com.yami.trading.service.c2c.C2cAdvertService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.rate.ExchangeRateService;
import com.yami.trading.service.syspara.SysparaService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.text.DecimalFormat;
import java.util.*;
 
@Service
public class C2cAdvertServiceImpl extends ServiceImpl<C2cAdvertMapper, C2cAdvert> implements C2cAdvertService {
    @Autowired
    SysparaService sysparaService;
 
    @Autowired
    RedisTemplate redisTemplate;
 
    @Autowired
    ExchangeRateService exchangeRateService;
 
    @Autowired
    ItemService itemService;
 
    @Autowired
    DataService dataService;
 
    /**
     * 获取 上架币种Map
     */
    @Override
    public Map<String, String> getSymbolMap() {
        // 获取 C2C上架币种配置
        Map<String, String> symMap = this.getC2cSyspara("c2c_advert_symbol");
        if (null == symMap) {
            symMap = new HashMap<String, String>();
        }
        Map<String, String> symbolMap = new HashMap<String, String>();
        List<Item> itemList = this.itemService.cacheGetAll();
        if (symMap.keySet().contains("USDT")) {
            symbolMap.put("usdt", "USDT");
        }
        for (Item item : itemList) {
            if (symMap.keySet().contains(item.getSymbol().toUpperCase())) {
                symbolMap.put(item.getSymbol(), item.getSymbol().toUpperCase());
            }
        }
        return symbolMap;
    }
 
    public List<C2cAdvert> getByC2cUserId(String c2c_user_id) {
 
        return list(Wrappers.<C2cAdvert>query().lambda().eq(C2cAdvert::getC2cUserId, c2c_user_id));
    }
 
 
 
 
    @Override
    public Page pagedQuery(long pageNo, long pageSize, String c2cUserCode, String c2cUserType,String userCode, String direction,String currency,String symbol){
        Page page=new Page(pageNo,pageSize);
        return baseMapper.pagedQuery(page,c2cUserCode,c2cUserType,userCode,direction,currency,symbol);
    }
 
    /**
     * 计算广告参数
     */
    public Map<String, Object> getComputeValue(double deposit_total, String currency, String symbol, double coin_amount, double symbol_value) {
 
        Map<String, Object> result = new HashMap<String, Object>();
        double symbol_close = 0d;
        if (symbol.equals("usdt")) {
            symbol_close = 1;
        } else {
            List<Realtime> list = this.dataService.realtime(symbol);
            if (0 == list.size()) {
//                throw new BusinessException(symbol.toUpperCase() + "行情获取异常,请重试");
                throw new YamiShopBindException("行情获取异常,请重试");
            }
            Realtime realtime = list.get(0);
            symbol_close = realtime.getClose().doubleValue();
        }
        if (0 == symbol_close) {
//            throw new BusinessException(symbol.toUpperCase() + "行情获取异常,请重试");
            throw new YamiShopBindException("行情获取异常,请重试");
        }
        ExchangeRate ex = this.exchangeRateService.findBy(Constants.OUT_OR_IN_DEFAULT, currency);
        // 支付比率=支付币种汇率*上架币种实时行情价/币种单价;例如,支付比率95%,1USDT=7.3CNY*1*95%=6.935CNY
        double payRate = Arith.mul(Arith.div(Arith.mul(ex.getRata().doubleValue(), symbol_close), symbol_value), 100);
        // 广告保证金=交易币种数量*上架币种实时行情价
        double depositOpen = Arith.mul(coin_amount, symbol_close);
        // 支付币种市价=支付币种汇率*上架币种实时行情价;
        double price = Arith.mul(ex.getRata().doubleValue(), symbol_close);
        // 交易币种数量=广告派单额度/上架币种实时行情价
        double coinAmountMax = Arith.div(deposit_total, symbol_close);
        // 最小支付金额
        double investmentMinLimit = 0;
        // C2C承兑商广告:单笔订单最低限额不能低于(支付币种金额折算成USDT)
        Object obj = this.sysparaService.find("c2c_user_advert_investment_min_limit");
        if (null != obj) {
            String c2c_user_advert_investment_min_limit = this.sysparaService.find("c2c_user_advert_investment_min_limit").getSvalue();
            if (!StringUtils.isEmptyString(c2c_user_advert_investment_min_limit)) {
                double limit_usdt = Double.valueOf(c2c_user_advert_investment_min_limit).doubleValue();
                investmentMinLimit = Arith.mul(Arith.div(limit_usdt, symbol_close), symbol_value);
            }
        }
        // 最大支付金额
        double investmentMaxLimit = Arith.mul(coinAmountMax, symbol_value);
        DecimalFormat df = new DecimalFormat("#.########");
        result.put("pay_rate", (int) payRate);
        result.put("deposit_open", df.format(new Double(depositOpen)));
        result.put("all_deposit", df.format(Arith.sub(deposit_total, depositOpen)));
        result.put("symbol_close", df.format(new Double(symbol_close)));
        result.put("price", df.format(new Double(price)));
        result.put("coin_amount_max", df.format(new Double(coinAmountMax)));
        result.put("investment_min_limit", df.format(new Double(investmentMinLimit)));
        result.put("investment_max_limit", df.format(new Double(investmentMaxLimit)));
        return result;
    }
 
    @Override
    public Page pagedQuery(int page_no, int page_size, String c2c_user_id, String direction, String currency, String symbol, String amount, Integer on_sale, Integer closed, boolean is_c2c_user) {
        Page page=new Page(page_no,page_size);
        double amount_double=0;
        if (StringUtils.isNotEmpty(amount)){
            amount_double= Double.valueOf(amount).doubleValue();
        }
 
 
        if (is_c2c_user) {
 
            baseMapper.pagedQueryC2cUser(page,c2c_user_id,direction,currency,symbol,
                    amount_double,on_sale,closed);
        } else {
            baseMapper.pagedQueryNotC2cUser(page,c2c_user_id,direction,currency,symbol,
                    amount_double,on_sale,closed);
        }
        DecimalFormat df = new DecimalFormat("#.##");
        // 币种默认保留8位
        DecimalFormat dfCoin = new DecimalFormat("#.########");
 
        for (Map<String, Object> data : (List<Map<String, Object>>) page.getRecords()) {
            data.put("symbol_value", df.format(data.get("symbol_value")));
            data.put("coin_amount", dfCoin.format(data.get("coin_amount")));
        }
        return page;
    }
 
    @Override
    public Map<String, String> getCurrencyMap() {
        // 获取 C2C支付币种配置
        Map<String, String> curMap = this.getC2cSyspara("c2c_advert_currency");
        if (null == curMap) {
            curMap = new HashMap<String, String>();
        }
        Map<String, String> currencyMap = new HashMap<String, String>();
        List<ExchangeRate> exchangeRateList = this.exchangeRateService.findBy(Constants.OUT_OR_IN_DEFAULT);
        for (ExchangeRate er : exchangeRateList) {
            if (curMap.keySet().contains(er.getCurrency())) {
                currencyMap.put(er.getCurrency(), String.format("%s(%s)", er.getCurrency(), er.getName()));
            }
        }
        return currencyMap;
    }
 
    /*
     * 获取 C2C支付方式类型、C2C支付币种配置、C2C上架币种配置、C2C广告支付时效
     */
 
    @Override
    public Map<String, String> getC2cSyspara(String syspara) {
 
        if ("c2c_payment_method_type".equals(syspara)) {
            // C2C支付方式类型
            Map<String, String> pmtMap = new HashMap<String, String>();
            Object obj = this.sysparaService.find("c2c_payment_method_type");
            if (null != obj) {
                String pmtStr = this.sysparaService.find("c2c_payment_method_type").getSvalue().toString();
                String[] pmtArray = pmtStr.split("&&");
                for (int i = 0; i < pmtArray.length; i++) {
                    String[] pmt = pmtArray[i].split("##");
                    pmtMap.put(pmt[0], pmt[1]);
                }
                return pmtMap;
            }
        } else if ("c2c_advert_currency".equals(syspara)) {
            // C2C支付币种配置
            Map<String, String> acMap = new HashMap<String, String>();
            Object obj = this.sysparaService.find("c2c_advert_currency");
            if (null != obj) {
                String acStr = this.sysparaService.find("c2c_advert_currency").getSvalue().toString();
                String[] acArray = acStr.split("&&");
                for (int i = 0; i < acArray.length; i++) {
                    String[] ac = acArray[i].split("##");
                    acMap.put(ac[0], ac[1]);
                }
                return acMap;
            }
        } else if ("c2c_advert_symbol".equals(syspara)) {
            // C2C上架币种配置
            Map<String, String> asMap = new HashMap<String, String>();
            Object obj = this.sysparaService.find("c2c_advert_symbol");
            if (null != obj) {
                String asStr = this.sysparaService.find("c2c_advert_symbol").getSvalue().toString();
                String[] asArray = asStr.split("##");
                for (int i = 0; i < asArray.length; i++) {
                    asMap.put(asArray[i], asArray[i]);
                }
                return asMap;
            }
        } else if ("c2c_advert_expire_time".equals(syspara)) {
            // C2C广告支付时效
            Map<String, String> aetMap = new HashMap<String, String>();
            Object obj = this.sysparaService.find("c2c_advert_expire_time");
            if (null != obj) {
                String aetStr = this.sysparaService.find("c2c_advert_expire_time").getSvalue().toString();
                String[] aetArray = aetStr.split("&&");
                for (int i = 0; i < aetArray.length; i++) {
                    String[] aet = aetArray[i].split("##");
                    aetMap.put(aet[0], aet[1]);
                }
                return aetMap;
            }
        }
        return new HashMap<String, String>();
    }
 
    /**
     * 获取所有上架币种单价
     */
    public Map<String, String> getAllSymbolPrice(String currency) {
 
        DecimalFormat df = new DecimalFormat("#.##");
 
        Map<String, String> allPrice = new HashMap<String, String>();
 
        ExchangeRate ex = this.exchangeRateService.findBy(Constants.OUT_OR_IN_DEFAULT, currency);
 
        Map<String, String> allSymbol = this.getC2cSyspara("c2c_advert_symbol");
        if (null == allSymbol || 0 == allSymbol.size()) {
            return new HashMap<String, String>();
        }
 
        for (String symbol : allSymbol.keySet()) {
            symbol = symbol.toLowerCase();
            double symbol_close = 0d;
 
            if (symbol.equals("usdt")) {
                symbol_close = 1;
            } else {
                List<Realtime> list = this.dataService.realtime(symbol);
                if (0 == list.size()) {
                    continue;
                }
                Realtime realtime = list.get(0);
                symbol_close = realtime.getClose().doubleValue();
            }
            if (0 == symbol_close) {
                continue;
            }
 
            // 支付币种市价=支付币种汇率*上架币种实时行情价;
            double price = Arith.mul(ex.getRata().doubleValue(), symbol_close);
            allPrice.put(symbol, df.format(new Double(price)));
        }
 
        return allPrice;
    }
 
    /*
     * 获取 语种说明
     */
    public String getLanguageIntro() {
 
        Map<String, String> langMap = Constants.LANGUAGE;
        String retStr = "";
        for (String lang : langMap.keySet()) {
            String langName = langMap.get(lang);
            if (StringUtils.isEmptyString(retStr)) {
                retStr = lang + " " + langName;
            } else {
                retStr += "; " + lang + " " + langName;
            }
        }
        return retStr + ";";
    }
 
    /*
     * 获取 支付方式类型说明
     */
    public String getMethodTypeIntro() {
 
        Map<String, String> pmtMap = this.getC2cSyspara("c2c_payment_method_type");
        String retStr = "";
        for (String pmt : pmtMap.keySet()) {
            String pmtName = pmtMap.get(pmt);
            if (StringUtils.isEmptyString(retStr)) {
                retStr = pmt + " " + pmtName;
            } else {
                retStr += "; " + pmt + " " + pmtName;
            }
        }
        return retStr + ";";
    }
 
}