ipo
zyy
2026-01-09 7d9bf660d08d1b39af670e54dedf784e32080dc2
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
package com.yami.trading.service.rate;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.bean.rate.domain.ExchangeRate;
import com.yami.trading.dao.rate.ExchangeRateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
 
/**
 * 汇率管理Service
 *
 * @author lucas
 * @version 2023-03-28
 */
@Service
@Transactional
public class ExchangeRateService extends ServiceImpl<ExchangeRateMapper, ExchangeRate> {
 
    @Autowired
    RedisTemplate redisTemplate;
 
 
    public ExchangeRate findBy(String out_or_in, String currency) {
 
        return getOne(Wrappers.<ExchangeRate>query().lambda()
                .eq(ExchangeRate::getOutOrIn, out_or_in)
                .eq(ExchangeRate::getCurrency, currency));
    }
 
    public List<ExchangeRate> findBy(String out_or_in) {
        List<ExchangeRate> list = list(Wrappers.<ExchangeRate>query().lambda().eq(ExchangeRate::getOutOrIn, out_or_in));
        return list;
 
    }
 
    /**
     * 根据股票类型获取对应法币金额
     *
     * @param usdt 钱包usdt余额
     * @param type 股票类型
     * @return currency
     */
    public BigDecimal getCurrencyByType(BigDecimal usdt, String type) {
        if (StringUtils.isEmpty(type)) {
            type = Item.UK_STOCKS;
        }
        ExchangeRate rate = getOne(new LambdaQueryWrapper<ExchangeRate>().like(ExchangeRate::getType, type));
        if (rate != null) {
            BigDecimal rata = rate.getRata();
            usdt = usdt.multiply(rata).setScale(4, RoundingMode.FLOOR);
        }
        return usdt;
    }
 
    /**
     * 根据股票类型获取对应USDT金额
     *
     * @param currency 币种价值
     * @param type     股票类型
     * @return currency
     */
    public BigDecimal getUsdtByType(BigDecimal currency, String type) {
        if (StringUtils.isEmpty(type)) {
            type = Item.US_STOCKS;
        }
        if (type.contains("A") && !Item.A_STOCKS.equalsIgnoreCase(type)) {
            type = type.replace("A", "");
        }
        ExchangeRate rate = getOne(new LambdaQueryWrapper<ExchangeRate>().like(ExchangeRate::getType, type));
        if (rate != null) {
            BigDecimal rata = rate.getRata();
            currency = currency.divide(rata, 4, RoundingMode.FLOOR);
        }
        return currency;
    }
 
    /**
     * 根据股票类型获取对应USDT金额
     *
     * @param currency 币种价值
     * @param type     股票类型
     * @return currency
     */
    public BigDecimal getUsdtByType(List<ExchangeRate> list, BigDecimal currency, String type) {
        if (StringUtils.isEmpty(type)) {
            type = Item.US_STOCKS;
        }
        if (type.contains("A") && !Item.A_STOCKS.equalsIgnoreCase(type)) {
            type = type.replace("A", "");
        }
        //ExchangeRate rate = getOne(new LambdaQueryWrapper<ExchangeRate>().like(ExchangeRate::getType, type));
        String finalType = type;
        ExchangeRate rate = list.stream()
                .filter(x -> {
                    if (x.getType() == null || x.getType().isEmpty()) {
                        return false; // 若任一为 null,不匹配
                    }
                    // 统一转为小写(或大写)后,判断是否包含目标子串
                    return x.getType().toLowerCase().contains(finalType.toLowerCase());
                })
                .findFirst().orElse(null);
        if (rate != null) {
            BigDecimal rata = rate.getRata();
            currency = currency.divide(rata, 4, RoundingMode.FLOOR);
        }
        return currency;
    }
 
}