<?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.watermeter.mapper.ReadmeterhisMapper">

    <resultMap id="readmeterhisMap" type="com.tofly.watermeter.entity.Readmeterhis">
        <result property="yhbh" column="YHBH"/>
        <result property="userId" column="USER_ID"/>
        <result property="cbkh" column="CBKH"/>
        <result property="cbxh" column="CBXH"/>
        <result property="childSort" column="CHILD_SORT"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="sbbh" column="SBBH"/>
        <result property="sbcj" column="SBCJ"/>
        <result property="sblx" column="SBLX"/>
        <result property="sbkj" column="SBKJ"/>
        <result property="ysxz" column="YSXZ"/>
        <result property="xzxf" column="XZXF"/>
        <result property="cbpq" column="CBPQ"/>
        <result property="cbb" column="CBB"/>
        <result property="cwrq" column="CWRQ"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="bcjjl" column="BCJJL"/>
        <result property="cbbz" column="CBBZ"/>
        <result property="jfzt" column="JFZT"/>
        <result property="cbry" column="CBRY"/>
        <result property="cbrq" column="CBRQ"/>
        <result property="jhrq" column="JHRQ"/>
        <result property="xzbs" column="XZBS"/>
        <result property="fileid" column="FILEID"/>
        <result property="cbfsCode" column="CBFS_CODE"/>
        <result property="shbz" column="SHBZ"/>
        <result property="auditUser" column="AUDIT_USER"/>
        <result property="auditCause" column="AUDIT_CAUSE"/>
        <result property="cbyf" column="CBYF"/>
        <result property="cbsl" column="CBSL"/>
        <result property="ysje" column="YSJE"/>
        <result property="auditResult" column="AUDIT_RESULT"/>
        <result property="sxjSbqd" column="SXJ_SBQD"/>
        <result property="auditReason" column="AUDIT_REASON"/>
        <result property="issyn" column="ISSYN"/>
        <result property="bz" column="BZ"/>
        <result property="lsbh" column="LSBH"/>
        <result property="sxjmessage" column="SXJMESSAGE"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="addressState" column="ADDRESS_STATE"/>
        <result property="isGeneral" column="IS_GENERAL"/>
        <result property="month1" column="MONTH1"/>
        <result property="month2" column="MONTH2"/>
        <result property="month3" column="MONTH3"/>
        <result property="cbrq1" column="CBRQ1"/>
        <result property="cbrq2" column="CBRQ2"/>
        <result property="cbrq3" column="CBRQ3"/>
        <result property="sinDouMonth" column="SIN_DOU_MONTH"/>
        <result property="startDay" column="START_DAY"/>
        <result property="endDay" column="END_DAY"/>
        <result property="cbqyMc" column="CBQY_MC"/>
        <result property="cbqyBm" column="CBQY_BM"/>
        <result property="cbqydBm" column="CBQYD_BM"/>
        <result property="qydmc" column="QYDMC"/>
        <result property="monthPlan" column="MONTH_PLAN"/>
        <result property="cbryName" column="CBRY_NAME"/>
        <result property="isOffline" column="IS_OFFLINE"/>
        <result property="startRq" column="START_RQ"/>
        <result property="endRq" column="END_RQ"/>
        <result property="hblx" column="HBLX"/>
        <result property="planId" column="PLAN_ID"/>
        <result property="jbzd" column="JBZD"/>
        <result property="xbqd" column="XBQD"/>
        <result property="hbrq" column="HBRQ"/>
        <result property="zeroCount" column="ZERO_COUNT"/>
        <result property="continuityUp" column="CONTINUITY_UP"/>
        <result property="continuityDown" column="CONTINUITY_DOWN"/>
        <result property="continuityEqual" column="CONTINUITY_EQUAL"/>
        <result property="diffTime" column="DIFF_TIME"/>
        <result property="yearonyear1" column="YEARONYEAR1"/>
        <result property="yearonyear2" column="YEARONYEAR2"/>
        <result property="yearonyear3" column="YEARONYEAR3"/>
        <result property="yearonmonth1" column="YEARONMONTH1"/>
        <result property="yearonmonth2" column="YEARONMONTH2"/>
        <result property="yearonmonth3" column="YEARONMONTH3"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="auditTimeStr" column="auditTimeStr"/>
        <result property="auditType" column="AUDIT_TYPE"/>
        <result property="qualityCount" column="QUALITY_COUNT"/>
        <result property="updateWater" column="UPDATE_WATER"/>
        <result property="updateScds" column="UPDATE_SCDS"/>
        <result property="updateBcds" column="UPDATE_BCDS"/>
        <result property="uploadDay" column="UPLOAD_DAY"/>
        <result property="isUpdatecbsl" column="IS_UPDATECBSL"/>
        <result property="isUpdateid" column="IS_UPDATEID"/>
        <result property="abnomalId" column="ABNOMAL_ID"/>
        <result property="sjsl" column="SJSL"/>
        <result property="yearRange" column="YEAR_RANGE"/>
        <result property="monthRange" column="MONTH_RANGE"/>
        <result property="auditResultName" column="AUDITRESULTNAME"/>
        <result property="cbrqStr" column="cbrqStr"/>
        <result property="isbreakRead" column="ISBREAK_READ"/>
        <result property="orderIds" column="ORDER_IDS"/>
        <result property="cbrq4" column="CBRQ4"/>
        <result property="isEstimate" column="IS_ESTIMATE"/>
        <result property="imgUrl" column="IMG_URL"/>
        <result property="sfqf" column="SFQF"/>
        <result property="qfje" column="QFJE"/>
        <result property="yhzt" column="YHZT"/>
        <result property="qfzq" column="QFZQ"/>
        <result property="clientBz" column="CLIENT_BZ"/>
        <result property="handlersTime" column="HANDLERS_TIME"/>
        <result property="handlersName" column="HANDLERS_NAME"/>
        <result property="handlersId" column="HANDLERS_ID"/>
        <result property="soleSign" column="SOLE_SIGN"/>
        <result property="uploadTime" column="UPLOAD_TIME"/>
        <result property="auditPassReson" column="AUDIT_PASS_RESON"/>
        <result property="gdId" column="GD_ID"/>
        <result property="zgl" column="ZGL"/>
        <result property="diffValue" column="DIFF_VALUE"/>
        <result property="updateUser" column="UPDATE_USER"/>
        <result property="updateDate" column="UPDATE_DATE"/>
        <result property="images" column="IMAGES"/>
        <association property="updateUserName" column="UPDATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="diffTimeStr" column="DIFF_TIME"
                     select="com.tofly.watermeter.mapper.QfcbqfmxMapper.getDiffTimeStr"/>
        <association property="abnomalName" column="ABNOMAL_ID"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="issynName" column="ISSYN"
                     select="getIssynName"/>
        <association property="cbbzName" column="CBBZ"
                     select="getCbbzName"/>
        <association property="sbkjName" column="SBKJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getKjByCode"/>
        <association property="sbcjName" column="SBCJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getCjByCode"/>
        <association property="sblxName" column="SBLX"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getLxByCode"/>
        <association property="cbfsName" column="CBFS_CODE"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sinDouMonthName" column="SIN_DOU_MONTH"
                     select="com.tofly.watermeter.mapper.QfcbplandetailsMapper.getSinDouMonthName"/>
        <association property="ysxzName" column="YSXZ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getYsxzName"/>
        <association property="cbpqName" column="CBPQ"
                     select="getPqName"/>
        <association property="cbbName" column="CBB" select="getCbbName"/>
        <association property="sbkh" column="YHBH"
                     select="getYhKh"/>
        <association property="auditUserName" column="AUDIT_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>

    <resultMap id="getClientMap" type="com.tofly.watermeter.entity.Readmetereashis">
        <result property="yhbh" column="YHBH"/>
        <result property="cbkh" column="CBKH"/>
        <result property="cbxh" column="CBXH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="sbbh" column="SBBH"/>
        <result property="sbcj" column="SBCJ"/>
        <result property="sblx" column="SBLX"/>
        <result property="sbkj" column="SBKJ"/>
        <result property="ysxz" column="YSXZ"/>
        <result property="xzxf" column="XZXF"/>
        <result property="cbpq" column="CBPQ"/>
        <result property="cbb" column="CBB"/>
        <result property="cwrq" column="CWRQ"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="bcjjl" column="BCJJL"/>
        <result property="cbbz" column="CBBZ"/>
        <result property="jfzt" column="JFZT"/>
        <result property="cbry" column="CBRY"/>
        <result property="cbrq" column="CBRQ"/>
        <result property="jhrq" column="JHRQ"/>
        <result property="xzbs" column="XZBS"/>
        <result property="fileid" column="FILEID"/>
        <result property="cbfsCode" column="CBFS_CODE"/>
        <result property="shbz" column="SHBZ"/>
        <result property="auditUser" column="AUDIT_USER"/>
        <result property="auditCause" column="AUDIT_CAUSE"/>
        <result property="cbyf" column="CBYF"/>
        <result property="cbsl" column="CBSL"/>
        <result property="ysje" column="YSJE"/>
        <result property="auditResult" column="AUDIT_RESULT"/>
        <result property="auditReason" column="AUDIT_REASON"/>
        <result property="issyn" column="ISSYN"/>
        <result property="bz" column="BZ"/>
        <result property="lsbh" column="LSBH"/>
        <result property="sxjmessage" column="SXJMESSAGE"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="addressState" column="ADDRESS_STATE"/>
        <result property="isGeneral" column="IS_GENERAL"/>
        <result property="month1" column="MONTH1"/>
        <result property="month2" column="MONTH2"/>
        <result property="month3" column="MONTH3"/>
        <result property="sinDouMonth" column="SIN_DOU_MONTH"/>
        <result property="startDay" column="START_DAY"/>
        <result property="endDay" column="END_DAY"/>
        <result property="cbqyMc" column="CBQY_MC"/>
        <result property="cbqyBm" column="CBQY_BM"/>
        <result property="cbqydBm" column="CBQYD_BM"/>
        <result property="monthPlan" column="MONTH_PLAN"/>
        <result property="cbryName" column="CBRY_NAME"/>
        <result property="qydmc" column="QYDMC"/>
        <result property="isOffline" column="IS_OFFLINE"/>
        <result property="startRq" column="START_RQ"/>
        <result property="endRq" column="END_RQ"/>
        <result property="cbrq1" column="CBRQ1"/>
        <result property="cbrq2" column="CBRQ2"/>
        <result property="cbrq3" column="CBRQ3"/>
        <result property="jbzd" column="JBZD"/>
        <result property="xbqd" column="XBQD"/>
        <result property="hblx" column="HBLX"/>
        <result property="hbrq" column="HBRQ"/>
        <result property="zeroCount" column="ZERO_COUNT"/>
        <result property="continuityUp" column="CONTINUITY_UP"/>
        <result property="continuityDown" column="CONTINUITY_DOWN"/>
        <result property="continuityEqual" column="CONTINUITY_EQUAL"/>
        <result property="diffTime" column="DIFF_TIME"/>
        <result property="yearonyear1" column="YEARONYEAR1"/>
        <result property="yearonyear2" column="YEARONYEAR2"/>
        <result property="yearonyear3" column="YEARONYEAR3"/>
        <result property="yearonmonth1" column="YEARONMONTH1"/>
        <result property="yearonmonth2" column="YEARONMONTH2"/>
        <result property="yearonmonth3" column="YEARONMONTH3"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="auditType" column="AUDIT_TYPE"/>
        <result property="qualityCount" column="QUALITY_COUNT"/>
        <result property="updateWater" column="UPDATE_WATER"/>
        <result property="uploadDay" column="UPLOAD_DAY"/>
        <result property="abnomalId" column="ABNOMAL_ID"/>
        <result property="sjsl" column="SJSL"/>
        <result property="updateBcds" column="UPDATE_BCDS"/>
        <result property="updateScds" column="UPDATE_SCDS"/>
        <result property="isUpdatecbsl" column="IS_UPDATECBSL"/>
        <result property="isUpdateid" column="IS_UPDATEID"/>
        <result property="isfirst" column="ISFIRST"/>
        <result property="isbreakRead" column="ISBREAK_READ"/>
        <result property="orderIds" column="ORDER_IDS"/>
        <result property="sxjSbqd" column="SXJ_SBQD"/>
        <result property="cbrq4" column="CBRQ4"/>
        <result property="isEstimate" column="IS_ESTIMATE"/>
        <result property="imgUrl" column="IMG_URL"/>
        <association property="diffTimeStr" column="DIFF_TIME"
                     select="com.tofly.watermeter.mapper.QfcbqfmxMapper.getDiffTimeStr"/>
        <association property="abnomalName" column="ABNOMAL_ID"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="issynName" column="ISSYN"
                     select="getIssynName"/>
        <association property="cbbzName" column="CBBZ"
                     select="getCbbzName"/>
        <association property="sbkjName" column="SBKJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getKjByCode"/>
        <association property="cbfsName" column="CBFS_CODE"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sinDouMonthName" column="SIN_DOU_MONTH"
                     select="com.tofly.watermeter.mapper.QfcbplandetailsMapper.getSinDouMonthName"/>
        <association property="ysxzName" column="YSXZ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getYsxzName"/>
        <association property="cbpqName" column="CBPQ"
                     select="getPqName"/>
        <association property="cbbName" column="CBB" select="getCbbName"/>
    </resultMap>

    <select id="getCbbName" resultType="string">
        select CBB_MC
        from TF_YWPN_DMACISCBB_W
        where CBB_BM = #{cbb}
    </select>
    <select id="getPqName" resultType="string">
        select PQMC
        from TF_YWPN_QFCBCBPQ_W
        where PQBM = #{cbpq}
    </select>
    <select id="getIssynName" resultType="string">
        select decode(#{issyn}, '0', '未同步', '1', '已同步')
        from dual
    </select>

    <select id="getYhKh" resultType="string">
        select sbkh
        from TF_YWPN_QFKHJBXX_W
        where yhbh = #{yhbh}
          and rownum = 1
    </select>

    <select id="getCbbzName" resultType="string">
        select decode(#{cbbz}, '0', '未抄表', '1', '已抄表')
        from dual
    </select>
    <select id="getInitCbData" resultMap="readmeterhisMap">
        SELECT
        temp.YHBH,
        temp.YHXM,
        temp.YHDZ,
        temp.LXDH,
        temp.SBBH,
        temp.SBCJ,
        temp.SBLX,
        temp.USER_ID,
        temp.SBKJ,
        temp.YSXZ,
        temp.XZXF,
        temp.CBPQ,
        temp.CBB,
        temp.CBXH,
        temp.CBRQ1,
        temp.CBRQ2,
        temp.CBRQ3,
        temp.CBRQ4,
        temp.MONTH1,
        temp.MONTH2,
        temp.MONTH3,
        temp.ZERO_COUNT,
        temp.CONTINUITY_UP,
        temp.CONTINUITY_DOWN,
        temp.CONTINUITY_EQUAL,
        temp.SFQF,
        temp.QFJE,
        temp.QFZQ,
        temp.YHZT,
        temp.SCDS
        FROM (SELECT a.YHBH,
        a.YHXM,
        a.YHDZ,
        a.LXDH,
        a.SBBH,
        a.SBCJ,
        a.SBLX,
        a.USER_ID,
        a.SBKJ,
        a.YSXZ,
        a.XZXF,
        a.CBPQ,
        a.CBB,
        a.YHZT,
        <!--机械表中的上次读数使用的是客户档案中的上次读数，卡表特殊性：客户档案里面的上次读数作为总购量，卡表的上次读数是由我们这边上次读数提供的；这点是卡表和机械表的区别之一-->
        NVL(a.SCDS , 0) as SCDS,
        b.CBXH,
        d.CBRQ as CBRQ1,
        d.CBRQ1 as CBRQ2,
        d.CBRQ2 as CBRQ3,
        d.CBRQ3 as CBRQ4,
        d.CBSL as MONTH1,
        d.MONTH1 as MONTH2,
        d.MONTH2 as MONTH3,
        d.ZERO_COUNT,
        d.CONTINUITY_UP,
        d.CONTINUITY_DOWN,
        d.CONTINUITY_EQUAL,
        a.SFQF,
        a.QFJE,
        a.QFZQ,
        <!--对相同用户编号，然后根据抄表月份字段排序，排列一个顺序号，然后取第一个-->
        row_number() OVER(PARTITION BY a.YHBH ORDER BY d.CBYF desc) as row_flag
        FROM TF_YWPN_YHBINGQY_W b
        JOIN TF_YWPN_QFKHJBXX_W a ON b.YHBH = a.YHBH
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W d ON b.YHBH =d.YHBH
        WHERE b.QYDBM = #{qydbm}
        <!--销户的用户不制定计划-->
        and a.YHZT != '9' and a.SBLX not like '%4'
        ) temp
        WHERE temp.row_flag=1
    </select>

    <select id="getInitCbDataCard" resultMap="readmeterhisMap">
        <!--这里有个问题，因为是把全部数据筛出一遍之后，再获取第一个最新数据，那么后面数据多了之后，肯定会报数据库内存不够的情况
        但是这里加了一个区段的筛选，一个区段最多就七八百个用户
        -->
        SELECT
        temp.YHBH,
        temp.YHXM,
        temp.YHDZ,
        temp.LXDH,
        temp.SBBH,
        temp.SBCJ,
        temp.SBLX,
        temp.SBKJ,
        temp.YSXZ,
        temp.XZXF,
        temp.CBPQ,
        temp.CBB,
        temp.USER_ID,
        temp.CBXH,
        temp.CBRQ1,
        temp.CBRQ2,
        temp.CBRQ3,
        temp.CBRQ4,
        temp.MONTH1,
        temp.MONTH2,
        temp.MONTH3,
        temp.ZERO_COUNT,
        temp.CONTINUITY_UP,
        temp.CONTINUITY_DOWN,
        temp.CONTINUITY_EQUAL,
        temp.SFQF,
        temp.QFJE,
        temp.QFZQ,
        temp.YHZT,
        temp.SCDS,
        temp.ZGL
        FROM (SELECT a.YHBH,
        a.YHXM,
        a.YHDZ,
        a.LXDH,
        a.SBBH,
        a.SBCJ,
        a.SBLX,
        a.SBKJ,
        a.YSXZ,
        a.XZXF,
        a.CBPQ,
        a.USER_ID,
        a.CBB,
        a.YHZT,
        <!--卡表特殊性：客户档案里面的上次读数作为总购量，卡表的上次读数是由我们这边上次读数提供的；机械表中的上次读数使用的是客户档案中的上次读数，这点是卡表和机械表的区别之一-->
        NVL(a.SCDS , 0) as ZGL,
        case when d.cbbz = '0' then NVL(d.SCDS , 0) else NVL(d.BCDS , 0) end as SCDS,
        b.CBXH,
        d.CBRQ as CBRQ1,
        d.CBRQ1 as CBRQ2,
        d.CBRQ2 as CBRQ3,
        d.CBRQ3 as CBRQ4,
        d.CBSL as MONTH1,
        d.MONTH1 as MONTH2,
        d.MONTH2 as MONTH3,
        NVL(d.ZERO_COUNT,0) as ZERO_COUNT,
        NVL(d.CONTINUITY_UP,0) as CONTINUITY_UP,
        NVL(d.CONTINUITY_DOWN,0) as CONTINUITY_DOWN,
        NVL(d.CONTINUITY_EQUAL,0) as CONTINUITY_EQUAL,
        a.SFQF,
        a.QFJE,
        a.QFZQ,
        row_number() OVER(PARTITION BY a.YHBH ORDER BY d.MONTH_PLAN desc) as row_flag
        FROM TF_YWPN_YHBINGQY_W b
        LEFT JOIN TF_YWPN_QFKHJBXX_W a ON b.YHBH = a.YHBH
        LEFT JOIN TF_YWPN_READMETERHIS_W d ON b.YHBH =d.YHBH
        WHERE b.QYDBM = #{qydbm}
        <!--销户的用户不制定计划，并且只制定水表类型是卡表的-->
        and a.YHZT != '9' and a.SBLX like '%4'
        ) temp
        WHERE temp.row_flag=1
    </select>

    <resultMap id="clloectMapId" type="com.tofly.watermeter.custom.entity.meterRead.readstatic.AllocationSta">
        <result property="cbry" column="cbry"/>
        <result property="qyNum" column="qyNum"/>
        <result property="qydNum" column="qydNum"/>
        <result property="allYhNum" column="allYhNum"/>
        <association property="cbryName" column="cbry"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>

    <select id="allocationClloect" resultMap="clloectMapId">
        SELECT cbry, COUNT(DISTINCT CBQY_BM) qyNum, COUNT(DISTINCT CBQYD_BM) qydNum, COUNT(1) allYhNum
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{search.cwyf}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        GROUP BY CBRY
    </select>
    <select id="getReadPercent" resultType="java.util.Map">
        SELECT a.*,
        trunc(a.READNUM / a.ALLNUM, 3) * 100 ALLPERCENT,
        trunc(a.APPCB / a.ALLNUM, 3) * 100 APPPERCENT,
        trunc(a.WEBCB / a.ALLNUM, 3) * 100 WEBPERCENT
        FROM (SELECT CBQY_MC,CBQY_BM,
        COUNT(1) ALLNUM,
        SUM(DECODE(CBBZ, '1', 1, 0)) READNUM,
        SUM(DECODE(CBBZ, '0', 1, 0)) NONUM,
        SUM(DECODE(IS_OFFLINE, '0', 1, 0)) APPCB,
        SUM(DECODE(IS_OFFLINE, '1', 1, 0)) WEBCB
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{cwyf}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        <if test="list != null ">
            AND CBQY_BM IN
            <foreach collection="list" open="(" separator="," item="qybm" close=")">
                #{qybm}
            </foreach>
        </if>
        GROUP BY CBQY_MC,CBQY_BM
        order by CBQY_MC) a
    </select>
    <select id="getReadPercentByQy" resultType="java.util.Map">
        SELECT a.*,
        trunc(a.READNUM / a.ALLNUM, 3) * 100 ALLPERCENT,
        trunc(a.APPCB / a.ALLNUM, 3) * 100 APPPERCENT,
        trunc(a.WEBCB / a.ALLNUM, 3) * 100 WEBPERCENT
        FROM (SELECT QYDMC,CBQYD_BM,CBQY_MC,
        COUNT(1) ALLNUM,
        SUM(DECODE(CBBZ, '1', 1, 0)) READNUM,
        SUM(DECODE(CBBZ, '0', 1, 0)) NONUM,
        SUM(DECODE(IS_OFFLINE, '0', 1, 0)) APPCB,
        SUM(DECODE(IS_OFFLINE, '1', 1, 0)) WEBCB
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{cwyf}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        <if test="qybmList != null ">
            AND CBQY_BM IN
            <foreach collection="qybmList" open="(" separator="," item="qybm" close=")">
                #{qybm}
            </foreach>
        </if>
        <if test="cbbList != null">
            AND CBQYD_BM IN
            <foreach collection="cbbList" open="(" separator="," item="cbb" close=")">
                #{cbb}
            </foreach>
        </if>
        GROUP BY QYDMC, CBQYD_BM, CBQY_MC
        ORDER BY CBQY_MC , QYDMC
        ) a
    </select>
    <select id="getReadPercentByCbry" resultType="java.util.Map">
        SELECT a.*,
        trunc(a.READNUM / a.ALLNUM, 3) * 100 ALLPERCENT,
        trunc(a.APPCB / a.ALLNUM, 3) * 100 APPPERCENT,
        trunc(a.WEBCB / a.ALLNUM, 3) * 100 WEBPERCENT
        FROM (SELECT CBRY_NAME,CBRY, COUNT(1) ALLNUM,
        SUM(DECODE(CBBZ, '1', 1, 0)) READNUM,
        SUM(DECODE(CBBZ, '0', 1, 0)) NONUM,
        SUM(DECODE(IS_OFFLINE, '0', 1, 0)) APPCB,
        SUM(DECODE(IS_OFFLINE, '1', 1, 0)) WEBCB
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{cwyf}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        <if test="cbryList != null">
            AND CBRY IN
            <foreach collection="cbryList" open="(" separator="," item="cbry" close=")">
                #{cbry}
            </foreach>
        </if>
        GROUP BY CBRY, CBRY_NAME
        ORDER BY CBRY_NAME
        ) a
    </select>

    <select id="getReadPercentDay" resultType="java.util.Map">
        SELECT a.*,
        trunc(a.READNUM / a.ALLNUM, 3) * 100 ALLPERCENT,
        trunc(a.APPCB / a.ALLNUM, 3) * 100 APPPERCENT,
        trunc(a.WEBCB / a.ALLNUM, 3) * 100 WEBPERCENT
        FROM (SELECT CBRY , CBRY_NAME , CBQY_MC , CBQY_BM , QYDMC , CBQYD_BM , TO_CHAR(START_RQ,'yyyy-MM-dd') as
        dayTime,
        COUNT(1) ALLNUM,
        SUM(DECODE(CBBZ, '1', 1, 0)) READNUM,
        SUM(DECODE(CBBZ, '0', 1, 0)) NONUM,
        SUM(DECODE(IS_OFFLINE, '0', 1, 0)) APPCB,
        SUM(DECODE(IS_OFFLINE, '1', 1, 0)) WEBCB
        FROM TF_YWPN_READMETERHIS_W
        <where>
            <!--IS_GENERAL = #{search.isGeneral}-->
            <if test="cbryList != null">
                AND CBRY IN
                <foreach collection="cbryList" open="(" separator="," item="cbry" close=")">
                    #{cbry}
                </foreach>
            </if>
            <if test="search.startDate != null and search.endDate != null">
                AND START_RQ BETWEEN #{search.startDate} AND #{search.endDate}
            </if>
        </where>
        GROUP BY CBRY , CBRY_NAME , CBQY_MC , CBQY_BM , QYDMC , CBQYD_BM , TO_CHAR(START_RQ,'yyyy-MM-dd')
        ORDER BY CBRY_NAME , CBQY_MC , QYDMC) a
    </select>


    <select id="getReadTimeDeatil" resultType="java.util.Map">
        SELECT a.*, round(to_number(a.LASTCB - a.FIRSTCB) * 24 , 1) DIFF_TIME , round((to_number(a.LASTCB -
        a.FIRSTCB)*24*60) / a.YETREADNUM , 1) AVERAGE_TIME
        FROM (SELECT CBQY_MC,
        QYDMC,
        CBRY_NAME,CBRY,
        MAX(START_RQ) START_RQ,
        MAX(END_RQ) END_RQ,
        count(1) READNUM,
        SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END) YETREADNUM ,
        SUM(CASE WHEN CBBZ = '0' THEN 1 ELSE 0 END) NOREADNUM ,
        min(CBRQ) FIRSTCB,
        max(CBRQ) LASTCB
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{cwyf}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        <if test="qybmList != null ">
            AND CBQY_BM IN
            <foreach collection="qybmList" open="(" separator="," item="qybm" close=")">
                #{qybm}
            </foreach>
        </if>
        <if test="cbbList != null">
            AND CBQYD_BM IN
            <foreach collection="cbbList" open="(" separator="," item="cbb" close=")">
                #{cbb}
            </foreach>
        </if>
        GROUP BY CBQY_MC, QYDMC, CBRY_NAME , CBRY
        ORDER BY QYDMC ) a where a.YETREADNUM > 0
    </select>
    <resultMap id="cbDataByHbMap" type="com.tofly.watermeter.custom.entity.meterRead.InitCbByHb">
        <result property="yhbh" column="yhbh"/>
        <result property="hbrq" column="hbrq"/>
        <result property="cbrq" column="cbrq"/>
        <result property="jbzd" column="jbzd"/>
        <result property="xbqd" column="xbqd"/>
        <result property="hblx" column="hblx"/>
    </resultMap>

    <select id="initCbDataByHb" resultMap="cbDataByHbMap">
        SELECT temp.YHBH    as yhbh,
               temp.BW_HBRQ as hbrq,
               temp.cbrq1   as cbrq,
               temp.BW_JBZD as jbzd,
               temp.BW_XBQD as xbqd,
               temp.BW_HBLX as hblx
        FROM (SELECT b.YHBH,
                     a.BW_HBRQ,
                     b.cbrq1,
                     nvl(a.BW_JBZD, 0)                                                  BW_JBZD,
                     nvl(a.BW_XBQD, 0)                                                  BW_XBQD,
                     a.BW_HBLX,
                     row_number() OVER (PARTITION BY b.YHBH ORDER BY a.BW_HBRQ desc) as row_flag
              FROM TF_YWPN_BWHBDATAIL_W a
                       JOIN TF_YWPN_READMETERHIS_W b ON a.BW_KHBH = b.YHBH
              WHERE b.MONTH_PLAN = #{monthPlan}
                AND b.cbrq1 <![CDATA[ < ]]> a.BW_HBRQ) temp
        WHERE temp.row_flag = 1
    </select>

    <select id="getInitFrontThreeData" resultMap="readmeterhisMap">
        SELECT temp.YHBH,
               NVL(MAX(DECODE(temp.row_flag, 1, temp.BCDS)), 0) as SCDS,
               NVL(MAX(DECODE(temp.row_flag, 1, temp.CBSL)), 0) as MONTH1,
               MAX(DECODE(temp.row_flag, 1, temp.CBRQ))         as CBRQ1,
               NVL(MAX(DECODE(temp.row_flag, 2, temp.CBSL)), 0) as MONTH2,
               MAX(DECODE(temp.row_flag, 2, temp.CBRQ))         as CBRQ2,
               NVL(MAX(DECODE(temp.row_flag, 3, temp.CBSL)), 0) as MONTH3,
               MAX(DECODE(temp.row_flag, 3, temp.CBRQ))         as CBRQ3
        FROM (
                 SELECT a.YHBH,
                        b.cbsl,
                        b.CBRQ,
                        b.BCDS,
                        row_number() OVER (PARTITION BY a.YHBH ORDER BY b.CBYF desc ) as row_flag
                 FROM TF_YWPN_READMETERHIS_W a
                          LEFT JOIN TF_YWPN_QFCBQFMXLS_W b ON a.YHBH = b.YHBH
                 WHERE a.MONTH_PLAN = #{monthPlan}
                   AND a.IS_GENERAL = '0') temp
        WHERE temp.row_flag IN (1, 2, 3)
        GROUP BY temp.YHBH
    </select>

    <update id="initFrontThreeData">
        <foreach collection="list" open="begin" close=";end;" item="threeInfo" separator=";">
            update TF_YWPN_READMETERHIS_W
            <set>
                <if test="threeInfo.cbrq1 != null">
                    CBRQ1 = #{threeInfo.cbrq1} ,
                </if>
                <if test="threeInfo.cbrq2 != null">
                    CBRQ2 = #{threeInfo.cbrq2} ,
                </if>
                <if test="threeInfo.cbrq3 != null">
                    CBRQ3 = #{threeInfo.cbrq3} ,
                </if>
                SCDS = #{threeInfo.scds} ,
                MONTH1 = #{threeInfo.month1} ,
                MONTH2 = #{threeInfo.month2} ,
                MONTH3 = #{threeInfo.month3}
            </set>
            where MONTH_PLAN = #{monthPlan} AND YHBH = #{threeInfo.yhbh}
        </foreach>
    </update>

    <update id="initCbPlanHb">
        <foreach collection="list" open="begin" close=";end;" item="hbInfo" separator=";">
            update TF_YWPN_READMETERHIS_W
            <set>
                <if test="hbInfo.hblx != null">
                    HBLX = #{hbInfo.hblx} ,
                </if>
                <if test="hbInfo.hbrq != null">
                    HBRQ = #{hbInfo.hbrq} ,
                </if>
                JBZD = #{hbInfo.jbzd} ,
                XBQD = #{hbInfo.xbqd}
            </set>
            where MONTH_PLAN = #{monthPlan} AND YHBH = #{hbInfo.yhbh}
        </foreach>
    </update>
    <update id="updateCbxh">
        <foreach collection="list" open="begin" close=";end;" item="yhInfo" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                CBXH = #{yhInfo.cbxh}
            </set>
            WHERE YHBH = #{yhInfo.yhbh}
            AND MONTH_PLAN = #{yearMonth}
            AND CBQYD_BM = #{qydbm}
        </foreach>
    </update>
    <update id="updateYearCbsl1">
        <foreach collection="list" open="begin" close=";end;" item="yhCbsl" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                YEARONYEAR1 = #{yhCbsl.cbsl ,jdbcType = NUMERIC},
                YEARONMONTH1 = #{yearOnMonth}
            </set>
            WHERE YHBH = #{yhCbsl.yhbh}
            AND MONTH_PLAN = #{yearMonth}
        </foreach>
    </update>
    <update id="updateYearCbsl2">
        <foreach collection="list" open="begin" close=";end;" item="yhCbsl" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                YEARONYEAR2 = #{yhCbsl.cbsl ,jdbcType = NUMERIC},
                YEARONMONTH2 = #{yearOnMonth}
            </set>
            WHERE YHBH = #{yhCbsl.yhbh}
            AND MONTH_PLAN = #{yearMonth}
        </foreach>
    </update>
    <update id="updateYearCbsl3">
        <foreach collection="list" open="begin" close=";end;" item="yhCbsl" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                YEARONYEAR3 = #{yhCbsl.cbsl ,jdbcType = NUMERIC},
                YEARONMONTH3 = #{yearOnMonth}
            </set>
            WHERE YHBH = #{yhCbsl.yhbh}
            AND MONTH_PLAN = #{yearMonth}
        </foreach>
    </update>
    <resultMap id="noSortYhListMap" type="com.tofly.watermeter.entity.Yhbingqy">
        <result property="yhbh" column="YHBH"/>
        <result property="cbxh" column="CBXH"/>
    </resultMap>
    <select id="getNoSortYhList" resultMap="noSortYhListMap">
        SELECT YHBH, CBXH
        FROM TF_YWPN_READMETERHIS_W
        WHERE CBQYD_BM = #{qydbm}
        AND YHBH NOT IN
        <foreach collection="list" open="(" separator="," item="yhbh" close=")">
            #{yhbh}
        </foreach>
        AND MONTH_PLAN = #{yearMonth}
        order by CBXH
    </select>
    <select id="getInitFrontThreeDataCount" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM (SELECT temp.YHBH,
                     NVL(MAX(DECODE(temp.row_flag, 1, temp.BCDS)), 0) as SCDS,
                     NVL(MAX(DECODE(temp.row_flag, 1, temp.CBSL)), 0) as MONTH1,
                     MAX(DECODE(temp.row_flag, 1, temp.CBRQ))         as CBRQ1,
                     NVL(MAX(DECODE(temp.row_flag, 2, temp.CBSL)), 0) as MONTH2,
                     MAX(DECODE(temp.row_flag, 2, temp.CBRQ))         as CBRQ2,
                     NVL(MAX(DECODE(temp.row_flag, 3, temp.CBSL)), 0) as MONTH3,
                     MAX(DECODE(temp.row_flag, 3, temp.CBRQ))         as CBRQ3
              FROM (
                       SELECT a.YHBH,
                              b.cbsl,
                              b.CBRQ,
                              b.BCDS,
                              row_number() OVER (PARTITION BY a.YHBH ORDER BY b.CBYF desc ) as row_flag
                       FROM TF_YWPN_READMETERHIS_W a
                                LEFT JOIN TF_YWPN_QFCBQFMXLS_W b ON a.YHBH = b.YHBH
                       WHERE a.MONTH_PLAN = #{monthPlan}
                         AND a.IS_GENERAL = '0') temp
              WHERE temp.row_flag IN (1, 2, 3)
              GROUP BY temp.YHBH)
    </select>
    <select id="getWaterByQyd" resultType="com.tofly.watermeter.custom.entity.meterRead.QydWater">
        SELECT CBQY_BM as qybm, CBQY_MC as qymc, CBQYD_BM as qydbm, QYDMC as qydmc, SUM(CBSL) as addValue
        FROM TF_YWPN_READMETERHIS_W
        WHERE UPLOAD_DAY = #{currentDay}
        GROUP BY CBQY_BM, CBQY_MC, CBQYD_BM, QYDMC
    </select>
    <select id="inspectCollectDeatils" resultMap="readmeterhisMap">
        SELECT /*+index(TF_YWPN_READMETERHIS_W READMETERHIS_CBRY_CBRQ)*/
        CBQY_BM , CBQYD_BM , CBQY_MC , QYDMC , YHBH,YHXM,YHDZ,YEARONYEAR1,YEARONYEAR2,YEARONYEAR3,SBKJ,YSXZ,
        YEARONMONTH1,YEARONMONTH2,YEARONMONTH3,IS_GENERAL,CBRY,SOLE_SIGN,AUDIT_PASS_RESON,zgl,diff_value,BCDS,
        MONTH1 , MONTH2 , MONTH3 , CBRQ1 , CBRQ2 , CBRQ3 ,MONTH_PLAN , CBRQ , CBYF , CBSL , AUDIT_REASON ,AUDIT_TYPE ,
        QUALITY_COUNT ,AUDIT_TIME , SJSL,decode(SHBZ,'0','未审核','1','人工审核通过','2','转工单','3','转工单完成') as
        AUDITRESULTNAME,SHBZ as AUDIT_RESULT,ABNOMAL_ID,
        CASE WHEN (YEARONYEAR1 >0 and YEARONYEAR2 >0 and YEARONYEAR3 >0) THEN
        trunc( ceil(CBSL-((YEARONYEAR1+YEARONYEAR2+YEARONYEAR3)/3)) / ((YEARONYEAR1+YEARONYEAR2+YEARONYEAR3)/3) * 100 ,
        2 ) ELSE 0 END as YEAR_RANGE,
        CASE WHEN (MONTH1 >0 and MONTH2 >0 and MONTH3 >0) THEN
        trunc( ceil(CBSL-((MONTH1+MONTH2+MONTH3)/3)) / ((MONTH1+MONTH2+MONTH3)/3) * 100 , 2 ) ELSE 0 END as MONTH_RANGE,
        AUDIT_USER
        FROM TF_YWPN_READMETERHIS_W
        <where>
            IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="qybmList != null">
                AND CBQY_BM IN
                <foreach collection="qybmList" open="(" separator="," item="qy" close=")">
                    #{qy}
                </foreach>
            </if>
            <if test="cbqdList != null">
                AND CBQYD_BM IN
                <foreach collection="cbqdList" open="(" separator="," item="qyd" close=")">
                    #{qyd}
                </foreach>
            </if>
            <if test="list != null">
                AND ABNOMAL_ID IN
                <foreach collection="list" open="(" separator="," item="abnomal" close=")">
                    #{abnomal}
                </foreach>
            </if>
            <if test="qualityStatis.queryType != 1 and qualityStatis.startTime != null and qualityStatis.endTime != null">
                AND CBRQ BETWEEN cast(#{qualityStatis.startTime} as date) AND cast(#{qualityStatis.endTime} as date)
            </if>
            <if test="qualityStatis.queryType == 0">
                AND SHBZ IS NOT NULL
            </if>
            <if test="qualityStatis.queryType == 2">
                AND SHBZ = '0'
            </if>
            <if test="qualityStatis.queryType == 3">
                AND (SHBZ = '1' or SHBZ = '3')
            </if>
            <if test="qualityStatis.queryType == 4">
                AND (SHBZ = '2' or SHBZ = '3')
            </if>
            <if test="qualityStatis.queryCount != null">
                AND ROWNUM <![CDATA[ <= ]]> #{qualityStatis.queryCount}
            </if>
            <if test="qualityStatis.inputcontent != null and qualityStatis.inputcontent != ''">
                and YHBH || YHXM || YHDZ like '%' || #{qualityStatis.inputcontent} || '%'
            </if>
        </where>
        order by CBQY_MC , QYDMC
    </select>
    <select id="inspectCollectDeatilsList" resultMap="readmeterhisMap" fetchSize="1000">
        SELECT CBQY_BM , CBQYD_BM , CBQY_MC , QYDMC , YHBH,YHXM,YHDZ,YEARONYEAR1,YEARONYEAR2,YEARONYEAR3,BCDS,
        YEARONMONTH1,YEARONMONTH2,YEARONMONTH3,zgl,diff_value,SOLE_SIGN,SBKJ,YSXZ,AUDIT_USER,
        MONTH1 , MONTH2 , MONTH3 , CBRQ1 , CBRQ2 , CBRQ3 ,MONTH_PLAN , CBRQ , CBYF , CBSL , AUDIT_REASON ,AUDIT_TYPE ,
        QUALITY_COUNT ,to_char(AUDIT_TIME, 'yyyy-mm-dd hh24:mi:ss') auditTimeStr,
        SJSL,decode(SHBZ,'0','未审核','1','人工审核通过','2','转工单','3','转工单完成的') as AUDITRESULTNAME,ABNOMAL_ID,AUDIT_RESULT,
        CASE WHEN (YEARONYEAR1 >0 and YEARONYEAR2 >0 and YEARONYEAR3 >0) THEN
        trunc( ceil(CBSL-((YEARONYEAR1+YEARONYEAR2+YEARONYEAR3)/3)) / ((YEARONYEAR1+YEARONYEAR2+YEARONYEAR3)/3) * 100 ,
        2 ) ELSE 0 END as YEAR_RANGE,
        CASE WHEN (MONTH1 >0 and MONTH2 >0 and MONTH3 >0) THEN
        trunc( ceil(CBSL-((MONTH1+MONTH2+MONTH3)/3)) / ((MONTH1+MONTH2+MONTH3)/3) * 100 , 2 ) ELSE 0 END as MONTH_RANGE
        FROM TF_YWPN_READMETERHIS_W
        <where>
            IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="qybmList != null">
                AND CBQY_BM IN
                <foreach collection="qybmList" open="(" separator="," item="qy" close=")">
                    #{qy}
                </foreach>
            </if>
            <if test="cbqdList != null">
                AND CBQYD_BM IN
                <foreach collection="qybmList" open="(" separator="," item="qyd" close=")">
                    #{qyd}
                </foreach>
            </if>
            <if test="list != null">
                AND ABNOMAL_ID IN
                <foreach collection="list" open="(" separator="," item="abnomal" close=")">
                    #{abnomal}
                </foreach>
            </if>
            <if test="qualityStatis.startTime != null and qualityStatis.endTime != null">
                AND CBRQ BETWEEN #{qualityStatis.startTime} AND #{qualityStatis.endTime}
            </if>
            <if test="qualityStatis.queryType == 0">
                AND SHBZ IS NOT NULL
            </if>
            <if test="qualityStatis.queryType == 2">
                AND SHBZ = '0'
            </if>
            <if test="qualityStatis.queryType == 3">
                AND (SHBZ = '1' or SHBZ = '3')
            </if>
            <if test="qualityStatis.queryType == 4">
                AND (SHBZ = '2' or SHBZ = '3')
            </if>
            <if test="qualityStatis.queryCount != null">
                AND ROWNUM <![CDATA[ <= ]]> #{qualityStatis.queryCount}
            </if>
        </where>
    </select>
    <resultMap id="fluctuateMap" type="com.tofly.watermeter.custom.entity.quality.QualityFluctuate">
        <result property="userCount" column="userCount"/>
        <result property="waterCount" column="waterCount"/>
        <result property="lineBm" column="YSXZ"/>
        <result property="lineBm" column="SBKJ"/>
        <result property="lineBm" column="ABNOMAL_ID"/>
        <association property="lineName" column="YSXZ"
                     select="getYsxz"/>
        <association property="lineName" column="SBKJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getKjByCode"/>
        <association property="lineName" column="ABNOMAL_ID"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
    </resultMap>

    <select id="getYsxz" resultType="string">
        select YSXZ_MC
        from TF_YWPN_QFMIDYSXZBM_E
        where YSXZ_BM = #{ysxz}
    </select>

    <select id="getFluctuateBySbkjUp" resultMap="fluctuateMap">
        select SBKJ , userCount ,
        waterCount
        from (
        SELECT SBKJ ,
        SUM(CASE WHEN CONTINUITY_UP > 0 THEN 1 ELSE 0 END) as userCount ,
        nvl(SUM(CASE WHEN CONTINUITY_UP > 0 THEN abs(SJSL) ELSE 0 END),0) as waterCount
        FROM TF_YWPN_READMETERHIS_W WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by sbkj order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W
        WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_UP > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} )
        GROUP BY SBKJ)
    </select>

    <select id="getFluctuateBySbkjDown" resultMap="fluctuateMap">
        select SBKJ ,userCount ,
        waterCount
        from (
        SELECT SBKJ ,
        SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN 1 ELSE 0 END) as userCount ,
        nvl(SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN abs(SJSL) ELSE 0 END),0) as waterCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by sbkj order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W
        WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_DOWN > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} ) GROUP
        BY SBKJ)
    </select>
    <select id="getFluctuateByYsxzUp" resultMap="fluctuateMap">
        select YSXZ ,userCount ,
        waterCount
        from (
        SELECT YSXZ ,
        SUM(CASE WHEN CONTINUITY_UP > 0 THEN 1 ELSE 0 END) as userCount ,
        SUM(CASE WHEN CONTINUITY_UP > 0 THEN abs(SJSL) ELSE 0 END) as waterCount
        FROM TF_YWPN_READMETERHIS_W WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by YSXZ order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_UP > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} )
        GROUP BY YSXZ)
    </select>
    <select id="getFluctuateByYsxzDown" resultMap="fluctuateMap">
        select YSXZ ,userCount ,
        waterCount
        from (
        SELECT YSXZ ,
        SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN 1 ELSE 0 END) as userCount ,
        SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN abs(SJSL) ELSE 0 END) as waterCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by YSXZ order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_DOWN > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} )
        GROUP BY YSXZ)
    </select>
    <select id="getFluctuateByAbnomalUp" resultMap="fluctuateMap">
        select ABNOMAL_ID ,userCount ,
        waterCount
        from (
        SELECT ABNOMAL_ID,
        SUM(CASE WHEN CONTINUITY_UP > 0 THEN 1 ELSE 0 END) as userCount ,
        SUM(CASE WHEN CONTINUITY_UP > 0 THEN abs(SJSL) ELSE 0 END) as waterCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by ABNOMAL_ID order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_UP > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} ) GROUP BY ABNOMAL_ID)
    </select>
    <select id="getFluctuateByAbnomalDown" resultMap="fluctuateMap">
        select ABNOMAL_ID , userCount ,
        waterCount
        from (
        SELECT ABNOMAL_ID,
        SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN 1 ELSE 0 END) as userCount ,
        SUM(CASE WHEN CONTINUITY_DOWN > 0 THEN abs(SJSL) ELSE 0 END) as waterCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by ABNOMAL_ID order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="list != null">
            AND CBQYD_BM IN
            <foreach collection="list" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        and CONTINUITY_DOWN > 0) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} ) GROUP BY ABNOMAL_ID)
    </select>
    <select id="getOvertimeCollect"
            resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadOverTime">
        SELECT a.CBRY as cbry, a.CBRY_NAME as cbryName, b.qydCount overTimeCount,
        ROUND((a.OVERTIME_COUNT/a.allCount)*100 , 1) as overTimeRatio,
        a.OVERTIME_COUNT overCount,a.allCount allCount
        FROM
        (SELECT CBRY , CBRY_NAME ,
        SUM(CASE WHEN (CBBZ = '1' and CBRQ > END_RQ) or (CBBZ = '0' and SYSDATE <![CDATA[ > ]]> END_RQ) THEN 1 ELSE 0
        END) OVERTIME_COUNT ,
        COUNT(1) allCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{yearMonth}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        <if test="list != null">
            AND CBRY IN
            <foreach collection="list" open="(" close=")" item="cbry" separator=",">
                #{cbry}
            </foreach>
        </if>
        GROUP BY CBRY , CBRY_NAME) a
        JOIN (SELECT CBRY , CBRY_NAME , COUNT(DISTINCT CBQYD_BM) qydCount
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{yearMonth}
        AND IS_GENERAL = #{isGeneral}
        <!--AND CBRQ <![CDATA[ > ]]> END_RQ AND CBBZ = '1'-->
        <if test="list != null">
            AND CBRY IN
            <foreach collection="list" open="(" close=")" item="cbry" separator=",">
                #{cbry}
            </foreach>
        </if>
        GROUP BY CBRY , CBRY_NAME) b ON a.CBRY = b.CBRY
        where a.OVERTIME_COUNT > 0
    </select>
    <select id="getOvertimeCollectDeatil"
            resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadOverTimeDetail">
        SELECT a.*,
        ROUND((overTimeReadCount / a.shouldReadCount) * 100, 1) as overTimeRatio
        FROM ( SELECT CBRY cbry, CBRY_NAME cbryName, CBQY_MC cbqyMc, CBQY_BM cbqyBm, QYDMC cbqydMc, CBQYD_BM cbqydBm,
        TO_CHAR(START_RQ,'yyyy-MM-dd') as startTime, TO_CHAR(END_RQ,'yyyy-MM-dd') endTime ,
        COUNT(1) shouldReadCount,
        SUM(CASE WHEN CBBZ = '1' and CBRQ <![CDATA[ <= ]]> END_RQ THEN 1 ELSE 0 END) accoTimeReadCount,
        <!--超过当前时间也没有抄表的也算是超时-->
        SUM(CASE WHEN (CBBZ = '1' and CBRQ <![CDATA[ > ]]> END_RQ) or (CBBZ = '0' and SYSDATE <![CDATA[ > ]]> END_RQ)
        THEN 1 ELSE 0 END) overTimeReadCount
        FROM TF_YWPN_READMETERHIS_W
        <where>
            <!--AND IS_GENERAL = #{search.isGeneral}-->
            <if test="cbry != null">
                AND CBRY = #{cbry}
            </if>
            <if test="yearMonth != null and yearMonth != ''">
                AND MONTH_PLAN = #{yearMonth}
            </if>
        </where>
        GROUP BY CBRY , CBRY_NAME , CBQY_MC , CBQY_BM , QYDMC , CBQYD_BM , TO_CHAR(START_RQ,'yyyy-MM-dd'),
        TO_CHAR(END_RQ,'yyyy-MM-dd')) a
        where overTimeReadCount > 0
        ORDER BY a.cbryName , a.cbqyMc , a.cbqydMc , a.startTime , a.endTime
    </select>
    <select id="getMaxId" resultType="java.lang.Long">
        select nvl(max(ID), 0) + 1
        from TF_YWPN_READMETERHIS_W
    </select>
    <select id="accuracyCollect" resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.AccuracyCollect">
        SELECT temp.* , TRUNC((temp.accuracyNum/temp.allNum) * 100, 1) as accuracyRatio
        FROM
        (SELECT CBRY , CBRY_NAME , CBQY_BM , CBQY_MC ,
        <!--allnum改成实际抄表数，不是之前的应抄数-->
        SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END ) as allNum ,
        SUM(CASE WHEN ISBREAK_READ = '1' THEN 1 ELSE 0 END ) as breakNum ,
        SUM(CASE WHEN CBBZ = '1' THEN DECODE(ISBREAK_READ, NULL , 1 , '1' , 0 , 0) ELSE 0 END ) as accuracyNum
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{yearMonth}
        <if test="list != null">
            AND CBRY IN
            <foreach collection="list" item="cbry" open="(" close=")" separator=",">
                #{cbry}
            </foreach>
        </if>
        AND IS_GENERAL = #{isGeneral}
        GROUP BY CBRY , CBRY_NAME , CBQY_BM , CBQY_MC
        order by CBQY_MC) temp
        WHERE temp.allNum != 0
    </select>

    <select id="cardUpdateCollect"
            resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.AccuracyCollect">
        SELECT temp.* , TRUNC((temp.accuracyNum/temp.allNum) * 100, 1) as accuracyRatio
        FROM
        (SELECT CBRY , CBRY_NAME , CBQY_BM , CBQY_MC ,
        <!--allnum改成实际抄表数，不是之前的应抄数-->
        SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END ) as allNum ,
        SUM(CASE WHEN IS_UPDATECBSL = '1' THEN 1 ELSE 0 END ) as breakNum ,
        SUM(CASE WHEN CBBZ = '1' THEN DECODE(IS_UPDATECBSL, NULL , 1 , '1' , 0 , 0) ELSE 0 END ) as accuracyNum
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = #{yearMonth}
        <if test="list != null">
            AND CBRY IN
            <foreach collection="list" item="cbry" open="(" close=")" separator=",">
                #{cbry}
            </foreach>
        </if>
        AND IS_GENERAL = #{isGeneral}
        GROUP BY CBRY , CBRY_NAME , CBQY_BM , CBQY_MC
        order by CBQY_MC) temp
        WHERE temp.allNum != 0
    </select>


    <update id="updateAuditCbData">
        <foreach collection="list" item="yhInfo" open="begin" close=";end;" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            SET QUALITY_COUNT = (SELECT NVL(MAX(QUALITY_COUNT), 0) + 1
            FROM TF_YWPN_READMETERHIS_W
            WHERE YHBH = #{yhInfo.yhbh}) , SHBZ = '2' , GD_ID = #{yhInfo.gdId}
            WHERE YHBH = #{yhInfo.yhbh} AND SOLE_SIGN = #{yhInfo.soleSign}
        </foreach>
    </update>

    <update id="updateAuditYcbData">
        <foreach collection="list" item="yhInfo" open="begin" close=";end;" separator=";">
            UPDATE TF_YWPN_READMETEREASHIS_W
            SET QUALITY_COUNT = (SELECT NVL(MAX(QUALITY_COUNT), 0) + 1
            FROM TF_YWPN_READMETEREASHIS_W
            WHERE YHBH = #{yhInfo.yhbh}) , SHBZ = '2' , GD_ID = #{yhInfo.gdId}
            WHERE YHBH = #{yhInfo.yhbh} AND SOLE_SIGN = #{yhInfo.soleSign}
        </foreach>
    </update>

    <update id="updateReadmeterhisYhInfo">
        <foreach collection="list" item="item" open="begin" close=";end;" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                <if test="item.yhxm != null and item.yhxm.trim() != ''">
                    YHXM = #{item.yhxm},
                </if>
                <if test="item.yhdz != null and item.yhdz.trim() != ''">
                    YHDZ = #{item.yhdz},
                </if>
                <if test="item.lxdh != null and item.lxdh.trim() != ''">
                    LXDH = #{item.lxdh},
                </if>
                <if test="item.sbbh != null and item.sbbh.trim() != ''">
                    SBBH = #{item.sbbh},
                </if>
                <if test="item.sbcj != null and item.sbcj.trim() != ''">
                    SBCJ = #{item.sbcj},
                </if>
                <if test="item.sblx != null and item.sblx.trim() != ''">
                    SBLX = #{item.sblx},
                </if>
                <if test="item.sbkj != null and item.sbkj.trim() != ''">
                    SBKJ = #{item.sbkj},
                </if>
                <if test="item.ysxz != null and item.ysxz.trim() != ''">
                    YSXZ = #{item.ysxz},
                </if>
                <if test="item.xzxf != null and item.xzxf.trim() != ''">
                    XZXF = #{item.xzxf},
                </if>
                <if test="item.cbpq != null and item.cbpq.trim() != ''">
                    CBPQ = #{item.cbpq},
                </if>
                <if test="item.cbb != null and item.cbb.trim() != ''">
                    CBB = #{item.cbb},
                </if>
                <if test="item.scds != null">
                    SCDS = #{item.scds},
                </if>
            </set>
            WHERE YHBH = #{item.yhbh} AND MONTH_PLAN = #{monthPlan} AND CBBZ = '0'
        </foreach>
    </update>

    <update id="updateReadmeterhisYhWaterVolume">
        <foreach collection="list" item="item" open="begin" close=";end;" separator=";">
            UPDATE TF_YWPN_READMETERHIS_W
            <set>
                SCDS = #{item.scds}
            </set>
            WHERE YHBH = #{item.yhbh} AND MONTH_PLAN = #{monthPlan} AND CBBZ = '0'
        </foreach>
    </update>
    <update id="updateInspectRatio">
        update TF_YWPN_SYSCHILDCODE_D
        SET CCODE = #{ratio}
        WHERE ID = 994
    </update>

    <!--select k.yhbh , k.yhxm , k.LXSJ as lxdh , k.yhdz , l.eventcount from TF_YWPN_QFKHJBXX_W k
    right join (SELECT aa.YHBH, (aa.cbEventCount + bb.gdEventCount + cc.zjEventCount) eventcount
    FROM (SELECT a.YHBH, SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END) cbEventCount
    FROM TF_YWPN_QFKHJBXX_W a
    LEFT JOIN TF_YWPN_READMETERHIS_W b ON a.YHBH = b.YHBH
    <where>
        <if test="startDate != null and endDate != null">
            AND (b.cbrq IS NULL OR (b.cbrq BETWEEN #{startDate} AND #{endDate}))
        </if>
        <if test="inputcontent != null and inputcontent != ''">
            AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
            or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
        </if>
    </where>
    GROUP BY a.YHBH
    order by a.YHBH) aa
    LEFT JOIN (SELECT a.YHBH, SUM(CASE WHEN b.ID IS NOT NULL THEN 1 ELSE 0 END) gdEventCount
    FROM TF_YWPN_QFKHJBXX_W a
    LEFT JOIN TF_YWPN_GDINFO_W b ON a.YHBH = b.KHBH
    <where>
        <if test="startDate != null and endDate != null">
            AND (b.DJSJ IS NULL OR (b.DJSJ BETWEEN #{startDate} AND #{endDate}))
        </if>
        <if test="inputcontent != null and inputcontent != ''">
            AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
            or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
        </if>
    </where>
    GROUP BY a.YHBH
    order by a.YHBH) bb
    ON aa.YHBH = bb.YHBH
    LEFT JOIN(SELECT a.YHBH, SUM(CASE WHEN b.ID IS NOT NULL THEN 1 ELSE 0 END) zjEventCount
    FROM TF_YWPN_QFKHJBXX_W a
    LEFT JOIN TF_YWPN_ZLRZ_W b ON a.YHBH = b.CLIENT_NUMBER
    <where>
        <if test="startDate != null and endDate != null">
            AND (b.DISPATCH_TIME IS NULL OR (b.DISPATCH_TIME BETWEEN #{startDate} AND #{endDate}))
        </if>
        <if test="inputcontent != null and inputcontent != ''">
            AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
            or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
        </if>
    </where>
    GROUP BY a.YHBH
    order by a.YHBH) cc ON aa.YHBH = cc.YHBH) l
        on k.yhbh = l.yhbh-->

    <select id="eventCollect" resultType="com.tofly.watermeter.custom.entity.quality.QualityEvent">
        select k.yhbh , k.yhxm , k.LXSJ as lxdh , k.yhdz , l.eventcount from TF_YWPN_QFKHJBXX_W k
        right join (SELECT aa.YHBH, (aa.cbEventCount + bb.gdEventCount + cc.zjEventCount) eventcount
        FROM (SELECT a.YHBH, SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END) cbEventCount
        FROM TF_YWPN_QFKHJBXX_W a
        LEFT JOIN TF_YWPN_READMETERHIS_W b ON a.YHBH = b.YHBH
        <where>
            <if test="startDate != null and endDate != null">
                AND (b.cbrq IS NULL OR (b.cbrq BETWEEN #{startDate} AND #{endDate}))
            </if>
            <if test="inputcontent != null and inputcontent != ''">
                AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
                or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
            </if>
        </where>
        GROUP BY a.YHBH
        order by a.YHBH) aa
        LEFT JOIN (SELECT a.YHBH, SUM(CASE WHEN b.ID IS NOT NULL THEN 1 ELSE 0 END) gdEventCount
        FROM TF_YWPN_QFKHJBXX_W a
        LEFT JOIN TF_YWPN_GDINFO_W b ON a.YHBH = b.KHBH
        <where>
            <if test="startDate != null and endDate != null">
                AND (b.DJSJ IS NULL OR (b.DJSJ BETWEEN #{startDate} AND #{endDate}))
            </if>
            <if test="inputcontent != null and inputcontent != ''">
                AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
                or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
            </if>
        </where>
        GROUP BY a.YHBH
        order by a.YHBH) bb
        ON aa.YHBH = bb.YHBH
        LEFT JOIN(SELECT a.YHBH, SUM(CASE WHEN b.ID IS NOT NULL THEN 1 ELSE 0 END) zjEventCount
        FROM TF_YWPN_QFKHJBXX_W a
        LEFT JOIN TF_YWPN_ZLRZ_W b ON a.YHBH = b.CLIENT_NUMBER
        <where>
            <if test="startDate != null and endDate != null">
                AND (b.DISPATCH_TIME IS NULL OR (b.DISPATCH_TIME BETWEEN #{startDate} AND #{endDate}))
            </if>
            <if test="inputcontent != null and inputcontent != ''">
                AND (a.YHBH LIKE '%' || #{inputcontent} || '%' or a.YHXM LIKE '%' || #{inputcontent} || '%'
                or a.YHDZ LIKE '%' || #{inputcontent} || '%' or a.LXSJ LIKE '%' || #{inputcontent} || '%')
            </if>
        </where>
        GROUP BY a.YHBH
        order by a.YHBH) cc ON aa.YHBH = cc.YHBH) l
        on k.yhbh = l.yhbh
    </select>
    <select id="getCbEventInfo" resultType="com.tofly.watermeter.custom.entity.quality.QualityEventInfo">
        SELECT YHBH,
               CBRQ                     as eventtime,
               DECODE(CBBZ, '1', '已抄表') as eventcontent,
               DECODE(CBBZ, '1', '1')   as eventtype,
               SOLE_SIGN                as eventid,
               SOLE_SIGN                as soleSign
        FROM TF_YWPN_READMETERHIS_W
        WHERE YHBH = #{yhbh}
          AND CBBZ = '1'
          AND cbrq BETWEEN #{startDate} AND #{endDate}
        ORDER BY CBRQ DESC
    </select>
    <select id="getGdEventInfo" resultType="com.tofly.watermeter.custom.entity.quality.QualityEventInfo">
        select *
        from (
                 SELECT KHBH                                                as yhbh,
                        DJSJ                                                as eventtime,
                        CASE
                            WHEN GDXF2 is not null THEN (b.GDLX_MC || '-' || bb.GDLX_MC || '-' || bbb.GDLX_MC || '(' ||
                                                         a.GDBH || ')')
                            ELSE
                                CASE
                                    WHEN GDXF1 is not null THEN (b.GDLX_MC || '-' || bb.GDLX_MC || '(' || a.GDBH || ')')
                                    else
                                        (b.GDLX_MC || '，' || '(' || a.GDBH || ')') end
                            END                                             as eventcontent,
                        (CASE WHEN a.ID is not null THEN '2' ELSE null end) as eventtype,
                        a.ID                                                as eventid,
                        c.OTHER_BH                                          as qwxid,
                        c.GDSTATE
                 FROM TF_YWPN_GDINFO_W a
                          LEFT JOIN
                      TF_YWPN_GDGLTYPEBM_W b
                      ON a.GDLX = b.ID
                          left JOIN
                      TF_YWPN_GDGLTYPEBM_W bb
                      on a.GDXF1 = bb.ID
                          left JOIN
                      TF_YWPN_GDGLTYPEBM_W bbb
                      on a.GDXF2 = bbb.ID
                          LEFT JOIN TF_YWPN_GDCIRCULATION_W c
                                    ON a.GDBH = c.GDBH
                 WHERE KHBH = #{yhbh}
                   AND DJSJ BETWEEN #{startDate} AND #{endDate}
                 ORDER BY DJSJ DESC)
        where GDSTATE is null
           or GDSTATE = '0'
    </select>
    <select id="getZjEventInfo" resultType="com.tofly.watermeter.custom.entity.quality.QualityEventInfo">
        SELECT CLIENT_NUMBER                                       as yhbh,
               DISPATCH_TIME                                       as eventtime,
               (b.GDLX_MC || '(' || a.CERTIFICATION_NUMBER || ')') as eventcontent,
               (CASE WHEN a.ID is not null THEN '3' ELSE null end) as eventtype,
               a.ID                                                as eventid
        FROM TF_YWPN_ZLRZ_W a
                 LEFT JOIN
             TF_YWPN_GDGLTYPEBM_W b
             ON a.CERTIFICATION_PROJECT = b.ID
        WHERE CLIENT_NUMBER = #{yhbh}
          AND DISPATCH_TIME BETWEEN #{startDate} AND #{endDate}
        ORDER BY DISPATCH_TIME DESC
    </select>


    <update id="updateSoleSign">
        <foreach collection="list" open="begin" close=";end;" item="read" separator=";">
            update TF_YWPN_READMETERHIS_W
            <set>
                SOLE_SIGN = #{read.soleSign}
            </set>
            where YHBH = #{read.yhbh}
            AND MONTH_PLAN = #{yearMonth}
        </foreach>
    </update>

    <update id="updateChildSort">
        <foreach collection="list" open="begin" close=";end;" item="read" separator=";">
            update TF_YWPN_READMETERHIS_W
            <set>
                CHILD_SORT = #{read.childSort},
                cbxh = #{read.cbxh}
            </set>
            where SOLE_SIGN = #{read.soleSign}
        </foreach>
    </update>
    <update id="updateTaskCbxh">
        UPDATE TF_YWPN_READMETERHIS_W a
        set CBXH       = (SELECT row_flag
                          from (SELECT SOLE_SIGN,
                                       row_number() OVER (PARTITION BY CBQYD_BM ORDER BY CBXH , CHILD_SORT) as row_flag
                                FROM TF_YWPN_READMETERHIS_W
                                WHERE CBQYD_BM = #{qydbm}
                                  and MONTH_PLAN = #{monthPlan}) b
                          WHERE a.SOLE_SIGN = b.SOLE_SIGN),
            CHILD_SORT = 0
        WHERE a.SOLE_SIGN IN (SELECT SOLE_SIGN
                              FROM TF_YWPN_READMETERHIS_W
                              WHERE CBQYD_BM = #{qydbm}
                                and MONTH_PLAN = #{monthPlan})
    </update>

    <select id="getOffline" resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadCollect">
        SELECT CBRY as CBRY_ID, CBRY_NAME as cbry, CBQY_BM as cbqybm, CBQY_MC as cbqy, CBQYD_BM as cbqydbm, QYDMC as
        cbqyd ,
        COUNT(1) as ALL_NUM ,
        SUM(CASE WHEN CBBZ = '1' THEN 1 ELSE 0 END) AS CB_NUM ,
        SUM(CASE WHEN CBBZ = '0' THEN 1 ELSE 0 END) AS NO_CB_NUM ,
        START_DAY , END_DAY , wm_concat(DISTINCT(TO_CHAR(cbb_mc))) as cbb_mc
        FROM TF_YWPN_READMETERHIS_W aa LEFT JOIN TF_YWPN_DMACISCBB_W bb on aa.cbb = bb.cbb_bm
        WHERE MONTH_PLAN = #{yearMonth}
        AND IS_GENERAL = #{isGeneral}
        <if test="cbryList != null">
            AND CBRY IN
            <foreach collection="cbryList" open="(" item="cbry" separator="," close=")">
                #{cbry}
            </foreach>
        </if>
        <if test="qybmList != null">
            AND CBQY_BM IN
            <foreach collection="qybmList" open="(" item="qybm" separator="," close=")">
                #{qybm}
            </foreach>
        </if>
        <if test="qydbmList != null">
            AND CBQYD_BM IN
            <foreach collection="qydbmList" open="(" item="qydbm" separator="," close=")">
                #{qydbm}
            </foreach>
        </if>
        GROUP BY CBRY , CBRY_NAME , CBQY_BM , CBQY_MC , CBQYD_BM , QYDMC , START_DAY , END_DAY
        ORDER BY CBRY_NAME , CBQY_MC , QYDMC
    </select>
    <select id="fluctuateCollectDeatil" resultMap="readmeterhisMap">
        SELECT YHBH , YHXM , CBQY_MC , QYDMC , YSXZ , SBKJ , ABNOMAL_ID , AUDIT_REASON ,
        MONTH1 , CBRQ1 , CBYF , CBSL , SJSL
        FROM TF_YWPN_READMETERHIS_W WHERE SOLE_SIGN IN (
        SELECT SOLE_SIGN FROM (
        SELECT SOLE_SIGN , ROW_NUMBER() OVER(partition by
        <if test="type == 1">
            SBKJ
        </if>
        <if test="type == 2">
            YSXZ
        </if>
        <if test="type == 3">
            ABNOMAL_ID
        </if>
        order by SOLE_SIGN) row_num
        FROM TF_YWPN_READMETERHIS_W
        WHERE SHBZ is NOT NULL
        AND IS_GENERAL = #{isGeneral}
        <if test="qybm != null and qybm != ''">
            AND CBQY_BM = #{qybm}
        </if>
        <if test="qydbmList != null">
            AND CBQYD_BM IN
            <foreach collection="qydbmList" open="(" close=")" separator="," item="qydbm">
                #{qydbm}
            </foreach>
        </if>
        <if test="sbkjList != null">
            AND SBKJ IN
            <foreach collection="sbkjList" open="(" close=")" separator="," item="sbkj">
                #{sbkj}
            </foreach>
        </if>
        <if test="abnormalList != null">
            AND ABNOMAL_ID IN
            <foreach collection="abnormalList" open="(" close=")" separator="," item="abnomal">
                #{abnomal}
            </foreach>
        </if>
        <if test="ysxzList != null">
            AND YSXZ IN
            <foreach collection="ysxzList" open="(" close=")" separator="," item="ysxz">
                #{ysxz}
            </foreach>
        </if>
        <if test="yearMonth != null">
            AND MONTH_PLAN = #{yearMonth}
        </if>
        <if test="queryType == 5">
            and CONTINUITY_UP > 0
        </if>
        <if test="queryType == 6">
            and CONTINUITY_DOWN > 0
        </if>
        ) temp
        WHERE temp.row_num <![CDATA[ <= ]]> #{queryCount} )
    </select>
    <select id="cardDataAnalyeCollect" resultType="java.util.Map">
        SELECT COUNT(1) ALL_NUM ,
        SUM(CASE WHEN DIFF_VALUE > 0 THEN 1 ELSE 0 END ) AS POSITIVE_CLIENT,
        SUM(CASE WHEN DIFF_VALUE > 0 THEN DIFF_VALUE ELSE 0 END) AS POSITIVE_NUM,
        SUM(CASE WHEN DIFF_VALUE <![CDATA[ < ]]> 0 THEN 1 ELSE 0 END) AS MINUS_CLIENT,
        SUM(CASE WHEN DIFF_VALUE <![CDATA[ < ]]> 0 THEN ABS(DIFF_VALUE) ELSE 0 END ) AS MINUS_NUM,
        SUM(CASE WHEN DIFF_VALUE = 0 THEN 1 ELSE 0 END) AS EQUAL_CLIENT
        FROM TF_YWPN_READMETERHIS_W
        WHERE cbbz = '1' and IS_GENERAL = '1'
        <if test="startDate!=null and endDate!=null">
            and CBRQ BETWEEN #{startDate} and #{endDate}
        </if>
        <if test="startDate==null and endDate!=null">
            and CBRQ <![CDATA[ <= ]]> #{endDate}
        </if>
        <if test="startDate!=null and endDate==null">
            and CBRQ <![CDATA[ >= ]]> #{startDate}
        </if>
    </select>
    <select id="getTimeAbnormalCollect"
            resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadOverTime">
        select
        CBRY_NAME , CBRY , COUNT(1) as over_Time_Count, ROUND(SUM(DIFF_TIME)/60/COUNT(1),2) as average_Diff_Time
        from TF_YWPN_READMETERHIS_W
        <where>
            CBBZ = '1'
            <!--AND IS_GENERAL = #{search.isGeneral}-->
            AND MONTH_PLAN = #{yearMonth}
            AND DIFF_TIME <![CDATA[ >= ]]> #{overDiffTime}
            <if test="list != null">
                AND CBRY IN
                <foreach collection="list" open="(" close=")" separator="," item="cbry">
                    #{cbry}
                </foreach>
            </if>
        </where>
        GROUP BY CBRY , CBRY_NAME
    </select>
    <select id="getTimeAbnormalTwo"
            resultType="com.tofly.watermeter.custom.entity.meterRead.readstatic.ReadOverTimeDetail">
        select
        CBRY , CBRY_NAME , CBQYD_BM , QYDMC CBQYD_MC, CBQY_BM , CBQY_MC , ROUND(DIFF_TIME/60,2) as DIFF_TIME, lag(CBRQ ,
        1) over
        (order by CBRQ) START_TIME ,CBRQ as END_TIME
        from TF_YWPN_READMETERHIS_W
        <where>
            CBBZ = '1'
            <!--AND IS_GENERAL = #{search.isGeneral}-->
            AND MONTH_PLAN = #{yearMonth}
            AND DIFF_TIME <![CDATA[ >= ]]> #{overDiffTime}
            <if test="list != null">
                AND CBRY IN
                <foreach collection="list" open="(" close=")" separator="," item="cbry">
                    #{cbry}
                </foreach>
            </if>
        </where>
        order by CBRY_NAME , CBRY , CBQY_MC , CBQYD_MC
    </select>


    <select id="getClient" resultMap="readmeterhisMap">
        select distinct yhbh, cbkh, cbxh, yhxm, yhdz, lxdh, sbbh, sbcj, sblx, sbkj, ysxz, xzxf, cbpq, cbb, cwrq, scds,
        bcds, bcjjl, cbbz, jfzt, cbry, cbrq, jhrq, xzbs, fileid, cbfs_code, shbz, audit_user, audit_cause, cbyf, cbsl,
        ysje, audit_result, audit_reason, issyn, bz, lsbh, sxjmessage, x, y, address_state, is_general, month1, month2,
        month3, sin_dou_month, start_day, end_day, cbqy_mc, cbqy_bm, cbqyd_bm, month_plan, cbry_name, qydmc, is_offline,
        start_rq, end_rq, cbrq1, cbrq2, cbrq3, jbzd, xbqd, hblx, hbrq, zero_count, continuity_up, continuity_down,
        continuity_equal, diff_time, yearonyear1, yearonyear2, yearonyear3, yearonmonth1, yearonmonth2, yearonmonth3,
        audit_time, audit_type, quality_count, update_water, upload_day, abnomal_id, sjsl, update_bcds, update_scds,
        is_updatecbsl, is_updateid, isfirst, isbreak_read, order_ids, sxj_sbqd, cbrq4, is_estimate, img_url
        from TF_YWPN_READMETERHIS_W
        where CBBZ = 1
        <if test="search.isGeneral != null and search.isGeneral != ''">
            and IS_GENERAL = #{search.isGeneral}
        </if>
        <if test="search.inputContent != null and search.inputContent != ''">
            and yhbh || yhxm || yhdz || lxdh like '%' || #{search.inputContent} || '%'
        </if>
        <if test="search.cbb != null and search.cbb != ''">
            and CBB in
            <foreach collection="search.cbb.split(',')" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="search.cbry != null">
            and CBRY = #{search.cbry}
        </if>
        <if test="search.sspq != null and search.sspq != ''">
            and CBQY_BM in
            <foreach collection="search.sspq.split(',')" item="s" open="(" separator="," close=")">
                #{s}
            </foreach>
        </if>
        <if test="search.startTime != null and search.startTime != '' and search.endTime != null and search.endTime != ''">
            and CBRQ between to_date(#{search.startTime},'yyyy-mm-dd hh24:mi:ss') and
            to_date(#{search.endTime},'yyyy-mm-dd hh24:mi:ss')
        </if>
        order by yhbh,cbrq
    </select>

    <select id="getFileInfoList" resultType="string">
        select FILE_PATH
        from TF_YWPN_FILE_W
        where ID in
        <foreach collection="fileId" item="fi" open="(" separator="," close=")">
            #{fi}
        </foreach>
    </select>

    <update id="handleAbnormalUser">
        update TF_YWPN_READMETERHIS_W r
        set SCDS = (select q.SCDS from TF_YWPN_QFKHJBXX_W q where q.YHBH = r.YHBH)
        where MONTH_PLAN = to_char(sysdate, 'yyyyMM')
          and YHBH in (select r.YHBH
                       from TF_YWPN_READMETERHIS_W r
                                left join TF_YWPN_QFKHJBXX_W q
                                          on r.YHBH = q.YHBH
                       where CBBZ = 0
                         and MONTH_PLAN = to_char(sysdate, 'yyyyMM')
                         and r.scds != q.SCDS
                         and r.XZBS = 0)
    </update>

</mapper>
