package com.fs.course.mapper;

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.course.domain.FsCourseAnswerLogs;
import com.fs.course.param.FsCourseAnswerLogsParam;
import com.fs.course.vo.FsCourseAnswerLogsListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 答题日志Mapper接口
 *
 * @author fs
 * @date 2024-09-11
 */
public interface FsCourseAnswerLogsMapper
{
    /**
     * 查询答题日志
     *
     * @param logId 答题日志主键
     * @return 答题日志
     */
    public FsCourseAnswerLogs selectFsCourseAnswerLogsByLogId(Long logId);

    /**
     * 查询答题日志列表
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 答题日志集合
     */
    public List<FsCourseAnswerLogs> selectFsCourseAnswerLogsList(FsCourseAnswerLogs fsCourseAnswerLogs);

    @Select("<script>" +
            "SELECT\n" +
            "\tfu.nick_name AS userName,\n" +
            "\tfu.avatar AS fsAvatar,\n" +
            "\tucv.title as videoName,\n" +
            "\tucv.course_id as courseId,\n" +
            "\tuc.course_name as courseName,\n" +
            "\tcu.nick_name as companyUserName,\n" +
            "\tqu.qw_user_name as qwUserName,\n" +
            "\tc.company_name as companyName,\n" +
            "\tcal.* \n" +
            "FROM\n" +
            "\tfs_course_answer_logs cal\n" +
            "\tLEFT JOIN fs_user fu ON cal.user_id = fu.user_id\n" +
            "\tLEFT JOIN fs_user_course_video ucv on cal.video_id=ucv.video_id\n" +
            "\tLEFT JOIN fs_user_course uc on uc.course_id=ucv.course_id\n" +
            "  LEFT JOIN company_user cu on cu.user_id = cal.company_user_id\n" +
            "\tLEFT JOIN qw_user qu on qu.id=cal.qw_user_id\n" +
            "\tLEFT JOIN company c on cal.company_id=c.company_id " +
            "        <where>  \n" +
            "            <if test=\"map.phone != null \"> and fu.phone = #{map.phone}</if>\n" +
            "            <if test=\"map.logId != null \"> and cal.log_id = #{map.logId}</if>\n" +
            "            <if test=\"map.courseId != null \"> and uc.course_id = #{map.courseId}</if>\n" +
            "            <if test=\"map.videoId != null \"> and cal.video_id = #{map.videoId}</if>\n" +
            "            <if test=\"map.watchLogId != null \"> and cal.watch_log_id = #{map.watchLogId}</if>\n" +
            "            <if test=\"map.userId != null \"> and cal.user_id = #{map.userId}</if>\n" +
            "            <if test=\"map.isRight != null \"> and cal.is_right = #{map.isRight}</if>\n" +
            "            <if test=\"map.qwUserId != null \"> and cal.qw_user_id = #{map.qwUserId}</if>\n" +
            "            <if test=\"map.companyUserName != null \"> and cu.nick_name  like concat('%', #{map.companyUserName}, '%') </if>\n" +
            "            <if test=\"map.nickName != null \"> and fu.nick_name  like concat('%', #{map.nickName}, '%') </if>\n" +
            "            <if test=\"map.companyId != null \"> and cal.company_id = #{map.companyId}</if>\n" +
            "            <if test=\"map.createTime != null \"> and Date(cal.create_time) = #{map.createTime}</if>\n" +
            "<if test=\"map.sTime != null \">  and DATE(cal.create_time) &gt;= DATE(#{map.sTime})</if>\n" +
            "<if test=\"map.eTime != null \">  and DATE(cal.create_time) &lt;= DATE(#{map.eTime})</if>\n" +
            "            <if test=\"map.companyUserIds != null and !map.companyUserIds.isEmpty()\">\n" +
            "                AND cal.company_user_id IN\n" +
            "                <foreach collection='map.companyUserIds' item='item' open='(' separator=',' close=')'>\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>" +
            "        </where>  " +
            "order by cal.log_id desc  " +
            " </script>")
    public List<FsCourseAnswerLogsListVO> selectFsCourseAnswerLogsListVO(@Param("map") FsCourseAnswerLogsParam param);

    /**
     * 新增答题日志
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 结果
     */
    public int insertFsCourseAnswerLogs(FsCourseAnswerLogs fsCourseAnswerLogs);

    /**
     * 修改答题日志
     *
     * @param fsCourseAnswerLogs 答题日志
     * @return 结果
     */
    public int updateFsCourseAnswerLogs(FsCourseAnswerLogs fsCourseAnswerLogs);

    /**
     * 删除答题日志
     *
     * @param logId 答题日志主键
     * @return 结果
     */
    public int deleteFsCourseAnswerLogsByLogId(Long logId);

    /**
     * 批量删除答题日志
     *
     * @param logIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsCourseAnswerLogsByLogIds(Long[] logIds);

    @Select({"<script> " +
            "select * from fs_course_answer_logs where video_id = #{videoId} and user_id = #{userId} and is_right = 1 " +
            "<if test = 'qwUserId !=null '> " +
            "and qw_user_id = #{qwUserId} " +
            "</if>" +
            "limit 1 " +
            "</script>"})
    FsCourseAnswerLogs selectRightLogByCourseVideo(@Param("videoId") Long videoId,@Param("userId") Long userId,@Param("qwUserId") String qwUserId);


    @Select({"<script> " +
            "select count(0) from fs_course_answer_logs where video_id = #{videoId} and user_id = #{userId} and is_right = 0 " +
            "<if test = 'qwUserId !=null '> " +
            "and qw_user_id = #{qwUserId} " +
            "</if>" +
            "<if test = 'project !=null '> " +
            "and project = #{project} " +
            "</if>" +
            "</script>"})
    int selectErrorCountByCourseVideo(@Param("videoId") Long videoId, @Param("userId") Long userId, @Param("qwUserId") String qwUserId,@Param("project") Long project);

    Long selectRedStatus(@Param("userId") Long userId, @Param("videoId") Long videoId, @Param("periodId") Long periodId);
    Integer selectRedStatus2(@Param("userId") Long userId, @Param("videoId") Long videoId, @Param("periodId") Long periodId);
    @DataSource(DataSourceType.SLAVE)
    List<FsCourseAnswerLogsListVO> selectFsCourseAnswerLogsListVONew(FsCourseAnswerLogsParam param);

    @DataSource(DataSourceType.SLAVE)
    Long selectFsCourseAnswerLogsListVONewCount(FsCourseAnswerLogsParam param);

    @Select("select * from fs_course_answer_logs where video_id = #{videoId} and user_id = #{userId} and is_right = 1 limit 1")
    FsCourseAnswerLogs selectRightLogByCourseVideoIsOpen(@Param("videoId") Long videoId,@Param("userId") Long userId);
}
