<!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 q.YHBH, YHXM, YHDZ,SBBH sid,X,Y
        from TF_YWPN_WATERMETER_W w right join TF_YWPN_QFKHJBXX_W q on w.YHBH = q.YHBH
        where not exists(select id from TF_YWPN_TABLELINKUSER_W t where q.YHBH = t.KHBH )
        and YHZT != 9
        <if test="str != null and str!=''">
            and q.YHBH || YHXM || YHDZ like '%' || #{str} || '%'
        </if>
    </select>

    <select id="unaffiliatedCustomer2" resultType="map" fetchSize="1000">
        select q.YHBH, SBBH sid, YHDZ
        from TF_YWPN_WATERMETER_W w
                 right join TF_YWPN_QFKHJBXX_W q on w.YHBH = q.YHBH
        where not exists(select * from TF_YWPN_TABLELINKUSER_W t where q.YHBH = t.KHBH)
          and YHZT != 9
    </select>

    <select id="viewTablePipe" resultType="map">
        select m.ID,
        m.SID,
        X,
        Y,
        ADDRESS,
        PIPE_SID,
        decode(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.ID = t.TABLE_ID
        where ISSUM = 1
        <if test="type != 3">
            and STATE = #{type,jdbcType = NUMERIC}
        </if>
        order by m.id
    </select>

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

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

    <select id="getCustomerInfo" resultType="map">
        select YHBH, YHXM, YHDZ, SBBH
        from TF_YWPN_QFKHJBXX_W
        where YHBH in
        <foreach collection="list" item="c" open="(" separator="," close=")">
            #{c}
        </foreach>
    </select>

    <select id="getWateMeter" resultType="map">
        select ADDRESS,X,Y
        from TF_YWPN_WATERMETER_W where id = #{tableId}
    </select>

    <insert id="insertPipeTable">
        insert into TF_YWPN_TABLELINKPIPE_W (ID, PIPE_SID, TABLE_ID, SID, STATE)
        <foreach collection="list" item="l" separator="union all">
            select
            #{l.id,jdbcType=NUMERIC},
            #{l.pipeSid,jdbcType=VARCHAR},
            #{l.tableId,jdbcType=VARCHAR},
            <choose>
                <when test="l.sid == null">
                    null,
                </when>
                <otherwise>
                    #{l.sid,jdbcType=VARCHAR},
                </otherwise>
            </choose>
            #{l.state,jdbcType=NUMERIC}
            from DUAL
        </foreach>
    </insert>

</mapper>