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

    <resultMap type="com.ruoyi.ems.domain.ElecUseH" id="elecUseHResult">
        <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="elecQuantity" column="elec_quantity"/>
    </resultMap>

    <sql id="selectElecUseHVo">
        select u.id,
               u.area_code,
               a.area_name,
               a.short_name as area_short_name,
               u.facs_code,
               f.facs_name,
               f.facs_category,
               f.facs_subcategory,
               u.record_time,
               u.`date`,
               u.`time`,
               u.time_index,
               u.elec_quantity
        from adm_ems_facs_use_h u
                 LEFT JOIN adm_service_area a ON u.`area_code` = a.`area_code`
                 LEFT JOIN adm_ems_facs f ON u.`facs_code` = f.`facs_code`
    </sql>

    <select id="selectElecUseHList" parameterType="com.ruoyi.ems.domain.ElecUseH" resultMap="elecUseHResult">
        <include refid="selectElecUseHVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and u.area_code = #{areaCode}</if>
            <if test="facsCode != null  and facsCode != ''">and u.facs_code = #{facsCode}</if>
            <if test="facsCategory != null  and facsCategory != ''">and f.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null  and facsSubCategory != ''">and f.facs_subcategory = #{facsSubCategory}</if>
            <if test="date != null ">and u.`date` = #{date}</if>
            <if test="time != null ">and u.`time` = #{time}</if>
            <if test="timeIndex != null ">and u.time_index = #{timeIndex}</if>
            <if test="elecQuantity != null ">and u.elec_quantity = #{elecQuantity}</if>
        </where>
    </select>

    <select id="selectElecUseHById" parameterType="Long" resultMap="elecUseHResult">
        <include refid="selectElecUseHVo"/>
        where u.`id` = #{id}
    </select>

    <insert id="insertElecUseH" parameterType="com.ruoyi.ems.domain.ElecUseH" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_facs_use_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="elecQuantity != null">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="elecQuantity != null">#{elecQuantity},</if>
        </trim>
    </insert>

    <update id="updateElecUseH" parameterType="com.ruoyi.ems.domain.ElecUseH">
        update adm_ems_facs_use_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="elecQuantity != null">elec_quantity = #{elecQuantity},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElecUseHById" parameterType="Long">
        delete
        from adm_ems_facs_use_h
        where id = #{id}
    </delete>

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

    <select id="selectElecUseHByMonth" resultType="Map">
        SELECT area_code areaCode,
        DATE_FORMAT(date, '%Y-%m') AS dateIndex,
        SUM(elec_quantity) as elecQuantity
        FROM adm_ems_facs_use_h
        <where>
            <if test="areaCode != null and areaCode != ''">
                and area_code = #{areaCode}
            </if>
            <if test="date != null and date != ''">
                and DATE >= #{date}
            </if>
        </where>
        GROUP BY
        area_code,
        dateIndex
    </select>

    <select id="selectThisDayElecUse" resultType="Map">
        SELECT area_code areaCode,
        DATE_FORMAT(date, '%Y-%m-%d') AS dateIndex,
        SUM(elec_quantity) as elecQuantity
        FROM adm_ems_facs_use_h
        <where>
            <if test="areaCode != null and areaCode != ''">
                and area_code = #{areaCode}
            </if>
            <if test="date != null and date != ''">
                and DATE >= #{date}
            </if>
        </where>
        GROUP BY
        area_code,
        dateIndex
    </select>

    <select id="selectThisDayFacsCategoryElecUse" resultType="Map">
        SELECT subcategory.`name`,
        DATE_FORMAT(DATE, '%Y-%m-%d') AS dateIndex,
        SUM(elec_quantity) AS elecQuantity
        FROM adm_ems_facs_use_h facUse
        inner JOIN adm_ems_facs facs ON facUse.facs_code = facs.facs_code
        <if test="areaCode != null and areaCode != ''">
            and facUse.area_code = #{areaCode}
        </if>
        LEFT JOIN dim_ems_facs_subcategory subcategory ON facs.facs_subcategory = subcategory.`code`
        AND subcategory.parent_code = 'Z'
        WHERE
        DATE >= #{date}
        GROUP BY
        subcategory.`name`,
        dateIndex
    </select>

    <select id="selectMonthElecUse" resultType="Map">
        SELECT area_code                     areaCode,
               DATE_FORMAT(date, '%Y-%m') AS dateIndex,
               SUM(elec_quantity)         as elecQuantity
        FROM adm_ems_facs_use_h
        WHERE DATE >= #{date}
        <if test="areaCode != null and areaCode != ''">
            and area_code = #{areaCode}
        </if>
        GROUP BY
            area_code,
            dateIndex
    </select>
</mapper>
