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

    <resultMap type="EmsStrategyTimeConfig" id="EmsStrategyTimeConfigResult">
        <result property="id"    column="id"    />
        <result property="strategyId"    column="strategy_id"    />
        <result property="month"    column="month"    />
        <result property="chargeDischargeMode"    column="charge_discharge_mode"    />
        <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"    />
        <result property="isPost"    column="isPost"    />
    </resultMap>

    <sql id="selectEmsStrategyTimeConfigVo">
        select id, strategy_id, month, charge_discharge_mode, create_by, create_time, update_by, update_time, remark, site_id, template_id, isPost from ems_strategy_time_config
    </sql>

    <select id="selectEmsStrategyTimeConfigList" parameterType="EmsStrategyTimeConfig" resultMap="EmsStrategyTimeConfigResult">
        <include refid="selectEmsStrategyTimeConfigVo"/>
        <where>
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="month != null "> and month = #{month}</if>
            <if test="chargeDischargeMode != null  and chargeDischargeMode != ''"> and charge_discharge_mode = #{chargeDischargeMode}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="templateId != null  and templateId != ''"> and template_id = #{templateId}</if>
            <if test="isPost != null "> and isPost = #{isPost}</if>
        </where>
    </select>

    <select id="selectEmsStrategyTimeConfigById" parameterType="Long" resultMap="EmsStrategyTimeConfigResult">
        <include refid="selectEmsStrategyTimeConfigVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsStrategyTimeConfig" parameterType="EmsStrategyTimeConfig" useGeneratedKeys="true" keyProperty="id">
        insert into ems_strategy_time_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">strategy_id,</if>
            <if test="month != null">month,</if>
            <if test="chargeDischargeMode != null">charge_discharge_mode,</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>
            <if test="isPost != null">isPost,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">#{strategyId},</if>
            <if test="month != null">#{month},</if>
            <if test="chargeDischargeMode != null">#{chargeDischargeMode},</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>
            <if test="isPost != null">#{isPost},</if>
        </trim>
    </insert>

    <update id="updateEmsStrategyTimeConfig" parameterType="EmsStrategyTimeConfig">
        update ems_strategy_time_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="strategyId != null">strategy_id = #{strategyId},</if>
            <if test="month != null">month = #{month},</if>
            <if test="chargeDischargeMode != null">charge_discharge_mode = #{chargeDischargeMode},</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>
            <if test="isPost != null">isPost = #{isPost},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsStrategyTimeConfigById" parameterType="Long">
        delete from ems_strategy_time_config where id = #{id}
    </delete>

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

    <select id="getStrategyTimeList" parameterType="EmsStrategyTimeConfig" resultType="com.xzzn.ems.domain.vo.StrategyTimeConfigVo">
        select  distinct t.id,
                t.month,
                t.charge_discharge_mode as chargeDischargeMode,
                t.template_id as templateId,
                temp.template_name as templateName,
                t.site_id as siteId,
                t.strategy_id as strategyId
        from ems_strategy_time_config t
         LEFT JOIN ems_strategy_temp temp on t.template_id = temp.template_id
        where t.site_id = #{siteId}
        and t.strategy_id = #{strategyId}
        order by month
    </select>

    <update id="cleanTemplateId" parameterType="String">
        update ems_strategy_time_config  set template_id = '' where template_id = #{templateId}
    </update>

    <select id="getAllTimeConfigByTempId" parameterType="String" resultMap="EmsStrategyTimeConfigResult">
        <include refid="selectEmsStrategyTimeConfigVo"/>
        where template_id = #{templateId}
        and isPost = 1
    </select>

    <update id="updateTimeConfigWaitingPost" parameterType="String">
        update ems_strategy_time_config  set isPost = 1 where template_id = #{templateId}
    </update>

    <select id="getExistTimeConfig" resultMap="EmsStrategyTimeConfigResult">
        <include refid="selectEmsStrategyTimeConfigVo"/>
        where  site_id = #{siteId}
        and strategy_id = #{strategyId}
        and month = #{month}
    </select>

    <select id="getTimeConfigByTempIdAndMonth" resultType="com.xzzn.ems.domain.EmsStrategyTimeConfig">
        <include refid="selectEmsStrategyTimeConfigVo"/>
        where template_id = #{templateId}
        and month = #{month}
    </select>
</mapper>