<?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.ruoyi.ems.mapper.EmsElecStoreHMapper">

    <resultMap type="com.ruoyi.ems.domain.ElecStoreH" id="StoreHResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="areaShortName" column="area_short_name"/>
        <result property="facsCode" column="facs_code"/>
        <result property="facsName" column="facs_name"/>
        <result property="recordTime" column="record_time"/>
        <result property="date" column="date"/>
        <result property="time" column="time"/>
        <result property="timeIndex" column="time_index"/>
        <result property="chargeElecQuantity" column="charge_elec_quantity"/>
        <result property="dischargeElecQuantity" column="discharge_elec_quantity"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.model.ElecStoreSta" id="StoreHSumResult">
        <result property="time" column="time"/>
        <result property="timeIndex" column="time_index"/>
        <result property="chargeElecQuantity" column="charge_elec_quantity"/>
        <result property="dischargeElecQuantity" column="discharge_elec_quantity"/>
    </resultMap>

    <sql id="selectStoreHVo">
        SELECT st.id,
               st.area_code,
               area.area_name,
               area.short_name as area_short_name,
               facs.facs_name,
               st.facs_code,
               st.`record_time`,
               st.`date`,
               st.`time`,
               st.time_index,
               st.charge_elec_quantity,
               st.discharge_elec_quantity
        FROM adm_ems_elec_store_h st
                 LEFT JOIN adm_ems_facs facs ON st.facs_code = facs.facs_code
                 LEFT JOIN adm_area area ON st.area_code = area.area_code
    </sql>

    <select id="selectStoreHList" parameterType="com.ruoyi.ems.domain.ElecStoreH" resultMap="StoreHResult">
        <include refid="selectStoreHVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and st.area_code = #{areaCode}</if>
            <if test="facsCode != null  and facsCode != ''">and st.facs_code = #{facsCode}</if>
            <if test="date != null ">and st.`date` = #{date}</if>
            <if test="chargeElecQuantity != null ">and st.charge_elec_quantity = #{chargeElecQuantity}</if>
            <if test="dischargeElecQuantity != null ">and st.discharge_elec_quantity = #{dischargeElecQuantity}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and st.`record_time` &gt;= #{startRecTime} and st.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        ORDER BY st.`date` DESC,
        st.`time` DESC,
        st.`time_index`
    </select>
    <select id="selectStoreHByDataRange" parameterType="com.ruoyi.ems.domain.ElecStoreH" resultMap="StoreHResult">
        SELECT
        sum(st.charge_elec_quantity) charge_elec_quantity,
        sum(st.discharge_elec_quantity) discharge_elec_quantity
        FROM adm_ems_elec_store_h st
        <where>
            <if test="areaCode != null  and areaCode != ''  and areaCode != '-1'">and st.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime !='' ">and st.`date` like concat(#{startRecTime},'%')</if>
        </where>
    </select>
    <select id="selectStorageIndex" resultType="EmsElecStoreIndex">
        SELECT
        SUM(aeesi.current_capacity) current_capacity
        FROM (SELECT MAX(id) id
        FROM adm_ems_elec_store_index storeIndex
        <where>
            <if test="areaCode != null  and areaCode != ''  and areaCode != '-1'">and storeIndex.area_code =
                #{areaCode}
            </if>
            <if test="startRecTime != null and startRecTime !='' ">and storeIndex.`date` like concat(#{startRecTime},'%')</if>
        </where>
        group by facs_code) AS latest_records
        inner join adm_ems_elec_store_index aeesi on latest_records.id = aeesi.id
    </select>
    <select id="selectStoreHAllArea" parameterType="com.ruoyi.ems.domain.ElecStoreH" resultMap="StoreHResult">
        SELECT facCategory.`code` facs_code,
        facCategory.`name` facs_name,
        st.`date`,
        st.`time`,
        st.`time_index`,
        sum(COALESCE(st.`charge_elec_quantity`, 0)) charge_elec_quantity,
        sum(COALESCE(st.`discharge_elec_quantity`, 0)) discharge_elec_quantity
        FROM adm_ems_elec_store_h st
        inner JOIN adm_ems_facs f ON st.`facs_code` = f.`facs_code`
        inner JOIN dim_ems_facs_category facCategory ON f.facs_category = facCategory.`code`
        <where>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and st.`record_time` &gt;= #{startRecTime} and st.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY facCategory.`code`,
        st.DATE,
        st.time_index,
        st.TIME
        ORDER BY st.`date` DESC,
        st.`time` DESC,
        st.`time_index`
    </select>

    <select id="selectStoreDayList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="StoreHResult">
        SELECT
        <if test="facsSubCategory != null and facsSubCategory !=''">
            sc.`name` as facs_name,
        </if>
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            s.area_code,
        </if>
        s.`date`,
        sum(COALESCE(s.`charge_elec_quantity`, 0)) charge_elec_quantity,
        sum(COALESCE(s.`discharge_elec_quantity`, 0)) discharge_elec_quantity
        FROM adm_ems_elec_store_h s
        <if test="facsSubCategory != null and facsSubCategory !=''">
            LEFT JOIN adm_ems_facs f ON s.`facs_code` = f.`facs_code`
            LEFT JOIN dim_ems_facs_subcategory sc ON f.facs_subcategory = sc.`code`
        </if>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and s.area_code = #{areaCode}
            </if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and s.`date` &gt;= #{startRecTime} and s.`date` &lt;= #{endRecTime}
            </if>
            <if test="facsSubCategory != null and facsSubCategory !=''">
                and f.facs_subcategory = #{facsSubCategory}
            </if>
        </where>
        GROUP BY s.`date`
        ORDER BY s.`date`
    </select>

    <select id="selectStoreHById" parameterType="Long" resultMap="StoreHResult">
        <include refid="selectStoreHVo"/>
        where st.id = #{id}
    </select>

    <select id="selectStoreHSumDate" parameterType="java.lang.String" resultMap="StoreHSumResult">
        SELECT st.`date`                       as `time`,
               sum(st.charge_elec_quantity)    as charge_elec_quantity,
               sum(st.discharge_elec_quantity) as discharge_elec_quantity
        FROM adm_ems_elec_store_h st
        where st.`date` = #{date}
        group by st.`date`
    </select>

    <select id="selectStoreHSumHour" parameterType="java.lang.String" resultMap="StoreHSumResult">
        SELECT st.`time`,
               st.`time_index`,
               sum(st.charge_elec_quantity)    as charge_elec_quantity,
               sum(st.discharge_elec_quantity) as discharge_elec_quantity
        FROM adm_ems_elec_store_h st
        where st.`date` = #{date}
        group by st.`time`
    </select>

    <insert id="insertStoreH" parameterType="com.ruoyi.ems.domain.ElecStoreH" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_store_h
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="facsCode != null and facsCode != ''">facs_code,</if>
            <if test="recordTime != null">record_time,</if>
            <if test="date != null">`date`,</if>
            <if test="time != null">`time`,</if>
            <if test="timeIndex != null">time_index,</if>
            <if test="chargeElecQuantity != null">charge_elec_quantity,</if>
            <if test="dischargeElecQuantity != null">discharge_elec_quantity,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">#{facsCode},</if>
            <if test="recordTime != null">#{recordTime},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="timeIndex != null">#{timeIndex},</if>
            <if test="chargeElecQuantity != null">#{chargeElecQuantity},</if>
            <if test="dischargeElecQuantity != null">#{dischargeElecQuantity},</if>
        </trim>
    </insert>

    <update id="updateStoreH" parameterType="com.ruoyi.ems.domain.ElecStoreH">
        update adm_ems_elec_store_h
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">facs_code = #{facsCode},</if>
            <if test="recordTime != null">record_time = #{recordTime},</if>
            <if test="date != null">`date` = #{date},</if>
            <if test="time != null">`time` = #{time},</if>
            <if test="timeIndex != null">time_index = #{timeIndex},</if>
            <if test="chargeElecQuantity != null">charge_elec_quantity = #{chargeElecQuantity},</if>
            <if test="dischargeElecQuantity != null">discharge_elec_quantity = #{dischargeElecQuantity},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStoreHById" parameterType="Long">
        delete
        from adm_ems_elec_store_h
        where id = #{id}
    </delete>

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