package com.bizzan.bitrade.mapper.market;

import com.bizzan.bitrade.entity.FuturesKline;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Market MySQL K线查询 Mapper
 * 使用 ${tableName} 处理动态表名（拼接，非参数化），
 * 其余参数使用 #{} 防止 SQL 注入。
 */
public interface FuturesKlineMapper {

    @Select("SELECT * FROM ${tableName} WHERE period=#{period} ORDER BY begin_time DESC LIMIT 1000")
    List<FuturesKline> findAllKLine(@Param("tableName") String tableName,
                                   @Param("period") String period);

    @Select("SELECT * FROM ${tableName} WHERE period=#{period} AND begin_time >= #{fromTime} AND begin_time <= #{toTime} ORDER BY begin_time ASC")
    List<FuturesKline> findKLineRange(@Param("tableName") String tableName,
                                     @Param("period") String period,
                                     @Param("fromTime") long fromTime,
                                     @Param("toTime") long toTime);

    @Select("SELECT * FROM ${tableName} WHERE period=#{period} ORDER BY begin_time DESC LIMIT #{offset}")
    List<FuturesKline> findKLineBefore(@Param("tableName") String tableName,
                                       @Param("period") String period,
                                       @Param("offset") int offset);

    @Select("SELECT MAX(begin_time) FROM ${tableName} WHERE period=#{period}")
    Long findMaxBeginTime(@Param("tableName") String tableName,
                          @Param("period") String period);

    @Select("SELECT * FROM ${tableName} WHERE period=#{period} ORDER BY begin_time DESC LIMIT 1")
    FuturesKline findLatest(@Param("tableName") String tableName,
                            @Param("period") String period);

    @Select("SELECT * FROM ${tableName} WHERE period=#{period} ORDER BY begin_time DESC LIMIT #{length}")
    List<FuturesKline> findLatestKLine(@Param("tableName") String tableName,
                                       @Param("period") String period,
                                       @Param("length") int length);
}
