<?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.xrtygis.mapper.UserandplotMapper">

    <resultMap id="userandplotMap" type="com.tofly.xrtygis.entity.Userandplot">
        <id property="id" column="ID"/>
        <result property="plotId" column="PLOT_ID"/>
        <result property="userCode" column="USER_CODE"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createDate" column="CREATE_DATE"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="plotName" column="PLOT_ID"
                     select="com.tofly.xrtygis.mapper.ValveandplotMapper.getPlotName"/>
        <association property="userInfo" column="USER_CODE"
                     select="getUserInfo"/>
    </resultMap>

    <select id="getUserInfo" resultType="map">
        select YHBH, YHDZ, YHXM, LXDH
        from TF_YWPN_QFKHJBXX_W
        where YHBH = #{userCode}
        and rownum = 1
    </select>

    <select id="queryUserInfo" resultType="map">
        select YHBH, YHDZ, YHXM, LXDH
        from TF_YWPN_QFKHJBXX_W q
        where YHZT != 9
        <if test="dto != null and dto != ''">
            and yhbh || yhxm || yhdz || lxdh like '%' || #{dto} || '%'
        </if>
        and not exists(select USER_CODE from TF_YWPN_USERANDPLOT_W u where u.USER_CODE = q.YHBH)
    </select>

    <select id="plotCount" resultType="map">
        select distinct p.ID PLOT_ID,count(PLOT_ID) num,NAME
        from TF_YWPN_USERANDPLOT_W u right join TF_YWPN_PLOT_W p on u.PLOT_ID = p.ID
        <where>
            <if test="plotIds != null and plotIds.size() != 0">
                PLOT_ID in
                <foreach collection="plotIds" item="pi" open="(" separator="," close=")">
                    #{pi}
                </foreach>
            </if>
        </where>
        group by p.id,name
    </select>

</mapper>
