<?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="objType" column="obj_type"/>
        <result property="objCode" column="obj_code"/>
        <result property="objName" column="obj_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>

    <resultMap type="com.ruoyi.ems.model.ElecUseSta" id="elecUseResult">
        <result property="objCode" column="obj_code"/>
        <result property="objName" column="obj_name"/>
        <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.obj_type,
               u.obj_code,
               CASE
                   WHEN u.obj_type = 1 THEN a2.area_name
                   WHEN u.obj_type = 2 THEN f.facs_name
                   WHEN u.obj_type = 3 THEN d.device_name
               END as obj_name,
               f.facs_category,
               f.facs_subcategory,
               u.record_time,
               u.`date`,
               u.`time`,
               u.time_index,
               u.elec_quantity
        from adm_ems_obj_use_h u
                 LEFT JOIN adm_area a ON u.`area_code` = a.`area_code`
                 LEFT JOIN adm_area a2 ON u.`obj_code` = a2.`area_code`
                 LEFT JOIN adm_ems_facs f ON u.`obj_code` = f.`facs_code`
                 LEFT JOIN adm_ems_device d ON u.`obj_code` = d.`device_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="objType != null">and u.obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and u.obj_code = #{objCode}</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>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and u.`record_time` &gt;= #{startRecTime} and u.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        ORDER BY u.`date` DESC,
        u.`time` DESC,
        u.`time_index`
    </select>

    <select id="selectElecUseDayList" parameterType="com.ruoyi.ems.model.QueryIndex" resultMap="elecUseHResult">
        select
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            u.area_code,
            a.area_name,
            a.short_name as area_short_name,
        </if>
        <if test="objCode != null and objCode != ''">
            u.obj_code,
            CASE
              WHEN u.obj_type = 1 THEN a2.area_name
              WHEN u.obj_type = 2 THEN f.facs_name
              WHEN u.obj_type = 3 THEN d.device_name
            END as obj_name,
        </if>
        <if test="(facsCategory != null and facsCategory != '') or (facsSubCategory != null and facsSubCategory != '')">
            fa.facs_category,
            fa.facs_subcategory,
            <if test="objCode == null or objCode == ''">
                fs.name as obj_name,
            </if>
        </if>
        u.`date`,
        sum(COALESCE(u.elec_quantity,0)) elec_quantity
        from adm_ems_obj_use_h u
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            LEFT JOIN adm_area a ON u.`area_code` = a.`area_code`
        </if>
        <if test="(facsCategory != null  and facsCategory != '') or (facsSubCategory != null  and facsSubCategory != '')">
            LEFT JOIN adm_ems_facs fa ON u.`obj_code` = fa.`facs_code`
            <if test="objCode == null or objCode == ''">
                LEFT JOIN dim_ems_facs_subcategory fs ON fa.`facs_subcategory` = fs.`code`
            </if>
        </if>
        <if test="objCode != null and objCode != ''">
            LEFT JOIN adm_area a2 ON u.`obj_code` = a2.`area_code`
            LEFT JOIN adm_ems_facs f ON u.`obj_code` = f.`facs_code`
            LEFT JOIN adm_ems_device d ON u.`obj_code` = d.`device_code`
        </if>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and u.area_code = #{areaCode}</if>
            <if test="objType != null ">and u.obj_type = #{objType}</if>
            <if test="objCode != null and objCode != ''">and u.obj_code = #{objCode}</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime !=''">
                and u.`date` &gt;= #{startTime} and u.`date` &lt;= #{endTime}
            </if>
            <if test="facsCategory != null and facsCategory != ''">and fa.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">and fa.facs_subcategory = #{facsSubCategory}</if>
        </where>
        GROUP BY u.`date`
        ORDER BY u.`date`
    </select>

    <select id="selectElecUseHListSum" parameterType="com.ruoyi.ems.domain.ElecUseH" resultMap="elecUseHResult">
        select
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
              u.area_code,
              a.area_name,
              a.short_name as area_short_name,
            </if>
            <if test="objCode != null and objCode != ''">
              u.obj_code,
              CASE
                WHEN u.obj_type = 1 THEN a2.area_name
                WHEN u.obj_type = 2 THEN f.facs_name
                WHEN u.obj_type = 3 THEN d.device_name
              END as obj_name,
            </if>
            <if test="(facsCategory != null and facsCategory != '') or (facsSubCategory != null and facsSubCategory != '')">
              fa.facs_category,
              fa.facs_subcategory,
              <if test="objCode == null or objCode == ''">
                fs.name as obj_name,
              </if>
            </if>
              u.`record_time`,
              u.`date`,
              u.`time`,
              u.`time_index`,
              sum(COALESCE(u.elec_quantity,0)) elec_quantity
        from adm_ems_obj_use_h u
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
              LEFT JOIN adm_area a ON u.`area_code` = a.`area_code`
            </if>
            <if test="(facsCategory != null  and facsCategory != '') or (facsSubCategory != null  and facsSubCategory != '')">
              LEFT JOIN adm_ems_facs fa ON u.`obj_code` = fa.`facs_code`
              <if test="objCode == null or objCode == ''">
                LEFT JOIN dim_ems_facs_subcategory fs ON fa.`facs_subcategory` = fs.`code`
              </if>
            </if>
            <if test="objCode != null and objCode != ''">
              LEFT JOIN adm_area a2 ON u.`obj_code` = a2.`area_code`
              LEFT JOIN adm_ems_facs f ON u.`obj_code` = f.`facs_code`
              LEFT JOIN adm_ems_device d ON u.`obj_code` = d.`device_code`
            </if>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and u.area_code = #{areaCode}</if>
            <if test="objType != null ">and u.obj_type = #{objType}</if>
            <if test="objCode != null and objCode != ''">and u.obj_code = #{objCode}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and u.`record_time` &gt;= #{startRecTime} and u.`record_time` &lt;= #{endRecTime}
            </if>
            <if test="facsCategory != null and facsCategory != ''">and fa.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">and fa.facs_subcategory = #{facsSubCategory}</if>
        </where>
        GROUP BY
            u.`date`,
            u.`time_index`,
            u.`time`
        ORDER BY
            u.`date` DESC,
            u.`time` DESC,
            u.`time_index`
    </select>

    <select id="selectSumBySubCategory" parameterType="com.ruoyi.ems.domain.ElecUseH" resultMap="elecUseResult">
        select
          sc.`code` as obj_code, sc.`name` as obj_name, sum(t.elec_quantity) as elec_quantity
        from dim_ems_facs_subcategory sc
        left join
          ( select
              f.facs_subcategory, sum(u.elec_quantity) as elec_quantity
            from
              adm_ems_facs f
            left join adm_ems_obj_use_h u on f.facs_code = u.obj_code
            <where>
              f.facs_category = 'Z'
              and u.obj_type = 2
              <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and f.ref_area = #{areaCode}</if>
              <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                  and u.record_time &gt;= #{startRecTime} and u.record_time &lt;= #{endRecTime}
              </if>
            </where>
            GROUP BY f.facs_subcategory
          ) t on sc.`code` = t.facs_subcategory
        where sc.parent_code = 'Z'
        GROUP BY sc.`code`, sc.`name`;
    </select>

    <select id="selectSumByFacs" parameterType="com.ruoyi.ems.domain.ElecUseH" resultMap="elecUseResult">
        select
          f.facs_code as obj_code, f.facs_name as obj_name, sum(u.elec_quantity) as elec_quantity
        from
          adm_ems_facs f
          left join adm_ems_obj_use_h u on f.facs_code = u.obj_code
        <where>
            <if test="facsSubCategory != null and facsSubCategory != ''">and f.facs_subcategory = #{facsSubCategory}</if>
            and u.obj_type = 2
            <if test="objCode != null and objCode != ''">and f.facs_code = #{objCode}</if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and u.area_code = #{areaCode}</if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and u.record_time &gt;= #{startRecTime} and u.record_time &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY f.facs_code, f.facs_name
    </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_obj_use_h
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="objType != null">obj_type,</if>
            <if test="objCode != null  and objCode != ''">obj_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="objType != null">#{obj_type},</if>
            <if test="objCode != null  and objCode != ''">#{obj_code},</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_obj_use_h
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="objType != null">obj_type = #{objType},</if>
            <if test="objCode != null  and objCode != ''">obj_code = #{objCode},</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_obj_use_h
        where id = #{id}
    </delete>

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