package com.fs.course.mapper;

import com.fs.course.vo.HyWatchLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * 企微看课表数据库访问接口
 */
@Mapper
public interface HyWatchLogMapper {

    /**
     * 新增记录
     *
     * @param hyWatchLog 记录对象
     * @return 影响行数
     */
    @Insert("INSERT INTO hy_watch_log (ext_id, qw_user_id, status, day, project, create_time, line_time, fs_user_id, company_id, company_user_id, course_id, video_id) " +
            "VALUES (#{extId}, #{qwUserId}, #{status}, #{day}, #{project}, #{createTime}, #{lineTime}, #{fsUserId}, #{companyId}, #{companyUserId}, #{courseId}, #{videoId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(HyWatchLog hyWatchLog);

    /**
     * 根据ID查询记录
     *
     * @param id 主键ID
     * @return 记录对象，如果不存在则返回 null
     */
    @Select("SELECT id, ext_id, qw_user_id, status, day, project, create_time, line_time, fs_user_id, company_id, company_user_id, course_id, video_id " +
            "FROM hy_watch_log WHERE id = #{id}")
    HyWatchLog selectById(@Param("id") Long id);

    /**
     * 根据ID更新记录
     * 注意：这里是全量更新，会更新所有字段。如果需要部分更新，请自行调整 SQL。
     *
     * @param hyWatchLog 包含更新信息的记录对象 (ID 必须存在)
     * @return 影响行数
     */
    @Update("UPDATE hy_watch_log SET " +
            "ext_id = #{extId}, " +
            "qw_user_id = #{qwUserId}, " +
            "status = #{status}, " +
            "day = #{day}, " +
            "project = #{project}, " +
            "create_time = #{createTime}, " +
            "line_time = #{lineTime}, " +
            "fs_user_id = #{fsUserId}, " +
            "company_id = #{companyId}, " +
            "company_user_id = #{companyUserId}, " +
            "course_id = #{courseId}, " +
            "video_id = #{videoId} " +
            "WHERE id = #{id}")
    int updateById(HyWatchLog hyWatchLog);

    /**
     * 根据外部联系人ID查询记录列表
     *
     * @param extId 外部联系人ID
     * @return 记录列表
     */
    @Select("SELECT id, ext_id, qw_user_id, status, day, project, create_time, line_time, fs_user_id, company_id, company_user_id, course_id, video_id " +
            "FROM hy_watch_log WHERE ext_id = #{extId}")
    List<HyWatchLog> selectByExtId(@Param("extId") Long extId);

    /**
     * 根据状态查询记录列表
     *
     * @param status 状态码
     * @return 记录列表
     */
    @Select("SELECT id, ext_id, qw_user_id, status, day, project, create_time, line_time, fs_user_id, company_id, company_user_id, course_id, video_id " +
            "FROM hy_watch_log WHERE status = #{status}")
    List<HyWatchLog> selectByStatus(@Param("status") Integer status);

    void insertHyWatchLogBatch(List<HyWatchLog> hyWatchLogs);
}
