package com.usky.ai.mapper;

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

import java.util.List;

@Mapper
public interface AiSessionMapper {
    @Insert("INSERT INTO ai_sessions (session_id, user_id, user_name, question, ask_time) " +
            "VALUES (#{sessionId}, #{userId}, #{userName}, #{question}, #{askTime})")
    void save(AiSession aiSession);

    //查询所有数据
    @Select("SELECT * FROM ai_sessions ORDER BY ask_time DESC")
    List<AiSession> findAll();

    // 根据 user_id 查询未删除的数据
    @Select("SELECT * FROM ai_sessions WHERE user_id = #{userId} AND deleted = false ORDER BY ask_time DESC")
    List<AiSession> findByUserId(Long userId);

    // 检查是否存在指定的 session_id 且未删除
    @Select("SELECT COUNT(*) FROM ai_sessions WHERE session_id = #{sessionId} ORDER BY ask_time DESC")
    boolean existsBySessionId(String sessionId);

    @Select("SELECT * FROM ai_questions WHERE session_id = #{sessionId} ORDER BY ask_time DESC")
    List<AiQuestion> findQuestionsBySessionId(String sessionId);

    // 更新会话主题
    @Update("UPDATE ai_sessions SET question = #{question} WHERE session_id = #{sessionId}")
    void updateQuestion(@Param("sessionId") String sessionId, @Param("question") String question);

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