<?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.system.mapper.SysUserFollowMapper">
    
    <resultMap type="SysUserFollow" id="SysUserFollowResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="followedUserId"    column="followed_user_id"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="followStatus" column="follow_status" />
    </resultMap>

    <resultMap id="FollowerListDtoResult" type="FollowerListDto">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="detail"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="followStatus" column="follow_status" />

    </resultMap>

    <sql id="selectSysUserFollowVo">
        select id, user_id, followed_user_id, status, follow_status,create_time, update_time from sys_user_follow
    </sql>

    <select id="selectSysUserFollowList" parameterType="SysUserFollow" resultMap="SysUserFollowResult">
        <include refid="selectSysUserFollowVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="followedUserId != null "> and followed_user_id = #{followedUserId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="followStatus != null  and followStatus != ''"> and follow_status = #{followStatus}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectSysUserFollowById" parameterType="Long" resultMap="SysUserFollowResult">
        <include refid="selectSysUserFollowVo"/>
        where id = #{id}
    </select>
    <select id="getFollowerList" resultMap="FollowerListDtoResult">

        select u.user_id, u.nick_name, u.avatar, u.remark, f.create_time, f.follow_status
        from sys_user_follow f
        left join sys_user u on f.followed_user_id = u.user_id
        where f.user_id = #{userId}

    </select>
    <select id="getFollowedList" resultMap="FollowerListDtoResult">
        select u.user_id, u.nick_name, u.avatar, u.remark, f.create_time, f.follow_status
        from sys_user_follow f
        left join sys_user u on f.user_id = u.user_id
        where f.followed_user_id = #{userId}
        order by f.create_time desc
    </select>
    <select id="getFollowingCount" resultType="java.lang.Long">
        select count(*) from sys_user_follow where user_id = #{userId}
    </select>
    <select id="getFollowedCount" resultType="java.lang.Long">
        select count(*) from sys_user_follow where followed_user_id = #{userId}
    </select>
    <select id="getAddFollowerList" resultMap="FollowerListDtoResult">

        select u.user_id, u.nick_name, u.avatar, u.remark, f.create_time, f.follow_status
        from sys_user_follow f
         left join sys_user u on f.user_id = u.user_id
         left join sys_user_notification n on f.user_id = n.user_id
        where f.followed_user_id = #{userId} and n.user_id = #{userId} and n.status = '0' and n.type = '1'
        order by f.create_time desc

    </select>

    <insert id="insertSysUserFollow" parameterType="SysUserFollow" useGeneratedKeys="true" keyProperty="id">
        insert into sys_user_follow
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="followedUserId != null">followed_user_id,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="followStatus != null">follow_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="followedUserId != null">#{followedUserId},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="followStatus != null">#{followStatus},</if>
         </trim>
    </insert>

    <update id="updateSysUserFollow" parameterType="SysUserFollow">
        update sys_user_follow
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="followedUserId != null">followed_user_id = #{followedUserId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="followStatus != null">follow_status = #{followStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysUserFollowUserId" parameterType="Long">
        delete from sys_user_follow where user_id = #{userId} or followed_user_id = #{userId}
    </delete>

    <delete id="deleteSysUserFollowById" parameterType="Long">
        delete from sys_user_follow where id = #{id}
    </delete>

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

        delete  from sys_user_follow where user_id = #{userId} and followed_user_id = #{followedUserId}

    </delete>
</mapper>