package com.market.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.market.entity.ConfigSymbol;
import com.market.mapper.SpotConfigSymbolMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 现货交易对配置 Service
 *
 * @author system
 * @date 2026-05-07
 */
@Service
public class SpotConfigSymbolService {

    private static final Logger log = LoggerFactory.getLogger(SpotConfigSymbolService.class);

    private final SpotConfigSymbolMapper configSymbolMapper;

    public SpotConfigSymbolService(SpotConfigSymbolMapper configSymbolMapper) {
        this.configSymbolMapper = configSymbolMapper;
    }

    /**
     * 查询所有开启且非自定义的现货交易对（小写符号列表）
     *
     * @return 交易对列表，查询失败返回空列表
     */
    @DS("kucoin")
    @Transactional(readOnly = true)
    public List<String> getActiveSymbols() {
        try {
            LambdaQueryWrapper<ConfigSymbol> qw = new LambdaQueryWrapper<>();
            qw.select(ConfigSymbol::getSymbol);
            qw.eq(ConfigSymbol::getIsOpen, 1);
            qw.eq(ConfigSymbol::getPublish, 1);
            qw.eq(ConfigSymbol::getCustomize, 2);
            qw.orderByAsc(ConfigSymbol::getId);
            return configSymbolMapper.selectList(qw).stream()
                    .map(cs -> cs.getSymbol().toLowerCase())
                    .collect(Collectors.toList());
        } catch (Exception e) {
            log.error("查询现货活跃交易对失败", e);
            return List.of();
        }
    }
}
