<?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.EmsStrategyLogMapper">
    
    <resultMap type="EmsStrategyLog" id="EmsStrategyLogResult">
        <result property="id"    column="id"    />
        <result property="strategyId"    column="strategy_id"    />
        <result property="templateId"    column="template_id"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceId"    column="device_id"    />
        <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="executionDate"    column="execution_date"    />
        <result property="antiReverse"    column="anti_reverse"    />
        <result property="powerDownType"    column="power_down_type"    />
        <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"    />
    </resultMap>

    <sql id="selectEmsStrategyLogVo">
        select id, strategy_id, template_id, site_id, device_id, start_time, end_time, charge_discharge_power, charge_status, execution_date, anti_reverse, power_down_type, create_by, create_time, update_by, update_time, remark from ems_strategy_log
    </sql>

    <select id="selectEmsStrategyLogList" parameterType="EmsStrategyLog" resultMap="EmsStrategyLogResult">
        <include refid="selectEmsStrategyLogVo"/>
        <where>  
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="templateId != null  and templateId != ''"> and template_id = #{templateId}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="startTime != null "> and date_format(start_time, '%H:%i:%s') = date_format(#{startTime}, '%H:%i:%s')</if>
            <if test="endTime != null "> and date_format(end_time, '%H:%i:%s') = date_format(#{endTime}, '%H:%i:%s')</if>
            <if test="chargeDischargePower != null "> and charge_discharge_power = #{chargeDischargePower}</if>
            <if test="chargeStatus != null  and chargeStatus != ''"> and charge_status = #{chargeStatus}</if>
            <if test="executionDate != null "> and execution_date = date_format(#{executionDate}, '%Y-%m-%d')</if>
            <if test="antiReverse != null "> and anti_reverse = #{antiReverse}</if>
            <if test="powerDownType != null "> and power_down_type = #{powerDownType}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectEmsStrategyLogById" parameterType="Long" resultMap="EmsStrategyLogResult">
        <include refid="selectEmsStrategyLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsStrategyLog" parameterType="EmsStrategyLog" useGeneratedKeys="true" keyProperty="id">
        insert into ems_strategy_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">strategy_id,</if>
            <if test="templateId != null">template_id,</if>
            <if test="siteId != null">site_id,</if>
            <if test="deviceId != null">device_id,</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="executionDate != null">execution_date,</if>
            <if test="antiReverse != null">anti_reverse,</if>
            <if test="powerDownType != null">power_down_type,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="strategyId != null">#{strategyId},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceId != null">#{deviceId},</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="executionDate != null">#{executionDate},</if>
            <if test="antiReverse != null">#{antiReverse},</if>
            <if test="powerDownType != null">#{powerDownType},</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>
         </trim>
    </insert>

    <update id="updateEmsStrategyLog" parameterType="EmsStrategyLog">
        update ems_strategy_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="strategyId != null">strategy_id = #{strategyId},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</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="executionDate != null">execution_date = #{executionDate},</if>
            <if test="antiReverse != null">anti_reverse = #{antiReverse},</if>
            <if test="powerDownType != null">power_down_type = #{powerDownType},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsStrategyLogById" parameterType="Long">
        delete from ems_strategy_log where id = #{id}
    </delete>

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

    <select id="getLastStrategyLog" parameterType="EmsStrategyLog" resultMap="EmsStrategyLogResult">
        <include refid="selectEmsStrategyLogVo"/>
        <where>
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="templateId != null  and templateId != ''"> and template_id = #{templateId}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="startTime != null ">  and date_format(start_time, '%H:%i:%s') = date_format(#{startTime}, '%H:%i:%s')</if>
            <if test="endTime != null "> and date_format(end_time, '%H:%i:%s') = date_format(#{endTime}, '%H:%i:%s')</if>
            <if test="chargeDischargePower != null "> and charge_discharge_power = #{chargeDischargePower}</if>
            <if test="chargeStatus != null  and chargeStatus != ''"> and charge_status = #{chargeStatus}</if>
            <if test="executionDate != null "> and execution_date = date_format(#{executionDate}, '%Y-%m-%d')</if>
            <if test="antiReverse != null "> and anti_reverse = #{antiReverse}</if>
            <if test="powerDownType != null "> and power_down_type = #{powerDownType}</if>
        </where>
        order by create_time desc
        limit 1
    </select>
</mapper>