<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.web.report.mapper.ReportMetaMapper">

    <resultMap id="ReportTableMetaResult" type="com.ruoyi.web.report.domain.meta.ReportTableMetaVo">
        <result property="tableName" column="table_name"/>
        <result property="tableComment" column="table_comment"/>
    </resultMap>

    <resultMap id="ReportColumnMetaResult" type="com.ruoyi.web.report.domain.meta.ReportColumnMetaVo">
        <result property="tableName" column="table_name"/>
        <result property="columnName" column="column_name"/>
        <result property="columnComment" column="column_comment"/>
        <result property="dataType" column="data_type"/>
    </resultMap>

    <resultMap id="ReportAssetTemplateResult" type="com.ruoyi.web.report.domain.meta.ReportAssetTemplateVo">
        <result property="reportId" column="report_id"/>
        <result property="reportCode" column="report_code"/>
        <result property="reportName" column="report_name"/>
        <result property="queryMode" column="query_mode"/>
        <result property="aggregateMode" column="aggregate_mode"/>
        <result property="status" column="status"/>
    </resultMap>

    <resultMap id="ReportAssetMetricResult" type="com.ruoyi.web.report.domain.meta.ReportAssetMetricVo">
        <result property="fieldKey" column="field_key"/>
        <result property="sourceTable" column="source_table"/>
        <result property="sourceColumn" column="source_column"/>
        <result property="aggregateFn" column="aggregate_fn"/>
        <result property="usedCount" column="used_count"/>
    </resultMap>

    <!-- 初期开放策略：直接读取当前库全部业务表。 -->
    <select id="selectWhitelistedTables" resultMap="ReportTableMetaResult">
        select t.table_name,
               t.table_comment
        from information_schema.tables t
        where t.table_schema = (select database())
          and t.table_type = 'BASE TABLE'
        order by t.table_name asc
    </select>

    <!-- 初期开放策略：指定表下全部字段均可选。 -->
    <select id="selectWhitelistedColumnsByTable" parameterType="String" resultMap="ReportColumnMetaResult">
        select c.table_name,
               c.column_name,
               c.column_comment,
               c.data_type
        from information_schema.columns c
        where c.table_schema = (select database())
          and c.table_name = #{tableName}
        order by c.ordinal_position asc
    </select>

    <select id="selectAssetTemplates" resultMap="ReportAssetTemplateResult">
        select id as report_id, report_code, report_name, query_mode, aggregate_mode, status
        from rpt_definition
        order by updated_at desc, id desc
        limit 30
    </select>

    <select id="selectAssetMetrics" resultMap="ReportAssetMetricResult">
        select field_key, source_table, source_column, aggregate_fn, count(1) as used_count
        from rpt_field
        where is_enabled = 1
        group by field_key, source_table, source_column, aggregate_fn
        order by used_count desc, field_key asc
        limit 50
    </select>
</mapper>
