package com.njcn.relational.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface DynamicSyncMapper {

    /**
     * 获取表所有列名（指定Schema）
     */
    @Select("SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS " +
            "WHERE TABLE_NAME = #{tableName} " +
            "AND OWNER = #{schema} " +
            "ORDER BY COLUMN_ID")
    List<String> getTableColumns(@Param("tableName") String tableName,
                                 @Param("schema") String schema);

    /**
     * 获取当前用户/Schema
     */
    @Select("SELECT USER FROM DUAL")
    String getCurrentUser();

    /**
     * 分页查询
     */
    @Select("SELECT * FROM ${tableName} LIMIT #{limit} OFFSET #{offset}")
    List<Map<String, Object>> selectPage(@Param("tableName") String tableName,
                                         @Param("limit") int limit,
                                         @Param("offset") int offset);

    /**
     * 增量查询（当天数据）
     */
    @Select("SELECT * FROM ${tableName} WHERE ${timeColumn} >= #{startTime} AND ${timeColumn} <= #{endTime}")
    List<Map<String, Object>> selectTodayData(@Param("tableName") String tableName,
                                              @Param("timeColumn") String timeColumn,
                                              @Param("startTime") String startTime,
                                              @Param("endTime") String endTime);

    /**
     * 清空表
     */
    @Update("TRUNCATE TABLE ${tableName}")
    void truncateTable(@Param("tableName") String tableName);

    /**
     * 批量插入
     */
    int batchInsert(@Param("tableName") String tableName,
                    @Param("columns") List<String> columns,
                    @Param("list") List<Map<String, Object>> list);
}
