<?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.huiju.estateagents.center.taUser.mapper.TaUserMapper">

    <select id="getPageList" resultType="map">
        SELECT
            t.user_id AS userId,
            t.org_id AS orgId,
            t.user_name AS userName,
            t.login_name AS loginName,
            t.phone,
            t.avatar,
            t.create_date AS createDate,
            t.status,
            o.NAME AS orgName,
            o.address,
            o.logo,
            o.remark,
            m.miniapp_id AS miniappId,
            m.secret,
            m.token,
            m.NAME AS miniappName,
            m.qr_code AS qrCode,
            t.exp_date as expire_date,
            m.org_id
        FROM
            ta_user t
            LEFT JOIN ta_org o ON t.org_id = o.org_id
            LEFT JOIN ta_miniapp m ON o.org_id = m.org_id
            left join ta_channel_app_relation n on o.org_id = n.org_id
            left join ta_channel_proxy z on z.channel_id = n.channel_id
        WHERE
            t.is_admin =1
            <if test="channelId != null and channelId != ''">
                and n.channel_id = #{channelId}
            </if>
            <if test="miniAppName != null and miniAppName != ''">
                and m.name like CONCAT('%', #{miniAppName}, '%')
            </if>
    </select>

    <select id="getTagsList" resultType="com.huiju.estateagents.center.taUser.entity.TaTags">
        SELECT
            t.*
        FROM
            ta_tag_user u
            LEFT JOIN ta_tags t ON u.tag_id = t.tag_id
            AND t.org_id = #{orgId}
        WHERE
            u.user_id = #{userId}
    </select>

    <select id="getUserList" resultType="com.huiju.estateagents.center.taUser.entity.TaUser">
    SELECT
        *
    FROM
        ta_user
    WHERE
        org_id = #{orgId}
        and phone = #{phone}
    </select>
    <select id="seleUserList" resultType="com.huiju.estateagents.center.taUser.entity.TaUser">
        SELECT
            t.*,
            b.building_id
        FROM
            ta_user t
            LEFT JOIN ta_person_building b ON t.user_id = b.user_id
        WHERE
            t.org_id = #{taUser.orgId} AND (t.is_admin != 1 or t.is_admin is null)
            <if test="taUser.userName != null and taUser.userName != ''">
                and t.user_name like CONCAT('%', #{taUser.userName}, '%')
            </if>
            <if test="taUser.phone != null and taUser.phone != ''">
                and t.phone like CONCAT('%', #{taUser.phone}, '%')
            </if>
            <if test="taUser.status != null and taUser.status != ''">
                and t.status = #{taUser.status}
            </if>
            <if test="taUser.isConsultant != null and taUser.isConsultant != '' or taUser.isConsultant ==0">
                and t.is_consultant = #{taUser.isConsultant}
            </if>
            <if test="taUser.buildingId != null and taUser.buildingId != ''">
                and b.building_id = #{taUser.buildingId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND b.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            GROUP BY user_id
            order by t.weight desc, create_date desc
    </select>

    <select id="validateWeight" resultType="integer">
      select count(*) from ta_user t
      LEFT JOIN ta_person_building a on t.user_id = a.user_id
      where 1=1
      <if test="weight != null and weight != ''">
          and t.weight = #{weight}
      </if>
      <if test="buildingIds != null and buildingIds.size() > 0 ">
          and a.building_id in
          <foreach collection="buildingIds" item="item" index="index" open="(" close=")" separator=",">
              #{item}
          </foreach>
      </if>
      <if test="buildingId != null and buildingId != '' ">
          and a.building_id = #{buildingId}
      </if>
      <if test="userId != null and userId != ''">
          and t.user_id != #{userId}
      </if>
    </select>

    <select id="getAdminByAppID" resultType="com.huiju.estateagents.center.taUser.entity.TaUser">
        SELECT
        m.*
        FROM
        ta_miniapp t
        JOIN ta_user m USING (org_id)
        WHERE
        t.miniapp_id = #{appid}
        AND m.is_admin = 1
    </select>

</mapper>
