<?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="org.springblade.modules.platform.mapper.UserRecomMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userRecomResultMap" type="org.springblade.modules.platform.entity.UserRecom">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="recom_id" property="recomId"/>
        <result column="recom_code" property="recomCode"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <!-- 查询团队和直推 -->
    <resultMap id="userRecomVOResultMap" type="org.springblade.modules.platform.vo.UserRecomVO">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="recom_id" property="recomId"/>
        <result column="recom_code" property="recomCode"/>
        <result column="avatar" property="avatar"/>
        <result column="state" property="state"/>
        <result column="real_name" property="name"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
    </resultMap>


    <select id="selectUserRecomPage" resultMap="userRecomResultMap">
        select * from t_user_recom where is_deleted = 0
    </select>

    <select id="selectUserRecomVoPage" resultMap="userRecomVOResultMap">
        SELECT
        a.user_id,
        a.state,
        u.avatar,
        u.`code`,
        u.`real_name`,
        r.create_time,
        u.phone
        FROM
        blade_user_app a
        left JOIN blade_user u ON a.user_id = u.id
        left JOIN t_user_recom r on a.user_id = r.user_id
        WHERE
        u.is_deleted = 0
        and a.alipay_user_id is not null
        <if test="parentId!=null and parentId != ''">
            and a.parent_id = #{parentId}
        </if>
        <if test="parentPath!=null and parentPath != ''">
            and a.parent_path LIKE concat(concat('%', #{parentPath}),'%') and a.parent_id != #{parentPath}
        </if>
    </select>

    <select id="selectUserConsignInfo" resultType="org.springblade.modules.platform.vo.UserConsignVO">
        select count(1) as count, c.consign_config_id consignConfigId, c.consign_user_id consignUserId, c.end_time endTime
        from blade_user_app ua
        left join  blade_user u ON ua.user_id = u.id
        left join t_consign c  on ua.user_id = c.consign_user_id
        where
        <if test="type == 2">
            ua.parent_id = #{userId}
        </if>

        <if test="type == 1">
            ua.parent_path LIKE concat(concat('%', #{userId}),'%') and ua.parent_id != #{userId}
        </if>
        and to_days(now())-to_days(c.create_time) = 1
        group by c.consign_config_id,c.consign_user_id, c.end_time
    </select>

</mapper>
