package com.fs.course.mapper;

import com.fs.course.domain.FsUserVideo;
import com.fs.course.param.FsUserVideoAuditParam;
import com.fs.course.param.FsUserVideoListUParam;
import com.fs.course.param.FsUserVideoParam;
import com.fs.course.vo.FsUserVideoListPVO;
import com.fs.course.vo.FsUserVideoListUVO;
import com.fs.course.vo.FsUserVideoPVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 课堂视频Mapper接口
 *
 * @author fs
 * @date 2024-05-15
 */
public interface FsUserVideoMapper
{
    /**
     * 查询课堂视频
     *
     * @param videoId 课堂视频主键
     * @return 课堂视频
     */
    public FsUserVideo selectFsUserVideoByVideoId(String videoId);

    /**
     * 查询课堂视频列表
     *
     * @param fsUserVideo 课堂视频
     * @return 课堂视频集合
     */
    public List<FsUserVideo> selectFsUserVideoList(FsUserVideo fsUserVideo);

    /**
     * 新增课堂视频
     *
     * @param fsUserVideo 课堂视频
     * @return 结果
     */
    public int insertFsUserVideo(FsUserVideo fsUserVideo);

    /**
     * 修改课堂视频
     *
     * @param fsUserVideo 课堂视频
     * @return 结果
     */
    public int updateFsUserVideo(FsUserVideo fsUserVideo);

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

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


    @Select({"<script> " +
            "select v.*  from fs_user_video v  " +
            "where 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<FsUserVideo> selectFsUserVideoListByCourseId(@Param("maps") FsUserVideo fsUserVideo);


    @Select({"<script> " +
            "select v.*,t.nick_name as talent_name,u.nick_name as audit_user_name  from fs_user_video v " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            "left join sys_user u on u.user_id = v.audit_by "+
            "where 1=1 and v.is_del = 0  " +
            "<if test = ' maps.title!=null and maps.title != \"\" '> " +
            "and v.title like concat('%', #{maps.title}, '%') " +
            "</if>" +
            "<if test = ' maps.talentId!=null  '> " +
            "and v.talent_id = #{maps.talentId} " +
            "</if>" +
            "<if test = ' maps.uploadType!=null  '> " +
            "and v.upload_type = #{maps.uploadType} " +
            "</if>" +
            "<if test = ' maps.source!=null  '> " +
            "and v.source = #{maps.source} " +
            "</if>" +
            "<if test = ' maps.isHot!=null  '> " +
            "and v.is_hot = #{maps.isHot} " +
            "</if>" +
            "<if test = ' maps.isAudit!=null  '> " +
            "and v.is_audit = #{maps.isAudit} " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and v.status = #{maps.status} " +
            "</if>" +
            "            <if test=\"maps.sTime != null \">  and DATE(v.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "            <if test=\"maps.eTime != null \">  and DATE(v.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            "            <if test=\"maps.auditsTime != null \">  and DATE(v.audit_time) &gt;= DATE(#{maps.auditsTime})</if>\n" +
            "            <if test=\"maps.auditeTime != null \">  and DATE(v.audit_time) &lt;= DATE(#{maps.auditeTime})</if>\n" +
            " order by v.video_id desc  "+
            "</script>"})
    List<FsUserVideoListPVO> selectFsUserVideoListPVO(@Param("maps") FsUserVideoParam fsUserVideo);



    @Update("update fs_user_video set likes=likes+1 where video_id=#{videoId}")
    int addLikes(Long videoId);


    @Update("update fs_user_video set likes=likes-1 where video_id=#{videoId}")
    int minusLikes(Long videoId);


    @Update("update fs_user_video set favorite_num=favorite_num+1 where video_id=#{videoId}")
    int addFavorite(Long videoId);


    @Update("update fs_user_video set favorite_num=favorite_num-1 where video_id=#{videoId}")
    int minusFavorite(Long videoId);

    @Select({"<script> " +
            "select v.video_id as id,v.title,v.description as msg,t.nick_name as username,t.avatar as headImg, " +
            "v.thumbnail as cover,v.url as src,v.likes as likeNum,v.comments as smsNum,v.favorite_num," +
            "v.create_time,v.views as playNumber,v.product_id,p.img_url,p.package_name,v.upload_type,v.shares from fs_user_video v " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            " left join fs_package p on p.package_id = v.product_id " +
            "where v.is_del = 0 and v.status = 1  " +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and v.title like CONCAT('%',#{maps.keyword},'%') " +
            "</if>" +
            " order by RAND() "+
            "</script>"})
    List<FsUserVideoListUVO> selectFsUserVideoListUVO(@Param("maps") FsUserVideoListUParam param);

    @Update("UPDATE fs_user_video SET comments = comments+#{commentCount} WHERE video_id = #{videoId}")
    void updateCommentCount(@Param("videoId") Long videoId, @Param("commentCount") Integer commentCount);

    @Select({"<script> " +
            "select v.video_id as id,v.title,v.description as msg,t.nick_name as username,t.avatar as headImg, " +
            "v.thumbnail as cover,v.url as src,v.likes as likeNum,v.comments as smsNum,v.favorite_num," +
            "v.create_time,v.views as playNumber,v.product_id,v.product_json,p.img_url,p.package_name,v.shares from fs_user_video_favorite f " +
            "left join fs_user_video v on v.video_id = f.video_id " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            " left join fs_package p on p.package_id = v.product_id " +
            "where v.is_del = 0 and v.status = 1 and f.user_id = #{userId} " +
            " order by v.video_id desc  "+
            "</script>"})
    List<FsUserVideoListUVO> selectFsUserFavoriteVideoListUVO(@Param("userId") Long userId);


    @Select({"<script> " +
            "select v.video_id as id,v.title,v.description as msg,t.nick_name as username,t.avatar as headImg, " +
            "v.thumbnail as cover,v.url as src,v.likes as likeNum,v.comments as smsNum,v.favorite_num," +
            "v.create_time,v.views as playNumber,v.product_id,v.product_json,p.img_url,p.package_name,v.shares from fs_user_video_like l " +
            "left join fs_user_video v on v.video_id = l.video_id " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            " left join fs_package p on p.package_id = v.product_id " +
            "where v.is_del = 0 and v.status = 1 and l.user_id = #{userId} " +
            " order by v.video_id desc  "+
            "</script>"})
    List<FsUserVideoListUVO> selectFsUserLikeVideoListUVO(@Param("userId")Long userId);


    @Select({"<script> " +
            "select v.video_id as id,v.title,v.description as msg,t.nick_name as username,t.avatar as headImg, " +
            "v.thumbnail as cover,v.url as src,v.likes as likeNum,v.comments as smsNum,v.favorite_num," +
            "v.create_time,v.views as playNumber,v.product_id,v.product_json,p.img_url,p.package_name,v.shares from fs_user_video_comment c " +
            "left join fs_user_video v on v.video_id = c.video_id " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            " left join fs_package p on p.package_id = v.product_id " +
            "where v.is_del = 0 and v.status = 1 and c.user_id = #{userId} " +
            " GROUP BY c.video_id " +
            " order by v.video_id desc  "+
            "</script>"})
    List<FsUserVideoListUVO> selectFsUserCommentVideoListUVO(@Param("userId")Long userId);

    @Select("select v.*,t.nick_name as talent_name,p.second_name as packageSecondName,u.nick_name as audit_by_name from fs_user_video v " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            "left join fs_package p on  p.package_id = v.product_id " +
            "left join sys_user u on v.audit_by = u.user_id " +
            " where v.video_id = #{videoId}" )
    FsUserVideoPVO selectFsUserVideoPVO(@Param("videoId") Long videoId);

    @Select("select video_id from fs_user_video where is_del = 0 and is_audit = 0 and audit_time is null and TIMESTAMPDIFF(HOUR, create_time, NOW()) >= 48")
    List<Long> selectNotAuditVideo();

    @Update("update fs_user_video set shares=shares+1 where video_id=#{videoId}")
    int updateShares(Long videoId);

    @Update("update fs_user_video set views=views+1 where video_id=#{videoId}")
    int updateViews(Long videoId);

    @Select({"<script> " +
            "select v.video_id as id,v.title,v.description as msg,t.nick_name as username,t.avatar as headImg, " +
            "v.thumbnail as cover,v.url as src,v.likes as likeNum,v.comments as smsNum,v.favorite_num," +
            "v.create_time,v.views as playNumber,v.product_id,p.img_url,p.package_name,v.upload_type,v.shares from fs_user_video v " +
            "left join fs_user_talent t on t.talent_id = v.talent_id " +
            " left join fs_package p on p.package_id = v.product_id " +
            "where v.is_del = 0 and v.status = 1 and v.video_id = #{videoId} " +
            " order by v.video_id desc  "+
            "</script>"})
    FsUserVideoListUVO selectFsUserVideoListUVOByVideoId(@Param("videoId") Long videoId);

    @Update({"<script> " +
            "UPDATE fs_user_video SET " +
            "<if test=\"maps.status != null\">" +
            "status = #{maps.status}," +
            "</if>" +
            "<if test = ' maps.tags!=null and maps.tags != \"\" '> " +
            "tags = #{maps.tags}," +
            "</if>" +
            " audit_time = #{maps.auditTime}, " +
            " audit_by = #{maps.auditBy},"+
            " is_audit = #{maps.isAudit} " +
            "where video_id in "+
            "<foreach item='videoId' collection='maps.videoIds' open='(' separator=',' close=')'> " +
            "            #{videoId} " +
            "</foreach>"+
            "</script>"})
    int auditUserVideos(@Param("maps") FsUserVideoAuditParam param);

    @Update({"<script> " +
            "UPDATE fs_user_video SET status = #{status} where video_id in "+
            "<foreach item='videoId' collection='videoIds' open='(' separator=',' close=')'> " +
            "            #{videoId} " +
            "</foreach>"+
            "</script>"})
    int updateFsUserVideoIsShow(@Param("videoIds")Long[] videoIds, @Param("status")int i);

    @Select("select * from fs_user_video where url like CONCAT('%','https://obs.jy.cc','%') ")
    List<FsUserVideo> selectVideo();
}

