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

    <resultMap type="com.ruoyi.ems.domain.ElecMeterH" id="ElecMeterHResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_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"/>
        <result property="meterType" column="meter_type"/>
        <result property="meterUnitPrice" column="meter_unit_price"/>
        <result property="useElecCost" column="use_elec_cost"/>
        <result property="meterReading" column="meter_reading"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.EnergyMeter" id="ElecMeterResult">
        <result property="areaCode" column="area_code"/>
        <result property="objType" column="obj_type"/>
        <result property="objCode" column="obj_code"/>
        <result property="objName" column="obj_name"/>
        <result property="quantity" column="elec_quantity"/>
        <result property="useCost" column="use_elec_cost"/>
    </resultMap>

    <sql id="selectElecMeterHVo">
        select id,
               area_code,
               device_code,
               record_time,
               `date`,
               `time`,
               time_index,
               elec_quantity,
               meter_type,
               meter_unit_price,
               use_elec_cost,
               meter_reading,
               create_time
        from adm_elec_meter_h
    </sql>

    <select id="selectElecMeterDayList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterHResult">
        SELECT
        <if test="areaCode != null and areaCode !='' and areaCode !='-1'">
            h.`area_code`,
        </if>
        h.`date`,
        sum(COALESCE(h.elec_quantity,0)) as elec_quantity,
        sum(COALESCE(h.use_elec_cost,0)) as use_elec_cost
        FROM adm_elec_meter_h h
            LEFT JOIN adm_area area on area.area_code = h.area_code
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and h.area_code = #{areaCode}
            </if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and h.`date` &gt;= #{startRecTime} and h.`date` &lt;= #{endRecTime}
            </if>
            and h.device_code in
            <foreach item="item" index="index" collection="deviceCodes" open="(" separator=", " close=")">
                #{item}
            </foreach>
        </where>
        GROUP BY h.`date`
        ORDER BY h.`date`
    </select>

    <select id="selectLatelyItem" parameterType="java.lang.String" resultMap="ElecMeterHResult">
        <include refid="selectElecMeterHVo"/>
        where device_code = #{deviceCode}
        ORDER BY record_time desc limit 1
    </select>

    <select id="selectElecDayAvg" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterResult">
        select
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            area_code,
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            device_code,
        </if>
        SUM(elec_quantity) / COUNT(DISTINCT date) AS elec_quantity,
        SUM(use_elec_cost) / COUNT(DISTINCT date) AS use_elec_cost
        from adm_elec_meter_h h
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                and h.area_code = #{areaCode}
            </if>
            <if test="deviceCode != null and deviceCode != ''">
                and h.device_code = #{deviceCode}
            </if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime != ''">
                and h.`date` &gt;= #{startRecTime} and h.`date` &lt;= #{endRecTime}
            </if>
        </where>
    </select>

    <insert id="insertElecMeterH" parameterType="com.ruoyi.ems.domain.ElecMeterH" useGeneratedKeys="true"
            keyProperty="id">
        insert into adm_elec_meter_h
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="deviceCode != null and deviceCode != ''">device_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>
            <if test="meterType != null">meter_type,</if>
            <if test="meterUnitPrice != null">meter_unit_price,</if>
            <if test="useElecCost != null">use_elec_cost,</if>
            <if test="meterReading != null">meter_reading,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</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>
            <if test="meterType != null">#{meterType},</if>
            <if test="meterUnitPrice != null">#{meterUnitPrice},</if>
            <if test="meterReading != null">#{meterReading},</if>
        </trim>
    </insert>

    <insert id="insertRelBatch" parameterType="java.util.List">
        insert into adm_co_charging_config_rel (price_cfg_id, area_code)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.priceCfgId}, #{item.areaCode})
        </foreach>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into adm_elec_meter_h (area_code, device_code, record_time, `date`, `time`, time_index, elec_quantity,
        meter_type, meter_unit_price, use_elec_cost, meter_reading)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.areaCode}, #{item.deviceCode}, #{item.recordTime}, #{item.date}, #{item.time}, #{item.timeIndex},
            #{item.elecQuantity}, #{item.meterType}, #{item.meterUnitPrice}, #{item.useElecCost}, #{item.meterReading})
        </foreach>
    </insert>

    <update id="updateElecMeterH" parameterType="com.ruoyi.ems.domain.ElecMeterH">
        update adm_elec_meter_h
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</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>
            <if test="meterType != null">meter_type = #{meterType},</if>
            <if test="meterUnitPrice != null">meter_unit_price = #{meterUnitPrice},</if>
            <if test="useElecCost != null">use_elec_cost = #{useElecCost},</if>
            <if test="meterReading != null">meter_reading = #{meterReading},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElecMeterHByDate" parameterType="String">
        delete
        from adm_elec_meter_h
        where `date` = #{date}
    </delete>


    <select id="qryElecMeterByDate" parameterType="com.ruoyi.ems.domain.ElecMeterH" resultMap="ElecMeterResult">
        SELECT sum(elec_quantity) elec_quantity,
        SUM(use_elec_cost) use_elec_cost
        from adm_elec_meter_h aemh
        where `date` like concat(#{startRecTime}, '%')
        <if test="areaCode != null and areaCode != '' and areaCode !='-1'">and aemh.area_code = #{areaCode}</if>
    </select>

    <select id="qryTimeIndexElecMeterByDay" resultMap="ElecMeterHResult">
        SELECT sum(use_elec_cost) use_elec_cost,
               time_index,
               `date`
        from adm_elec_meter_h aemh
        where `date` >= #{dataRange}
        GROUP by `date`, time_index
        order by `date`, time_index
    </select>
    <select id="qryDateElecMeterByDate" resultMap="ElecMeterHResult">
        SELECT sum(use_elec_cost) use_elec_cost,
               `date`
        from adm_elec_meter_h aemh
        where `date` >= #{dataRange}
        GROUP by `date`
        order by `date` asc
    </select>

    <select id="qryDateElecMeterByYear" resultMap="ElecMeterHResult">
        SELECT sum(use_elec_cost)              use_elec_cost,
               DATE_FORMAT(`date`, '%Y-%m') as startRecTime
        from adm_elec_meter_h aemh
        where `date` >= #{dataRange}
        GROUP by startRecTime
        order by startRecTime asc
    </select>
    <select id="qryDeviceTypeElecMeterByDay" parameterType="ElecMeterH" resultMap="ElecMeterHResult">
        SELECT sum(elec_quantity) elec_quantity,
        aed.device_name
        from adm_ems_device aed
        join adm_elec_meter_h aemh
        on aed.device_code = aemh.device_code
        where aemh.date >= #{startRecTime}
        <if test="areaCode != null and areaCode != ''">and aemh.area_code = #{areaCode}</if>
        GROUP by aed.device_name
    </select>
</mapper>