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

    <resultMap id="BaseResultMap" type="com.common.entity.AnalysisAnalysisRule">
        <id column="rule_id" property="ruleId" jdbcType="VARCHAR"/>
        <result column="create_user" property="createUser" jdbcType="VARCHAR"/>
        <result column="rule_name" property="ruleName" jdbcType="VARCHAR"/>
        <result column="analysis_method" property="analysisMethod" jdbcType="VARCHAR"/>
        <result column="run_mode" property="runMode" jdbcType="VARCHAR"/>
        <result column="rule_output" property="ruleOutput" jdbcType="VARCHAR"/>
        <result column="organization" property="organization" jdbcType="VARCHAR"/>
        <result column="task_status" property="taskStatus" jdbcType="VARCHAR"/>
        <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="rule_desc" property="ruleDesc" jdbcType="VARCHAR"/>
        <result column="rule_type" property="ruleType" jdbcType="INTEGER"/>
        <result column="rule_status" property="ruleStatus" jdbcType="INTEGER"/>
        <result column="rule_content" property="ruleContent" jdbcType="VARCHAR"/>
        <result column="rule_expression" property="ruleExpression" jdbcType="VARCHAR"/>
        <result column="priority" property="priority" jdbcType="BIGINT"/>
        <result column="tags" property="tags" jdbcType="VARCHAR"/>
        <result column="version" property="version" jdbcType="INTEGER"/>
        <result column="subset_id" property="subsetId" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        rule_id, create_user, rule_name, analysis_method, run_mode, rule_output,
        organization, task_status, create_dept, del_flag, create_time, update_time,
        create_by, update_by, remark, tenant_id, rule_desc, rule_type, rule_status,
        rule_content, rule_expression, priority, tags, version, subset_id
    </sql>

    <!-- 查询指定运行模式的活动规则 -->
    <select id="selectActiveRulesByRunMode" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_analysis_rule
        WHERE run_mode = #{runMode}
        AND del_flag = '0'
        AND  rule_status =1
        ORDER BY priority DESC, create_time ASC
    </select>

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

    <!-- 根据ID查询规则（与selectByRuleId相同） -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM analysis_analysis_rule
        WHERE rule_id = #{ruleId, jdbcType=OTHER}
        AND del_flag = '0'
    </select>

    <!-- 更新规则任务状态 -->
    <update id="updateTaskStatus">
        UPDATE analysis_analysis_rule
        SET task_status = #{taskStatus},
            update_time = SYSDATE
        <if test="updateBy != null">
            ,update_by = #{updateBy}
        </if>
        WHERE rule_id = #{ruleId, jdbcType=OTHER}
    </update>

</mapper>
