package com.usky.ai.mapper;

import com.usky.ai.domain.AiQuestion;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AiQuestionMapper {
    @Insert("INSERT INTO ai_questions (model, session_id, user_id, user_name, question, answer, ask_time) " +
            "VALUES (#{model},  #{sessionId}, #{userId}, #{userName}, #{question}, #{answer}, #{askTime})")
    void save(AiQuestion aiQuestion);

    // 查询所有数据
    @Select("SELECT * FROM ai_questions ORDER BY ask_time ASC")
    List<AiQuestion> findAll();

    // 根据 userId 查询数据
    @Select("SELECT * FROM ai_questions WHERE user_id = #{userId} ORDER BY ask_time ASC")
    List<AiQuestion> findByUserId(Long userId);

    // 根据 id 删除数据
    @Delete("DELETE FROM ai_questions WHERE id = #{id}")
    void deleteById(Long id);

    // 根据 sessionId 和 userId 查询数据
    @Select("SELECT * FROM ai_questions WHERE session_id = #{sessionId} AND user_id = #{userId} ORDER BY ask_time ASC")
    List<AiQuestion> findByUserIdAndSessionId(@Param("sessionId") String sessionId, @Param("userId") Long userId);

    //根据 sessionId查询数据
    @Select("SELECT * FROM ai_questions WHERE session_id = #{sessionId} ORDER BY ask_time ASC")
    List<AiQuestion> findBySessionId(String sessionId);

    // 根据 sessionId 查询 ai_questions 表中的数据
    @Select("SELECT * FROM ai_questions WHERE session_id = #{sessionId} ORDER BY ask_time ASC")
    List<AiQuestion> findQuestionsBySessionId(String sessionId);

    //标记会话为删除
    @Update("UPDATE ai_questions SET deleted = true WHERE session_id = #{sessionId}")
    void delete(String sessionId);
}
