package com.fs.course.mapper;

import java.util.List;
import java.util.Map;

import com.fs.course.domain.FsUserVideoFavorite;
import com.fs.course.dto.VideoFavoriteStatusDTO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 课堂视频收藏Mapper接口
 *
 * @author fs
 * @date 2024-05-15
 */
public interface FsUserVideoFavoriteMapper
{
    /**
     * 查询课堂视频收藏
     *
     * @param favoriteId 课堂视频收藏主键
     * @return 课堂视频收藏
     */
    public FsUserVideoFavorite selectFsUserVideoFavoriteByFavoriteId(Long favoriteId);

    /**
     * 查询课堂视频收藏列表
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 课堂视频收藏集合
     */
    public List<FsUserVideoFavorite> selectFsUserVideoFavoriteList(FsUserVideoFavorite fsUserVideoFavorite);

    /**
     * 新增课堂视频收藏
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 结果
     */
    public int insertFsUserVideoFavorite(FsUserVideoFavorite fsUserVideoFavorite);

    /**
     * 修改课堂视频收藏
     *
     * @param fsUserVideoFavorite 课堂视频收藏
     * @return 结果
     */
    public int updateFsUserVideoFavorite(FsUserVideoFavorite fsUserVideoFavorite);

    /**
     * 删除课堂视频收藏
     *
     * @param favoriteId 课堂视频收藏主键
     * @return 结果
     */
    public int deleteFsUserVideoFavoriteByFavoriteId(Long favoriteId);

    /**
     * 批量删除课堂视频收藏
     *
     * @param favoriteIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserVideoFavoriteByFavoriteIds(Long[] favoriteIds);


    @Select("select ifnull(count(1),0) from fs_user_video_favorite  where video_id =#{videoId} and user_id=#{userId}")
    int checkFavorite(@Param("videoId") Long videoId, @Param("userId")long userId);


    @Delete("delete from fs_user_video_favorite  where video_id =#{videoId} and user_id=#{userId} ")
    int deleteFavorite(@Param("videoId") Long videoId,@Param("userId")long userId);


    @Select("<script>" +
            "select video_id, ifnull(count(1), 0) > 0 as favorite " +
            "from fs_user_video_favorite " +
            "where video_id in " +
            "<foreach item='videoId' collection='videoIds' open='(' separator=',' close=')'>" +
            "#{videoId}" +
            "</foreach> " +
            "and user_id = #{userId} " +
            "group by video_id" +
            "</script>")
    @MapKey("videoId")
    Map<Long, VideoFavoriteStatusDTO> checkFavorites(@Param("videoIds") List<Long> videoIds, @Param("userId") long userId);
}
