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

    <resultMap type="EmsStrategyTemp" id="EmsStrategyTempResult">
        <result property="id"    column="id"    />
        <result property="strategyId"    column="strategy_id"    />
        <result property="templateName"    column="template_name"    />
        <result property="sdcLimit"    column="sdc_limit"    />
        <result property="sdcDown"    column="sdc_down"    />
        <result property="sdcUp"    column="sdc_up"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="chargeDischargePower"    column="charge_discharge_power"    />
        <result property="chargeStatus"    column="charge_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"    />
        <result property="siteId"    column="site_id"    />
        <result property="templateId"    column="template_id"    />
    </resultMap>

    <sql id="selectEmsStrategyTempVo">
        select id, strategy_id, template_name, sdc_limit, sdc_down, sdc_up, start_time, end_time, charge_discharge_power, charge_status, create_by, create_time, update_by, update_time, remark, site_id, template_id from ems_strategy_temp
    </sql>

    <select id="selectEmsStrategyTempList" parameterType="EmsStrategyTemp" resultMap="EmsStrategyTempResult">
        <include refid="selectEmsStrategyTempVo"/>
        <where>
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="templateName != null  and templateName != ''"> and template_name like concat('%', #{templateName}, '%')</if>
            <if test="sdcLimit != null "> and sdc_limit = #{sdcLimit}</if>
            <if test="sdcDown != null "> and sdc_down = #{sdcDown}</if>
            <if test="sdcUp != null "> and sdc_up = #{sdcUp}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="chargeDischargePower != null "> and charge_discharge_power = #{chargeDischargePower}</if>
            <if test="chargeStatus != null  and chargeStatus != ''"> and charge_status = #{chargeStatus}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="templateId != null  and templateId != ''"> and template_id = #{templateId}</if>
        </where>
    </select>

    <select id="selectEmsStrategyTempById" parameterType="Long" resultMap="EmsStrategyTempResult">
        <include refid="selectEmsStrategyTempVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsStrategyTemp" parameterType="EmsStrategyTemp" useGeneratedKeys="true" keyProperty="id">
        insert into ems_strategy_temp
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">strategy_id,</if>
            <if test="templateName != null">template_name,</if>
            <if test="sdcLimit != null">sdc_limit,</if>
            <if test="sdcDown != null">sdc_down,</if>
            <if test="sdcUp != null">sdc_up,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="chargeDischargePower != null">charge_discharge_power,</if>
            <if test="chargeStatus != null">charge_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>
            <if test="siteId != null">site_id,</if>
            <if test="templateId != null">template_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">#{strategyId},</if>
            <if test="templateName != null">#{templateName},</if>
            <if test="sdcLimit != null">#{sdcLimit},</if>
            <if test="sdcDown != null">#{sdcDown},</if>
            <if test="sdcUp != null">#{sdcUp},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="chargeDischargePower != null">#{chargeDischargePower},</if>
            <if test="chargeStatus != null">#{chargeStatus},</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>
            <if test="siteId != null">#{siteId},</if>
            <if test="templateId != null">#{templateId},</if>
        </trim>
    </insert>

    <update id="updateEmsStrategyTemp" parameterType="EmsStrategyTemp">
        update ems_strategy_temp
        <trim prefix="SET" suffixOverrides=",">
            <if test="strategyId != null">strategy_id = #{strategyId},</if>
            <if test="templateName != null">template_name = #{templateName},</if>
            <if test="sdcLimit != null">sdc_limit = #{sdcLimit},</if>
            <if test="sdcDown != null">sdc_down = #{sdcDown},</if>
            <if test="sdcUp != null">sdc_up = #{sdcUp},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="chargeDischargePower != null">charge_discharge_power = #{chargeDischargePower},</if>
            <if test="chargeStatus != null">charge_status = #{chargeStatus},</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>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsStrategyTempById" parameterType="Long">
        delete from ems_strategy_temp where id = #{id}
    </delete>

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

    <select id="getTempNameList" resultType="Map">
        select DISTINCT template_id as templateId,template_name as templateName
        from ems_strategy_temp
        where site_id = #{siteId}
        and strategy_id = #{strategyId}
    </select>

    <select id="getStrategyTempByTempId" parameterType="String" resultType="int">
        select count(1) from ems_strategy_temp where template_id = #{templateId}
    </select>

    <select id="selectStrategyTempByTempId" parameterType="String" resultMap="EmsStrategyTempResult">
        <include refid="selectEmsStrategyTempVo"/>
        where template_id = #{templateId}
        order by start_time, end_time
    </select>

    <delete id="deleteTempByTempId" parameterType="String">
        delete from ems_strategy_temp where template_id = #{templateId}
    </delete>

    <select id="getTempListBySiteIdAndStrategyId" resultMap="EmsStrategyTempResult">
        <include refid="selectEmsStrategyTempVo"/>
        where site_id = #{siteId} and strategy_id = #{strategyId}
        order by start_time, end_time
    </select>

</mapper>