<?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.WaterMeterHMapper">
    
    <resultMap type="com.ruoyi.ems.domain.WaterMeterH" id="WaterMeterHResult">
        <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="waterQuantity"    column="water_quantity"    />
        <result property="meterUnitPrice"    column="meter_unit_price"    />
        <result property="createTime"    column="create_time"    />
        <result property="useWaterCost"    column="use_water_cost"    />
        <result property="meterReading" column="meter_reading"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.EnergyMeter" id="WaterMeterResult">
        <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="water_quantity"/>
        <result property="useCost" column="use_water_cost"/>
    </resultMap>

    <sql id="selectWaterMeterHVo">
        select id, area_code, device_code, record_time, `date`, `time`, time_index, water_quantity, meter_unit_price, use_water_cost, meter_reading, create_time from adm_water_meter_h
    </sql>

    <select id="getElecMeterHByObj" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterMeterHResult">
        select
          m.id,
          m.area_code,
          m.device_code,
          d.device_name,
          sum(m.water_quantity) as water_quantity,
          sum(m.use_water_cost) as use_elec_cost
        from adm_water_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="insertBatch" parameterType="java.util.List">
        insert into adm_water_meter_h (area_code, device_code, record_time, `date`, `time`, time_index, water_quantity, meter_unit_price, use_water_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.waterQuantity}, #{item.meterUnitPrice}, #{item.useWaterCost}, #{item.meterReading})
        </foreach>
    </insert>

    <insert id="insertWaterMeterH" parameterType="com.ruoyi.ems.domain.WaterMeterH" useGeneratedKeys="true" keyProperty="id">
        insert into adm_water_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="waterQuantity != null">water_quantity,</if>
            <if test="meterUnitPrice != null">meter_unit_price,</if>
            <if test="useWaterCost != null">use_water_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="waterQuantity != null">#{waterQuantity},</if>
            <if test="meterUnitPrice != null">#{meterUnitPrice},</if>
            <if test="useWaterCost != null">#{useWaterCost},</if>
            <if test="meterReading != null">#{meterReading},</if>
         </trim>
    </insert>

    <update id="updateWaterMeterH" parameterType="com.ruoyi.ems.domain.WaterMeterH">
        update adm_water_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="waterQuantity != null">water_quantity = #{waterQuantity},</if>
            <if test="meterUnitPrice != null">meter_unit_price = #{meterUnitPrice},</if>
            <if test="useWaterCost != null">use_water_cost = #{useWaterCost},</if>
            <if test="meterReading != null">meter_reading = #{meterReading},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <select id="qryWaterMeterByDate" parameterType="WaterMeterH" resultMap="WaterMeterResult">
        SELECT sum(water_quantity) water_quantity,
               SUM(use_water_cost) use_water_cost
        from adm_water_meter_h aemh
        where `date` like concat(#{startRecTime}, '%')
        <if test="areaCode != null and areaCode != ''">and aemh.area_code = #{areaCode}</if>
    </select>

    <select id="qryTimeIndexWaterMeterByDay" resultMap="WaterMeterHResult">
        SELECT sum(use_water_cost) use_water_cost,
               time_index,
               `date`
        from adm_water_meter_h aemh
        where `date` >= #{dataRange}
        GROUP by `date`, time_index
        order by `date`, time_index
    </select>

    <select id="qryDateWaterMeterByDay" resultMap="WaterMeterHResult">
        SELECT sum(use_water_cost) use_water_cost,
               `date`
        from adm_water_meter_h aemh
        where `date` >= #{dataRange}
        GROUP by `date`
        order by `date` asc
    </select>

    <select id="qryDateWaterMeterByYear" resultMap="WaterMeterHResult">
        SELECT
            sum(use_water_cost) use_water_cost,
            DATE_FORMAT(`date`, '%Y-%m') as startRecTime
        from
            adm_water_meter_h aemh
        where
            `date` >= #{dataRange}
        GROUP by
            startRecTime
        order by
            startRecTime asc
    </select>

    <select id="selectWaterDayAvg" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterMeterResult">
        select
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            area_code,
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            device_code,
        </if>
        SUM(water_quantity) / COUNT(DISTINCT date) AS water_quantity,
        SUM(use_water_cost) / COUNT(DISTINCT date) AS use_water_cost
        from adm_water_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>
</mapper>