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

    <resultMap type="com.ruoyi.ems.domain.ElecPgSupplyH" id="PgSupplyHResult">
        <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="meterType"    column="meter_type"    />
        <result property="meterUnitPrice"    column="meter_unit_price"    />
        <result property="useElecQuantity"    column="use_elec_quantity"    />
        <result property="useElecCost"    column="use_elec_cost"    />
    </resultMap>

    <sql id="selectPgSupplyHVo">
        select
            pg.`id`, pg.`area_code`, a.`area_name`, a.`short_name` as area_short_name, pg.`facs_code`, f.`facs_name`, pg.`record_time`, pg.`date`, pg.`time`, pg.`time_index`, pg.`meter_type`, pg.`meter_unit_price`, pg.`use_elec_quantity`, pg.`use_elec_cost`
        from adm_ems_pg_supply_h pg
            LEFT JOIN adm_area a ON pg.`area_code` = a.`area_code`
            LEFT JOIN adm_ems_facs f ON pg.`facs_code` = f.`facs_code`
    </sql>

    <select id="selectPgSupplyHList" parameterType="com.ruoyi.ems.domain.ElecPgSupplyH" resultMap="PgSupplyHResult">
        <include refid="selectPgSupplyHVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and pg.`area_code` = #{areaCode}</if>
            <if test="facsCode != null  and facsCode != ''">and pg.`facs_code` = #{facsCode}</if>
            <if test="date != null ">and pg.`date` = #{date}</if>
            <if test="time != null ">and pg.`time` = #{time}</if>
            <if test="timeIndex != null ">and pg.`time_index` = #{timeIndex}</if>
            <if test="meterType != null ">and pg.`meter_type` = #{meterType}</if>
            <if test="meterUnitPrice != null ">and pg.`meter_unit_price` = #{meterUnitPrice}</if>
            <if test="useElecQuantity != null ">and pg.`use_elec_quantity` = #{useElecQuantity}</if>
            <if test="useElecCost != null ">and pg.`use_elec_cost` = #{useElecCost}</if>
            <if test="startRecTime != null  and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and pg.`record_time` &gt;= #{startRecTime} and pg.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        ORDER BY pg.`date` DESC,
        pg.`time` DESC,
        pg.`time_index`
    </select>

    <select id="selectPgSupplyHAllArea" parameterType="com.ruoyi.ems.domain.ElecPgSupplyH" resultMap="PgSupplyHResult">
        SELECT facCategory.`code` facs_code,
        facCategory.`name` facs_name,
        pg.`date`,
        pg.`time`,
        pg.`time_index`,
        pg.`meter_type`,
        pg.`meter_unit_price`,
        sum(COALESCE(pg.`use_elec_quantity`, 0)) use_elec_quantity,
        sum(COALESCE(pg.`use_elec_cost`, 0)) use_elec_cost
        FROM adm_ems_pg_supply_h pg
        inner JOIN adm_ems_facs f ON pg.`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 pg.`record_time` &gt;= #{startRecTime} and pg.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY facCategory.`code`,
        pg.DATE,
        pg.time_index,
        pg.TIME
        ORDER BY pg.`date` DESC,
        pg.`time` DESC,
        pg.`time_index`
    </select>

    <select id="calThisDaySupply" parameterType="String" resultType="Map">
        SELECT sum(
                       COALESCE(CAST(use_elec_cost AS DECIMAL(10, 2)), 0))     cost,
               sum(
                       COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) quantity
        FROM adm_ems_pg_supply_h
        WHERE
            DATE = #{thisDay}
        GROUP BY
            DATE
    </select>

    <select id="calThisDaySupplyByH" parameterType="String" resultType="Map">
        SELECT time_index                                                      timeIndex,
               sum(
                       COALESCE(CAST(use_elec_cost AS DECIMAL(10, 2)), 0))     cost,
               sum(
                       COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) quantity
        FROM adm_ems_pg_supply_h
        WHERE
            DATE = #{thisDay}
        GROUP BY
            DATE, time_index
    </select>

    <select id="calDaySupplyByH" parameterType="com.ruoyi.ems.model.QueryMeter" resultType="ElecPgSupplyH">
        SELECT
        sum(
        COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) use_elec_quantity,
        sum(
        COALESCE(CAST(use_elec_cost AS DECIMAL(10, 2)), 0)) use_elec_cost,
        `date`
        FROM
        adm_ems_pg_supply_h pg
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and pg.area_code = #{areaCode}
            </if>
            <if test="date != null">and pg.`date` = #{date}</if>
        </where>
    </select>

    <select id="calMonthSupplyByH" parameterType="ElecPgSupplyH" resultType="ElecPgSupplyH">
        SELECT
        sum(
        COALESCE(CAST(use_elec_quantity AS DECIMAL(10, 2)), 0)) use_elec_quantity,
        sum(
        COALESCE(CAST(use_elec_cost AS DECIMAL(10, 2)), 0)) use_elec_cost,
        DATE_FORMAT(date, '%Y-%m') AS startRecTime
        FROM
        adm_ems_pg_supply_h pg
        <where>
            <if test="startRecTime != null and startRecTime != ''">and `date` &gt;= #{startRecTime}</if>
            <if test="endRecTime != null and endRecTime != ''">and `date` &lt;= #{endRecTime}</if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and pg.area_code = #{areaCode}
            </if>
        </where>
        group by
        startRecTime
    </select>

    <select id="selectPgSupplyH" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="PgSupplyHResult">
        <include refid="selectPgSupplyHVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''"> and pg.`area_code` = #{areaCode}</if>
            <if test="startTime != null  and startTime != '' and endTime != null and endTime !=''">
             and pg.`record_time` &gt;= #{startRecTime} and pg.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
    </select>

    <select id="selectPgSupplyHById" parameterType="Long" resultMap="PgSupplyHResult">
        <include refid="selectPgSupplyHVo"/>
        where pg.`id` = #{id}
    </select>

    <insert id="insertPgSupplyH" parameterType="com.ruoyi.ems.domain.ElecPgSupplyH" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_pg_supply_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 and recordTime != ''">record_time,</if>
            <if test="date != null">`date`,</if>
            <if test="time != null">`time`,</if>
            <if test="timeIndex != null">time_index,</if>
            <if test="meterType != null">meter_type,</if>
            <if test="meterUnitPrice != null">meter_unit_price,</if>
            <if test="useElecQuantity != null">use_elec_quantity,</if>
            <if test="useElecCost != null">use_elec_cost,</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="meterType != null">#{meterType},</if>
            <if test="meterUnitPrice != null">#{meterUnitPrice},</if>
            <if test="useElecQuantity != null">#{useElecQuantity},</if>
            <if test="useElecCost != null">#{useElecCost},</if>
         </trim>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into adm_ems_pg_supply_h (area_code, facs_code, record_time, `date`, `time`, time_index, meter_type, meter_unit_price, use_elec_quantity, use_elec_cost)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.areaCode}, #{item.facsCode}, #{item.recordTime}, #{item.date}, #{item.time}, #{item.timeIndex},
            #{item.meterType}, #{item.meterUnitPrice}, #{item.useElecQuantity}, #{item.useElecCost})
        </foreach>
    </insert>

    <update id="updatePgSupplyH" parameterType="com.ruoyi.ems.domain.ElecPgSupplyH">
        update adm_ems_pg_supply_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 and recordTime != ''">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="meterType != null">meter_type = #{meterType},</if>
            <if test="meterUnitPrice != null">meter_unit_price = #{meterUnitPrice},</if>
            <if test="useElecQuantity != null">use_elec_quantity = #{useElecQuantity},</if>
            <if test="useElecCost != null">use_elec_cost = #{useElecCost},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePgSupplyHById" parameterType="Long">
        delete from adm_ems_pg_supply_h where id = #{id}
    </delete>

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