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

    <resultMap id="BaseResultMap" type="com.common.entity.AnalysisGroupByHaving">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="group_by_id" property="groupById" jdbcType="INTEGER"/>
        <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="operator" property="operator" jdbcType="VARCHAR"/>
        <result column="value" property="value" 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"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, group_by_id, data_source, database, table_name, table_alias,
        column_name, column_desc, data_type, fn, arguments, operator, value,
        base_type, category_id, create_dept, del_flag, create_time,
        update_time, create_by, update_by, remark, tenant_id
    </sql>

    <!-- 根据分组ID查询HAVING条件 -->
    <select id="selectByGroupById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_group_by_having
        WHERE group_by_id = #{groupById}
        AND del_flag = '0'
    </select>

    <!-- 根据规则ID查询HAVING条件（通过关联分组表） -->
    <select id="selectByRuleId" resultMap="BaseResultMap">
        SELECT
        h.id, h.group_by_id, h.data_source, h.database, h.table_name, h.table_alias,
        h.column_name, h.column_desc, h.data_type, h.fn, h.arguments, h.operator, h.value,
        h.base_type, h.category_id, h.create_dept, h.del_flag, h.create_time,
        h.update_time, h.create_by, h.update_by, h.remark, h.tenant_id
        FROM analysis_group_by_having h
        INNER JOIN analysis_group_by g ON h.group_by_id = g.id
        WHERE g.rule_id =#{ruleId, jdbcType=OTHER}
        AND h.del_flag = '0'
        AND g.del_flag = '0'
        ORDER BY h.id ASC
    </select>

</mapper>
