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

    <resultMap type="com.ruoyi.ems.domain.OpEnergyStrategyTrigger" id="TriggerResult">
        <result property="id" column="id"/>
        <result property="strategyCode" column="strategy_code"/>
        <result property="triggerName" column="trigger_name"/>
        <result property="triggerType" column="trigger_type"/>
        <result property="sourceObjType" column="source_obj_type"/>
        <result property="sourceObjCode" column="source_obj_code"/>
        <result property="sourceModelCode" column="source_model_code"/>
        <result property="eventKey" column="event_key"/>
        <result property="attrKey" column="attr_key"/>
        <result property="conditionExpr" column="condition_expr"/>
        <result property="enable" column="enable"/>
        <result property="priority" column="priority"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectTriggerVo">
        select id, strategy_code, trigger_name, trigger_type, source_obj_type, source_obj_code,
               source_model_code, event_key, attr_key, condition_expr, enable, priority,
               create_time, update_time
        from adm_op_energy_strategy_trigger
    </sql>

    <select id="selectByStrategyCode" parameterType="String" resultMap="TriggerResult">
        <include refid="selectTriggerVo"/>
        where strategy_code = #{strategyCode}
        order by priority desc, id
    </select>

    <select id="findBySourceAndEvent" resultMap="TriggerResult">
        <include refid="selectTriggerVo"/>
        where source_obj_code = #{sourceObjCode}
        and trigger_type = 'EVENT'
        and event_key = #{eventKey}
        and enable = 1
        order by priority desc
    </select>

    <select id="findBySourceAndAttr" resultMap="TriggerResult">
        <include refid="selectTriggerVo"/>
        where source_obj_code = #{sourceObjCode}
        and trigger_type = 'ATTR'
        and attr_key = #{attrKey}
        and enable = 1
        order by priority desc
    </select>

    <select id="selectById" parameterType="Long" resultMap="TriggerResult">
        <include refid="selectTriggerVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrigger" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyTrigger" useGeneratedKeys="true" keyProperty="id">
        insert into adm_op_energy_strategy_trigger
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">strategy_code,</if>
            <if test="triggerName != null and triggerName != ''">trigger_name,</if>
            <if test="triggerType != null and triggerType != ''">trigger_type,</if>
            <if test="sourceObjType != null">source_obj_type,</if>
            <if test="sourceObjCode != null and sourceObjCode != ''">source_obj_code,</if>
            <if test="sourceModelCode != null and sourceModelCode != ''">source_model_code,</if>
            <if test="eventKey != null">event_key,</if>
            <if test="attrKey != null">attr_key,</if>
            <if test="conditionExpr != null">condition_expr,</if>
            <if test="enable != null">enable,</if>
            <if test="priority != null">priority,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyCode != null and strategyCode != ''">#{strategyCode},</if>
            <if test="triggerName != null and triggerName != ''">#{triggerName},</if>
            <if test="triggerType != null and triggerType != ''">#{triggerType},</if>
            <if test="sourceObjType != null">#{sourceObjType},</if>
            <if test="sourceObjCode != null and sourceObjCode != ''">#{sourceObjCode},</if>
            <if test="sourceModelCode != null and sourceModelCode != ''">#{sourceModelCode},</if>
            <if test="eventKey != null">#{eventKey},</if>
            <if test="attrKey != null">#{attrKey},</if>
            <if test="conditionExpr != null">#{conditionExpr},</if>
            <if test="enable != null">#{enable},</if>
            <if test="priority != null">#{priority},</if>
        </trim>
    </insert>

    <update id="updateTrigger" parameterType="com.ruoyi.ems.domain.OpEnergyStrategyTrigger">
        update adm_op_energy_strategy_trigger
        <trim prefix="SET" suffixOverrides=",">
            <if test="triggerName != null and triggerName != ''">trigger_name = #{triggerName},</if>
            <if test="triggerType != null and triggerType != ''">trigger_type = #{triggerType},</if>
            <if test="sourceObjType != null">source_obj_type = #{sourceObjType},</if>
            <if test="sourceObjCode != null and sourceObjCode != ''">source_obj_code = #{sourceObjCode},</if>
            <if test="sourceModelCode != null and sourceModelCode != ''">source_model_code = #{sourceModelCode},</if>
            <if test="eventKey != null">event_key = #{eventKey},</if>
            <if test="attrKey != null">attr_key = #{attrKey},</if>
            <if test="conditionExpr != null">condition_expr = #{conditionExpr},</if>
            <if test="enable != null">enable = #{enable},</if>
            <if test="priority != null">priority = #{priority},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTrigger" parameterType="Long">
        delete from adm_op_energy_strategy_trigger where id = #{id}
    </delete>

    <delete id="deleteTriggerByIds">
        delete from adm_op_energy_strategy_trigger where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>