<?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.dcs.hnyz.mapper.ActionConfigMapper">

    <resultMap type="ActionConfig" id="ActionConfigResult">
        <result property="actionId"    column="action_id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="actionType"    column="action_type"    />
        <result property="value"    column="value"    />
        <result property="unit"    column="unit"    />
        <result property="triggerType"    column="trigger_type"    />
        <result property="triggerCondition"    column="trigger_condition"    />
        <result property="triggerDelay"    column="trigger_delay"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectActionConfigVo">
        select action_id, equipment_id,(select equipment_name from equipment where equipment_id = action_config.equipment_id ) equipmentName, action_type, value, unit, trigger_type, trigger_condition, trigger_delay, status, create_by, create_time, update_by, update_time, remark from action_config
    </sql>

    <select id="selectActionConfigList" parameterType="ActionConfig" resultMap="ActionConfigResult">
        <include refid="selectActionConfigVo"/>
        <where>
            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
            <if test="actionType != null  and actionType != ''"> and action_type = #{actionType}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="triggerType != null  and triggerType != ''"> and trigger_type = #{triggerType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="selectActionConfigByActionId" parameterType="Long" resultMap="ActionConfigResult">
        <include refid="selectActionConfigVo"/>
        where action_id = #{actionId}
    </select>

    <insert id="insertActionConfig" parameterType="ActionConfig" useGeneratedKeys="true" keyProperty="actionId">
        insert into action_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id,</if>
            <if test="actionType != null">action_type,</if>
            <if test="value != null">value,</if>
            <if test="unit != null">unit,</if>
            <if test="triggerType != null">trigger_type,</if>
            <if test="triggerCondition != null">trigger_condition,</if>
            <if test="triggerDelay != null">trigger_delay,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="actionType != null">#{actionType},</if>
            <if test="value != null">#{value},</if>
            <if test="unit != null">#{unit},</if>
            <if test="triggerType != null">#{triggerType},</if>
            <if test="triggerCondition != null">#{triggerCondition},</if>
            <if test="triggerDelay != null">#{triggerDelay},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateActionConfig" parameterType="ActionConfig">
        update action_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="actionType != null">action_type = #{actionType},</if>
            <if test="value != null">value = #{value},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="triggerType != null">trigger_type = #{triggerType},</if>
            <if test="triggerCondition != null">trigger_condition = #{triggerCondition},</if>
            <if test="triggerDelay != null">trigger_delay = #{triggerDelay},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where action_id = #{actionId}
    </update>

    <delete id="deleteActionConfigByActionId" parameterType="Long">
        delete from action_config where action_id = #{actionId}
    </delete>

    <delete id="deleteActionConfigByActionIds" parameterType="String">
        delete from action_config where action_id in
        <foreach item="actionId" collection="array" open="(" separator="," close=")">
            #{actionId}
        </foreach>
    </delete>
</mapper>
