package com.fs.course.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.course.domain.FsCourseSopAppLink;
import com.fs.course.vo.FsSopMyCourseLinkVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * sop发送app消息的记录Mapper接口
 *
 * @author fs
 * @date 2025-03-20
 */
public interface FsCourseSopAppLinkMapper extends BaseMapper<FsCourseSopAppLink> {
    /**
     * 查询sop发送app消息的记录
     *
     * @param id sop发送app消息的记录主键
     * @return sop发送app消息的记录
     */
    FsCourseSopAppLink selectFsCourseSopAppLinkById(Long id);

    /**
     * 查询sop发送app消息的记录列表
     *
     * @param fsCourseSopAppLink sop发送app消息的记录
     * @return sop发送app消息的记录集合
     */
    List<FsCourseSopAppLink> selectFsCourseSopAppLinkList(FsCourseSopAppLink fsCourseSopAppLink);

    /**
     * 新增sop发送app消息的记录
     *
     * @param fsCourseSopAppLink sop发送app消息的记录
     * @return 结果
     */
    int insertFsCourseSopAppLink(FsCourseSopAppLink fsCourseSopAppLink);

    void insertFsCourseSopAppLinkBatch(@Param("courseAppSopLinks")List<FsCourseSopAppLink> courseSopAppLinkToInsert);

    /**
     * 修改sop发送app消息的记录
     *
     * @param fsCourseSopAppLink sop发送app消息的记录
     * @return 结果
     */
    int updateFsCourseSopAppLink(FsCourseSopAppLink fsCourseSopAppLink);

    /**
     * 删除sop发送app消息的记录
     *
     * @param id sop发送app消息的记录主键
     * @return 结果
     */
    int deleteFsCourseSopAppLinkById(Long id);

    /**
     * 批量删除sop发送app消息的记录
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteFsCourseSopAppLinkByIds(Long[] ids);

    @Select("SELECT\n" +
            "al.id," +
            "al.link," +
            "\tal.create_time," +
            "\tal.update_time,\n" +
            "\tal.qw_user_name,\n" +
            "\tal.course_title AS courseName,\n" +
            "\tal.course_url,\n" +
            "\tal.video_title AS title,\n" +
            "\tal.app_real_Link, \n" +
            "\tal.is_read \n" +
            "FROM\n" +
            "\t`fs_course_sop_app_link` al\n" +
            "\tLEFT JOIN qw_external_contact ec ON al.qw_external_id = ec.id \n" +
            "WHERE\n" +
            "\tec.fs_user_id = #{userId} \n" +
            "\tAND al.update_time >= NOW() " +
            "ORDER BY al.is_read asc,al.create_time desc ")
    List<FsSopMyCourseLinkVO> getSopCourseStudyList(@Param("userId") Long userId);

    @Select("SELECT\n" +
            "\tcount(1) as isNotRead\n" +
            "FROM\n" +
            "\tfs_course_sop_app_link al\n" +
            "\tLEFT JOIN qw_external_contact ec ON al.qw_external_id = ec.id \n" +
            "WHERE\n" +
            "\tec.fs_user_id = #{userId} \n" +
            "\tAND al.is_read = 0 \n" +
            "\tAND al.update_time >= NOW() ")
    Integer selectSopCountIsNotRead(@Param("userId") Long userId);

    @Update("update fs_course_sop_app_link set is_read = 1 where link = #{id}")
    int updateFsCourseSopAppLinkByLink(@Param("id") String id);
}
