<?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.template.mapper.WechatScanLoginMapper">

    <resultMap id="Tj" type="com.template.model.tongji.Tj">
        <result property="name" column="name"/>
        <result property="num" column="num"/>
    </resultMap>

    <resultMap id="Tjt" type="com.template.model.tongji.All">
        <result property="total" column="total"/>
    </resultMap>

    <resultMap id="Elct" type="com.template.model.tongji.DAll">
        <result property="num" column="num"/>
    </resultMap>

    <resultMap id="Energy" type="com.template.model.tongji.EnergyTj">
        <result property="name" column="name"/>
        <result property="num" column="num"/>
    </resultMap>

    <resultMap id="MonthT" type="com.template.model.tongji.EnergyTj">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="date" column="date"/>
        <result property="num" column="num"/>
    </resultMap>

    <resultMap id="DayT" type="com.template.model.tongji.EnergyTj">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="date" column="date"/>
        <result property="num" column="num"/>
    </resultMap>

    <resultMap id="meterMonthData" type="com.template.model.tongji.MeterMonthData">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="date" column="date"/>
        <result property="wNum" column="w_num"/>
        <result property="eNum" column="e_num"/>
    </resultMap>

    <!--用户类别统计-->
    <select id="getUserIdTj" parameterType="com.template.model.tongji.Tj" resultMap="Tj">
        select a.name,IFNULL(b.num,0) as num from `smart_identity` a
        left join (
            select identity_id,COUNT(*) as num from `smart_user`
            where is_cancel=0 and deleted=0 group by identity_id ) b
        on a.id=b.identity_id where a.`deleted`=0
    </select>

    <!--用户总数-->
    <select id="getUserIdTjt" parameterType="com.template.model.tongji.Tj" resultMap="Tjt">
        select count(*) as total from `smart_user` where is_cancel=0 and deleted=0
    </select>

    <!--水表总计-->
    <select id="getWaterTj" parameterType="com.template.model.tongji.Tj" resultMap="Elct">
        select ifnull(sum(meter_power),0) as num from `smart_meter_detail` where meter_type=0 and deleted=0
    </select>

    <!--电表总计-->
    <select id="getElcTj" parameterType="com.template.model.tongji.Tj" resultMap="Elct">
        select ifnull(sum(meter_power),0) as num from `smart_meter_detail` where meter_type=1 and deleted=0
    </select>

    <!--每个月各水表数据统计-->
    <select id="getWaterTjByMonAndBuild" parameterType="String" resultMap="Energy">
        select a.name,SUM(IFNULL(b.num,0)) as num from `smart_build` a left join (
            select a.`build_id`,IFNULL(b.num,0) as num from `smart_build_meter` a left join (
            select meter_id,IFNULL(SUM(meter_power),0) as num from `smart_meter_detail`
            where deleted=0 and meter_type=0 and update_time like concat(#{month}, '%')
            group by meter_id ) b on a.`meter_id`=b.`meter_id`) b on a.id=b.build_id
        group by a.`name`
    </select>

    <!--每个月各电表数据统计-->
    <select id="getElcTjByMonAndBuild" parameterType="String" resultMap="Energy">
        select a.name,SUM(IFNULL(b.num,0)) as num from `smart_build` a left join (
            select a.`build_id`,IFNULL(b.num,0) as num from `smart_build_meter` a left join (
                select meter_id,IFNULL(SUM(meter_power),0) as num from `smart_meter_detail`
                where deleted=0 and meter_type=1 and update_time like concat(#{month}, '%')
                group by meter_id ) b on a.`meter_id`=b.`meter_id`) b on a.id=b.build_id
        group by a.`name`
    </select>

    <!--每个月每个楼栋查询-->
    <select id="getTjByMon" parameterType="String" resultMap="MonthT">
        select * from `month_meter_detail` where `name`=#{name} and `date`=#{month} and `type`=#{type}
    </select>

    <insert id="insertMonthMeter" parameterType="com.template.model.tongji.EnergyTj" useGeneratedKeys="true">
        INSERT INTO `month_meter_detail`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">`name`,</if>
            <if test="type != null">`type`,</if>
            <if test="date != null">`date`,</if>
            <if test="num != null">`num`,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="date != null">#{date},</if>
            <if test="num != null">#{num},</if>
        </trim>
    </insert>

    <update id="updateMonthMeter">
        UPDATE `month_meter_detail`
        <trim prefix="SET" suffixOverrides=",">
            num=#{energyTj.num}
        </trim>
        <where>
            and id=#{energyTj.id}
        </where>
    </update>

    <!--每天各水表数据统计-->
    <select id="getWaterTjByDayAndBuild" parameterType="String" resultMap="Energy">
        select a.name,SUM(IFNULL(b.num,0)) as num from `smart_build` a left join (
            select a.`build_id`,IFNULL(b.num,0) as num from `smart_build_meter` a left join (
                select meter_id,IFNULL(SUM(meter_power),0) as num from `smart_meter_detail`
                where deleted=0 and meter_type=0 and update_time like concat(#{day}, '%')
                group by meter_id ) b on a.`meter_id`=b.`meter_id`) b on a.id=b.build_id
        group by a.`name`
    </select>

    <!--每天各电表数据统计-->
    <select id="getElcTjByDayAndBuild" parameterType="String" resultMap="Energy">
        select a.name,SUM(IFNULL(b.num,0)) as num from `smart_build` a left join (
            select a.`build_id`,IFNULL(b.num,0) as num from `smart_build_meter` a left join (
                select meter_id,IFNULL(SUM(meter_power),0) as num from `smart_meter_detail`
                where deleted=0 and meter_type=1 and update_time like concat(#{day}, '%')
                group by meter_id ) b on a.`meter_id`=b.`meter_id`) b on a.id=b.build_id
        group by a.`name`
    </select>

    <select id="getTjByDay" parameterType="String" resultMap="DayT">
        select * from `day_meter_detail` where `name`=#{name} and `date`=#{day} and `type`=#{type}
    </select>

    <insert id="insertDayMeter" parameterType="com.template.model.tongji.EnergyTj" useGeneratedKeys="true">
        INSERT INTO `day_meter_detail`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">`name`,</if>
            <if test="type != null">`type`,</if>
            <if test="date != null">`date`,</if>
            <if test="num != null">`num`,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="date != null">#{date},</if>
            <if test="num != null">#{num},</if>
        </trim>
    </insert>

    <update id="updateDayMeter">
        UPDATE `day_meter_detail`
        <trim prefix="SET" suffixOverrides=",">
            num=#{energyTj.num}
        </trim>
        <where>
            and id=#{energyTj.id}
        </where>
    </update>

    <!--每个月水费数据-->
    <select id="getMonWater" parameterType="String" resultMap="Tj">
        select * from `month_meter_detail` where type=0 and `date`=#{month}
    </select>

    <!--每个月电费数据-->
    <select id="getMonElc" parameterType="String" resultMap="Tj">
        select * from `month_meter_detail` where type=1 and `date`=#{month}
    </select>

    <!--区域能耗统计-->
    <select id="getMeterMonthPage" resultMap="meterMonthData">
        select * from (select a.id,a.`name`,a.`date`,a.`num` w_num,b.num as e_num
           from `month_meter_detail` a,`month_meter_detail` b
           where a.`type`=0 and b.`type`=1 and a.name=b.`name`
             and a.`date`=b.`date` ) a
        <where>
            <if test="meterMonthData.date != null and meterMonthData.date != ''"> AND a.date like concat(#{meterMonthData.date}, '%')</if>
        </where>
        order by a.date desc
    </select>

    <!--实时抄表-->
    <select id="getMeterDayPage" resultMap="meterMonthData">
        select * from (select a.id,a.`name`,a.`date`,a.`num` w_num,b.num as e_num
        from `day_meter_detail` a,`day_meter_detail` b
        where a.`type`=0 and b.`type`=1 and a.name=b.`name`
        and a.`date`=b.`date` ) a
        <where>
            <if test="meterMonthData.date != null and meterMonthData.date != ''"> AND a.date like concat(#{meterMonthData.date}, '%')</if>
        </where>
        order by a.date desc
    </select>

</mapper>
