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

    <resultMap type="EmsEnergyPriceConfig" id="EmsEnergyPriceConfigResult">
        <result property="id"    column="id"    />
        <result property="siteId"    column="site_id"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
        <result property="peak"    column="peak"    />
        <result property="high"    column="high"    />
        <result property="flat"    column="flat"    />
        <result property="valley"    column="valley"    />
        <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="selectEmsEnergyPriceConfigVo">
        select id, site_id, year, month, peak, high, flat, valley, create_by, create_time, update_by, update_time, remark from ems_energy_price_config
    </sql>

    <select id="selectEmsEnergyPriceConfigList" parameterType="EmsEnergyPriceConfig" resultMap="EmsEnergyPriceConfigResult">
        <include refid="selectEmsEnergyPriceConfigVo"/>
        <where>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="month != null  and month != ''"> and month = #{month}</if>
            <if test="peak != null "> and peak = #{peak}</if>
            <if test="high != null "> and high = #{high}</if>
            <if test="flat != null "> and flat = #{flat}</if>
            <if test="valley != null "> and valley = #{valley}</if>
        </where>
    </select>

    <select id="selectEmsEnergyPriceConfigById" parameterType="Long" resultMap="EmsEnergyPriceConfigResult">
        <include refid="selectEmsEnergyPriceConfigVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsEnergyPriceConfig" parameterType="EmsEnergyPriceConfig" useGeneratedKeys="true" keyProperty="id">
        insert into ems_energy_price_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="siteId != null">site_id,</if>
            <if test="year != null and year != ''">year,</if>
            <if test="month != null and month != ''">month,</if>
            <if test="peak != null">peak,</if>
            <if test="high != null">high,</if>
            <if test="flat != null">flat,</if>
            <if test="valley != null">valley,</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="siteId != null">#{siteId},</if>
            <if test="year != null and year != ''">#{year},</if>
            <if test="month != null and month != ''">#{month},</if>
            <if test="peak != null">#{peak},</if>
            <if test="high != null">#{high},</if>
            <if test="flat != null">#{flat},</if>
            <if test="valley != null">#{valley},</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="updateEmsEnergyPriceConfig" parameterType="EmsEnergyPriceConfig">
        update ems_energy_price_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="year != null and year != ''">year = #{year},</if>
            <if test="month != null and month != ''">month = #{month},</if>
            <if test="peak != null">peak = #{peak},</if>
            <if test="high != null">high = #{high},</if>
            <if test="flat != null">flat = #{flat},</if>
            <if test="valley != null">valley = #{valley},</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="deleteEmsEnergyPriceConfigById" parameterType="Long">
        delete from ems_energy_price_config where id = #{id}
    </delete>

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

    <select id="getCurrentYearConfigList"  resultMap="EmsEnergyPriceConfigResult">
        <include refid="selectEmsEnergyPriceConfigVo"/>
        <where>
            year = #{currentYear}
        <if test="siteId != null and siteId != ''">
            and site_id = #{siteId}
        </if>
        </where>
    </select>

    <insert id="insertOrUpdateEmsEnergyPriceConfig" parameterType="com.xzzn.ems.domain.EmsEnergyPriceConfig"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ems_energy_price_config (
            id,
            site_id,
            year,
            month,
            peak,
            high,
            flat,
            valley,
            create_by,
            create_time,
            update_by,
            update_time,
            remark
        ) VALUES (
                     #{id},
                     #{siteId},
                     #{year},
                     #{month},
                     #{peak},
                     #{high},
                     #{flat},
                     #{valley},
                     'system',
                     NOW(),
                     'system',
                     NOW(),
                     #{remark}
                 ) ON DUPLICATE KEY UPDATE
            peak = #{peak},
            high = #{high},
            flat = #{flat},
            valley = #{valley},
            update_time = NOW();
    </insert>

    <select id="getConfigListByYearAndMonth"  resultMap="EmsEnergyPriceConfigResult">
        <include refid="selectEmsEnergyPriceConfigVo"/>
        where site_id = #{siteId}
        and year = #{currentYear}
        and month = #{month}
    </select>

    <select id="getTimeRangeByDate"  resultType="com.xzzn.ems.domain.vo.EnergyPriceTimeRange">
        select t2.cost_type as costType,
               t2.start_time as startTime,
               t2.end_time as endTime
        from ems_energy_price_config t
        left join  ems_price_time_config t2 on t.id = t2.price_id
        where t.site_id = #{siteId}
        and t.`year` = #{currentYear}
        and t.`month` = #{currentMonth}
    </select>

    <select id="getAllSitePriceConfig"  resultMap="EmsEnergyPriceConfigResult">
        select t.*
        from ems_energy_price_config t
        LEFT JOIN ems_site_setting t2 on t.site_id = t2.site_id
        where t.`year` = #{currentYear}
          and t.`month` = #{currentMonth}
    </select>
    <select id="getConfigListByTimeFrame" resultType="com.xzzn.ems.domain.vo.EnergyPriceConfigVo">
        select
            epc.yearMonth as yearMonth,
            epc.peak,
            epc.high,
            epc.flat,
            epc.valley,
            eptc.start_time as startTime,
            eptc.end_time as endTime,
            eptc.cost_type as costType
        from
        (
        select
            id, site_id, peak, high, flat, valley,
            CONCAT(year, '-', LPAD(month, 2, '0')) as yearMonth,
            STR_TO_DATE(CONCAT(year, '-', month, '-01'), '%Y-%m-%d') as dateTime
        from ems_energy_price_config
        where 1 = 1
        <if test="siteId != null and siteId != ''">
            and site_id = #{siteId}
        </if>
        order by year, month
        ) epc
        left join ems_price_time_config eptc on eptc.price_id = epc.id
        <where>
            <if test="startDate != null and endDate != null">
                (
                    (#{startDate} between dateTime and LAST_DAY(dateTime))
                    or
                    ( #{endDate} between dateTime and LAST_DAY(dateTime))
                    or
                    (dateTime between #{startDate} and  #{endDate})
                )
            </if>
        </where>
        order by epc.yearMonth, eptc.start_time
    </select>
</mapper>