package com.fs.course.mapper;

import java.util.List;
import com.fs.course.domain.FsUserCourseComment;
import com.fs.course.param.FsUserCourseCommentParam;
import com.fs.course.param.FsUserCourseCommentUParam;
import com.fs.course.vo.FsUserCourseCommentListUVO;
import com.fs.course.vo.FsUserCourseCommentListVO;
import com.fs.course.vo.FsUserCourseCommentReplyListUVO;
import com.fs.course.vo.FsUserCourseCommentVO;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 课堂评论Mapper接口
 *
 * @author fs
 * @date 2024-05-15
 */
public interface FsUserCourseCommentMapper
{
    /**
     * 查询课堂评论
     *
     * @param commentId 课堂评论主键
     * @return 课堂评论
     */
    public FsUserCourseComment selectFsUserCourseCommentByCommentId(Long commentId);

    /**
     * 查询课堂评论列表
     *
     * @param fsUserCourseComment 课堂评论
     * @return 课堂评论集合
     */
    public List<FsUserCourseComment> selectFsUserCourseCommentList(FsUserCourseComment fsUserCourseComment);

    /**
     * 新增课堂评论
     *
     * @param fsUserCourseComment 课堂评论
     * @return 结果
     */
    public int insertFsUserCourseComment(FsUserCourseComment fsUserCourseComment);

    /**
     * 修改课堂评论
     *
     * @param fsUserCourseComment 课堂评论
     * @return 结果
     */
    public int updateFsUserCourseComment(FsUserCourseComment fsUserCourseComment);

    /**
     * 删除课堂评论
     *
     * @param commentId 课堂评论主键
     * @return 结果
     */
    public int deleteFsUserCourseCommentByCommentId(Long commentId);

    /**
     * 批量删除课堂评论
     *
     * @param commentIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserCourseCommentByCommentIds(Long[] commentIds);

    @Select({"<script> " +
            "select c.*,u.nick_name,u.phone,u.avatar from fs_user_course_comment c LEFT JOIN fs_user u ON c.user_id=u.user_id"+
            "<where>  \n" +
            "            <if test=\"userId != null \"> and c.user_id = #{userId}</if>\n" +
            "            <if test=\"courseId != null \"> and c.course_id = #{courseId}</if>\n" +
            "            <if test=\"type != null \"> and c.type = #{type}</if>\n" +
            "            <if test=\"parentId != null \"> and c.parent_id = #{parentId}</if>\n" +
            "            <if test=\"content != null  and content != ''\"> and c.content = #{content}</if>\n" +
            "            <if test=\"replyCount != null \"> and c.reply_count = #{replyCount}</if>\n" +
            "            <if test=\"phone != null \"> and u.phone = #{phone}</if>\n" +
            "        </where>" +
            "order by comment_id desc"+
            "</script>"})
    List<FsUserCourseCommentListVO> selectFsUserCourseCommentListVO(FsUserCourseCommentParam fsUserCourseComment);
    @Select("select c.*,u.nick_name,u.phone from fs_user_course_comment c LEFT JOIN fs_user u ON c.user_id=u.user_id where comment_id=#{commentId}")
    FsUserCourseCommentVO selectFsUserCourseCommentVOByCommentId(Long commentId);
    @Select("select c.*,u.nick_name,u.phone,u,avatar from fs_user_course_comment c LEFT JOIN fs_user u ON c.user_id=u.user_id where parent_id=#{pid}")
    List<FsUserCourseCommentVO> selectFsUserCourseCommentAllListVO(Long pid);
    @Select({"<script> " +
            "select c.*,u.nick_name,u.avatar,tu.nick_name to_nick_name, IFNULL(l.id, 0) AS is_like from fs_user_course_comment c " +
            "LEFT JOIN fs_user u ON c.user_id=u.user_id " +
            "LEFT  JOIN fs_user tu ON tu.user_id =c.to_user_id " +
            "LEFT JOIN fs_user_course_comment_like l ON c.comment_id=l.comment_id and #{userId}=l.user_id "+
            "where c.is_del = 0 and  c.type = 1  \n" +
            "<if test=\"courseId != null \"> and c.course_id = #{courseId}</if>\n" +
            "<if test=\"sortType != null and sortType == 1\"> order by c.likes desc </if>" +
            "<if test=\"sortType != null and sortType == 2\"> order by c.comment_id desc </if>" +
            "</script>"})
    List<FsUserCourseCommentListUVO> selectFsUserCourseCommentListUVO(FsUserCourseCommentUParam param);
    @Select("select c.*,u.nick_name,u.avatar,tu.nick_name to_nick_name from fs_user_course_comment c LEFT JOIN fs_user u ON c.user_id=u.user_id LEFT  JOIN fs_user tu ON tu.user_id =c.to_user_id where c.user_id=#{userId}")
    List<FsUserCourseCommentListUVO> selectFsUserCourseCommentMyListUVO(Long userId);

    @Update("update fs_user_course_comment set likes=likes+1 where comment_id=#{commentId}")
    int addLikes(Long commentId);


    @Update("update fs_user_course_comment set likes=likes-1 where comment_id=#{commentId}")
    int minusLikes(Long commentId);
    @Update("update fs_user_course_comment set reply_count=reply_count-1 where comment_id=#{parentId}")
    int minusComment(Long parentId);
    @Update("update fs_user_course_comment set reply_count=reply_count+1 where comment_id=#{parentId}")
    int addComment(Long parentId);

    @Select("select count(0) from fs_user_course_comment where course_id=#{courseId} and type = 1 and is_del = 0 ")
    Long selectCommentNumByCourseId(Long courseId);


    @Select("select c.*,u.nick_name,u.phone,u.avatar from fs_user_course_comment c LEFT JOIN fs_user u ON c.user_id=u.user_id where c.course_id = #{courseId} and c.type = 1 and c.is_del = 0 order by c.create_time desc limit 3")
    List<FsUserCourseCommentListUVO> selectFsUserCourseCommentListVOByCourseId(Long courseId);

    @Select({"<script> " +
            "select c.*,u.nick_name,u.avatar,tu.nick_name to_nick_name  from fs_user_course_comment c " +
            " LEFT JOIN fs_user u ON c.user_id=u.user_id " +
            " LEFT  JOIN fs_user tu ON tu.user_id =c.to_user_id " +
            " where c.is_del = 0 and c.type = 2 and c.parent_id = #{commentId} " +
            " order by c.comment_id desc "+
            "</script>"})
    List<FsUserCourseCommentReplyListUVO> selectFsUserCourseCommentReplyListUVO(Long commentId);

    @Select({"<script> " +
            "select c.*,u.nick_name,u.avatar,tu.nick_name to_nick_name from fs_user_course_comment c " +
            "LEFT JOIN fs_user u ON c.user_id=u.user_id " +
            "LEFT  JOIN fs_user tu ON tu.user_id =c.to_user_id " +
            "where c.is_del = 0 and  c.type = 1  \n" +
            "<if test=\"courseId != null \"> and c.course_id = #{courseId}</if>\n" +
            "<if test=\"sortType != null and sortType == 1\"> order by c.likes desc </if>" +
            "<if test=\"sortType != null and sortType == 2\"> order by c.comment_id desc </if>" +
            "</script>"})
    List<FsUserCourseCommentListUVO> selectFsUserCourseCommentListUVOAll(FsUserCourseCommentUParam param);
}
