package com.boman.report.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

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

/**
 * 导入mapper
 */
public interface ImportMapper {


    /**
     * 功能描述: 根据表名称获取表对应的设计器数据
     * {@link SqlProvider#reportDesignData(Map)}
     *
     * @param tableName tableName
     * @return com.alibaba.fastjson.JSONObject
     */
    @SelectProvider(type = SqlProvider.class, method = "reportDesignData")
    JSONObject getReportDesignData(@Param("tableName") String tableName);

    /**
     * 功能描述: 根据sql获取数据
     *
     * {@link SqlProvider#getDataByDynamicSql(Map)}
     *
     * @param sql
     * @return List<com.alibaba.fastjson.JSONObject>
     */
    @SelectProvider(type = SqlProvider.class, method = "getDataByDynamicSql")
    List<JSONObject> getDataByDynamicSql(@Param("sql") String sql);

    class SqlProvider {
        public String reportDesignData(Map<String, Object> params) {
            String tableName = (String) params.get("tableName");
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT rd.db_ch_name as dbChName, r.json_str as jsonStr")
                    .append(" FROM ").append(" jimu_report r ")
                    .append(" JOIN jimu_report_db rd ON rd.jimu_report_id = r.id ")
                    .append(" where rd.db_code = '").append(tableName).append("'");
            return sql.toString();
        }

        public String getDataByDynamicSql(Map<String, Object> params) {
            return (String) params.get("sql");
        }
    }
}
