<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tofly.xrtygis.custom.mapper.TheOnlineCheckMapper">

    <select id="unaffiliatedCustomer" resultType="map">
        select w.YHBH, YHXM, YHDZ, SID,X,Y
        from TF_YWPN_WATERMETER_W w left join yh_khjbxx@YXDBLINK q on w.YHBH = q.YHBH
        where (select count(*) num from TF_YWPN_TABLELINKUSER_W u where u.KHBH = w.YHBH) = 0
        and ISSUM = 0
        <if test="str != null and str!=''">
            and w.YHBH || YHXM || YHDZ like '%' || #{str} || '%'
        </if>
    </select>

    <select id="viewTablePipe" resultType="map">
        select m.ID,
        m.SID,
        m.X,
        m.Y,
        m.ADDRESS,
        t.PIPE_SID,
        decode(t.STATE, '0', '未确定', '1', '确定', '2', '自动匹配') state,
        (select count(u.ID) from TF_YWPN_TABLELINKUSER_W u where u.TABLE_ID = m.ID) num
        from TF_YWPN_WATERMETER_W m
        left join TF_YWPN_TABLELINKPIPE_W t on m.SID = t.SID
        where m.ISSUM = 1
        <if test="type != 3 and type != null">
            and t.STATE = #{type,jdbcType = NUMERIC}
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and m.SID || m.ADDRESS like '%' || #{likeStr} || '%'
        </if>
        order by m.SID
    </select>

    <select id="getDmaByTableId" resultType="map">
        select *
        from TF_DMA_WATER_W
        where 1 = 1
        <if test="waterMeterId != null">
            and WATER_METER_ID = #{waterMeterId}
        </if>
    </select>

    <delete id="deleteTableById">
        delete
        from TF_YWPN_WATERMETER_W
        where 1 = 1
        <if test="id != null">
            and ID = #{id}
        </if>
    </delete>

    <delete id="deleteUserById">
        delete
        from TF_YWPN_TABLELINKUSER_W
        where 1 = 1
        <if test="id != null">
            and TABLE_ID = #{id}
        </if>
    </delete>

    <delete id="deleteDmaWaterById">
        delete
        from TF_DMA_WATER_W
        where 1 = 1
        <if test="id != null">
            and WATER_METER_ID = #{id}
        </if>
    </delete>

    <insert id="insertUserToTable" parameterType="list">
        insert all
        <foreach collection="list" item="u" separator=" ">
            into TF_YWPN_TABLELINKUSER_W (ID, KHBH, SID, TABLE_ID, STATE)
            values
            (#{u.id,jdbcType = NUMERIC},
            #{u.khbh,jdbcType = VARCHAR},
            #{u.sid,jdbcType = VARCHAR},
            #{u.tableId,jdbcType = NUMERIC},
            #{u.state,jdbcType = NUMERIC})
        </foreach>
        select 1 from dual
    </insert>

    <update id="updatePlotName">
        update TF_YWPN_WATERMETER_W set VILLAGE = #{name} where sid in
        <foreach collection="codes" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
    </update>

    <select id="getTableDmaUserBindList" resultType="com.tofly.xrtygis.entity.vo.ResultUserWaterMeterInfoVo">
        SELECT
        w.sid tableCode,
        w.address tableAddress,
        t1.SSCBB meterReadingBook,
        t1.SSCBPQ meterReadingArea,
        t1.YHBH userCode,
        t1.YHXM userName,
        t1.YHDZ userAddress,
        t1.LXDH contactNumber,
        t2.SBBH waterMeterCode,
        t2.SBKJ waterMeterCaliber,
        q.cbbmc
        FROM
        yh_khjbxx @YXDBLINK t1
        LEFT JOIN TF_YWPN_TABLELINKUSER_W r ON r.khbh = t1.yhbh
        LEFT JOIN TF_YWPN_WATERMETER_W w ON w.id = r.TABLE_ID
        LEFT JOIN TF_DMA_WATER_W z ON z.WATER_METER_ID = w.ID
        LEFT JOIN yh_cbkxx @YXDBLINK t2 ON t2.YHBH = t1.YHBH
        left join bm_cbb @YXDBLINK q on q.cbbbm = t1.sscbb
        WHERE
        1 = 1
        <if test="dmaId!= null">
            and z.DMA_ID = #{dmaId}
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.SSCBB || t1.YHXM || t1.YHDZ like '%' || #{likeStr} || '%'
        </if>
        <if test="tableLikeStr != null and tableLikeStr.trim() != ''">
            and w.sid || w.address like '%' || #{tableLikeStr} || '%'
        </if>
    </select>

    <select id="getTableUserBindList" resultType="com.tofly.xrtygis.entity.vo.UserWaterMeterInfoVo">
        select t1.sscbb as meterReadingBook,
               q.cbbmc,
               z.CBPQBM as meterReadingArea,
               z.cbpqmc as meterReadingAreaName,
               t1.YHBH as userCode,
               t1.YHXM as userName,
               t1.YHDZ as userAddress,
               t1.LXDH,
               t2.SBSCCJ,
               t2.SBBH,
               t2.SBKJ,
               t2.SBLB,
               t2.sjbm
        from yh_khjbxx@YXDBLINK t1
        left join yh_cbkxx@YXDBLINK t2 on t1.YHBH = t2.YHBH
        left join bm_cbb@YXDBLINK q on t1.sscbb = q.cbbbm
        left join bm_cbpq@YXDBLINK z on q.sscbpq = z.CBPQBM
        where 1 = 1
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.YHBH || t1.YHXM || t1.YHDZ like '%' || #{likeStr} || '%'
        </if>
        <if test="cbbmc != null and cbbmc.trim() != ''">
            and q.cbbmc || t1.sscbb like '%' || #{cbbmc} || '%'
        </if>
        <if test="cbpqLikeStr != null and cbpqLikeStr.trim() != ''">
            and z.cbpqmc || to_char( z.CBPQBM ) like '%' || #{cbpqLikeStr} || '%'
        </if>
        order by t1.yhbh
    </select>
    <select id="getTableListByUserCode" resultType="String">
        select t.SID
        from TF_YWPN_TABLELINKUSER_W r
        left join TF_YWPN_WATERMETER_W t on t.ID = r.TABLE_ID
        where t.ISSUM = 1
        <if test="userCode != null and userCode.trim() != ''">
            and r.KHBH = #{userCode}
        </if>
    </select>
    <select id="getYxUserInfoPage" resultType="com.tofly.xrtygis.entity.vo.UserWaterMeterInfoVo">
        SELECT t.phone,
               t.SSCBB                                                 meterReadingBook,
               t.SSCBPQ                                                meterReadingArea,
               t.YHBH                                                  userCode,
               t.YHXM                                                  userName,
               t.YHDZ                                                  userAddress,
               t.LXDH                                                  contactNumber,
               (select mc from bm_sbsccj@YXDBLINK where bm = t.SBSCCJ) waterMeterManufacturer,
               t.SBBH                                                  waterMeterCode,
               t.SBKJ                                                  waterMeterCaliber,
               (select mc from bm_sblb@YXDBLINK where bm = t.SBLB)     waterMeterType,
               (select c.mc
                from yh_cbkxx@YXDBLINK a
                         left join bm_sj@YXDBLINK b on b.bm = a.sjbm
                         left join bm_yslx@YXDBLINK c on c.bm = b.yslx
                WHERE a.sjbm = t.sjbm
                GROUP BY a.sjbm, c.mc)                                 waterMeterNature

        FROM (
                 SELECT T1.sj phone,
                        t1.SSCBB,
                        t1.SSCBPQ,
                        t1.YHBH,
                        t1.YHXM,
                        t1.YHDZ,
                        t1.LXDH,
                        t2.SBSCCJ,
                        t2.SBBH,
                        t2.SBKJ,
                        t2.SBLB,
                        t2.sjbm
                 FROM yh_khjbxx@YXDBLINK t1
                          LEFT JOIN yh_cbkxx@YXDBLINK t2
                                    on t1.YHBH = t2.YHBH
                          LEFT JOIN YY_QFMX@YXDBLINK t3
                                    on t2.yhbh = t3.YHBH
                 GROUP BY T1.sj, t1.SSCBB, t1.SSCBPQ, t1.YHBH, t1.YHXM, t1.YHDZ, t1.LXDH, t2.SBSCCJ, t2.SBBH, t2.SBKJ,
                          t2.SBLB, t2.sjbm) t
        where 1 = 1
        <if test="meterReadingArea != null and meterReadingArea.trim() != ''">
          and t.SSCBPQ = #{meterReadingArea}
        </if>
        <if test="yhbhs != null and yhbhs != '' ">
            and t.userCode in
            <foreach collection="yhbhs.split(',')" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and t.YHBH || t.YHXM || t.YHDZ || t.LXDH like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="getYxChargeDetail" resultType="com.tofly.xrtygis.entity.vo.ChargeDetailInfoVo">
        SELECT YHBH userId,
               SCDS  meterRise,
               BCDS meterStop,
               YSSL waterVolume,
               YSYF meterMonth,
               YSJE amountReceivable,
               SFBZ chargeFlag,
               decode(SFBZ,'0','未收','1','已收','4','不收费') chargeFlagName,
               ZNJ defaultCharge,
               WSF sewageFee,
               YSLJF garbageFee,
               SFRQ chargeDate,
               SFRYXM chargePersonName
        FROM yy_sspjxx@YXDBLINK
        where 1 = 1
        <if test="userId != null">
            and YHBH = #{userId}
        </if>
    </select>

    <select id="getYxChargeUserInfo" resultType="com.tofly.xrtygis.entity.vo.ChargeUserInfoVo">
        SELECT t1.YHBH userId,
               t1.YHXM userName,
               t1.LXDH phone,
               t1.YHDZ address,
               t1.SSFQ supplyWaterArea,
               (SELECT CBBMC FROM bm_cbb@YXDBLINK where CBBBM = t1.SSFQ) upplyWaterAreaName,
               t1.SSCBPQ chargeArea,
               (SELECT CBPQMC FROM bm_cbpq@YXDBLINK where CBPQBM = t1.SSCBPQ) chargeAreaName,
               t1.YHZH chargeAccount,
               t1.YHZT status,
               decode(t1.YHZT,'0','申请用户','1','正式用户','2','报停用户','3','销户用户','4','作废用户') statusName,
               t1.SSHY nature,
               t1.KHLB type,
               t1.SQRQ accountDate,
               t2.SBLB meterType,
                (SELECT MC FROM bm_sblb@YXDBLINK where BM = t2.SBLB) meterTypeName,
               t2.ZXCBRQ meterReadDate,
               t2.SBBH meterNumber,
               t2.SBSCCJ meterMill,
               (SELECT MC FROM bm_sbsccj@YXDBLINK where BM = t2.SBSCCJ) meterMillName
        FROM YH_KHJBXX@YXDBLINK t1
        LEFT JOIN YH_CBKXX@YXDBLINK t2 ON t1.YHBH = t2.YHBH
        where 1 = 1
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.YHXM || t1.LXDH || t1.YHDZ || to_char(t1.YHBH) like '%' || #{likeStr} || '%'
        </if>
        <if test="chargeArea != null">
            and t1.YHBH = #{chargeArea}
        </if>
        <if test="sex != null and sex.trim() != ''">
            and t1.XB = #{sex}
        </if>
    </select>

    <select id="getYxChargeUserInfoList" resultType="com.tofly.xrtygis.entity.vo.ChargeUserInfoVo">
        SELECT t1.YHBH userId,
        t1.YHXM userName,
        t1.LXDH phone,
        t1.YHDZ address,
        t1.SSFQ supplyWaterArea,
        (SELECT CBBMC FROM bm_cbb@YXDBLINK where CBBBM = t1.SSFQ) upplyWaterAreaName,
        t1.SSCBPQ chargeArea,
        (SELECT CBPQMC FROM bm_cbpq@YXDBLINK where CBPQBM = t1.SSCBPQ) chargeAreaName,
        t1.YHZH chargeAccount,
        t1.YHZT status,
        decode(t1.YHZT,'0','申请用户','1','正式用户','2','报停用户','3','销户用户','4','作废用户') statusName,
        t1.SSHY nature,
        t1.KHLB type,
        t1.SQRQ accountDate,
        t2.SBLB meterType,
        (SELECT MC FROM bm_sblb@YXDBLINK where BM = t2.SBLB) meterTypeName,
        t2.ZXCBRQ meterReadDate,
        t2.SBBH meterNumber,
        t2.SBSCCJ meterMill,
        (SELECT MC FROM bm_sbsccj@YXDBLINK where BM = t2.SBSCCJ) meterMillName
        FROM YH_KHJBXX@YXDBLINK t1
        LEFT JOIN YH_CBKXX@YXDBLINK t2 ON t1.YHBH = t2.YHBH
        where 1 = 1
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.YHXM || t1.LXDH || t1.YHDZ || to_char(t1.YHBH) like '%' || #{likeStr} || '%'
        </if>
        <if test="chargeArea != null">
            and t1.YHBH = #{chargeArea}
        </if>
        <if test="sex != null and sex.trim() != ''">
            and t1.XB = #{sex}
        </if>
    </select>

    <select id="getMeterReadingAreaList" resultType="map">
        select  SSCBPQ,
                (SELECT CBPQMC FROM bm_cbpq@YXDBLINK where CBPQBM = SSCBPQ) chargeAreaName
        from yh_khjbxx@YXDBLINK
        where  SSCBPQ is not null
        GROUP BY SSCBPQ
        order by SSCBPQ
    </select>
    <select id="getTableUserListNum" resultType="java.lang.Integer">
        select count(1)
        FROM yh_khjbxx@YXDBLINK
        WHERE YHZT = '1'
    </select>

    <select id="affiliatedCustomer" resultType="java.util.Map">
        select u.KHBH,
        u.ID,
        u.SID,
        q.YHDZ,
        q.YHXM,
        m.X,
        m.Y,
        decode(STATE,'0','未确认','1','确认','2','自动匹配') state,
        VILLAGE
        from TF_YWPN_TABLELINKUSER_W u
        left join yh_khjbxx@YXDBLINK q on u.KHBH = q.YHBH
        left join TF_YWPN_WATERMETER_W m on u.SID = m.SID
        where 1 = 1
        <if test="tableIds != null and tableIds != '' ">
            AND table_id in
            <foreach collection="tableIds.split(',')" item="code" open="(" close=")" separator=",">
                #{code}
            </foreach>
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and u.KHBH || m.ADDRESS || q.YHXM like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="affiliatedCustomerPage" resultType="java.util.Map">
        select u.KHBH,
        u.ID,
        u.SID,
        q.YHDZ,
        q.YHXM,
        m.X,
        m.Y,
        decode(STATE,'0','未确认','1','确认','2','自动匹配') state,
        VILLAGE
        from TF_YWPN_TABLELINKUSER_W u
        left join yh_khjbxx@YXDBLINK q on u.KHBH = q.YHBH
        left join TF_YWPN_WATERMETER_W m on u.SID = m.SID
        where 1 = 1
        <if test="tableIds != null and tableIds != '' ">
            AND table_id in
            <foreach collection="tableIds.split(',')" item="code" open="(" close=")" separator=",">
                #{code}
            </foreach>
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and u.KHBH || m.ADDRESS || q.YHXM like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="getWaterStopAreaPage" resultType="com.tofly.xrtygis.entity.vo.WaterStopAreaInfoVo">
        SELECT t5.X,T5.Y,T2.khbh userCode, t4.YHXM userName,t3.SBBH waterMeterCode,t4.YHDZ userAddress,t4.LXDH
        contactNumber,t4.SSCBPQ meterReadingArea,t4.SSCBB meterReadingBook,T1.PIPE_SID pipeSid
        FROM TF_YWPN_TABLELINKPIPE_W t1 LEFT JOIN TF_YWPN_TABLELINKUSER_W t2
        on T1.TABLE_ID = t2.TABLE_ID
        LEFT JOIN yh_cbkxx@YXDBLINK t3 on t2.KHBH = t3.YHBH
        LEFT JOIN yh_khjbxx@YXDBLINK t4 on t3.yhbh = t4.YHBH
        LEFT JOIN TF_YWPN_WATERMETER_W t5 on t1.TABLE_ID = t5.id
        WHERE t2.khbh is not null
        <if test="pipeIds != null and pipeIds != '' ">
            AND t1.PIPE_SID in
            <foreach collection="pipeIds.split(',')" item="code" open="(" close=")" separator=",">
                #{code}
            </foreach>
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and T2.khbh || t4.YHXM || t4.YHDZ || t4.LXDH like '%' || #{likeStr} || '%'
        </if>
    </select>

    <select id="getCustomerWaterVoList" resultType="com.tofly.xrtygis.custom.entity.marketing.CustomerWaterVo">
        SELECT t1.YHBH userCode,t1.YHXM userName,t1.YHDZ userAddress,t1.LXDH contactNumber,t1.SSCBPQ
        meterReadingArea,t1.SSCBB meterReadingBook,t2.SBBH waterMeterCode,t3.CWRQ month,sum(t3.sl) waterConsumption
        FROM yh_khjbxx@YXDBLINK t1 LEFT JOIN yh_cbkxx@YXDBLINK t2
        on t1.YHBH = t2.YHBH
        LEFT JOIN YY_QFMX@YXDBLINK t3
        on t2.YHBH = t3.YHBH
        WHERE 1=1
        <if test="sbbhs != null and sbbhs.size>0 ">
            AND t2.sbbh in
            <foreach collection="sbbhs" item="code" open="(" close=")" separator=",">
                #{code}
            </foreach>
        </if>
        <if test="startTime != null and endTime != null">
            and to_date(t3.CWRQ,'yyyyMM') >= to_date(#{startTime},'yyyyMM')
            and to_date(t3.CWRQ,'yyyyMM') &lt;= to_date(#{endTime},'yyyyMM')
        </if>
        <if test="likeStr != null and likeStr.trim() != ''">
            and t1.YHBH || t1.YHXM || t1.YHDZ || t1.LXDH like '%' || #{likeStr} || '%'
        </if>
        GROUP BY t1.YHBH,t1.YHXM,t1.YHDZ,t1.LXDH,t1.SSCBPQ,t1.SSCBB,t2.SBBH,t3.CWRQ,t3.CBRQ
        ORDER BY month desc
    </select>
    <select id="getUseWaterHistoryPage" resultType="com.tofly.xrtygis.custom.entity.marketing.CustomerWaterVo">
        SELECT t1.YHBH userCode,t1.YHXM userName,t1.YHDZ userAddress,t1.LXDH contactNumber,t1.SSCBPQ
        meterReadingArea,t1.SSCBB meterReadingBook,t2.SBBH waterMeterCode,t3.CWRQ month,sum(t3.sl) waterConsumption
        FROM yh_khjbxx@YXDBLINK t1 LEFT JOIN yh_cbkxx@YXDBLINK t2
        on t1.YHBH = t2.YHBH
        LEFT JOIN YY_QFMX@YXDBLINK t3
        on t2.YHBH = t3.YHBH
        WHERE 1=1
        <if test="yhbh != null and yhbh.trim() !='' ">
            AND t1.yhbh = #{yhbh}
        </if>
        <if test="startTime != null and endTime != null">
            and to_date(t3.CWRQ,'yyyyMM') >= to_date(#{startTime},'yyyyMM')
            and to_date(t3.CWRQ,'yyyyMM') &lt;= to_date(#{endTime},'yyyyMM')
        </if>
        GROUP BY t1.YHBH,t1.YHXM,t1.YHDZ,t1.LXDH,t1.SSCBPQ,t1.SSCBB,t2.SBBH,t3.CWRQ,t3.CBRQ
        ORDER BY month
    </select>

    <select id="getDmaLevelListByTableId" resultType="Integer">
        SELECT distinct t2.PARTITION_LEVEL dmaLevel
        FROM TF_DMA_WATER_W t1
        LEFT JOIN TF_YWPN_DMAREGION_W t2 on t1.DMA_ID = t2.ID
        WHERE 1=1
        <if test="tableId != null">
            AND t1.WATER_METER_ID = #{tableId}
        </if>
        <if test="dmaId != null">
            AND t1.DMA_ID != #{dmaId}
        </if>
    </select>
</mapper>