package com.spot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spot.entity.ConfigCoinSymbol;
import com.spot.mapper.ConfigCoinSymbolMapper;
import com.spot.service.ConfigCoinSymbolService;
import com.spot.utils.Constant;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 币种配置服务实现类
 *
 * @author jean
 * @date 2026-05-06
 */
@Service
public class ConfigCoinSymbolServiceImpl extends ServiceImpl<ConfigCoinSymbolMapper, ConfigCoinSymbol> implements ConfigCoinSymbolService {

    /**
     * 获取所有开放的币种配置
     *
     * @return 开放的币种配置列表
     */
    @Override
    public List<ConfigCoinSymbol> getOpenCoinSymbols() {
        return list(new LambdaQueryWrapper<ConfigCoinSymbol>()
                .eq(ConfigCoinSymbol::getIsOpen, Constant.IS_OPEN_ENABLED)
                .orderByAsc(ConfigCoinSymbol::getSort));
    }

    /**
     * 获取指定币种配置（先查缓存，缓存未命中再查库）
     *
     * @param coinSymbol 币种符号
     * @return 币种配置
     */
    @Override
    public ConfigCoinSymbol getConfigCoinSymbol(String coinSymbol) {
        ConfigCoinSymbol cached = Constant.COIN_SYMBOL_CONFIG_CACHE.getIfPresent(coinSymbol);
        if (cached != null) {
            return cached;
        }
        ConfigCoinSymbol config = getOne(new LambdaQueryWrapper<ConfigCoinSymbol>()
                .eq(ConfigCoinSymbol::getCoinSymbol, coinSymbol)
                .last(" LIMIT 1"));
        if (config != null) {
            Constant.COIN_SYMBOL_CONFIG_CACHE.put(coinSymbol, config);
        }
        return config;
    }
}
