<?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.greenorange.promotion.mapper.UserInfoMapper">

    <resultMap id="BaseResultMap" type="com.greenorange.promotion.model.entity.UserInfo">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="nickName" column="nickName" jdbcType="VARCHAR"/>
            <result property="userAvatar" column="userAvatar" jdbcType="VARCHAR"/>
            <result property="phoneNumber" column="phoneNumber" jdbcType="VARCHAR"/>
            <result property="userPassword" column="userPassword" jdbcType="VARCHAR"/>
            <result property="invitationCode" column="invitationCode" jdbcType="VARCHAR"/>
            <result property="userRole" column="userRole" jdbcType="OTHER"/>
            <result property="parentUserId" column="parentUserId" jdbcType="BIGINT"/>
            <result property="superUserList" column="superUserList" jdbcType="VARCHAR"/>
            <result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
            <result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,nickName,userAvatar,
        phoneNumber,userPassword,invitationCode,
        userRole,parentUserId,superUserList,
        isDelete,createTime,updateTime
    </sql>




<!--    查询从 userId 一路到根节点的所有 id，按 depth 倒序（根先出）-->
    <select id="findPathToRoot" resultType="java.lang.Long">
        WITH RECURSIVE user_path AS (
            SELECT id, parentUserId, 1 AS depth
            FROM user_info
            WHERE id = #{userId}

            UNION ALL

            SELECT u.id, u.parentUserId, up.depth + 1
            FROM user_info u
                     JOIN user_path up ON u.id = up.parentUserId
            WHERE up.depth &lt; 50000
        )
        SELECT id
        FROM user_path
        ORDER BY depth DESC
    </select>
</mapper>
