<?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.ReadmeterMapper">

    <resultMap id="readmeterMap" type="com.tofly.watermeter.entity.Readmeter">
        <id property="id" column="ID"/>
        <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="auditResult" column="AUDIT_RESULT"/>
        <result property="cbsl" column="CBSL"/>
        <result property="auditReason" column="AUDIT_REASON"/>
        <result property="issyn" column="ISSYN"/>
        <result property="month1" column="MONTH1"/>
        <result property="month2" column="MONTH2"/>
        <result property="month3" column="MONTH3"/>
        <result property="bz" column="BZ"/>
        <result property="cbzq" column="CBZQ"/>
        <result property="sxjmessage" column="SXJMESSAGE"/>
        <result property="cbyf" column="CBYF"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="addressState" column="ADDRESS_STATE"/>
        <result property="isGeneral" column="IS_GENERAL"/>
        <result property="cbryName" column="CBRY_NAME"/>
        <result property="isOffline" column="IS_OFFLINE"/>
        <result property="cbrq1" column="CBRQ1"/>
        <result property="cbrq2" column="CBRQ2"/>
        <result property="cbrq3" column="CBRQ3"/>
        <result property="yearonyear1" column="YEARONYEAR1"/>
        <result property="yearonyear2" column="YEARONYEAR2"/>
        <result property="yearonyear3" column="YEARONYEAR3"/>
    </resultMap>
    <select id="allocationInfo" resultType="map">
        SELECT a.YHBH , a.YHDZ , a.YHXM , a.CBQY_MC , a.QYDMC ,
        a.SBBH , d.PQMC as CBPQ, c.CBB_MC as CBB , e.YSXZ_MC as YSXZ
        FROM TF_YWPN_READMETERHIS_W a
        LEFT JOIN TF_YWPN_QFMIDYSXZBM_E e ON a.YSXZ = e.YSXZ_BM
        LEFT JOIN TF_YWPN_DMACISCBB_W c ON a.CBB = c.CBB_BM
        LEFT JOIN TF_YWPN_QFCBCBPQ_W d ON a.CBPQ = d.PQBM
        WHERE a.CBRY = #{search.cbry}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        and a.MONTH_PLAN = #{search.cwyf}
        <if test="qybmList != null ">
            AND a.CBQY_BM IN
            <foreach collection="qybmList" open="(" separator="," item="qybm" close=")">
                #{qybm}
            </foreach>
        </if>
        <if test="cbbList != null">
            AND a.CBQYD_BM IN
            <foreach collection="cbbList" open="(" separator="," item="cbb" close=")">
                #{cbb}
            </foreach>
        </if>
        ORDER BY a.QYDMC
    </select>

    <select id="allocationInfoPage" resultType="java.util.Map">
        SELECT a.YHBH , a.YHDZ , a.YHXM , a.CBQY_MC , a.QYDMC ,
        a.SBBH , d.PQMC as CBPQ, c.CBB_MC as CBB , e.YSXZ_MC as YSXZ
        FROM TF_YWPN_READMETERHIS_W a
        LEFT JOIN TF_YWPN_QFMIDYSXZBM_E e ON a.YSXZ = e.YSXZ_BM
        LEFT JOIN TF_YWPN_DMACISCBB_W c ON a.CBB = c.CBB_BM
        LEFT JOIN TF_YWPN_QFCBCBPQ_W d ON a.CBPQ = d.PQBM
        WHERE a.CBRY = #{search.cbry}
        <!--AND IS_GENERAL = #{search.isGeneral}-->
        and a.MONTH_PLAN = #{search.cwyf}
        <if test="qybmList != null ">
            AND a.CBQY_BM IN
            <foreach collection="qybmList" open="(" separator="," item="qybm" close=")">
                #{qybm}
            </foreach>
        </if>
        <if test="cbbList != null">
            AND a.CBQYD_BM IN
            <foreach collection="cbbList" open="(" separator="," item="cbb" close=")">
                #{cbb}
            </foreach>
        </if>
        ORDER BY a.QYDMC
    </select>
    <select id="inspectCollect" resultType="com.tofly.watermeter.custom.entity.quality.QualityResult">
        SELECT temp.* , temp.allCountTemp allCount ,
        CEIL((temp.waitCountTemp * #{ratio}) / 100) waitCount ,
        CEIL((temp.disposeCountTemp * #{ratio}) / 100) disposeCount ,
        CEIL((temp.deployCountTemp * #{ratio}) / 100) deployCount
        FROM (SELECT CBQY_BM qybm, CBQY_MC qymc, CBQYD_BM qydbm, QYDMC qydmc,
        COUNT(1) allCountTemp ,
        SUM(CASE WHEN SHBZ = '0' THEN 1 ELSE 0 END) waitCountTemp ,
        SUM(CASE WHEN SHBZ = '1' or SHBZ = '3' THEN 1 ELSE 0 END) disposeCountTemp ,
        SUM(case when SHBZ = '2' or SHBZ = '3' then 1 else 0 end) deployCountTemp
        FROM TF_YWPN_READMETERHIS_W
        <where>
            IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="qualityStatis.cbqy != null and qualityStatis.cbqy != ''">
                AND CBQY_BM = #{qualityStatis.cbqy}
            </if>
            <if test="qualityStatis.startTime != null and qualityStatis.endTime != null">
                AND START_RQ BETWEEN #{qualityStatis.startTime} AND #{qualityStatis.endTime}
            </if>
        </where>
        GROUP BY CBQY_BM , CBQY_MC , CBQYD_BM , QYDMC
        ORDER BY CBQY_MC , QYDMC) temp
    </select>
    <select id="analyzeCollect" resultType="com.tofly.watermeter.custom.entity.quality.QualityResult">
        SELECT CBQY_BM qybm,
        CBQY_MC qymc,
        SUM(
        CASE
        WHEN SHBZ IS NOT null THEN 1
        ELSE 0
        END
        ) abnormalCount,
        SUM(
        CASE
        WHEN SHBZ = '0' THEN 1
        ELSE 0
        END
        ) waitCount,
        SUM(
        CASE
        WHEN SHBZ = '1'
        or SHBZ = '3' THEN 1
        ELSE 0
        END
        ) disposeCount,
        SUM(
        case
        when SHBZ = '2'
        or SHBZ = '3' then 1
        else 0
        end
        ) deployCount
        FROM (select
        /*+index(TF_YWPN_READMETERHIS_W READMETERHIS_CBRY_CBRQ)*/
        CBQY_BM,
        CBQY_MC,
        SHBZ,
        IS_GENERAL,
        CBRQ
        from TF_YWPN_READMETERHIS_W
        <where>
            IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="list != null">
                AND CBQY_BM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
            <if test="qualityStatis.startTime != null and qualityStatis.endTime != null ">
               and CBRQ BETWEEN cast (#{qualityStatis.startTime} as date )and cast(#{qualityStatis.endTime} as date)
            </if>
        </where>
        union all
        select
        /*+index(TF_YWPN_READMETEREASHIS_W INDEX_MY_CBRQ)*/
        CBQY_BM,
        CBQY_MC,
        SHBZ,
        IS_GENERAL,
        CBRQ
        from TF_YWPN_READMETEREASHIS_W
        <where>
            IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="list != null">
                AND CBQY_BM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
            <if test="qualityStatis.startTime != null and qualityStatis.endTime != null ">
                and CBRQ BETWEEN cast (#{qualityStatis.startTime} as date )and cast(#{qualityStatis.endTime} as date)
            </if>
        </where>
        )
        GROUP BY CBQY_BM , CBQY_MC
        ORDER BY CBQY_MC
    </select>
    <resultMap id="analyzeMap" type="com.tofly.watermeter.custom.entity.quality.QualityResult">
        <result property="abnomalId" column="ABNOMAL_ID"/>
        <result property="abnormalCount" column="abnormalCount"/>
        <result property="waitCount" column="waitCount"/>
        <result property="disposeCount" column="disposeCount"/>
        <result property="deployCount" column="deployCount"/>
        <association property="abnomalName" column="ABNOMAL_ID"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
    </resultMap>
    <select id="analyzeAbnromal" resultMap="analyzeMap">
        SELECT ABNOMAL_ID ,
        SUM(CASE WHEN SHBZ IS NOT null THEN 1 ELSE 0 END) abnormalCount ,
        SUM(CASE WHEN SHBZ = '0' THEN 1 ELSE 0 END) waitCount ,
        SUM(CASE WHEN SHBZ = '1' or SHBZ = '3' THEN 1 ELSE 0 END) disposeCount ,
        SUM(case when SHBZ = '2' or SHBZ = '3' then 1 else 0 end) deployCount
        FROM (select
        /*+index(TF_YWPN_READMETERHIS_W READMETERHIS_CBRY_CBRQ)*/
        SHBZ,
        ABNOMAL_ID
        from TF_YWPN_READMETERHIS_W
        <where>
            ABNOMAL_ID is not null
            and IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="list != null">
                AND CBQY_BM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
            <if test="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 == 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>
        </where>
        union all
        select
        /*+index(TF_YWPN_READMETEREASHIS_W INDEX_MY_CBRQ)*/
        SHBZ,
        ABNOMAL_ID
        from TF_YWPN_READMETEREASHIS_W
        <where>
            ABNOMAL_ID is not null
            and IS_GENERAL = #{qualityStatis.isGeneral}
            <if test="list != null">
                AND CBQY_BM IN
                <foreach collection="list" open="(" item="qybm" separator="," close=")">
                    #{qybm}
                </foreach>
            </if>
            <if test="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 == 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>
        </where>)
        GROUP BY ABNOMAL_ID
        ORDER BY ABNOMAL_ID
    </select>


    <select id="queryCollect" resultType="java.util.Map">
        SELECT b.* , a.ljsl
        FROM (SELECT YHBH , SUM(CBSL) ljsl
        FROM TF_YWPN_QFCBQFMXLS_W
        <where>
            <if test="qualityStatis.startDate != null and qualityStatis.endDate != null and qualityStatis.startDate != '' and qualityStatis.endDate != ''">
                AND CBYF BETWEEN #{qualityStatis.startDate} AND #{qualityStatis.endDate}
            </if>
        </where>
        GROUP BY YHBH
        ) a
        JOIN (SELECT aa.YHBH , bb.QYMC , cc.QYDMC, dd.YHXM , dd.LXDH , dd.YHDZ
        FROM TF_YWPN_YHBINGQY_W aa LEFT JOIN TF_YWPN_CBQYBM_W bb ON aa.QYBM = bb.QYBM LEFT JOIN TF_YWPN_CBQYDBM_W cc ON
        aa.QYDBM = cc.QYDBM
        JOIN TF_YWPN_QFKHJBXX_W dd ON aa.YHBH = dd.YHBH
        <where>
            <if test="qualityStatis.inputcontent != null and qualityStatis.inputcontent != ''">
                AND (dd.YHBH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHXM like '%' ||
                #{qualityStatis.inputcontent} || '%'
                or dd.LXDH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHDZ like '%' ||
                #{qualityStatis.inputcontent} || '%')
            </if>
            <if test="qualityStatis.cbqy != null and qualityStatis.cbqy != ''">
                AND aa.QYBM = #{qualityStatis.cbqy}
            </if>
            <if test="list != null">
                AND aa.QYDBM IN
                <foreach collection="list" item="qyd" open="(" close=")" separator=",">
                    #{qyd}
                </foreach>
            </if>
        </where>
        ) b ON a.YHBH = b.YHBH
        order by QYMC , QYDMC
    </select>
    <select id="queryCollectHistory" resultType="java.util.Map">
        select aaa.* , bbb.ljsl from
        <!--根据条件查询筛选用户信息-->
        (SELECT aa.YHBH , bb.QYMC , cc.QYDMC, dd.YHXM , dd.LXDH , dd.YHDZ
        FROM TF_YWPN_YHBINGQY_W aa
        LEFT JOIN TF_YWPN_CBQYBM_W bb ON aa.QYBM = bb.QYBM LEFT JOIN TF_YWPN_CBQYDBM_W cc ON aa.QYDBM = cc.QYDBM
        JOIN TF_YWPN_QFKHJBXX_W dd ON aa.YHBH = dd.YHBH
        <where>
            <if test="qualityStatis.inputcontent != null and qualityStatis.inputcontent != ''">
                AND (dd.YHBH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHXM like '%' ||
                #{qualityStatis.inputcontent} || '%'
                or dd.LXDH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHDZ like '%' ||
                #{qualityStatis.inputcontent} || '%')
            </if>
            <if test="qualityStatis.cbqy != null and qualityStatis.cbqy != ''">
                AND aa.QYBM = #{qualityStatis.cbqy}
            </if>
            <if test="list != null">
                AND aa.QYDBM IN
                <foreach collection="list" item="qyd" open="(" close=")" separator=",">
                    #{qyd}
                </foreach>
            </if>
        </where>
        ) aaa
        join
        (SELECT
        <!--用户编号：因为存在当月抄表中历史记录没有情况，所以对用户编号判断处理-->
        NVL(a.YHBH, b.YHBH) as yhbh ,
        <!--处理历史水量和当月水量数据-->
        CASE WHEN a.aljsl is not null THEN ( CASE WHEN b.bcbsl is not null THEN a.aljsl + b.bcbsl ELSE a.aljsl END )
        ELSE b.bcbsl END as ljsl
        FROM
        <!--查询历史水量-->
        (SELECT aa.YHBH , nvl(SUM(aa.CBSL) , 0) aljsl
        FROM TF_YWPN_QFCBQFMXLS_W aa join TF_YWPN_YHBINGQY_W bb
        on aa.yhbh = bb.yhbh
        <where>
            <if test="qualityStatis.startDate != null and qualityStatis.endDate != null and qualityStatis.startDate != '' and qualityStatis.endDate != ''">
                AND aa.CBYF BETWEEN #{qualityStatis.startDate} AND #{qualityStatis.endDate}
            </if>
        </where>
        GROUP BY aa.YHBH
        ) a
        FULL JOIN
        <!--查询本系统当月水量，然后再关联历史数据处理-->
        (SELECT YHBH , nvl(cbsl , 0) bcbsl
        FROM TF_YWPN_READMETERHIS_W
        <where>
            MONTH_PLAN = #{currentMonth}
            and IS_GENERAL = '0'
            and cbbz = '1'
        </where>
        ) b on a.yhbh=b.yhbh) bbb ON aaa.yhbh = bbb.yhbh
        order by aaa.QYMC , aaa.QYDMC
    </select>
    <select id="queryCollectHistoryCard" resultType="java.util.Map">
        select aaa.* , bbb.ljsl from
        <!--根据条件查询筛选用户信息-->
        (SELECT aa.YHBH , bb.QYMC , cc.QYDMC, dd.YHXM , dd.LXDH , dd.YHDZ
        FROM TF_YWPN_YHBINGQY_W aa
        LEFT JOIN TF_YWPN_CBQYBM_W bb ON aa.QYBM = bb.QYBM LEFT JOIN TF_YWPN_CBQYDBM_W cc ON aa.QYDBM = cc.QYDBM
        JOIN TF_YWPN_QFKHJBXX_W dd ON aa.YHBH = dd.YHBH
        <where>
            <if test="qualityStatis.inputcontent != null and qualityStatis.inputcontent != ''">
                AND (dd.YHBH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHXM like '%' ||
                #{qualityStatis.inputcontent} || '%'
                or dd.LXDH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHDZ like '%' ||
                #{qualityStatis.inputcontent} || '%')
            </if>
            <if test="qualityStatis.cbqy != null and qualityStatis.cbqy != ''">
                AND aa.QYBM = #{qualityStatis.cbqy}
            </if>
            <if test="list != null">
                AND aa.QYDBM IN
                <foreach collection="list" item="qyd" open="(" close=")" separator=",">
                    #{qyd}
                </foreach>
            </if>
        </where>
        ) aaa
        join
        <!--查询历史水量-->
        (SELECT aa.YHBH , nvl(SUM(aa.CBSL) , 0) ljsl
        FROM TF_YWPN_READMETERHIS_W aa join TF_YWPN_YHBINGQY_W bb
        on aa.yhbh = bb.yhbh
        <where>
            aa.IS_GENERAL = '1'
            <if test="qualityStatis.startDate != null and qualityStatis.endDate != null and qualityStatis.startDate != '' and qualityStatis.endDate != ''">
                AND aa.CBYF BETWEEN #{qualityStatis.startDate} AND #{qualityStatis.endDate}
            </if>
        </where>
        GROUP BY aa.YHBH) bbb ON aaa.yhbh = bbb.yhbh
        order by aaa.QYMC , aaa.QYDMC
    </select>

    <select id="queryCollectHistoryCardTwo" resultType="map">
        select aaa.* , bbb.ljsl from
        <!--根据条件查询筛选用户信息-->
        (SELECT aa.YHBH , bb.QYMC , cc.QYDMC, dd.YHXM , dd.LXDH , dd.YHDZ
        FROM TF_YWPN_YHBINGQY_W aa
        LEFT JOIN TF_YWPN_CBQYBM_W bb ON aa.QYBM = bb.QYBM LEFT JOIN TF_YWPN_CBQYDBM_W cc ON aa.QYDBM = cc.QYDBM
        JOIN TF_YWPN_QFKHJBXX_W dd ON aa.YHBH = dd.YHBH
        <where>
            <if test="qualityStatis.inputcontent != null and qualityStatis.inputcontent != ''">
                AND (dd.YHBH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHXM like '%' ||
                #{qualityStatis.inputcontent} || '%'
                or dd.LXDH like '%' || #{qualityStatis.inputcontent} || '%' or dd.YHDZ like '%' ||
                #{qualityStatis.inputcontent} || '%')
            </if>
            <if test="qualityStatis.cbqy != null and qualityStatis.cbqy != ''">
                AND aa.QYBM = #{qualityStatis.cbqy}
            </if>
            <if test="list != null">
                AND aa.QYDBM IN
                <foreach collection="list" item="qyd" open="(" close=")" separator=",">
                    #{qyd}
                </foreach>
            </if>
        </where>
        ) aaa
        join
        <!--查询历史水量-->
        (SELECT aa.YHBH , nvl(SUM(aa.CBSL) , 0) ljsl
        FROM TF_YWPN_READMETEREASHIS_W aa join TF_YWPN_YHBINGQY_W bb
        on aa.yhbh = bb.yhbh
        <where>
            <if test="qualityStatis.startDate != null and qualityStatis.endDate != null and qualityStatis.startDate != '' and qualityStatis.endDate != ''">
                AND aa.CBYF BETWEEN #{qualityStatis.startDate} AND #{qualityStatis.endDate}
            </if>
        </where>
        GROUP BY aa.YHBH) bbb ON aaa.yhbh = bbb.yhbh
        order by aaa.QYMC , aaa.QYDMC
    </select>

</mapper>
