package com.fs.course.mapper;

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

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.param.FsCourseListBySidebarParam;
import com.fs.course.param.FsUserCourseAddStudyCourseParam;
import com.fs.course.param.FsUserCourseListUParam;
import com.fs.course.param.FsUserCourseParam;
import com.fs.course.param.newfs.FsUserCourseListParam;
import com.fs.course.vo.*;
import com.fs.course.vo.newfs.FsUserCourseListVO;
import com.fs.his.vo.OptionsVO;
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 FsUserCourseMapper
{
    /**
     * 查询课程
     *
     * @param courseId 课程主键
     * @return 课程
     */
    public FsUserCourse selectFsUserCourseByCourseId(Long courseId);

    /**
     * 查询课程列表
     *
     * @param fsUserCourse 课程
     * @return 课程集合
     */
    public List<FsUserCourse> selectFsUserCourseList(FsUserCourse fsUserCourse);

    /**
     * 新增课程
     *
     * @param fsUserCourse 课程
     * @return 结果
     */
    public int insertFsUserCourse(FsUserCourse fsUserCourse);

    /**
     * 修改课程
     *
     * @param fsUserCourse 课程
     * @return 结果
     */
    public int updateFsUserCourse(FsUserCourse fsUserCourse);

    /**
     * 删除课程
     *
     * @param courseId 课程主键
     * @return 结果
     */
    public int deleteFsUserCourseByCourseId(Long courseId);

    /**
     * 批量删除课程
     *
     * @param courseIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserCourseByCourseIds(Long[] courseIds);


    @Select({"<script> " +
            "select c.*,SEC_TO_TIME(c.duration) as total_duration,t.nick_name as talent_name,t.avatar as talent_avatar from fs_user_course c " +
            " left join fs_user_talent t on c.talent_id = t.talent_id    " +
            "where c.course_id = #{courseId} " +
            "</script>"})
    FsUserCourseUVO selectFsUserCourseUVOByCourseId(Long courseId);


    @Update("update fs_user_course set views=views+1 where course_id=#{courseId}")
    int updateViews(Long courseId);


    @Update("update fs_user_course set shares=shares+1 where course_id=#{courseId}")
    int updateShares(Long courseId);

    @Update("update fs_user_course set likes=likes+1 where course_id=#{courseId}")
    int addLikes(Long courseId);


    @Update("update fs_user_course set likes=likes-1 where course_id=#{courseId}")
    int minusLikes(Long courseId);


    @Update("update fs_user_course set favorite_num=favorite_num+1 where course_id=#{courseId}")
    int addFavorite(Long courseId);


    @Update("update fs_user_course set favorite_num=favorite_num-1 where course_id=#{courseId}")
    int minusFavorite(Long courseId);


    @Select({"<script> " +
            "select c.*,t.nick_name as talent_name,t.avatar from fs_user_course  c  " +
            "left join fs_user_talent t on c.talent_id = t.talent_id " +
            "where c.is_del = 0 and c.is_show = 1 and c.is_private = 0 " +
            "<if test = ' maps.cateId !=null '> " +
            "and c.cate_id = #{maps.cateId} " +
            "</if>" +
            "<if test = ' maps.subCateId !=null '> " +
            "and c.sub_cate_id = #{maps.subCateId} " +
            "</if>" +
            "<if test = ' maps.isTui !=null '> " +
            "and c.is_tui = #{maps.isTui} " +
            "</if>" +
            "<if test = ' maps.isBest !=null '> " +
            "and c.is_best = #{maps.isBest} " +
            "</if>" +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and (c.course_name like concat('%', #{maps.keyword}, '%') " +
            "or c.description like concat('%', #{maps.keyword}, '%')) " +
            "</if>" +
            " order by c.course_id  "+
            "</script>"})
    List<FsUserCourseListUVO> selectFsUserCourseListUVO(@Param("maps") FsUserCourseListUParam param);


    @Select({"<script> " +
            "select c.*,cc.cate_name,ucc.cate_name as sub_cate_name from fs_user_course  c " +
            " left join fs_user_course_category cc on c.cate_id=cc.cate_id" +
            " left join fs_user_course_category ucc on  ucc.cate_id = c.sub_cate_id " +
            "where c.is_del = 0   " +
            "<if test = ' maps.cateId !=null '> " +
            "and (cc.cate_id =#{maps.cateId} or cc.pid=#{maps.cateId} )" +
            "</if>" +
            "<if test = ' maps.subCateId !=null '> " +
            "and c.sub_cate_id =#{maps.subCateId}" +
            "</if>" +
            "<if test = ' maps.userId !=null '> " +
            "and c.user_id =#{maps.userId}" +
            "</if>" +
            "<if test = ' maps.courseName!=null and maps.courseName != \"\" '> " +
            "and c.course_name like concat('%', #{maps.courseName}, '%') " +
            "</if>" +
            "<if test = ' maps.isPrivate !=null '> " +
            "and c.is_private = #{maps.isPrivate} " +
            "</if>" +
            "<if test = ' maps.isShow !=null '> " +
            "and c.is_show = #{maps.isShow} " +
            "</if>" +
            "<if test ='maps.companyIdsList != null and maps.companyIdsList.length !=0 '> " +
            " and <foreach collection='maps.companyIdsList'  item='item' index='index'  open='( 1=2 ' separator='' close=')'> or find_in_set(  #{item} , REGEXP_REPLACE ( c.company_ids, '[\"\\\\[\\\\]]', '' ) )  </foreach> " +
            "</if> " +
            " order by c.sort, c.course_id  "+
            "</script>"})
    List<FsUserCourseListPVO> selectFsUserCourseListPVO(@Param("maps") FsUserCourse param);


    @Update("update fs_user_course set duration=duration+#{duration} where course_id=#{courseId}")
    int updateDuration(FsUserCourseAddStudyCourseParam param);

    @Update({"<script> " +
            "UPDATE fs_user_course SET is_show = #{status} where course_id in "+
            "<foreach item='courseId' collection='courseIds' open='(' separator=',' close=')'> " +
            "            #{courseId} " +
            "</foreach>"+
            "</script>"})
    int updateFsUserCourseIsShow(@Param("courseIds") Long[] courseIds, @Param("status")int status);


    @Select({"<script> " +
            "select c.*,t.nick_name as talent_name,t.avatar from fs_user_course_comment cc  " +
            "left join fs_user_course c on c.course_id = cc.course_id " +
            "left join fs_user_talent t on c.talent_id = t.talent_id " +
            "where c.is_del = 0 and c.is_show = 1  " +
            "<if test = ' maps.userId !=null '> " +
            "and cc.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.keyword!=null and maps.keyword != \"\" '> " +
            "and c.course_name like concat('%', #{maps.keyword}, '%') " +
            "or c.description like concat('%', #{maps.keyword}, '%')) " +
            "</if>" +
            " GROUP BY cc.course_id" +
            " order by c.course_id  "+
            "</script>"})
    List<FsUserCourseListUVO> selectFsUserCourseCommentListUVO(@Param("maps") FsUserCourseListUParam param);


    @Select("select v.video_id,v.title,v.course_id,v.video_url,v.question_bank_id," +
            "SEC_TO_TIME(c.duration) as total_duration,c.views,c.course_name,c.description,c.img_url,c.config_json  " +
            " from fs_user_course_video v " +
            "left join fs_user_course c on v.course_id = c.course_id " +
            "where v.video_id = #{videoId}")
    FsUserCourseVideoH5VO selectFsUserCourseVideoH5VOByVideoId(@Param("videoId") Long videoId);


    @Select("select v.*," +
            "c.course_name,c.img_url,c.description as courseDescription " +
            " from fs_user_course_video v " +
            "left join fs_user_course c on v.course_id = c.course_id " +
            "where v.video_id = #{videoId}")
    FsUserCourseVideoH5DVO selectFsUserCourseVideoH5DVOByVideoId(@Param("videoId") Long videoId);

    @Select({"<script> " +
            "select c.*,cc.cate_name,ucc.cate_name as sub_cate_name from fs_user_course  c " +
            " left join fs_user_course_category cc on c.cate_id=cc.cate_id" +
            " left join fs_user_course_category ucc on  ucc.cate_id = c.sub_cate_id " +
            "where c.is_del = 0 and " +
            " FIND_IN_SET(#{maps.companyId}, company_ids) " +
            "<if test = ' maps.cateId !=null '> " +
            "and (cc.cate_id =#{maps.cateId} or cc.pid=#{maps.cateId} )" +
            "</if>" +
            "<if test = ' maps.courseName!=null and maps.courseName != \"\" '> " +
            "and c.course_name like concat('%', #{maps.courseName}, '%') " +
            "</if>" +
            "<if test = ' maps.isPrivate !=null '> " +
            "and c.is_private = #{maps.isPrivate} " +
            "</if>" +
            "<if test = ' maps.isShow !=null '> " +
            "and c.is_show = #{maps.isShow} " +
            "</if>" +
            "<if test ='maps.companyIdsList != null and maps.companyIdsList.length !=0 '> " +
            " and <foreach collection='maps.companyIdsList'  item='item' index='index'  open='( 1=2 ' separator='' close=')'> or find_in_set(  #{item} , REGEXP_REPLACE ( c.company_ids, '[\"\\\\[\\\\]]', '' ) )  </foreach> " +
            "</if> " +
            " order by c.sort,c.course_id  "+
            "</script>"})
    List<FsUserCourseListPVO> selectFsUserCourseListCompanyPVO(@Param("maps")FsUserCourseParam fsUserCourse);


    @Select("select course_id dict_value, course_name dict_label,img_url dict_imgUrl  from fs_user_course where is_del = 0 and is_private = 1 ")
    List<OptionsVO> selectFsUserCourseAllList();

    @Select("select course_id dict_value, course_name dict_label,img_url dict_imgUrl  from fs_user_course where is_del = 0 and is_private = 1" +
            " and find_in_set(#{companyId},company_ids) ORDER BY sort ASC, course_id DESC ")
    List<OptionsVO> selectFsUserCourseByCompany(@Param("companyId") Long companyId);

    @Select("select course_id ,project   from fs_user_course where is_del = 0 and is_private = 1")
    List<FsUserCourse> selectFsUserCourseAllCourse();

    @Select({"<script> " +
            "SELECT DISTINCT\n" +
            "        fcp.period_id,\n" +
            "        fcp.period_name\n" +
            "        FROM\n" +
            "        fs_user_course_period fcp\n" +
            "        LEFT JOIN fs_user_course_period_days fcpd ON fcpd.period_id = fcp.period_id\n" +
            "        LEFT JOIN fs_user_course c ON c.course_id = fcpd.course_id\n" +
            "        WHERE\n" +
            "        c.is_del = 0 and fcp.del_flag = '0'\n" +
            "        AND FIND_IN_SET(#{companyId}, fcp.company_id)\n" +
            "        <if test=\"keyword != null and keyword !='' \">\n" +
            "            AND fcp.period_name LIKE concat('%',#{keyword},'%'\n" +
            "            )\n" +
            "        </if>\n" +
            "        ORDER BY\n" +
            "        fcp.create_time desc, fcp.period_status asc"+
            "</script>"})
    List<FsUserCourseListVO> getFsUserCourseList(FsUserCourseListParam param);

    @Select("select course_id ,img_url   from fs_user_course where is_del = 0 and is_private = 1")
    List<FsUserCourse> selectFsUserCourseAllCourseByQw();

    @Select("<script> " +
            "select course_id,course_name,title,img_url from fs_user_course where is_del=0  and is_private = 1 " +
            "and find_in_set(#{data.companyId},company_ids)" +
            "        <if test=\"data.keyword != null and data.keyword !='' \">\n" +
            "            AND course_name LIKE concat('%',#{data.keyword},'%')\n" +
            "        </if>" +
            "order by sort asc, course_id asc" +
            "</script> ")
    List<FsCourseListBySidebarVO> getFsCourseListBySidebar(@Param("data") FsCourseListBySidebarParam param);

    @Select("<script> " +
            " select  course_id,course_name,title,img_url from fs_user_course where is_del=0  and is_private = 1  " +
            "  <if test=\"courseIds != null \">\n" +
            " and course_id in " +
            "<foreach collection='courseIds'  item='item' index='index'  open='(' separator=',' close=')'>" +
            " #{item} " +
            " </foreach> " +
            "        </if>" +
            "</script> ")
    List<FsCourseListBySidebarVO> getFsCourseListBySidebarFilterCourseId(@Param("courseIds")  List<Long> courseIds);

    @DataSource(DataSourceType.SOP)
    @Select(" select  " +
            "l.start_time,\n" +
            "qstd.day_num,\n" +
            "MAX(qstr.course_id) AS course_id,\n" +
            "MAX(qstr.video_id) AS video_id\n" +
            "FROM sop_user_logs_info i\n" +
            " inner join sop_user_logs l  on l.id = i.user_logs_id \n" +
            "inner join qw_sop qs on qs.id = l.sop_id AND qs.`status` IN (2,3,4)\n" +
            "left join qw_sop_temp_day qstd on qs.temp_id = qstd.temp_id AND qstd.day_num = DATEDIFF(CURRENT_DATE, l.start_time) + 1 \n" +
            "LEFT JOIN qw_sop_temp_rules qstr ON qstd.id = qstr.day_id  AND qstr.content_type = 2 \n" +
            "where i.external_contact_id = #{externalUserId} \n" +
            "GROUP BY qstd.id, l.start_time ")
    List<SidebarTodayCourseVO> getFsCourseListBySidebarTodayList(@Param("externalUserId") String externalUserId);

    List<FsUserCourseParticipationRecordVO> getParticipationRecordByMap(@Param("params") Map<String, Object> params);

    /**
     * 查询当天用户-项目看课记录条数
     */
    Integer selectTodayCourseWatchLogCountByUserIdAndProjectId(@Param("userId") Long userId, @Param("projectId") Long projectId);

    @Select("select course_id,course_name,description,img_url,second_img secondImg,views from fs_user_course where " +
            " is_private = 0 and is_del = 0 and is_show = 1 and is_tui = 1 order by sort,course_id")
    List<FsUserCourseVideoAppletVO> selectFsUserCourseVideoApplet();

    @Select("select video_id,title,course_id,video_url,SEC_TO_TIME(duration) as total_duration," +
            "thumbnail videoImgUrl,description videoDescription,video_url videoUrl,question_bank_id questionBankId " +
            " from fs_user_course_video where course_id = #{courseId} and is_del = 0 order by course_sort DESC,video_id")
    List<FsUserCourseVideoAppletVO.FsUserCourseVideo> selectFsUserCourseVideoAppletByCourseId(@Param("courseId") Long courseId);

    @Select("SELECT course_id dict_value, course_name dict_label, img_url dict_imgUrl  \n" +
            "         FROM fs_user_course \n" +
            "         WHERE is_del = 0 \n" +
            "         AND is_private = 1 \n" +
            "         AND user_id =#{userId}" +
            "         ORDER BY create_time DESC")
    List<OptionsVO> selectFsUserCourseAllListByUserId(@Param("userId") Long userId);

    @Select("select course_id,course_name,description,img_url,second_img secondImg,views from fs_user_course where course_id = #{courseId} and is_del = 0")
    List<FsUserCourseVideoAppletVO> selectFsUserCourseVideoAppletListByCourseId(@Param("courseId") Long courseId);


    /**
     * 修改课堂配置
     */
    @Update("update fs_user_course set config_json = #{configJson} where course_id = #{id}")
    void editConfig(@Param("id") Long id, @Param("configJson") String configJson);
}
