package com.fs.course.mapper;

import com.fs.course.domain.FsUserCourseVideo;
import com.fs.course.param.CourseVideoUpdates;
import com.fs.course.param.FsCourseListBySidebarParam;
import com.fs.course.param.FsUserCourseVideoListUParam;
import com.fs.course.param.FsUserCourseVideoParam;
import com.fs.course.param.newfs.UserCourseVideoPageParam;
import com.fs.course.vo.FsCourseVideoListBySidebarVO;
import com.fs.course.vo.FsUserCourseVideoListUVO;
import com.fs.course.vo.FsUserCourseVideoVO;
import com.fs.course.vo.newfs.FsUserCourseVideoPageListVO;
import com.fs.his.vo.OptionsVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 课堂视频Mapper接口
 *
 * @author fs
 * @date 2024-05-17
 */
public interface FsUserCourseVideoMapper
{
    /**
     * 查询课堂视频
     *
     * @param videoId 课堂视频主键
     * @return 课堂视频
     */
    public FsUserCourseVideo selectFsUserCourseVideoByVideoId(Long videoId);

    /**
     * 查询课堂视频列表
     *
     * @param fsUserCourseVideo 课堂视频
     * @return 课堂视频集合
     */
    public List<FsUserCourseVideo> selectFsUserCourseVideoList(FsUserCourseVideo fsUserCourseVideo);

    /**
     * 新增课堂视频
     *
     * @param fsUserCourseVideo 课堂视频
     * @return 结果
     */
    public int insertFsUserCourseVideo(FsUserCourseVideo fsUserCourseVideo);

    /**
     * 修改课堂视频
     *
     * @param fsUserCourseVideo 课堂视频
     * @return 结果
     */
    public int updateFsUserCourseVideo(FsUserCourseVideo fsUserCourseVideo);

    /**
     * 删除课堂视频
     *
     * @param videoId 课堂视频主键
     * @return 结果
     */
    public int deleteFsUserCourseVideoByVideoId(String videoId);

    /**
     * 批量删除课堂视频
     *
     * @param videoIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserCourseVideoByVideoIds(String[] videoIds);


    @Select({"<script> " +
            "select v.*  from fs_user_course_video v  " +
            "where v.is_del = 0 and  v.course_id = #{maps.courseId}   " +
            "<if test = ' maps.title!=null and maps.title != \"\" '> " +
            "and v.title = #{maps.title} " +
            "</if>" +
            " order by v.course_sort  "+
            "</script>"})
    List<FsUserCourseVideo> selectFsUserCourseVideoListByCourseId(@Param("maps") FsUserCourseVideo fsUserCourseVideo);


    @Select({"<script> " +
            "select v.video_id, v.title, v.video_url,v.package_json, v.thumbnail,v.duration as seconds, SEC_TO_TIME(v.duration) as duration,v.create_time, v.talent_id, v.course_id, " +
            " v.status, v.course_sort,v.line_one,v.line_two,v.line_three,v.upload_type,1 as is_vip,CASE \n" +
            "        WHEN l.log_id IS NOT NULL THEN 1\n" +
            "        ELSE 0\n" +
            "    END AS is_buy  from fs_user_course_video v  " +
            " left join fs_user_course_study_log l on l.video_id = v.video_id and l.user_id = #{maps.userId} and l.is_buy = 1 " +
            " where v.is_del = 0 and  v.course_id = #{maps.courseId} " +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and v.title like CONCAT('%',#{maps.keyword},'%') " +
            "</if>" +
            " order by v.course_sort  "+
            "</script>"})
    List<FsUserCourseVideoListUVO> selectFsUserCourseVideoListUVOByCourseId(@Param("maps") FsUserCourseVideoListUParam param);

    @Select({"<script> " +
            "select v.video_id, v.title,v.package_json, v.video_url, v.thumbnail,v.duration as seconds, SEC_TO_TIME(v.duration) as duration,v.create_time, v.talent_id, v.course_id, " +
            " v.status, v.course_sort,v.line_one,v.line_two,v.line_three,v.upload_type,1 as is_vip,0 as is_buy  from fs_user_course_video v  " +
            " where v.is_del = 0 and  v.course_id = #{maps.courseId}   " +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and v.title like CONCAT('%',#{maps.keyword},'%') " +
            "</if>" +
            " order by v.course_sort  "+
            "</script>"})
    List<FsUserCourseVideoListUVO> selectFsUserCourseVideoListByCourseIdAll(@Param("maps") FsUserCourseVideoListUParam param);


    @Select("select v.*  from fs_user_course_video v  " +
            "where v.is_del = 0 and v.course_id = #{courseId}  order by v.course_sort  ")
    List<FsUserCourseVideo> selectFsUserCourseListByCourseId(Long courseId);
    @Select({"<script> " +
            "select v.video_id, v.title, v.video_url, v.thumbnail, SEC_TO_TIME(v.duration) as duration,v.create_time, v.talent_id, v.course_id, " +
            " v.status, v.course_sort  from fs_user_course_video v  " +
            "where v.video_id = #{videoId}   " +
            "</script>"})
    FsUserCourseVideoListUVO selectFsUserCourseVideoListUVOByVideoId(Long videoId);

    @Select("select count(0) from fs_user_course_video where course_id = #{courseId}")
    Long selectVideoCountByCourseId(@Param("courseId") Long courseId);

    @Select("select course_sort from fs_user_course_video where course_id = #{courseId} and is_del = 0 order by course_sort desc limit 1 ")
    Long selectCourseVideoSort(@Param("courseId")Long courseId);

    @Select("select count(0) from fs_user_course_video where course_id = #{courseId} and course_sort = #{courseSort} and is_del = 0 ")
    Long selectFsUserCourseVideoByCourseSort(@Param("courseId")Long courseId, @Param("courseSort")Long courseSort);


    @Select("select video_id dict_value, title dict_label  from fs_user_course_video where course_id=#{id} and is_del = 0 ")
    List<OptionsVO> selectFsUserCourseVodeAllList(Long id);

    @Select({"<script> " +
            "select v.*,p.red_packet_money company_red_packet_money from fs_user_course_video v LEFT JOIN fs_user_course_video_red_package p on p.video_id= v.video_id and p.company_id =#{maps.companyId} " +
            "where v.is_del = 0 and  v.course_id = #{maps.courseId}   " +
            "<if test = ' maps.title!=null and maps.title != \"\" '> " +
            "and v.title = #{maps.title} " +
            "</if>" +
            " order by v.course_sort  "+
            "</script>"})
    List<FsUserCourseVideoVO> selectFsUserCourseVideoListByCourseIdAndCompany(@Param("maps") FsUserCourseVideoParam fsUserCourseVideo);

    @Select("select v.* from fs_user_course_video v " +
            "left join fs_user_course c on c.course_id = v.course_id " +
            "where c.is_private = 1 and v.is_del = 0 ")
    List<FsUserCourseVideo> selectVideoIsPrivate();
    @Select("select * from fs_user_course_video WHERE video_id=#{videoId}")
    FsUserCourseVideo selectFsUserCourseVideoByVideoStringId(String videoId);
    @Select("select * from fs_user_course_video WHERE course_id=#{courseId}")
    List<FsUserCourseVideo> selectVideoByCourseId(Long courseId);
    @Select("select v.* from fs_user_course_video v " +
            "left join fs_user_course c on c.course_id = v.course_id " +
            "where c.is_private = 1 and v.is_del = 0 and v.is_transcode = #{isTranscode}")
    List<FsUserCourseVideo> selectVideoTranscode(@Param("isTranscode") Integer isTranscode);


    @Select("select v.* from fs_user_course_video v " +
            "left join fs_user_course c on c.course_id = v.course_id " +
            "where c.is_private = 1 and v.is_del = 0 and v.is_transcode = 1 and transcode_file_key is null ")
    List<FsUserCourseVideo> selectVideoIsTranscode();

    @Select("select video_id from fs_user_course_video WHERE is_first=1")
    List<Long> selectVideoByFirst();

    List<FsUserCourseVideoPageListVO> selectFsUserCourseVideoPageList(UserCourseVideoPageParam param);

    void updates(CourseVideoUpdates vo);

    void insertBatchFsUserCourseVideo(@Param("collect") List<FsUserCourseVideo> collect);

    void updateRedPacketMoney(@Param("videoId") Long videoId, @Param("redPacketMoney") BigDecimal redPacketMoney);

    /**
     * 获取选项列表
     * @param params    参数
     * @return  list
     */
    List<OptionsVO> selectVideoListByMap(@Param("params") Map<String, Object> params);

    @Select("<script> " +
            "select v.video_id,v.course_id,v.title,v.video_url,v.thumbnail,v.duration,v.create_time from fs_user_course_video v " +
            "left join fs_user_course c on c.course_id = v.course_id " +
            "where c.is_private = 1 and v.is_del = 0 and v.course_id=#{data.courseId} " +
            "        <if test=\"data.keyword != null and data.keyword !='' \">\n" +
            "            AND v.title LIKE concat('%',#{data.keyword},'%')\n" +
            "        </if>" +
            "order by v.video_id asc " +
            "</script>")
    List<FsCourseVideoListBySidebarVO> getFsCourseVideoListBySidebar(@Param("data") FsCourseListBySidebarParam param);

    /**
     * 查询课程列表
     * @param params 参数
     * @return  list
     */
    List<FsUserCourseVideoPageListVO> selectFsUserCourseVideoListByMap(@Param("params") Map<String, Object> params);
}
