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

    <resultMap id="yhbingqyMap" type="com.tofly.watermeter.entity.Yhbingqy">
        <id property="yhbh" column="YHBH"/>
        <result property="qybm" column="QYBM"/>
        <result property="qydbm" column="QYDBM"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="cbxh" column="CBXH"/>
        <result property="isDown" column="IS_DOWN"/>
        <result property="cbbz" column="CBBZ"/>
        <result property="pcCbbz" column="PC_CBBZ"/>
        <result property="pcIsDown" column="PC_IS_DOWN"/>
        <result property="updateFlag" column="UPDATE_FLAG"/>
        <result property="childSort" column="CHILD_SORT"/>
        <result property="updateUser" column="UPDATE_USER"/>
        <result property="updateDate" column="UPDATE_DATE"/>
    </resultMap>


    <select id="getMaxYhByTime" resultType="string">
        SELECT YHBH
        FROM (SELECT b.YHBH
              FROM TF_YWPN_YHBINGQY_W a
                       LEFT JOIN TF_YWPN_QFCBQFMX_W b ON a.YHBH = b.YHBH
              WHERE a.QYDBM = #{qydbm}
              ORDER BY b.CBXH DESC)
        WHERE ROWNUM = 1
    </select>

    <select id="allocationPage" resultType="map">
        SELECT a.SBKH,b.YHBH, a.YHXM , a.YHDZ , a.LXDH , a.LXSJ , d.PQMC as CBPQ, c.CBB_MC as CBB , a.SBBH , e.YSXZ_MC
        as YSXZ
        , e.YSXZ_MC as YSXZ_NAME , b.cbxh, a.sbkj,f.CLIENT_BZ,UPDATE_USER,(select REAL_NAME from TF_SMPN_USER_X where id
        = b.UPDATE_USER) user_name,UPDATE_DATE
        FROM TF_YWPN_QFKHJBXX_W a
        RIGHT JOIN TF_YWPN_YHBINGQY_W b ON a.YHBH = b.YHBH
        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
        left join TF_YWPN_YHFOREVERINFO_W f on b.YHBH = f.YHBH
        <where>
            <if test="cbbList != null">
                AND a.CBB IN
                <foreach collection="cbbList" open="(" item="cbb" separator="," close=")">
                    #{cbb}
                </foreach>
            </if>
            <if test="inputcontent != null and inputcontent !=''">
                AND
                (a.YHBH like '%' || #{inputcontent} || '%'
                or
                a.YHXM like '%' || #{inputcontent} || '%'
                or
                a.YHDZ like '%' || #{inputcontent} || '%'
                or
                a.LXDH like '%' || #{inputcontent} || '%'
                or
                f.CLIENT_BZ like '%' || #{inputcontent} || '%'
                or
                a.LXSJ like '%' || #{inputcontent} || '%')
            </if>
            <if test="pqList != null">
                AND a.CBPQ IN
                <foreach collection="pqList" open="(" item="pq" separator="," close=")">
                    #{pq}
                </foreach>
            </if>
            <if test="yhbh != null and yhbh != ''">
                and a.YHBH like '%' || #{yhbh} || '%'
            </if>
            <if test="yhxm != null and yhxm != ''">
                and a.YHXM like '%' || #{yhxm} || '%'
            </if>
            <if test="qybm != null and qybm != ''">
                and b.QYBM = #{qybm}
            </if>
            <if test="yhdz != null and yhdz != '' ">
                AND a.YHDZ LIKE '%' || #{yhdz} || '%'
            </if>
            <if test="qydbm != null and qydbm != ''">
                and b.QYDBM = #{qydbm}
            </if>
        </where>
        order by b.CBXH
    </select>

    <resultMap id="getNoAllocationTwoMap" type="java.util.Map">
        <result property="yhbh" column="YHBH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="cbpq" column="CBPQ"/>
        <result property="cbb" column="CBB"/>
        <result property="sbbh" column="SBBH"/>
        <result property="ysxz" column="YSXZ"/>
        <result property="jdrq" column="JDRQ"/>
        <association property="sbcjName" column="SBCJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getCjByCode"/>
    </resultMap>
    <select id="getallFpYh" resultType="java.lang.String">
        SELECT a.YHBH
        FROM TF_YWPN_QFKHJBXX_W a
        <where>
            a.YHZT != '9'
            <if test="cbbList != null">
                AND a.CBB IN
                <foreach collection="cbbList" open="(" item="cbb" separator="," close=")">
                    #{cbb}
                </foreach>
            </if>
            <if test="pqList != null">
                AND a.CBPQ IN
                <foreach collection="pqList" open="(" item="pq" separator="," close=")">
                    #{pq}
                </foreach>
            </if>
            <if test="sbcjList != null">
                AND a.SBCJ IN
                <foreach collection="sbcjList" open="(" item="sbcj" separator="," close=")">
                    #{sbcj}
                </foreach>
            </if>
            <if test="allocation.yhbh != null and allocation.yhbh != ''">
                AND a.YHBH LIKE #{allocation.yhbh} || '%'
            </if>
            <if test="allocation.inputcontent != null and allocation.inputcontent != ''">
                AND
                (a.YHBH like '%' || #{allocation.inputcontent} || '%'
                or
                a.YHXM like '%' || #{allocation.inputcontent} || '%'
                or
                a.YHDZ like '%' || #{allocation.inputcontent} || '%'
                or
                a.LXDH like '%' || #{allocation.inputcontent} || '%')
            </if>
            <if test="allocation.startDate != null and allocation.endDate != null">
                AND a.JDRQ BETWEEN #{allocation.startDate} AND #{allocation.endDate}
            </if>
            AND NOT EXISTS(SELECT YHBH FROM TF_YWPN_YHBINGQY_W WHERE YHBH = a.YHBH)
        </where>
    </select>

    <select id="getNoAllocationTwo" resultMap="getNoAllocationTwoMap">
        SELECT a.YHBH, a.YHXM , a.YHDZ , a.LXDH , d.PQMC as CBPQ, c.CBB_MC as CBB , a.SBBH , b.YSXZ_MC as YSXZ , a.SBCJ
        ,
        TO_CHAR(a.JDRQ , 'yyyy-MM-dd hh24:mi:ss') JDRQ
        FROM TF_YWPN_QFKHJBXX_W a LEFT JOIN TF_YWPN_QFMIDYSXZBM_E b ON a.YSXZ = b.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.YHZT != '9'
            <if test="cbbList != null">
                AND a.CBB IN
                <foreach collection="cbbList" open="(" item="cbb" separator="," close=")">
                    #{cbb}
                </foreach>
            </if>
            <if test="pqList != null">
                AND a.CBPQ IN
                <foreach collection="pqList" open="(" item="pq" separator="," close=")">
                    #{pq}
                </foreach>
            </if>
            <if test="sbcjList != null">
                AND a.SBCJ IN
                <foreach collection="sbcjList" open="(" item="sbcj" separator="," close=")">
                    #{sbcj}
                </foreach>
            </if>
            <if test="allocation.yhbh != null and allocation.yhbh != ''">
                AND a.YHBH LIKE #{allocation.yhbh} || '%'
            </if>
            <if test="allocation.inputcontent != null and allocation.inputcontent != ''">
                AND
                (a.YHBH like '%' || #{allocation.inputcontent} || '%'
                or
                a.YHXM like '%' || #{allocation.inputcontent} || '%'
                or
                a.YHDZ like '%' || #{allocation.inputcontent} || '%'
                or
                a.LXDH like '%' || #{allocation.inputcontent} || '%')
            </if>
            <if test="allocation.startDate != null and allocation.endDate != null">
                AND a.JDRQ BETWEEN #{allocation.startDate} AND #{allocation.endDate}
            </if>
            AND NOT EXISTS(SELECT YHBH FROM TF_YWPN_YHBINGQY_W WHERE YHBH = a.YHBH)
        </where>
        order by a.YHBH
    </select>


    <resultMap id="getCbryxyMap" type="com.tofly.watermeter.custom.entity.meterRead.AppRead">
        <result property="yhbh" column="YHBH"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="cbsl" column="CBSL"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="lxsj" column="LXSJ"/>
        <result property="sbbh" column="SBBH"/>
        <result property="ysxzName" column="YSXZ_MC"/>
        <result property="month1" column="MONTH1"/>
        <result property="cbrq1" column="CBRQ1"/>
        <result property="month2" column="MONTH2"/>
        <result property="cbrq2" column="CBRQ3"/>
        <result property="month3" column="MONTH3"/>
        <result property="cbrq3" column="CBRQ3"/>
        <result property="cbfsCode" column="CBFS_CODE"/>
        <result property="jfzt" column="jfzt"/>
        <result property="cbbz" column="CBBZ"/>
        <result property="bz" column="BZ"/>
        <result property="clientBz" column="CLIENT_BZ"/>
        <result property="clientFileIds" column="CLIENT_FILE"/>
        <result property="x" column="X"/>
        <result property="y" column="Y"/>
        <result property="cbrq" column="CBRQ"/>
        <result property="cbbzName" column="CBBZNAME"/>
        <result property="addressState" column="ADDRESS_STATE"/>
        <result property="zhye" column="ZHYE"/>
        <result property="zgl" column="ZGL"/>
        <result property="soleSign" column="SOLE_SIGN"/>
        <result property="diffValue" column="DIFF_VALUE"/>
        <result property="isGeneral" column="IS_GENERAL"/>
        <association property="sbcj" column="SBCJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getCjByCode"/>
        <association property="sbkj" column="SBKJ"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getKjByCode"/>
        <association property="sblx" column="SBLX"
                     select="com.tofly.watermeter.mapper.QfkhjbxxMapper.getLxByCode"/>
        <association property="cbfsName" column="CBFS_CODE"
                     select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <collection property="fileInfo" column="{fileIds=FILEID,cbry=CBRY}"
                    select="com.tofly.watermeter.mapper.QfcbqfmxMapper.getFileByIdsAndCbry"/>
        <collection property="clientFileInfo" column="CLIENT_FILE"
                    select="com.tofly.watermeter.mapper.QfcbqfmxMapper.getFileByIds"/>
    </resultMap>

    <select id="getCbryXY" resultMap="getCbryxyMap">
        SELECT a.YHBH,
               a.YHXM,
               a.YHDZ,
               a.SBBH,
               a.SBKJ,
               a.SBLX,
               a.SBCJ,
               a.CBRQ,
               b.YSXZ_MC,
               a.CBBZ,
               decode(a.CBBZ, '0', '未抄表', '1', '已抄表')                                                       as CBBZNAME,
               f.CLIENT_BZ,
               f.FILE_ID                                                                                    as CLIENT_FILE,
               a.BZ,
               a.SCDS,
               a.BCDS,
               a.CBSL,
               a.FILEID,
               a.CBFS_CODE,
               a.X,
               a.Y,
               a.ADDRESS_STATE,
               a.IS_GENERAL,
               a.CBRQ1,
               a.CBRQ2,
               a.CBRQ3,
               a.MONTH1,
               a.MONTH2,
               a.MONTH3,
               l.SFQF                                                                                       as ISQF,
               l.LXSJ,
               l.LXDH,
               l.QFJE,
               l.QFZQ,
               DECODE(l.YHZT, '0', '未启用', '1', '正常', '2', '报停', '3', '停水', '4', '锁定', '5', '关阀', '9', '销户') as YHZT,
               a.CBRQ,
               l.ZHYE,
               a.ZGL,
               a.DIFF_VALUE,
               a.SOLE_SIGN,
               a.CBRY,
               a.HANDLERS_ID
        FROM TF_YWPN_READMETERHIS_W a
                 LEFT JOIN TF_YWPN_QFMIDYSXZBM_E b ON a.YSXZ = b.YSXZ_BM
                 LEFT JOIN TF_YWPN_QFKHJBXX_W l ON a.YHBH = l.YHBH
                 LEFT JOIN TF_YWPN_YHFOREVERINFO_W f ON a.YHBH = f.YHBH
        WHERE a.CBRY = #{cbry}
          AND a.CBBZ = '1'
          AND a.CBRQ BETWEEN #{startDate} AND #{endDate}
        order by a.CBRQ
    </select>

    <select id="getQyCllect" resultType="java.util.Map">
        SELECT aa.QYBM, aa.QYMC, aa.qydNum,
        SUM(DECODE(bb.yhbh, null, 0,1))YHBHNUM
        FROM (
        SELECT a.QYBM, a.QYMC, COUNT(b.QYDBM) qydNum
        FROM TF_YWPN_CBQYBM_W a left join
        TF_YWPN_CBQYDBM_W b
        on a.QYBM = b.qybm
        <where>
            (a.YXBZ = '0' or b.YXBZ = '0')
            <if test="inputcontent != null">
                and(a.QYBM like '%' || #{inputcontent} || '%'
                or
                a.QYMC like '%' || #{inputcontent} || '%')
            </if>
        </where>
        GROUP BY a.QYBM, a.QYMC) aa
        LEFT JOIN TF_YWPN_YHBINGQY_W bb
        ON aa.QYBM = bb.QYBM
        GROUP BY aa.QYBM, aa.QYMC, aa.qydNum
        ORDER BY aa.QYBM
    </select>

    <update id="updateBingAndXh">
        <foreach collection="list" open="begin" close=";end;" item="bingYh" separator=";">
            UPDATE TF_YWPN_YHBINGQY_W
            <set>
                CBXH = #{bingYh.cbxh}
                <if test="bingYh.childSort != null">
                    ,
                    CHILD_SORT = #{bingYh.childSort}
                </if>
            </set>
            WHERE YHBH = #{bingYh.yhbh}
        </foreach>
    </update>


    <select id="getNoOrderYh" resultType="java.util.Map">
        SELECT a.YHBH, a.YHXM, b.CBXH
        FROM TF_YWPN_QFKHJBXX_W a
        RIGHT JOIN TF_YWPN_YHBINGQY_W b
        ON a.YHBH = b.YHBH
        <where>
            <if test="qydbm != null and qydbm != ''">
                b.QYDBM = #{qydbm}
            </if>
            <if test="qybm != null and qybm != ''">
                and b.QYBM = #{qybm}
            </if>
            <if test="inputcontent != null and inputcontent != ''">
                AND
                (b.CBXH like '%' || #{inputcontent} || '%'
                or
                a.YHBH like '%' || #{inputcontent} || '%'
                or
                a.YHXM like '%' || #{inputcontent} || '%')
            </if>
            <if test="list != null">
                AND b.YHBH NOT IN
                <foreach collection="list" open="(" separator="," item="yhbh" close=")">
                    #{yhbh}
                </foreach>
            </if>
        </where>
        order by b.CBXH
    </select>
    <select id="getCbbOrCbpq" resultType="java.util.Map">
        SELECT
        <if test="isPqOrcbb == 0">
            PQBM as bm , PQMC as mc
        </if>
        <if test="isPqOrcbb == 1">
            CBB_BM as bm , CBB_MC as mc
        </if>
        FROM
        <if test="isPqOrcbb == 0">
            TF_YWPN_QFCBCBPQ_W
        </if>
        <if test="isPqOrcbb == 1">
            TF_YWPN_DMACISCBB_W
        </if>
        <where>
            <if test="cbpq != null">
                <if test="isPqOrcbb == 0">
                    PQMC like '%' || #{cbpq} || '%'
                </if>
                <if test="isPqOrcbb == 1">
                    CBB_MC like '%' || #{cbpq} || '%'
                </if>
            </if>
        </where>
    </select>
    <select id="getMaxCbxh" resultType="java.lang.Integer">
        SELECT CBXH
        FROM (SELECT CBXH
              FROM TF_YWPN_YHBINGQY_W
              WHERE QYDBM = #{qydbm}
              ORDER BY cbxh DESC)
        WHERE ROWNUM = 1
    </select>

    <insert id="insertShouldCbYh">
        INSERT INTO TF_YWPN_READMETERHIS_W
        (YHBH , YHXM , YHDZ , LXDH , SBBH , SBCJ , SBLX , SBKJ ,YSXZ , XZXF , CBPQ , CBB ,
        CBXH, CBBZ, CBRY, XZBS, IS_GENERAL, SIN_DOU_MONTH, START_DAY, END_DAY, CBQY_MC, CBQY_BM, CBQYD_BM,
        MONTH_PLAN)
        <foreach collection="list" item="yhInfo" separator="union all">
            select
            #{yhInfo.yhbh},
            #{yhInfo.yhxm},
            #{yhInfo.yhdz},
            #{yhInfo.lxdh},
            #{yhInfo.sbbh},
            #{yhInfo.sbcj},
            #{yhInfo.sblx},
            #{yhInfo.sbkj},
            #{yhInfo.ysxz},
            #{yhInfo.xzxf},
            #{yhInfo.cbpq},
            #{yhInfo.cbb},
            #{yhInfo.cbxh},
            '0',
            #{qfcbplandetail.cbry},
            '0',
            #{qfcbplandetail.isGeneral},
            #{qfcbplandetail.sinDouMonth},
            #{qfcbplandetail.startDay},
            #{qfcbplandetail.endDay},
            #{qfcbplandetail.cbqyMc},
            #{qfcbplandetail.cbqyBm},
            #{qfcbplandetail.cbqydBm},
            #{monthPlan}
            from dual
        </foreach>

    </insert>

    <select id="loadQy" resultType="com.tofly.watermeter.custom.vo.ForeignYhQyQdVo">
        SELECT y.YHBH as KHBH,y.QYBM as QYBH,y.QYDBM as CBDBH,q.QYMC,d.QYDMC as CBDMC
        FROM TF_YWPN_YHBINGQY_W y left join TF_YWPN_CBQYBM_W q ON (y.QYBM = q.QYBM)
        left join TF_YWPN_CBQYDBM_W d ON (y.QYDBM = d.QYDBM)
        <where>
            <if test="dto.qybh != null and dto.qybh.trim() != ''">
                AND y.QYBM = #{dto.qybh}
            </if>
            <if test="dto.cbdbh != null and dto.cbdbh.trim() != ''">
                AND y.QYDBM = #{dto.cbdbh}
            </if>
            <if test="dto.khbh != null and dto.khbh.trim() != ''">
                AND y.YHBH = #{dto.khbh}
            </if>
        </where>
        ORDER BY y.YHBH
    </select>

    <select id="getYhQyAndQydInfo" resultType="com.tofly.watermeter.custom.vo.YhQyQdInfoVo">
        SELECT *
        FROM (
                 SELECT y.YHBH        as KHBH,
                        y.QYBM        as QYBH,
                        y.QYDBM       as CBDBH,
                        q.QYMC,
                        d.QYDMC       as CBDMC,
                        y.UPDATE_FLAG as yh_flag
                 FROM TF_YWPN_YHBINGQY_W y
                          left join TF_YWPN_CBQYBM_W q ON (y.QYBM = q.QYBM)
                          left join TF_YWPN_CBQYDBM_W d ON (y.QYDBM = d.QYDBM)
                 WHERE y.UPDATE_FLAG = 1
                   AND q.YXBZ = 0
                   AND d.YXBZ = 0
                 ORDER BY q.UPDATE_FLAG, q.UPDATE_FLAG, y.YHBH)
        <![CDATA[
        WHERE ROWNUM < #{row}
        ]]>
    </select>
    <select id="getNoOrderYhPlan" resultType="java.util.Map">
        SELECT YHBH, YHXM, CBXH
        FROM TF_YWPN_READMETERHIS_W
        <where>
            MONTH_PLAN = #{month}
            <if test="qydbm != null and qydbm != ''">
                AND CBQYD_BM = #{qydbm}
            </if>
            <if test="qybm != null and qybm != ''">
                AND CBQY_BM = #{qybm}
            </if>
            <if test="inputcontent != null and inputcontent != ''">
                AND
                (CBXH like '%' || #{inputcontent} || '%'
                or
                YHBH like '%' || #{inputcontent} || '%'
                or
                YHXM like '%' || #{inputcontent} || '%')
            </if>
            <if test="list != null">
                AND YHBH NOT IN
                <foreach collection="list" open="(" separator="," item="yhbh" close=")">
                    #{yhbh}
                </foreach>
            </if>
        </where>
        order by CBXH
    </select>

    <update id="aginSort">
        MERGE INTO TF_YWPN_YHBINGQY_W a
        USING (
            SELECT yhbh,
                   row_number() OVER (PARTITION BY QYDBM ORDER BY CBXH, CHILD_SORT) AS row_flag
            FROM TF_YWPN_YHBINGQY_W
            WHERE QYDBM = #{qydbm}
        ) b ON (a.yhbh = b.yhbh)
        WHEN MATCHED THEN
            UPDATE SET a.CBXH = b.row_flag,
                       a.UPDATE_FLAG = '1',
                       a.CHILD_SORT = 0
    </update>

    <select id="notAssignedClient" resultType="map" fetchSize="100">
        select q.YHBH,q.CBB
        from TF_YWPN_QFKHJBXX_W q left join TF_YWPN_YHBINGQY_W y on q.YHBH = y.YHBH
        where (QYDBM is null or CBB != QYDBM)
    </select>

</mapper>
