<?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="top.continew.admin.business.mapper.TgUserMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="top.continew.admin.business.model.entity.TgUser">
        <id column="id" property="id"/>
        <result column="tg_id" property="tgId"/>
        <result column="tg_account" property="tgAccount"/>
        <result column="first_name" property="firstName"/>
        <result column="last_name" property="lastName"/>
        <result column="nickname" property="nickname"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="language_code" property="languageCode"/>
        <result column="wallet_address" property="walletAddress"/>
        <result column="age_limit" property="ageLimit"/>
        <result column="old_user" property="oldUser"/>
        <result column="check_language" property="checkLanguage"/>
        <result column="login_ip" property="loginIp"/>
        <result column="login_time" property="loginTime"/>
        <result column="ip_address_convert" property="ipAddressConvert"/>
        <result column="disable_flag" property="disableFlag"/>
        <result column="channel" property="channel"/>
        <result column="ppassenger_flow_way" property="passengerFlowWay"/>
        <result column="login_telegram" property="loginTelegram"/>
        <result column="coin_address" property="coinAddress"/>
        <result column="password" property="password"/>
        <result column="mobile" property="mobile"/>
        <result column="sex" property="sex"/>
        <result column="invite_code" property="inviteCode"/>
        <result column="referrer_id" property="referrerId"/>
        <result column="airdrop_coin" property="airdropCoin"/>
        <result column="gold_coin_amount" property="goldCoinAmount"/>
        <result column="gold_coin_total_his" property="goldCoinTotalHis"/>
        <result column="online_time" property="onlineTime"/>
        <result column="user_amount" property="userAmount"/>
        <result column="created_time" property="createdTime"/>
        <result column="updated_time" property="updatedTime"/>
        <result column="referrerName" property="referrerName"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, tg_id, tg_account, first_name, last_name, nickname, real_name, avatar, language_code, wallet_address, age_limit, old_user, check_language, login_ip, login_time, ip_address_convert, disable_flag, channel,passenger_flow_way, login_telegram, coin_address, password, mobile, sex, invite_code, referrer_id, invite_reward, new_user_flag, history_key, key_num, airdrop_coin, gold_coin_amount, gold_coin_total_his, development_gold_coin, online_time, user_amount, created_time, updated_time
    </sql>

    <select id="getTgUserOfPage" resultMap="BaseResultMap">
        select tu.*,
        case
        when ru.first_name IS NOT NULL or ru.last_name IS NOT NULL
        then concat(ru.first_name, ' ', ru.last_name)
        else ru.tg_account
        end as referrerName
        from b_tg_user tu
        left join b_tg_user ru on tu.referrer_id=ru.id
        <where>
            <if test="query.userName != null and query.userName != ''">
                AND (
                tu.tg_account LIKE CONCAT('%', #{query.userName}, '%')
                OR tu.first_name LIKE CONCAT('%', #{query.userName}, '%')
                OR tu.last_name LIKE CONCAT('%', #{query.userName}, '%')
                )
            </if>
            <if test="query.time != null and query.time.size() == 2">
                AND tu.created_time BETWEEN #{query.time[0]} AND #{query.time[1]}
            </if>
        </where>
        order by tu.created_time desc
    </select>

    <select id="getMyTeamUserListByType" resultMap="BaseResultMap">
        WITH RECURSIVE team_hierarchy AS (
            -- A队：直接被你邀请的用户
            SELECT id,
                   tg_id,
                   tg_account,
                   first_name,
                   last_name,
                   nickname,
                   real_name,
                   avatar,
                   passenger_flow_way,
                   referrer_id,
                   created_time,
                   'A' as team_type,
                   1   as level
            FROM b_tg_user
            WHERE referrer_id = '{#userId}'

            UNION ALL

            -- B队：被A队邀请的用户
            SELECT u.id,
                   u.tg_id,
                   u.tg_account,
                   u.first_name,
                   u.last_name,
                   u.nickname,
                   u.real_name,
                   u.avatar,
                   u.passenger_flow_way,
                   u.referrer_id,
                   u.created_time,
                   'B' as team_type,
                   h.level + 1
            FROM b_tg_user u
                     JOIN team_hierarchy h ON u.referrer_id = h.id
            WHERE h.level = 1 -- 只获取A队成员邀请的用户
        )

        SELECT id,
               tg_id,
               tg_account,
               first_name,
               last_name,
               nickname,
               real_name,
               avatar,
               passenger_flow_way,
               referrer_id,
               team_type
        FROM team_hierarchy
        where team_type = '#{teamType}'
        ORDER BY created_time desc
    </select>
</mapper>
