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

    <resultMap type="EmsStrategyRunning" id="EmsStrategyRunningResult">
        <result property="id"    column="id"    />
        <result property="mainStrategyId"    column="main_strategy_id"    />
        <result property="auxiliaryStrategyId"    column="auxiliary_strategy_id"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectEmsStrategyRunningVo">
        select id, main_strategy_id, auxiliary_strategy_id, status, create_time, update_time, site_id, device_id, remark from ems_strategy_running
    </sql>

    <select id="selectEmsStrategyRunningList" parameterType="EmsStrategyRunning" resultMap="EmsStrategyRunningResult">
        <include refid="selectEmsStrategyRunningVo"/>
        <where>
            <if test="mainStrategyId != null "> and main_strategy_id = #{mainStrategyId}</if>
            <if test="auxiliaryStrategyId != null "> and auxiliary_strategy_id = #{auxiliaryStrategyId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
        </where>
    </select>

    <select id="selectEmsStrategyRunningById" parameterType="Long" resultMap="EmsStrategyRunningResult">
        <include refid="selectEmsStrategyRunningVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsStrategyRunning" parameterType="EmsStrategyRunning" useGeneratedKeys="true" keyProperty="id">
        insert into ems_strategy_running
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mainStrategyId != null">main_strategy_id,</if>
            <if test="auxiliaryStrategyId != null">auxiliary_strategy_id,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mainStrategyId != null">#{mainStrategyId},</if>
            <if test="auxiliaryStrategyId != null">#{auxiliaryStrategyId},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateEmsStrategyRunning" parameterType="EmsStrategyRunning">
        update ems_strategy_running
        <trim prefix="SET" suffixOverrides=",">
            <if test="mainStrategyId != null">main_strategy_id = #{mainStrategyId},</if>
            <if test="auxiliaryStrategyId != null">auxiliary_strategy_id = #{auxiliaryStrategyId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsStrategyRunningById" parameterType="Long">
        delete from ems_strategy_running where id = #{id}
    </delete>

    <delete id="deleteEmsStrategyRunningByIds" parameterType="String">
        delete from ems_strategy_running where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getRunningList" parameterType="String" resultType="com.xzzn.ems.domain.vo.StrategyRunningVo">
        select t.id,
               t.`status`,
               t.site_id as siteId,
               main.strategy_name as mainStrategyName,
               aux.strategy_name as auxStrategyName,
               main.id as mainStrategyId,
               aux.id as auxStrategyId
        from ems_strategy_running t
                 LEFT JOIN ems_strategy main on t.main_strategy_id = main.id
                 LEFT JOIN ems_strategy aux on t.auxiliary_strategy_id = aux.id
        where t.`status` != 4
        <if test="siteId != null  and siteId != ''">
            and  t.site_id = #{siteId}
        </if>
    </select>

    <update id="stopEmsStrategyRunning" parameterType="Long">
        update ems_strategy_running set `status`= 4 where id = #{id}
    </update>

    <select id="selectEmsStrategyRunning" resultMap="EmsStrategyRunningResult">
        <include refid="selectEmsStrategyRunningVo"/>
        where site_id = #{siteId}
        and main_strategy_id = #{mainStrategyId}
        and auxiliary_strategy_id = #{auxiliaryStrategyId}
        and `status` = 1
    </select>

    <select id="getPendingPollerStrategy" parameterType="String" resultType="com.xzzn.ems.domain.vo.StrategyRunningVo">
        select t.id,
            t.`status`,
            t.site_id as siteId,
            main.strategy_name as mainStrategyName,
            aux.strategy_name as auxStrategyName,
            main.id as mainStrategyId,
            aux.id as auxStrategyId
        from ems_strategy_running t
            LEFT JOIN ems_strategy main on t.main_strategy_id = main.id
            LEFT JOIN ems_strategy aux on t.auxiliary_strategy_id = aux.id
        where t.`status` = 1
        <if test="siteId != null  and siteId != ''">
            and  t.site_id = #{siteId}
        </if>
    </select>

    <select id="getRunningStrategy" parameterType="String" resultMap="EmsStrategyRunningResult">
        <include refid="selectEmsStrategyRunningVo"/>
        where site_id = #{siteId}
        and `status` = 1
        order by update_time desc
        limit 1
    </select>

    <select id="getPendingStrategy" parameterType="String" resultMap="EmsStrategyRunningResult">
        <include refid="selectEmsStrategyRunningVo"/>
        where site_id = #{siteId}
        and `status` in (0, 2)
        order by update_time desc
        limit 1
    </select>
</mapper>