<?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.system.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userResultMap" type="org.springblade.modules.system.entity.User">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="code" property="code"/>
        <result column="user_type" property="userType"/>
        <result column="account" property="account"/>
        <result column="password" property="password"/>
        <result column="name" property="name"/>
        <result column="real_name" property="realName"/>
        <result column="email" property="email"/>
        <result column="phone" property="phone"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="role_id" property="roleId"/>
        <result column="dept_id" property="deptId"/>
        <result column="post_id" property="postId"/>
    </resultMap>

    <select id="selectUserPage" resultMap="userResultMap">
        select * from blade_user where is_deleted = 0
        <if test="tenantId!=null and tenantId != ''">
            and tenant_id = #{tenantId}
        </if>
        <if test="user.tenantId!=null and user.tenantId != ''">
            and tenant_id = #{user.tenantId}
        </if>
        <if test="user.account!=null and user.account != ''">
            and account = #{user.account}
        </if>
        <if test="user.realName!=null and user.realName != ''">
            and real_name = #{user.realName}
        </if>
        <if test="user.userType!=null and user.userType != ''">
            and user_type = #{user.userType}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and id in (
            SELECT
            user_id
            FROM
            blade_user_dept
            WHERE
            dept_id IN
            <foreach collection="deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
        ORDER BY id
    </select>

    <select id="getUser" resultMap="userResultMap">
        SELECT
            *
        FROM
            blade_user
        WHERE
            tenant_id = #{param1} and account = #{param2} and password = #{param3} and is_deleted = 0
    </select>

    <select id="exportUser" resultType="org.springblade.modules.system.excel.UserExcel">
        SELECT id, tenant_id, user_type, account, name, real_name, email, phone, birthday, role_id, dept_id, post_id FROM blade_user ${ew.customSqlSegment}
    </select>
    <select id="getUserTree" resultType="org.springblade.modules.system.vo.UserLvVo">
        SELECT DISTINCT
            u1.LEVEL - 1 level,
            u2.*
        FROM
            (
                SELECT
                    @ids AS _ids,
                    ( SELECT @ids := GROUP_CONCAT( user_id ) FROM blade_user_app WHERE FIND_IN_SET( parent_id, @ids ) ) AS cids,
                    @l := @l + 1 AS LEVEL
                FROM
                    blade_user_app,
                    ( SELECT @ids := #{userId}, @l := 0 ) b
            ) u1,
            blade_user u2
        WHERE
            FIND_IN_SET( u2.id, u1._ids )
          and LEVEL&lt;=7
        ORDER BY
            level,
            id
    </select>

    <select id="getParentIdList" resultType="java.lang.Long">
        select
            b.parent_id as user_id
        from (
                 select a.parent_id, count(1) as num
                 from blade_user_app a
                 group by a.parent_id
             ) b
        where b.num &gt; 5
    </select>

    <select id="getMakerList" resultType="java.lang.Long">
        select
            b.user_id as user_id
        from (
                 select a.parent_id user_id, count(1) as num
                 from blade_user_app a
                 where a.is_role &gt; 1
                 group by a.parent_id
             ) b
        where b.num &gt;= #{num}
        and b.user_id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getNewList" resultType="java.lang.Long">
        select
        a.user_id
        from (
        select
        tc.consign_user_id user_id, min(tc.create_time) min_time
        from t_consign tc
        group by tc.consign_user_id
        )  a
        where 1=1
        and (to_days(now()) - to_days(a.min_time)) &lt;= 15
        and a.user_id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getTeamList" resultType="java.lang.Long">
        select
        b.user_id as user_id
        from (
        select bu.id user_id,count(1) num
        from blade_user bu
        left join blade_user_app bua on bua.parent_path like concat('%',bu.id,'%') and bu.id != bua.parent_id
        left join blade_user bu2 on bua.user_id = bu2.id
        where bu2.real_name is not null
        group by bu.id
        ) b
        where b.num &gt;= #{num}
        and b.user_id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getTeamConsignList" resultType="java.lang.Long">
        select
        b.user_id as user_id
        from (
        select bu.id user_id, count(1) num
        from blade_user bu
        left join blade_user_app bua on bua.parent_path like concat('%',bu.id,'%') and bu.id != bua.parent_id
        left join blade_user bu2 on bua.user_id = bu2.id
        left join (
        select bu.id user_id,bu.real_name,count(1) num
        from blade_user bu
        left join t_consign tc on tc.consign_user_id = bu.id
        where to_days(tc.create_time) = to_days(#{consignDate})
        group by bu.id
        ) a on a.user_id = bu2.id
        where bu2.real_name is not null
        and a.num > 0
        group by bu.id
        ) b
        where b.num &gt;= #{num}
        and b.user_id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getRecomConsignList" resultType="java.lang.Long">
        select
        b.user_id as user_id
        from (
        select bu.id user_id, count(1) num
        from blade_user bu
        left join blade_user_app bua on  bu.id = bua.parent_id
        left join blade_user bu2 on bua.user_id = bu2.id
        left join (
        select bu.id user_id,bu.real_name,count(1) num
        from blade_user bu
        left join t_consign tc on tc.consign_user_id = bu.id
        where to_days(tc.create_time) = to_days(#{consignDate})
        group by bu.id
        ) a on a.user_id = bu2.id
        where bu2.real_name is not null
        and a.num > 0
        group by bu.id
        ) b
        where b.num &gt;= #{num}
        and b.user_id in
        <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="countRecomAndTeamConsign" resultType="java.util.Map">
        select '当日直推购买人数' name,count(1) num
        from blade_user bu
                 left join blade_user_app bua on  bu.id = bua.parent_id
                 left join blade_user bu2 on bua.user_id = bu2.id
                 left join (
            select bu.id user_id,bu.real_name,count(1) num
            from blade_user bu
                     left join t_consign tc on tc.consign_user_id = bu.id
            where 1=1
              and to_days(tc.create_time) = to_days(#{consignDate})
            group by bu.id
        ) a on a.user_id = bu2.id
        where bu2.real_name is not null
          and a.num is not null
          and bu.id = #{userId}
        group by bu.id
        union all
        select '当日团队购买人数', count(1) num
        from blade_user bu
                 left join blade_user_app bua on bua.parent_path like concat('%',bu.id,'%') and bu.id != bua.parent_id
    left join blade_user bu2 on bua.user_id = bu2.id
            left join (
            select bu.id user_id,bu.real_name,count(1) num
            from blade_user bu
            left join t_consign tc on tc.consign_user_id = bu.id
            where to_days(tc.create_time) = to_days(#{consignDate})
            group by bu.id
            ) a on a.user_id = bu2.id
        where bu2.real_name is not null
          and a.num > 0
          and bu.id = #{userId}
        group by bu.id
        union all
        select '当场直推购买人数' name,count(1) num
        from blade_user bu
                 left join blade_user_app bua on  bu.id = bua.parent_id
                 left join blade_user bu2 on bua.user_id = bu2.id
                 left join (
            select bu.id user_id,bu.real_name,count(1) num
            from blade_user bu
                     left join t_consign tc on tc.consign_user_id = bu.id
            where 1=1
              and tc.consign_config_id = #{consignConfigId}
            group by bu.id
        ) a on a.user_id = bu2.id
        where bu2.real_name is not null
          and a.num is not null
          and bu.id = #{userId}
        group by bu.id
        union all
        select '当场团队购买人数', count(1) num
        from blade_user bu
                 left join blade_user_app bua on bua.parent_path like concat('%',bu.id,'%') and bu.id != bua.parent_id
         left join blade_user bu2 on bua.user_id = bu2.id
            left join (
            select bu.id user_id,bu.real_name,count(1) num
            from blade_user bu
            left join t_consign tc on tc.consign_user_id = bu.id
            where 1=1
            and tc.consign_config_id = #{consignConfigId}
            group by bu.id
            ) a on a.user_id = bu2.id
        where bu2.real_name is not null
          and a.num > 0
          and bu.id = #{userId}
        group by bu.id
    </select>
    <select id="getUserAndType" resultType="org.springblade.modules.system.entity.User">
        SELECT
            *
        FROM
            blade_user
        WHERE
            tenant_id = #{param1} and account = #{param2} and password = #{param3} and user_type = #{param4} and is_deleted = 0
    </select>
</mapper>
