<?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.ems.mapper.InspectionRuleMapper">

    <resultMap type="com.ruoyi.ems.domain.InspectionRule" id="InspectionRuleResult">
        <id property="id" column="id"/>
        <result property="planCode" column="plan_code"/>
        <result property="ruleCode" column="rule_code"/>
        <result property="ruleName" column="rule_name"/>
        <result property="deviceModel" column="device_model"/>
        <result property="deviceModelName" column="device_model_name"/>
        <result property="attrKey" column="attr_key"/>
        <result property="attrName" column="attr_name"/>
        <result property="checkType" column="check_type"/>
        <result property="minValue" column="min_value"/>
        <result property="maxValue" column="max_value"/>
        <result property="expectValue" column="expect_value"/>
        <result property="ruleOrder" column="rule_order"/>
        <result property="enabled" column="enabled"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        SELECT r.id, r.plan_code, r.rule_code, r.rule_name, r.device_model,
               m.model_name as device_model_name, r.attr_key, r.attr_name,
               r.check_type, r.min_value, r.max_value, r.expect_value,
               r.rule_order, r.enabled, r.create_time, r.update_time
        FROM adm_op_inspection_rule r
                 LEFT JOIN adm_ems_obj_model m ON r.device_model = m.model_code
    </sql>

    <select id="selectById" parameterType="Long" resultMap="InspectionRuleResult">
        <include refid="selectVo"/>
        WHERE r.id = #{id}
    </select>

    <select id="selectByPlanCode" parameterType="String" resultMap="InspectionRuleResult">
        <include refid="selectVo"/>
        WHERE r.plan_code = #{planCode}
        ORDER BY r.rule_order
    </select>

    <select id="selectEnabledByPlanCode" parameterType="String" resultMap="InspectionRuleResult">
        <include refid="selectVo"/>
        WHERE r.plan_code = #{planCode} AND r.enabled = 1
        ORDER BY r.rule_order
    </select>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.InspectionRule" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO adm_op_inspection_rule (
            plan_code, rule_code, rule_name, device_model, attr_key, attr_name,
            check_type, min_value, max_value, expect_value, rule_order, enabled, create_time
        ) VALUES (
                     #{planCode}, #{ruleCode}, #{ruleName}, #{deviceModel}, #{attrKey}, #{attrName},
                     #{checkType}, #{minValue}, #{maxValue}, #{expectValue}, #{ruleOrder}, #{enabled}, NOW()
                 )
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO adm_op_inspection_rule (
        plan_code, rule_code, rule_name, device_model, attr_key, attr_name,
        check_type, min_value, max_value, expect_value, rule_order, enabled, create_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.planCode}, #{item.ruleCode}, #{item.ruleName}, #{item.deviceModel},
            #{item.attrKey}, #{item.attrName}, #{item.checkType}, #{item.minValue},
            #{item.maxValue}, #{item.expectValue}, #{item.ruleOrder}, #{item.enabled}, NOW())
        </foreach>
    </insert>

    <update id="update" parameterType="com.ruoyi.ems.domain.InspectionRule">
        UPDATE adm_op_inspection_rule
        <set>
            <if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
            <if test="deviceModel != null">device_model = #{deviceModel},</if>
            <if test="attrKey != null and attrKey != ''">attr_key = #{attrKey},</if>
            <if test="attrName != null">attr_name = #{attrName},</if>
            <if test="checkType != null">check_type = #{checkType},</if>
            <if test="minValue != null">min_value = #{minValue},</if>
            <if test="maxValue != null">max_value = #{maxValue},</if>
            <if test="expectValue != null">expect_value = #{expectValue},</if>
            <if test="ruleOrder != null">rule_order = #{ruleOrder},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        DELETE FROM adm_op_inspection_rule WHERE id = #{id}
    </delete>

    <delete id="deleteByPlanCode" parameterType="String">
        DELETE FROM adm_op_inspection_rule WHERE plan_code = #{planCode}
    </delete>
</mapper>