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

    <resultMap id="qfcbqfmxlsMap" type="com.tofly.watermeter.entity.Qfcbqfmxls">
        <result property="lsbh" column="LSBH"/>
        <result property="cbyf" column="CBYF"/>
        <result property="cbsl" column="CBSL"/>
        <result property="ysje" column="YSJE"/>
        <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="auditReason" column="AUDIT_REASON"/>
        <result property="auditResult" column="AUDIT_RESULT"/>
        <result property="issyn" column="ISSYN"/>
        <result property="sxjmessage" column="SXJMESSAGE"/>
        <result property="bz" column="BZ"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="addressState" column="ADDRESS_STATE"/>
        <result property="ljsl" column="LJSL"/>
        <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="zeroCount" column="ZERO_COUNT"/>
        <result property="continuityUp" column="CONTINUITY_UP"/>
        <result property="continuityDown" column="CONTINUITY_DOWN"/>
        <result property="continuityEqual" column="CONTINUITY_EQUAL"/>
        <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="monthTime" column="MONTH_TIME"/>
        <result property="cbzq" column="CBZQ"/>
        <result property="sfqf" column="SFQF"/>
        <result property="qfje" column="QFJE"/>
        <result property="qfzq" column="QFZQ"/>
        <association property="cbfsName" column="CBFS_CODE"
                     select="cbFsName"/>
        <association property="sbkjName" column="SBKJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getKjByCode"/>
        <collection property="fileInfo" column="FILEID"
                    select="com.tofly.watermeter.mapper.QfcbqfmxMapper.getFileByIds"/>
    </resultMap>


    <select id="cbFsName" resultType="string">
        select decode(#{code},1,'估抄',2,'实抄') from DUAL
    </select>

    <!--YHBH, CBKH, CBXH, CBPQ, CBB, CWRQ, SCDS, BCDS, BCJJL, CBBZ, JFZT, CBRY, CBRQ, JHRQ, XZBS, FILEID-->
    <insert id="insertBatch">
        insert into TF_YWPN_QFCBQFMXLS_W (LSBH, YHBH, CBKH, CBXH, CBPQ, CBB, CWRQ, SCDS, BCDS, BCJJL, CBBZ, JFZT, CBRY,
        CBRQ, JHRQ, XZBS, FILEID)
        <foreach collection="qflsList" item="list" separator="union all">
            select
            #{list.lsbh},
            #{list.yhbh},
            #{list.cbkh},
            #{list.cbxh},
            #{list.cbpq},
            #{list.cbb},
            #{list.cwrq},
            #{list.scds},
            #{list.bcds},
            #{list.bcjjl},
            #{list.cbbz},
            #{list.jfzt},
            #{list.cbry},
            #{list.cbrq},
            #{list.jhrq},
            #{list.xzbs},
            #{list.fileid}
            from dual
        </foreach>

    </insert>

    <select id="getCbpqName" resultType="string">
        select PQMC
        from TF_YWPN_QFCBCBPQ_W
        where PQBM = #{cbpq}
    </select>
    <select id="getCbbName" resultType="string">
        select CBDZ
        from TF_YWPN_QFCBCBBBM_W
        where CBBBM = #{cbb}
    </select>
    <select id="getCbbzName" resultType="string">
        select decode(#{cbbz}, '0', '未抄表', '1', '已抄表')
        from dual
    </select>
    <select id="getJfztName" resultType="string">
        select decode(#{jfzt}, '1', '正常计费', '2', '停用')
        from dual
    </select>
    <select id="getXzbsName" resultType="string">
        select decode(#{xzbz}, '0', '未下载', '1', '已下载')
        from dual
    </select>
    <!--    nvl(CBSL , 0) cbsl-->
    <select id="getWaterYieldByTime" resultType="long">
        SELECT CBSL
        FROM TF_YWPN_QFCBQFMXLS_W
        WHERE YHBH = #{yhbh}
          AND CBYF = #{readTime}
          AND ROWNUM = 1
    </select>


    <select id="selectYhbhAdYsl" resultType="com.tofly.watermeter.custom.entity.meterRead.YhbhAndSl">
        SELECT YHBH || CBYF yhbh, (BCDS - scds) as usesl
        FROM TF_YWPN_QFCBQFMXLS_W
        where CBRQ > ADD_MONTHS(trunc(#{bgcbrq}, 'mm'), -2)
        <![CDATA[ AND CBRQ < trunc(#{bgcbrq}, 'mm')
        ]]>
    </select>

    <select id="getBeforeReadData" resultType="com.tofly.watermeter.custom.entity.meterRead.YhbhAndSl">
        select YHBH yhbh, (BCDS - SCDS) usesl
        from TF_YWPN_QFCBQFMXLS_W
        where CBYF =
              (select to_char(add_months(sysdate, #{month}), 'yyyyMM') from dual)
    </select>


    <select id="getShouldReadQYDNum" resultType="java.util.Map">
        select a.QYDBM, d.QYDMC, COUNT(1) ALLREAD
        from TF_YWPN_YHBINGQY_W a
                 LEFT JOIN TF_YWPN_QFCBQFMX_W b ON a.YHBH = b.YHBH
                 LEFT JOIN TF_YWPN_CBQYDBM_W d ON a.QYDBM = d.QYDBM
        group by a.QYDBM, d.QYDMC
    </select>
    <select id="getReadQYDNum" resultType="java.util.Map">
        select a.QYDBM ,
        COUNT(1) read
        from TF_YWPN_YHBINGQY_W a LEFT JOIN TF_YWPN_QFCBQFMXLS_W b ON a.YHBH = b.YHBH
        <where>
            <if test="cwyf != null and cwyf != '' ">
                AND b.CBYF = #{cwyf}
            </if>
            <if test="qydbmList != null">
                AND a.QYDBM IN
                <foreach collection="qydbmList" item="qydbm" open="(" close=")" separator=",">
                    #{qydbm}
                </foreach>
            </if>
        </where>
        group by a.QYDBM
    </select>
    <resultMap id="shouldRyMap" type="map">
        <result property="cbry" column="CBRY"/>
        <result property="allread" column="ALLREAD"/>
        <association property="cbqyName" column="CBRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>

    <select id="getReadRYNum" resultType="java.util.Map">
        select c.CBRY , COUNT(1) read
        from TF_YWPN_YHBINGQY_W a
        LEFT JOIN TF_YWPN_QFCBQFMXLS_W b ON a.YHBH = b.YHBH
        LEFT JOIN TF_YWPN_CBQYDBM_W c ON c.QYDBM = a.QYDBM
        <where>
            <if test="cwyf != null and cwyf != '' ">
                AND b.CBYF = #{cwyf}
            </if>
            <if test="cbryList != null">
                AND c.CBRY IN
                <foreach collection="cbryList" item="cbry" open="(" close=")" separator=",">
                    #{cbry}
                </foreach>
            </if>
        </where>
        group by c.CBRY
    </select>

    <insert id="insertQfcbqfmxls" >
        INSERT INTO TF_YWPN_QFCBQFMXLS_W (YHBH, YHXM,YHDZ,LXDH,SBBH,SBCJ,SBLX,SBKJ,YSXZ,XZXF,CBPQ,CBB,CBZQ,CWRQ,SCDS,BCDS,BCJJL,CBBZ,JFZT,CBRY,CBRQ ,CBSL,CBYF,MONTH1,MONTH2,MONTH3,CBRQ1,CBRQ2,CBRQ3,YEARONYEAR1,YEARONYEAR2,YEARONYEAR3,YEARONMONTH1,YEARONMONTH2,YEARONMONTH3,MONTH_TIME,ZERO_COUNT,CONTINUITY_UP,CONTINUITY_DOWN,CONTINUITY_EQUAL,LJSL)
        SELECT YHBH,YHXM,YHDZ,LXDH,SBBH,SBCJ,SBLX,SBKJ,YSXZ,XZXF,CBPQ,CBB,CBZQ,CWRQ,SCDS,BCDS,BCJJL,CBBZ,JFZT,CBRY,CBRQ ,CBSL,CBYF,MON MONTH1,MONTH1 as MONTH2,MONTH2 as MONTH3,RQ CBRQ1,CBRQ1 CBRQ2,CBRQ2 CBRQ3,YEARONYEAR1,YEARONYEAR2,YEARONYEAR3,YEARONMONTH1,YEARONMONTH2,YEARONMONTH3,MONTH_TIME
                , (case WHEN BCJJL=0 THEN decode(ZERO_COUNT,null,0,ZERO_COUNT)+1 ELSE 0 END)ZERO_COUNT
                , (case WHEN BCJJL> MON or MON is null  THEN decode(CONTINUITY_UP,NULL,0,CONTINUITY_UP)+1 ELSE 0 END)CONTINUITY_UP
        <![CDATA[, (case WHEN MON is not null and BCJJL< MON THEN decode(CONTINUITY_DOWN,NULL,0,CONTINUITY_DOWN)+1 ELSE 0 END)CONTINUITY_DOWN ]]>
                , (case WHEN MON is not null and BCJJL= MON THEN decode(CONTINUITY_EQUAL,NULL,0,CONTINUITY_EQUAL)+1 ELSE 0 END)CONTINUITY_EQUAL
                ,(NVL(LJ,0)+NVL(BCJJL,0))LJSL
        FROM (
        <foreach collection="list" item="info" index="index" separator="union all">
            SELECT * FROM
            (SELECT #{info.yhbh} YHBH,
            <choose><when test="info.yhxm!=null">#{info.yhxm} YHXM, </when><otherwise>NULL as YHXM,</otherwise></choose>
            <choose><when test="info.yhdz!=null">#{info.yhdz} YHDZ, </when><otherwise>NULL as YHDZ,</otherwise></choose>
            <choose><when test="info.lxdh!=null">#{info.lxdh} LXDH, </when><otherwise>NULL as LXDH, </otherwise></choose>
            <choose><when test="info.sbbh!=null">#{info.sbbh} SBBH, </when><otherwise>NULL as SBBH, </otherwise></choose>
            <choose><when test="info.sbcj!=null">#{info.sbcj} SBCJ,</when><otherwise>NULL as SBCJ, </otherwise></choose>
            <choose><when test="info.sblx!=null">#{info.sblx} SBLX,</when><otherwise>NULL as SBLX, </otherwise></choose>
            <choose><when test="info.sbkj!=null">#{info.sbkj} SBKJ,</when><otherwise>NULL as SBKJ, </otherwise></choose>
            <choose><when test="info.ysxz!=null">#{info.ysxz} YSXZ, </when><otherwise>NULL as YSXZ, </otherwise></choose>
            <choose><when test="info.xzxf!=null">#{info.xzxf} XZXF,</when><otherwise>NULL as XZXF, </otherwise></choose>
            <choose><when test="info.cbpq!=null">#{info.cbpq} CBPQ, </when><otherwise>NULL as CBPQ, </otherwise></choose>
            <choose><when test="info.cbb!=null">#{info.cbb} CBB,</when><otherwise>NULL as CBB, </otherwise></choose>
            <choose><when test="info.cbzq!=null">#{info.cbzq} CBZQ,</when><otherwise>NULL as CBZQ, </otherwise></choose>
            <choose><when test="info.cwrq!=null">#{info.cwrq} CWRQ,</when><otherwise>NULL as CWRQ, </otherwise></choose>
            <choose><when test="info.scds!=null">#{info.scds} SCDS,</when><otherwise>NULL as SCDS, </otherwise></choose>
            <choose><when test="info.bcds!=null">#{info.bcds} BCDS,</when><otherwise>NULL as BCDS, </otherwise></choose>
            <choose><when test="info.bcjjl!=null">#{info.bcjjl} BCJJL,</when><otherwise>NULL as BCJJL, </otherwise></choose>
            <choose><when test="info.cbbz!=null">#{info.cbbz} CBBZ,</when><otherwise>NULL as CBBZ, </otherwise></choose>
            <choose><when test="info.jfzt!=null">#{info.jfzt} JFZT,</when><otherwise>NULL as JFZT, </otherwise></choose>
            <choose><when test="info.cbry!=null">#{info.cbry} CBRY,</when><otherwise>NULL as CBRY, </otherwise></choose>
            <choose><when test="info.cbrq!=null">#{info.cbrq} CBRQ,</when><otherwise>NULL as CBRQ, </otherwise></choose>
            <choose><when test="info.bcjjl!=null">#{info.bcjjl} CBSL,</when><otherwise>NULL as CBSL, </otherwise></choose>
            #{info.cbyf} CBYF,
                    #{info.monthTime} MONTH_TIME,#{yearonmonth1} as YEARONMONTH1,#{yearonmonth2} as YEARONMONTH2,
            #{yearonmonth3} as YEARONMONTH3
            FROM DUAL) t1
            <![CDATA[  FULL JOIN (SELECT * FROM (SELECT distinct YHBH YHBH2, MONTH2,MONTH1,CBRQ2 ,CBRQ1 ,ZERO_COUNT,CONTINUITY_UP,CONTINUITY_DOWN,CONTINUITY_EQUAL ,nvl(LJSL-BCJJL,0) LJ,CBRQ RQ,BCJJL MON
        FROM TF_YWPN_QFCBQFMXLS_W t WHERE YHBH = #{info.yhbh}  and MONTH_TIME = (SELECT Max(MONTH_TIME) FROM TF_YWPN_QFCBQFMXLS_W WHERE  YHBH = #{info.yhbh}  AND MONTH_TIME <#{info.monthTime} )),
            (SELECT * FROM (SELECT  TO_CHAR(MONTH_TIME,'yyyy-mm') MONTH_TIME,BCJJL FROM TF_YWPN_QFCBQFMXLS_W WHERE YHBH = #{info.yhbh} and MONTH_TIME >= #{yearonmonth3} ORDER BY MONTH_TIME
                )pivot (max(BCJJL) for  MONTH_TIME in(${column}))) ) t2 ON (t1.YHBH = t2.YHBH2)]]>
        </foreach>
        ) tb
    </insert>

    <update id="truncateQfcbqfmxlsTable">
        truncate table TF_YWPN_QFCBQFMXLS_W
    </update>

    <resultMap id="readHistoryMap" type="com.tofly.watermeter.entity.Qfcbqfmxls">
        <result property="yhbh" column="YHBH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="bcjjl" column="BCJJL"/>
        <result property="cbsl" column="CBSL"/>
        <result property="cbyf" column="CBYF"/>
        <association property="bz" column="{cbyf=CBYF,yhbh=YHBH}"
                     select="getReadHistoryBz"/>
    </resultMap>

    <select id="getReadHistoryBz" resultType="string">
        select bz
        from TF_YWPN_READMETERHIS_W
        where YHBH = #{yhbh}
          and CBYF = #{cbyf}
          and ROWNUM = 1
    </select>
    <select id="getReadHistory" resultMap="readHistoryMap">
        select YHBH , yhxm , yhdz , cbsl , scds , bcds , CBYF , bcjjl , cbrq
        from TF_YWPN_QFCBQFMXLS_W
        where <![CDATA[ MONTH_TIME < #{dateTime}]]>
        <if test="yhbh != null and yhbh != ''">
            AND YHBH = #{yhbh}
        </if>
        ORDER BY CBYF DESC
    </select>


</mapper>
