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

    <resultMap id="BaseResultMap" type="com.common.entity.AnalysisWhereCondition">
        <id column="cond_id" property="condId" jdbcType="INTEGER"/>
        <result column="rule_id" property="ruleId" jdbcType="VARCHAR"/>
        <result column="logical_op" property="logicalOp" jdbcType="VARCHAR"/>
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="parent_cond_id" property="parentCondId" 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">
        cond_id, rule_id, logical_op, seq_num, parent_cond_id,
        create_dept, del_flag, create_time, update_time,
        create_by, update_by, remark, tenant_id
    </sql>

    <!-- 根据规则ID查询WHERE条件 -->
    <select id="selectByRuleId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_where_condition
        WHERE rule_id = #{ruleId}::uuid
        AND del_flag = '0'
        ORDER BY seq_num ASC
    </select>

    <!-- 查询根节点条件（无父节点） -->
    <select id="selectRootConditions" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_where_condition
        WHERE rule_id = #{ruleId}::uuid
        AND (parent_cond_id IS NULL OR parent_cond_id = 0)
        AND del_flag = '0'
        ORDER BY seq_num ASC
    </select>

    <!-- 查询指定条件的子条件 -->
    <select id="selectChildConditions" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_where_condition
        WHERE parent_cond_id = #{parentCondId}
        AND del_flag = '0'
        ORDER BY seq_num ASC
    </select>

</mapper>
