package com.fs.course.mapper;

import java.util.List;
import com.fs.course.domain.FsUserCourseNote;
import com.fs.course.param.FsUserCourseNoteUParam;
import com.fs.course.vo.FsUserCourseMyNoteListUVO;
import com.fs.course.vo.FsUserCourseNoteListUVO;
import com.fs.course.vo.FsUserCourseNoteUVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 课堂笔记Mapper接口
 *
 * @author fs
 * @date 2024-05-15
 */
public interface FsUserCourseNoteMapper
{
    /**
     * 查询课堂笔记
     *
     * @param noteId 课堂笔记主键
     * @return 课堂笔记
     */
    public FsUserCourseNote selectFsUserCourseNoteByNoteId(Long noteId);

    /**
     * 查询课堂笔记列表
     *
     * @param fsUserCourseNote 课堂笔记
     * @return 课堂笔记集合
     */
    public List<FsUserCourseNote> selectFsUserCourseNoteList(FsUserCourseNote fsUserCourseNote);

    /**
     * 新增课堂笔记
     *
     * @param fsUserCourseNote 课堂笔记
     * @return 结果
     */
    public int insertFsUserCourseNote(FsUserCourseNote fsUserCourseNote);

    /**
     * 修改课堂笔记
     *
     * @param fsUserCourseNote 课堂笔记
     * @return 结果
     */
    public int updateFsUserCourseNote(FsUserCourseNote fsUserCourseNote);

    /**
     * 删除课堂笔记
     *
     * @param noteId 课堂笔记主键
     * @return 结果
     */
    public int deleteFsUserCourseNoteByNoteId(Long noteId);

    /**
     * 批量删除课堂笔记
     *
     * @param noteIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserCourseNoteByNoteIds(Long[] noteIds);

    @Select({"<script> " +
            "select n.*,c.course_name,cc.cate_name as sub_cate_name,v.title as video_name,1 as note_type,v.thumbnail from fs_user_course_note n " +
            " left join fs_user_course c on c.course_id = n.course_id " +
            " left join fs_user_course_video v on v.video_id = n.video_id " +
            " left join fs_user_course_category cc on cc.cate_id = c.sub_cate_id " +
            "where n.is_del = 0 and n.status = 2   " +
            "<if test = ' maps.cateId !=null '> " +
            "and n.note_cate_id = #{maps.cateId} " +
            "</if>" +
            "<if test = ' maps.userId !=null '> " +
            "and n.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and n.content like concat('%', #{maps.keyword}, '%') " +
            "</if>" +
            " order by n.create_time  "+
            "</script>"})
    List<FsUserCourseMyNoteListUVO> selectFsUserCourseMyNoteListUVO(@Param("maps") FsUserCourseNoteUParam param);

    @Select({"<script> " +
            "select s.save_id,n.*,c.course_name,cc.cate_name as sub_cate_name,v.title as video_name,2 as note_type,v.thumbnail from fs_user_course_note_save s" +
            " left join fs_user_course_note n on n.note_id = s.note_id " +
            " left join fs_user_course c on c.course_id = n.course_id " +
            " left join fs_user_course_video v on v.video_id = n.video_id " +
            " left join fs_user_course_category cc on cc.cate_id = c.sub_cate_id " +
            "where n.is_del = 0 and n.status = 2   " +
            "<if test = ' maps.cateId !=null '> " +
            "and n.note_cate_id = #{maps.cateId} " +
            "</if>" +
            "<if test = ' maps.userId !=null '> " +
            "and s.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and n.content like concat('%', #{maps.keyword}, '%') " +
            "</if>" +
            " order by s.create_time  "+
            "</script>"})
    List<FsUserCourseMyNoteListUVO> selectFsUserCourseMyNoteListBySave(@Param("maps") FsUserCourseNoteUParam param);

    @Select( "select n.*,c.course_name,v.title as video_name,c.img_url from fs_user_course_note n " +
            " left join fs_user_course c on c.course_id = n.course_id " +
            " left join fs_user_course_video v on v.video_id = n.video_id " +
            " where n.note_id = #{noteId} " )
    FsUserCourseNoteUVO selectFsUserCourseNoteUVOByNoteId(Long noteId);

    @Select("select count(1) from fs_user_course_note n where n.course_id = #{courseId} and status = 2 and is_del = 0 ")
    Long selectFsUserCourseNoteCountByCourseId(Long courseId);


    @Select({"<script> " +
            "select n.*,u.nick_name as user_nick_name,u.avatar,CASE WHEN l.user_id IS NOT NULL THEN 1 ELSE 0 END AS is_like," +
            " CASE WHEN s.user_id IS NOT NULL THEN 1 ELSE 0 END AS is_save " +
            " from fs_user_course_note n " +
            " left join fs_user u on u.user_id = n.user_id " +
            " left join fs_user_course_note_like l on n.note_id = l.note_id and l.user_id = #{maps.userId}" +
            " left join fs_user_course_note_save s on n.note_id = s.note_id and s.user_id = #{maps.userId}" +
            " where n.is_del = 0 and n.status = 2   " +
            "<if test = ' maps.courseId !=null '> " +
            "and n.course_id = #{maps.courseId} " +
            "</if>" +
            "<if test = ' maps.cateId !=null '> " +
            "and n.note_cate_id = #{maps.cateId} " +
            "</if>" +
            "<if test = 'maps.sortType != null and maps.sortType == 3 '> " +
            "and n.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and n.content like concat('%', #{maps.keyword}, '%') " +
            "</if>" +
            "<if test='maps.sortType != null and maps.sortType == 1'> order by n.likes desc </if>" +
            "<if test='maps.sortType != null and maps.sortType == 2'> order by n.create_time desc </if>" +

            "</script>"})
    List<FsUserCourseNoteListUVO> selectFsUserCourseNoteListUVO(@Param("maps") FsUserCourseNoteUParam param);

    @Update("update fs_user_course_note set likes=likes+1 where note_id=#{noteId}")
    int addLikes(Long noteId);


    @Update("update fs_user_course_note set likes=likes-1 where note_id=#{noteId}")
    int minusLikes(Long noteId);


}
