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

    <resultMap type="EmsPriceTimeConfig" id="EmsPriceTimeConfigResult">
        <result property="id"    column="id"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="costType"    column="cost_type"    />
        <result property="priceId"    column="price_id"    />
        <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="selectEmsPriceTimeConfigVo">
        select id, start_time, end_time, cost_type, price_id, create_by, create_time, update_by, update_time, remark from ems_price_time_config
    </sql>

    <select id="selectEmsPriceTimeConfigList" parameterType="EmsPriceTimeConfig" resultMap="EmsPriceTimeConfigResult">
        <include refid="selectEmsPriceTimeConfigVo"/>
        <where>
            <if test="startTime != null  and startTime != ''"> and start_time = #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and end_time = #{endTime}</if>
            <if test="costType != null  and costType != ''"> and cost_type = #{costType}</if>
            <if test="priceId != null "> and price_id = #{priceId}</if>
        </where>
    </select>

    <select id="selectEmsPriceTimeConfigById" parameterType="Long" resultMap="EmsPriceTimeConfigResult">
        <include refid="selectEmsPriceTimeConfigVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsPriceTimeConfig" parameterType="EmsPriceTimeConfig" useGeneratedKeys="true" keyProperty="id">
        insert into ems_price_time_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="startTime != null and startTime != ''">start_time,</if>
            <if test="endTime != null and endTime != ''">end_time,</if>
            <if test="costType != null and costType != ''">cost_type,</if>
            <if test="priceId != null">price_id,</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="startTime != null and startTime != ''">#{startTime},</if>
            <if test="endTime != null and endTime != ''">#{endTime},</if>
            <if test="costType != null and costType != ''">#{costType},</if>
            <if test="priceId != null">#{priceId},</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="updateEmsPriceTimeConfig" parameterType="EmsPriceTimeConfig">
        update ems_price_time_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="startTime != null and startTime != ''">start_time = #{startTime},</if>
            <if test="endTime != null and endTime != ''">end_time = #{endTime},</if>
            <if test="costType != null and costType != ''">cost_type = #{costType},</if>
            <if test="priceId != null">price_id = #{priceId},</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="deleteEmsPriceTimeConfigById" parameterType="Long">
        delete from ems_price_time_config where id = #{id}
    </delete>

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

    <select id="getTimeRangeByPriceId"  resultType="com.xzzn.ems.domain.vo.EnergyPriceTimeRange">
        Select start_time as startTime,
               end_time  as endTime,
               cost_type as costType
        from ems_price_time_config
        where price_id = #{priceId}
    </select>


    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO ems_price_time_config (
        start_time, end_time, cost_type, price_id, create_by, create_time, update_by, update_time, remark
        ) VALUES
        <!-- 遍历集合生成多条记录，注意separator和括号闭合 -->
        <foreach collection="list" item="item" separator=",">
            (
            #{item.startTime},
            #{item.endTime},
            #{item.costType},
            #{item.priceId},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.remark}
            )
        </foreach>
    </insert>

    <delete id="deleteTimeRangeByPriceId" parameterType="Long">
        delete from ems_price_time_config  where price_id in
        <foreach item="priceId" collection="array" open="(" separator="," close=")">
            #{priceId}
        </foreach>
    </delete>
</mapper>