<?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.MeterReadingMapper">
    
    <resultMap type="com.ruoyi.ems.domain.MeterReading" id="MeterReadingManualResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="areaCode"    column="area_code"    />
        <result property="year"    column="year"    />
        <result property="meterMonth"    column="meter_month"    />
        <result property="lastReading"    column="last_reading"    />
        <result property="lastTime"    column="last_time"    />
        <result property="meterReading"    column="meter_reading"    />
        <result property="meterTime"    column="meter_time"    />
        <result property="increase"    column="increase"    />
        <result property="increase"    column="increase"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.MeterReading" id="readingManualResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="areaCode"    column="area_code"    />
        <result property="year"    column="year"    />
        <result property="meterMonth"    column="meter_month"    />
        <result property="lastReading"    column="last_reading"    />
        <result property="lastTime"    column="last_time"    />
        <result property="meterReading"    column="meter_reading"    />
        <result property="meterTime"    column="meter_time"    />
        <result property="increase"    column="increase"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectMeterReadingManualVo">
        select id, device_code, area_code, `year`, meter_month, last_reading, last_time, meter_reading, meter_time, increase, create_time, update_time from adm_meter_reading
    </sql>

    <select id="selectById"  parameterType="Long" resultMap="MeterReadingManualResult">
        <include refid="selectMeterReadingManualVo"/>
        where id = #{id}
    </select>

    <select id="selectLastItem"  resultMap="readingManualResult">
        select
            r.id, r.device_code, m.device_name, r.area_code, r.`year`, r.meter_month, r.last_reading, r.last_time, r.meter_reading, r.meter_time, r.increase, r.create_time, r.update_time
        from adm_meter_reading r
          left join adm_meter_device m on r.device_code = m.device_code
        where r.area_code = #{areaCode}
          and r.device_code = #{deviceCode}
        order by meter_time desc
        limit 1
    </select>

    <!-- 根据设备代码列表查询区域用能汇总 -->
    <select id="selectSummaryByDevices" resultType="java.math.BigDecimal">
        SELECT
        ROUND(SUM(r.increase), 2) as quantity
        FROM adm_meter_reading r
        WHERE r.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.areaCode != null and queryMeter.areaCode != ''">
            AND r.area_code = #{queryMeter.areaCode}
        </if>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND r.meter_month &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND r.meter_month &lt;= #{queryMeter.endRecTime}
        </if>
    </select>

    <select id="selectList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="MeterReadingManualResult">
        <include refid="selectMeterReadingManualVo"/>
        <where>  
            <if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="areaCode != null and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="year != null and year != ''"> and `year` = #{year}</if>
            <if test="startRecTime != null and startRecTime != ''"> and meter_month &gt;= #{startRecTime}</if>
            <if test="endRecTime != null and endRecTime != ''"> and meter_month &lt;= #{endRecTime}</if>
        </where>
        ORDER BY meter_month ${orderFlag}
    </select>

    <select id="selectByTag" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="MeterReadingManualResult">
        select
            r.id, r.device_code, r.area_code, r.`year`, r.meter_month, r.last_reading, r.last_time, r.meter_reading, r.meter_time, r.increase, r.create_time, r.update_time
        from adm_meter_reading r
        left join
            adm_meter_device d on r.device_code = d.device_code
        <where>
            <if test="areaCode != null  and areaCode != ''"> and r.area_code = #{areaCode}</if>
            <if test="startRecTime != null  and startRecTime != ''"> and meter_month &gt;= #{startRecTime}</if>
            <if test="endRecTime != null  and endRecTime != ''"> and meter_month &lt;= #{endRecTime}</if>
        </where>
    </select>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.MeterReading" useGeneratedKeys="true" keyProperty="id">
        insert into adm_meter_reading
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="year != null and year != ''">year,</if>
            <if test="meterMonth != null and meterMonth != ''">meter_month,</if>
            <if test="lastReading != null">last_reading,</if>
            <if test="lastTime != null">last_time,</if>
            <if test="meterReading != null">meter_reading,</if>
            <if test="meterTime != null">meter_time,</if>
            <if test="increase != null">increase,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="year != null and year != ''">#{year},</if>
            <if test="meterMonth != null and meterMonth != ''">#{meterMonth},</if>
            <if test="lastReading != null">#{lastReading},</if>
            <if test="lastTime != null">#{lastTime},</if>
            <if test="meterReading != null">#{meterReading},</if>
            <if test="meterTime != null">#{meterTime},</if>
            <if test="increase != null">#{increase},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="update" parameterType="com.ruoyi.ems.domain.MeterReading">
        update adm_meter_reading
        <trim prefix="SET" suffixOverrides=",">
            <if test="meterReading != null">meter_reading = #{meterReading},</if>
            <if test="meterTime != null">meter_time = #{meterTime},</if>
            <if test="increase != null">increase = #{increase},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        delete from adm_meter_reading where id = #{id}
    </delete>
</mapper>