<?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="createTime"    column="create_time"    />
    </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, create_time from adm_elec_meter_h
    </sql>

    <select id="selectElecMeterHList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterHResult">
        select
            m.id, m.area_code, m.device_code, d.device_name, m.record_time, m.`date`, m.`time`, m.time_index, m.elec_quantity, m.meter_type, m.meter_unit_price, m.use_elec_cost, m.create_time
        from adm_elec_meter_h m
            left join adm_meter_device d on m.device_code = d.device_code
        <where>  
            <if test="areaCode != null  and areaCode != ''"> and m.area_code = #{areaCode}</if>
            <if test="deviceCode != null  and deviceCode != ''"> and m.device_code = #{deviceCode}</if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime != ''"> and m.record_time between #{startRecTime} and #{endRecTime}</if>
        </where>
        order by m.record_time ${orderFlag}
    </select>

    <select id="getElecMeterHByObj" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterHResult">
        select
            m.id,
            m.area_code,
            m.device_code,
            d.device_name,
            m.record_time,
            m.`date`,
            m.`time`,
            m.time_index,
            sum(m.elec_quantity) as elec_quantity,
            m.meter_type,
            m.meter_unit_price,
            sum(m.use_elec_cost) as use_elec_cost
        from adm_elec_meter_h m
            left join adm_meter_device d on m.device_code = d.device_code
        <where>
            m.area_code = #{areaCode}
            and m.device_code in (
                select
                    r.meter_device from adm_meter_boundary_rel r
                where
                    r.obj_type = #{objType}
                    and r.meter_cls = #{meterCls}
                    and r.boundary_obj in
                    <foreach item="item" index="index" collection="objCodes" open="in (" separator=", " close=")">
                        #{item}
                    </foreach>
            )
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime != ''"> and m.record_time between #{startRecTime} and #{endRecTime}</if>
        </where>
        group by device_code, `date`, time_index
    </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>
        </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="useElecCost != null">#{useElecCost},</if>
         </trim>
    </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>
        </trim>
        where id = #{id}
    </update>

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