<?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.ruoyi.frequency.refereeuser.mapper.RefereeUserMapper">

    <resultMap type="com.ruoyi.frequency.refereeuser.entity.RefereeUser" id="RefereeUserResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="parentUserType"    column="parent_user_type"    />
        <result property="parentUserId"    column="parent_user_id"    />
    </resultMap>

    <sql id="selectRefereeUserVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_type, user_id, parent_user_type, parent_user_id from t_referee_user
    </sql>

    <select id="selectRefereeUserList" parameterType="com.ruoyi.frequency.refereeuser.entity.RefereeUser" resultMap="RefereeUserResult">
        <include refid="selectRefereeUserVo"/>
        <where>
            <if test="createBy != null "> and create_by = #{createBy}</if>
            <if test="updateBy != null "> and update_by = #{updateBy}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="parentUserType != null "> and parent_user_type = #{parentUserType}</if>
            <if test="parentUserId != null "> and parent_user_id = #{parentUserId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectRefereeUserById" parameterType="Long" resultMap="RefereeUserResult">
        <include refid="selectRefereeUserVo"/>
        where id = #{id}
    </select>

    <insert id="insertRefereeUser" parameterType="com.ruoyi.frequency.refereeuser.entity.RefereeUser">
        insert into t_referee_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="parentUserType != null">parent_user_type,</if>
            <if test="parentUserId != null">parent_user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="parentUserType != null">#{parentUserType},</if>
            <if test="parentUserId != null">#{parentUserId},</if>
        </trim>
    </insert>

    <update id="updateRefereeUser" parameterType="com.ruoyi.frequency.refereeuser.entity.RefereeUser">
        update t_referee_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="parentUserType != null">parent_user_type = #{parentUserType},</if>
            <if test="parentUserId != null">parent_user_id = #{parentUserId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRefereeUserById" parameterType="Long">
        delete from t_referee_user where id = #{id}
    </delete>

    <delete id="deleteRefereeUserByIds" parameterType="String">
        delete from t_referee_user where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="refereeUserPage" resultType="com.ruoyi.controller.resp.RefereeUserPageResp">
        SELECT
            *
        FROM
            (
                SELECT
                    r.id,
                    r.create_time,
                    r.user_type,
                    r.user_id,
                    IFNULL(
                            (
                                SELECT
                                    SUM(ir.integral)
                                FROM
                                    t_integral_record ir
                                        LEFT JOIN t_order o ON o.id = ir.object_id
                                        AND ir.user_type = 1
                                WHERE
                                    ir.del_flag = 1
                                  AND ir.record = 2
                                  AND o.customer_id = r.user_id
                                  AND ir.user_id = r.parent_user_id
                                  AND ir.user_type = r.parent_user_type
                            ),
                            0
                    ) integral
                FROM
                    t_referee_user r
                WHERE
                    r.del_flag = 1
                  AND r.parent_user_type = #{userVo.userType}
                  AND r.parent_user_id = #{userVo.userId}
                ORDER BY
                    r.create_time DESC
            ) tt
        ORDER BY
            tt.integral DESC,
            tt.id
    </select>
</mapper>
