<?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.tofly.xrtygis.mapper.DmatableMapper">

    <resultMap id="dmatableMap" type="com.tofly.xrtygis.entity.Dmatable">
        <id property="id" column="ID"/>
        <result property="deviceId" column="DEVICE_ID"/>
        <!--        <result property="schCode" column="SCH_CODE"/>-->
        <!--        <result property="sch" column="SCH"/>-->
        <!--        <result property="schName" column="SCH_NAME"/>-->

        <!--        <result property="phenotype" column="PHENOTYPE"/>-->
        <!--        <result property="factory" column="FACTORY"/>-->
        <result property="minFlow" column="MIN_FLOW"/>
        <result property="isflow" column="ISFLOW"/>
        <result property="ispress" column="ISPRESS"/>
        <!--        <result property="address" column="ADDRESS"/>-->
        <!--        <result property="lgtd" column="LGTD"/>-->
        <!--        <result property="lttd" column="LTTD"/>-->
        <result property="usfl" column="USFL"/>
        <!--        <result property="iskente" column="ISKENTE"/>-->
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <association property="bore" column="DEVICE_ID" select="getBore"/>
        <association property="schCode" column="DEVICE_ID" select="getSchCode"/>
        <association property="schName" column="DEVICE_ID" select="getSchName"/>
        <association property="phenotype" column="DEVICE_ID" select="getPhenotype"/>
        <association property="factory" column="DEVICE_ID" select="getFactory"/>
        <association property="address" column="DEVICE_ID" select="getAddress"/>
        <association property="lgtd" column="DEVICE_ID" select="getX"/>
        <association property="lttd" column="DEVICE_ID" select="getY"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.xrtygis.mapper.IFileMapper.getUserName"/>
    </resultMap>

    <select id="getBore" resultType="string">
        select BORE
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>
    <select id="getSchCode" resultType="string">
        select CODE
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getSchName" resultType="string">
        select NAME
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getPhenotype" resultType="string">
        select MODEL_SPECIFICATION
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getFactory" resultType="string">
        select MANUFACTURER
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getAddress" resultType="string">
        select ADDRESS
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getX" resultType="java.lang.Double">
        select LONGITUDE
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getY" resultType="java.lang.Double">
        select LATITUDE
        from TF_DEVICE_ARCHIVE_MANAGE
        where ID = #{deviceId}
    </select>

    <select id="getDmatablePage" resultMap="dmatableMap">
        select a.*
        from TF_YWPN_DMATABLE_W a
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE b ON b.ID = a.DEVICE_ID
        where
        1=1
        <if test="dmaTableDto.code != null and dmaTableDto.code.trim() != ''">
            AND b.CODE like '%'||#{dmaTableDto.code}||'%'
        </if>
        <if test="dmaTableDto.name != null and dmaTableDto.name.trim() != ''">
            AND b.NAME like '%'||#{dmaTableDto.name}||'%'
        </if>
    </select>


    <select id="getUserWaterMeterInfoVoPage" resultType="com.tofly.xrtygis.entity.vo.UserWaterMeterInfoVo">
        select k.meterReadingBook,k.meterReadingArea,k.userCode,k.userName,k.userAddress,k.contactNumber,
               k.waterMeterManufacturer,k.waterMeterCode,k.waterMeterCaliber,k.waterMeterType,k.waterMeterNature,
               k.averageDailyWater
        from (
        SELECT t.SSCBB meterReadingBook,t.SSCBPQ meterReadingArea,t.YHBH userCode,t.YHXM userName,t.YHDZ userAddress,
        t.LXDH contactNumber,
        (select mc from bm_sbsccj@YXDBLINK where bm= t.SBSCCJ) waterMeterManufacturer,
        t.SBBH waterMeterCode,t.SBKJ waterMeterCaliber,
        (select mc from bm_sblb@YXDBLINK where bm=t.SBLB) waterMeterType,
        (select c.mc from yh_cbkxx@YXDBLINK a
        left join bm_sj@YXDBLINK b on b.bm = a.sjbm
        left join bm_yslx@YXDBLINK c on c.bm = b.yslx
        WHERE
        a.sjbm = t.sjbm
        GROUP BY a.sjbm,c.mc) waterMeterNature,
        t.days cumulativeDays,t.sl cumulativeWaterUse,round(decode(t.days,0,0,t.sl/t.days),2) averageDailyWater
        FROM(
        SELECT t1.SSCBB,t1.SSCBPQ,t1.YHBH,t1.YHXM,t1.YHDZ,t1.LXDH,t2.SBSCCJ,t2.SBBH,t2.SBKJ,t2.SBLB,t2.sjbm,
        sum(to_number(to_char(last_day(trunc(to_date(t3.cwrq,'yyyy/mm'))),'dd'))) days,sum(t3.SL)sl
        FROM yh_khjbxx@YXDBLINK t1 LEFT JOIN yh_cbkxx@YXDBLINK t2
        on t1.YHBH = t2.YHBH
        LEFT JOIN YY_QFMX@YXDBLINK t3
        on t2.yhbh = t3.YHBH
        where  length(t3.cwrq)>=6
        GROUP BY t1.SSCBB,t1.SSCBPQ,t1.YHBH,t1.YHXM,t1.YHDZ,t1.LXDH,t2.SBSCCJ,t2.SBBH,t2.SBKJ,t2.SBLB,t2.sjbm) t)k
        <if test="likeStr != null and likeStr.trim() != ''">
            where k.meterReadingBook || k.userName || k.userAddress || k.waterMeterManufacturer || k.waterMeterNature like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="getUserWaterMeterAnalyze" resultType="com.tofly.xrtygis.entity.vo.UserWaterMeterAnalyzeVo">
        SELECT t3.CWRQ month,sum(t3.sl) waterConsumption,t3.CBRQ receiptDate
        FROM yh_khjbxx@YXDBLINK t1 LEFT JOIN yh_cbkxx@YXDBLINK t2
        on t1.YHBH = t2.YHBH
        LEFT JOIN YY_QFMX@YXDBLINK t3
        on t2.YHBH = t3.YHBH
        WHERE t1.yhbh = #{userCode}
        <if test="waterMeterCode != null and waterMeterCode.trim() != ''">
           and  t2.sbbh = #{waterMeterCode}
        </if>
        <if test="startTime != null and endTime != null">
            and to_date(t3.CWRQ,'yyyy-MM') >= to_date(to_char(#{startTime},'yyyy-MM'),'yyyy-MM')
            and to_date(t3.CWRQ,'yyyy-MM') &lt;= to_date(to_char(#{endTime},'yyyy-MM'),'yyyy-MM')
        </if>
        GROUP BY t3.CWRQ,t3.CBRQ
        ORDER BY month
    </select>
</mapper>
