<?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.ruoyi.ibs.rules.mapper.RelationRuleMapper">

    <resultMap type="RelationRule" id="RelationRuleResult">
        <result property="id" column="id"/>
        <result property="ruleId" column="rule_id"/>
        <result property="ruleName" column="rule_name"/>
        <result property="ruleType" column="rule_type"/>
        <result property="ruleLevel" column="rule_level"/>
        <result property="param" column="param"/>
        <result property="updateBy" column="update_by"/>
        <result property="deptId" column="dept_id"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateCounts" column="update_counts"/>
        <result property="ruleNameId" column="rule_name_id"/>
        <result property="addSource" column="add_source"/>
        <result property="addSourceId" column="add_source_id"/>
    </resultMap>

    <sql id="selectRelationRuleVo">
        select id,
               rule_id,
               rule_name,
               rule_type,
               rule_level,
               param,
               update_by,
               dept_id,
               update_time,
               update_counts,
               rule_name_id,add_source,add_source_id
        from relation_rule
    </sql>

    <select id="selectRelationRuleListById" parameterType="RelationRule" resultMap="RelationRuleResult">
        <include refid="selectRelationRuleVo"/>
        <where>
            <if test="ruleId != null  and ruleId != ''">and rule_id = #{ruleId}</if>
        </where>
    </select>


    <select id="selectRelationRuleList" parameterType="RelationRule" resultMap="RelationRuleResult">
        <include refid="selectRelationRuleVo"/>
        <where>
            status = '1'
            <if test="ruleId != null  and ruleId != ''">and rule_id = #{ruleId}</if>
            <if test="ruleName != null  and ruleName != ''">and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="ruleType != null  and ruleType != ''">and rule_type = #{ruleType}</if>
            <if test="ruleLevel != null and ruleLevel != ''">and rule_level = #{ruleLevel}</if>
            <if test="param != null  and param != ''">and param = #{param}</if>
            <if test="deptId != null  and deptId != ''">and dept_id = #{deptId}</if>
            <if test="updateCounts != null  and updateCounts != ''">and update_counts = #{updateCounts}</if>
        </where>
    </select>


    <insert id="insertRelationRule" parameterType="RelationRule" useGeneratedKeys="true" keyProperty="id">
        insert into relation_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ruleId != null">rule_id,</if>
            <if test="ruleName != null">rule_name,</if>
            <if test="ruleType != null">rule_type,</if>
            <if test="ruleLevel != null">rule_level,</if>
            <if test="param != null">param,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateCounts != null">update_counts,</if>
            <if test="status != null">status,</if>
            <if test="ruleNameId != null">rule_name_id,</if>
            <if test="addSource != null">add_source,</if>
            <if test="addSourceId != null">add_source_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ruleId != null">#{ruleId},</if>
            <if test="ruleName != null">#{ruleName},</if>
            <if test="ruleType != null">#{ruleType},</if>
            <if test="ruleLevel != null">#{ruleLevel},</if>
            <if test="param != null">#{param},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateCounts != null">#{updateCounts},</if>
            <if test="status != null">#{status},</if>
            <if test="ruleNameId != null">#{ruleNameId},</if>
            <if test="addSource != null">#{addSource},</if>
            <if test="addSourceId != null">#{addSourceId},</if>
        </trim>
    </insert>

    <update id="updateRelationRule" parameterType="RelationRule">
        update relation_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
        </trim>
        where rule_id = #{ruleId}
    </update>

    <delete id="deleteRelationRuleById" parameterType="String">
        update relation_rule
        set status = '0'
        where rule_id = #{id}
    </delete>


</mapper>