<?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.ReportDesignMapper">

    <resultMap id="ReportFieldDesignResult" type="com.ruoyi.web.report.domain.design.ReportFieldDesign">
        <result property="fieldKey" column="field_key"/>
        <result property="sourceTable" column="source_table"/>
        <result property="sourceColumn" column="source_column"/>
        <result property="fieldAlias" column="field_alias"/>
        <result property="exprText" column="expr_text"/>
        <result property="aggregateFn" column="aggregate_fn"/>
        <result property="valueType" column="value_type"/>
        <result property="isGroupDim" column="is_group_dim"/>
        <result property="displayOrder" column="display_order"/>
        <result property="isEnabled" column="is_enabled"/>
    </resultMap>

    <resultMap id="ReportJoinDesignResult" type="com.ruoyi.web.report.domain.design.ReportJoinDesign">
        <result property="leftTable" column="left_table"/>
        <result property="leftColumn" column="left_column"/>
        <result property="rightTable" column="right_table"/>
        <result property="rightColumn" column="right_column"/>
        <result property="joinType" column="join_type"/>
        <result property="extraCondition" column="extra_condition"/>
        <result property="joinOrder" column="join_order"/>
    </resultMap>

    <resultMap id="ReportAggregatePolicyResult" type="com.ruoyi.web.report.domain.design.ReportAggregatePolicyDesign">
        <result property="aggregateGranularity" column="aggregate_granularity"/>
        <result property="cronExpr" column="cron_expr"/>
        <result property="timeDimensionField" column="time_dimension_field"/>
        <result property="retentionDays" column="retention_days"/>
        <result property="isEnabled" column="is_enabled"/>
    </resultMap>

    <select id="selectFieldsByReportId" parameterType="Long" resultMap="ReportFieldDesignResult">
        select field_key, source_table, source_column, field_alias, expr_text, aggregate_fn,
               value_type, is_group_dim, display_order, is_enabled
        from rpt_field
        where report_id = #{reportId}
        order by display_order asc, id asc
    </select>

    <select id="selectJoinsByReportId" parameterType="Long" resultMap="ReportJoinDesignResult">
        select left_table, left_column, right_table, right_column, join_type, extra_condition, join_order
        from rpt_join
        where report_id = #{reportId}
        order by join_order asc, id asc
    </select>

    <select id="selectCustomSqlByReportId" parameterType="Long" resultType="String">
        select sql_text
        from rpt_sql_custom
        where report_id = #{reportId}
        limit 1
    </select>

    <select id="selectAggregatePolicyByReportId" parameterType="Long" resultMap="ReportAggregatePolicyResult">
        select aggregate_granularity, cron_expr, time_dimension_field, retention_days, is_enabled
        from rpt_aggregate_policy
        where report_id = #{reportId}
        limit 1
    </select>

    <delete id="deleteFieldsByReportId" parameterType="Long">
        delete from rpt_field where report_id = #{reportId}
    </delete>

    <delete id="deleteJoinsByReportId" parameterType="Long">
        delete from rpt_join where report_id = #{reportId}
    </delete>

    <insert id="insertFieldBatch">
        insert into rpt_field (
            report_id, field_key, source_table, source_column, field_alias, expr_text,
            aggregate_fn, value_type, is_group_dim, display_order, is_enabled
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
                #{reportId},
                #{item.fieldKey},
                #{item.sourceTable},
                #{item.sourceColumn},
                #{item.fieldAlias},
                #{item.exprText},
                #{item.aggregateFn},
                #{item.valueType},
                #{item.isGroupDim},
                #{item.displayOrder},
                #{item.isEnabled}
            )
        </foreach>
    </insert>

    <insert id="insertJoinBatch">
        insert into rpt_join (
            report_id, left_table, left_column, right_table, right_column, join_type, extra_condition, join_order
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
                #{reportId},
                #{item.leftTable},
                #{item.leftColumn},
                #{item.rightTable},
                #{item.rightColumn},
                #{item.joinType},
                #{item.extraCondition},
                #{item.joinOrder}
            )
        </foreach>
    </insert>

    <delete id="deleteCustomSqlByReportId" parameterType="Long">
        delete from rpt_sql_custom where report_id = #{reportId}
    </delete>

    <insert id="insertCustomSql">
        insert into rpt_sql_custom (report_id, sql_text, risk_level, audit_status)
        values (#{reportId}, #{sqlText}, 'LOW', 'PENDING')
    </insert>

    <delete id="deleteAggregatePolicyByReportId" parameterType="Long">
        delete from rpt_aggregate_policy where report_id = #{reportId}
    </delete>

    <insert id="insertAggregatePolicy">
        insert into rpt_aggregate_policy (
            report_id, aggregate_granularity, cron_expr, time_dimension_field, retention_days, is_enabled
        ) values (
            #{reportId},
            #{policy.aggregateGranularity},
            #{policy.cronExpr},
            #{policy.timeDimensionField},
            #{policy.retentionDays},
            #{policy.isEnabled}
        )
    </insert>
</mapper>
