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

    <resultMap type="EmsStrategyCurve" id="EmsStrategyCurveResult">
        <result property="id"    column="id"    />
        <result property="strategyId"    column="strategy_id"    />
        <result property="taskNumber"    column="task_number"    />
        <result property="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="powerData"    column="power_data"    />
        <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="month"    column="month"    />
        <result property="isDelete"    column="isDelete"    />
    </resultMap>

    <sql id="selectEmsStrategyCurveVo">
        select id, strategy_id, task_number, start_date, end_date, power_data, create_by, create_time, update_by, update_time, remark, site_id, template_id, month, isDelete from ems_strategy_curve
    </sql>

    <select id="selectEmsStrategyCurveList" parameterType="EmsStrategyCurve" resultMap="EmsStrategyCurveResult">
        <include refid="selectEmsStrategyCurveVo"/>
        <where>
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="taskNumber != null "> and task_number = #{taskNumber}</if>
            <if test="startDate != null "> and start_date = #{startDate}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
            <if test="powerData != null  and powerData != ''"> and power_data = #{powerData}</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="month != null "> and month = #{month}</if>
            <if test="isDelete != null "> and isDelete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectEmsStrategyCurveById" parameterType="Long" resultMap="EmsStrategyCurveResult">
        <include refid="selectEmsStrategyCurveVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsStrategyCurve" parameterType="EmsStrategyCurve" useGeneratedKeys="true" keyProperty="id">
        insert into ems_strategy_curve
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">strategy_id,</if>
            <if test="taskNumber != null">task_number,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="powerData != null">power_data,</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="month != null">month,</if>
            <if test="isDelete != null">isDelete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">#{strategyId},</if>
            <if test="taskNumber != null">#{taskNumber},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="powerData != null">#{powerData},</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="month != null">#{month},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateEmsStrategyCurve" parameterType="EmsStrategyCurve">
        update ems_strategy_curve
        <trim prefix="SET" suffixOverrides=",">
            <if test="strategyId != null">strategy_id = #{strategyId},</if>
            <if test="taskNumber != null">task_number = #{taskNumber},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="powerData != null">power_data = #{powerData},</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="month != null">month = #{month},</if>
            <if test="isDelete != null">isDelete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsStrategyCurveById" parameterType="Long">
        delete from ems_strategy_curve where id = #{id}
    </delete>

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

    <select id="getTemplateCurve" parameterType="String" resultMap="EmsStrategyCurveResult">
        <include refid="selectEmsStrategyCurveVo"/>
        <where> template_id = #{templateId}
         and isDelete = 1
        </where>
    </select>

    <update id="physicalDeleteCurve">
        update ems_strategy_curve set isDelete = 0
        where site_id = #{siteId}
        and strategy_id = #{strategyId}
    </update>

    <update id="physicalDeleteByTemplateId">
        update ems_strategy_curve set isDelete = 0
        where template_id = #{templateId}
    </update>
</mapper>