<?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.inspect.guideline.mapper.PatrolGuidelineRuleMapper">

    <resultMap type="PatrolGuidelineRule" id="PatrolGuidelineRuleResult">
        <result property="guidelineRuleId" column="guideline_rule_id"/>
        <result property="guidelineId" column="guideline_id"/>
        <result property="levelType" column="level_type"/>
        <result property="ruleCondition" column="rule_condition"/>
        <result property="description" column="description"/>
        <result property="isEnable" column="is_enable"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectPatrolGuidelineRuleVo">
        select guideline_rule_id, guideline_id, level_type, rule_condition, description, is_enable, create_time
        from patrol_guideline_rule
    </sql>

    <select id="selectPatrolGuidelineRuleList" parameterType="PatrolGuidelineRule"
            resultMap="PatrolGuidelineRuleResult">
        <include refid="selectPatrolGuidelineRuleVo"/>
        <where>
            <if test="guidelineId != null ">and guideline_id = #{guidelineId}</if>
            <if test="levelType != null  and levelType != ''">and level_type = #{levelType}</if>
            <if test="ruleCondition != null  and ruleCondition != ''">and rule_condition = #{ruleCondition}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="isEnable != null  and isEnable != ''">and is_enable = #{isEnable}</if>
        </where>
        order by guideline_id, level_type
    </select>

    <select id="selectPatrolGuidelineRuleByGuidelineRuleId" parameterType="Long" resultMap="PatrolGuidelineRuleResult">
        <include refid="selectPatrolGuidelineRuleVo"/>
        where guideline_rule_id = #{guidelineRuleId}
    </select>

    <insert id="insertPatrolGuidelineRule" parameterType="PatrolGuidelineRule" useGeneratedKeys="true"
            keyProperty="guidelineRuleId">
        insert into patrol_guideline_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="guidelineId != null">guideline_id,</if>
            <if test="levelType != null">level_type,</if>
            <if test="ruleCondition != null">rule_condition,</if>
            <if test="description != null">description,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="guidelineId != null">#{guidelineId},</if>
            <if test="levelType != null">#{levelType},</if>
            <if test="ruleCondition != null">#{ruleCondition},</if>
            <if test="description != null">#{description},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updatePatrolGuidelineRule" parameterType="PatrolGuidelineRule">
        update patrol_guideline_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="guidelineId != null">guideline_id = #{guidelineId},</if>
            <if test="levelType != null">level_type = #{levelType},</if>
            <if test="ruleCondition != null">rule_condition = #{ruleCondition},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isEnable != null">is_enable = #{isEnable},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where guideline_rule_id = #{guidelineRuleId}
    </update>

    <delete id="deletePatrolGuidelineRuleByGuidelineRuleId" parameterType="Long">
        delete
        from patrol_guideline_rule
        where guideline_rule_id = #{guidelineRuleId}
    </delete>

    <delete id="deletePatrolGuidelineRuleByGuidelineRuleIds" parameterType="String">
        delete from patrol_guideline_rule where guideline_rule_id in
        <foreach item="guidelineRuleId" collection="array" open="(" separator="," close=")">
            #{guidelineRuleId}
        </foreach>
    </delete>
</mapper>