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

    <!--
      结果映射：
      将报表定义主表字段映射到 ErpReportDefinition 实体。
    -->
    <resultMap id="ErpReportDefinitionResult" type="com.ruoyi.web.report.domain.ErpReportDefinition">
        <id property="id" column="id"/>
        <result property="reportCode" column="report_code"/>
        <result property="reportName" column="report_name"/>
        <result property="dataSource" column="data_source"/>
        <result property="queryMode" column="query_mode"/>
        <result property="aggregateMode" column="aggregate_mode"/>
        <result property="status" column="status"/>
        <result property="versionNo" column="version_no"/>
        <result property="description" column="description"/>
        <result property="createBy" column="created_by"/>
        <result property="updateBy" column="updated_by"/>
        <result property="createTime" column="created_at"/>
        <result property="updateTime" column="updated_at"/>
    </resultMap>

    <resultMap id="ReportFieldMetaResult" type="com.ruoyi.web.report.domain.meta.ReportFieldMeta">
        <result property="fieldKey" column="field_key"/>
        <result property="sourceTable" column="source_table"/>
        <result property="sourceColumn" column="source_column"/>
        <result property="exprText" column="expr_text"/>
        <result property="aggregateFn" column="aggregate_fn"/>
        <result property="isGroupDim" column="is_group_dim"/>
        <result property="displayOrder" column="display_order"/>
        <result property="isEnabled" column="is_enabled"/>
    </resultMap>

    <resultMap id="ReportJoinMetaResult" type="com.ruoyi.web.report.domain.meta.ReportJoinMeta">
        <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>

    <!-- 基础查询列，供多个 SQL 复用，避免字段列表重复维护。 -->
    <sql id="baseSelect">
        select id, report_code, report_name, data_source, query_mode, aggregate_mode, status, version_no, description,
               created_by, updated_by, created_at, updated_at
        from rpt_definition
    </sql>

    <!-- 按 ID 查询详情 -->
    <select id="selectErpReportDefinitionById" parameterType="Long" resultMap="ErpReportDefinitionResult">
        <include refid="baseSelect"/>
        where id = #{id}
    </select>

    <!-- 条件分页查询 -->
    <select id="selectErpReportDefinitionList" parameterType="com.ruoyi.web.report.domain.ErpReportDefinition" resultMap="ErpReportDefinitionResult">
        <include refid="baseSelect"/>
        <where>
            <if test="reportCode != null and reportCode != ''">
                and report_code like concat('%', #{reportCode}, '%')
            </if>
            <if test="reportName != null and reportName != ''">
                and report_name like concat('%', #{reportName}, '%')
            </if>
            <if test="dataSource != null and dataSource != ''">
                and data_source = #{dataSource}
            </if>
            <if test="queryMode != null and queryMode != ''">
                and query_mode = #{queryMode}
            </if>
            <if test="aggregateMode != null and aggregateMode != ''">
                and aggregate_mode = #{aggregateMode}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
        </where>
        order by id desc
    </select>

    <!-- 按编码查询，用于唯一性校验 -->
    <select id="selectErpReportDefinitionByCode" parameterType="String" resultMap="ErpReportDefinitionResult">
        <include refid="baseSelect"/>
        where report_code = #{reportCode}
        limit 1
    </select>

    <!-- 新增报表定义 -->
    <insert id="insertErpReportDefinition" parameterType="com.ruoyi.web.report.domain.ErpReportDefinition" useGeneratedKeys="true" keyProperty="id">
        insert into rpt_definition
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="reportCode != null and reportCode != ''">report_code,</if>
            <if test="reportName != null and reportName != ''">report_name,</if>
            <if test="dataSource != null and dataSource != ''">data_source,</if>
            <if test="queryMode != null and queryMode != ''">query_mode,</if>
            <if test="aggregateMode != null and aggregateMode != ''">aggregate_mode,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="versionNo != null">version_no,</if>
            <if test="description != null">description,</if>
            created_by, updated_by
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="reportCode != null and reportCode != ''">#{reportCode},</if>
            <if test="reportName != null and reportName != ''">#{reportName},</if>
            <if test="dataSource != null and dataSource != ''">#{dataSource},</if>
            <if test="queryMode != null and queryMode != ''">#{queryMode},</if>
            <if test="aggregateMode != null and aggregateMode != ''">#{aggregateMode},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="versionNo != null">#{versionNo},</if>
            <if test="description != null">#{description},</if>
            #{createBy}, #{updateBy}
        </trim>
    </insert>

    <!-- 修改报表定义 -->
    <update id="updateErpReportDefinition" parameterType="com.ruoyi.web.report.domain.ErpReportDefinition">
        update rpt_definition
        <trim prefix="set" suffixOverrides=",">
            <if test="reportName != null and reportName != ''">report_name = #{reportName},</if>
            <if test="dataSource != null and dataSource != ''">data_source = #{dataSource},</if>
            <if test="queryMode != null and queryMode != ''">query_mode = #{queryMode},</if>
            <if test="aggregateMode != null and aggregateMode != ''">aggregate_mode = #{aggregateMode},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="description != null">description = #{description},</if>
            updated_by = #{updateBy},
            updated_at = now()
        </trim>
        where id = #{id}
    </update>

    <!-- 删除报表定义 -->
    <delete id="deleteErpReportDefinitionById" parameterType="Long">
        delete from rpt_definition where id = #{id}
    </delete>

    <!-- 发布报表定义：状态置为已发布，并提升版本号 -->
    <update id="publishErpReportDefinition">
        update rpt_definition
        set status = 'PUBLISHED',
            version_no = version_no + 1,
            updated_by = #{updateBy},
            updated_at = now()
        where id = #{id}
    </update>

    <!-- 查询低代码字段元数据 -->
    <select id="selectFieldMetaByReportId" parameterType="Long" resultMap="ReportFieldMetaResult">
        select field_key, source_table, source_column, expr_text, aggregate_fn, is_group_dim, display_order, is_enabled
        from rpt_field
        where report_id = #{reportId}
          and is_enabled = 1
        order by display_order asc, id asc
    </select>

    <!-- 查询低代码关联元数据 -->
    <select id="selectJoinMetaByReportId" parameterType="Long" resultMap="ReportJoinMetaResult">
        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>

    <!-- 查询审核通过的自定义SQL -->
    <select id="selectApprovedCustomSqlByReportId" parameterType="Long" resultType="String">
        select sql_text
        from rpt_sql_custom
        where report_id = #{reportId}
          and audit_status = 'APPROVED'
        limit 1
    </select>
</mapper>
