<?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.common.mapper.AnalysisFieldMapper">

    <resultMap id="BaseResultMap" type="com.common.entity.AnalysisField">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="rule_id" property="ruleId" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="data_source" property="dataSource" jdbcType="VARCHAR"/>
        <result column="database" property="database" jdbcType="VARCHAR"/>
        <result column="table_name" property="tableName" jdbcType="VARCHAR"/>
        <result column="table_alias" property="tableAlias" jdbcType="VARCHAR"/>
        <result column="column_name" property="columnName" jdbcType="VARCHAR"/>
        <result column="column_desc" property="columnDesc" jdbcType="VARCHAR"/>
        <result column="data_type" property="dataType" jdbcType="VARCHAR"/>
        <result column="fn" property="fn" jdbcType="VARCHAR"/>
        <result column="arguments" property="arguments" jdbcType="VARCHAR"/>
        <result column="placeholder" property="placeholder" jdbcType="VARCHAR"/>
        <result column="base_type" property="baseType" jdbcType="INTEGER"/>
        <result column="category_id" property="categoryId" jdbcType="INTEGER"/>
        <result column="create_dept" property="createDept" jdbcType="BIGINT"/>
        <result column="del_flag" property="delFlag" jdbcType="CHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="create_by" property="createBy" jdbcType="BIGINT"/>
        <result column="update_by" property="updateBy" jdbcType="BIGINT"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="tenant_id" property="tenantId" jdbcType="VARCHAR"/>
        <result column="alarm_column_name" property="alarmColumnName" jdbcType="VARCHAR"/>
        <result column="alarm_column_desc" property="alarmColumnDesc" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, rule_id, type, data_source, database, table_name, table_alias,
        column_name, column_desc, data_type, fn, arguments, placeholder,
        base_type, category_id, create_dept, del_flag, create_time,
        update_time, create_by, update_by, remark, tenant_id,alarm_column_name,alarm_column_desc
    </sql>

    <!-- 根据规则ID查询字段配置 -->
    <select id="selectByRuleId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_field
        WHERE rule_id = #{ruleId, jdbcType=OTHER}
        AND del_flag = '0'
        ORDER BY id ASC
    </select>

    <!-- 查询规则中用于SELECT的字段（通过type字段区分） -->
    <select id="selectSelectFieldsByRuleId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_field
        WHERE rule_id =#{ruleId, jdbcType=OTHER}
        AND del_flag = '0'
        AND type IN ('measure', 'calc')
        ORDER BY id ASC
    </select>

    <!-- 查询规则中用于GROUP BY的字段（通过type字段区分） -->
    <select id="selectGroupByFieldsByRuleId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_field
        WHERE rule_id = #{ruleId, jdbcType=OTHER}
        AND del_flag = '0'
        AND type = 'dimension'
        ORDER BY id ASC
    </select>

</mapper>
