package cn.teammodel.repository;

import cn.teammodel.model.entity.ai.ChatSession;
import cn.teammodel.model.entity.ai.DeepSeekSession;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * 数据库操作接口
 * Created by jiangjingming on 17/6/19.
 */
@Repository
public interface DeepSeekRepository extends CosmosRepository<DeepSeekSession, String> {
    /**
     * 根据id和code查询会话信息
     * @param id
     * @param code
     * @return
     */
    DeepSeekSession findSessionByIdAndCode(String id, String code);

    /**
     * 根据会话id查询会话信息
     * @param sessionId
     * @return
     */
    @Query("select * from c where c.code='DeepSeekSession' and c.sessionId = @sessionId")
    List<DeepSeekSession> findBySessionId(String sessionId);

    /**
     * 根据用户id查询会话信息
     * @param userId
     * @return
     */
    @Query("select c.id,c.userId,c.title,c.model,c.createTime,c.updateTime from c where c.code='DeepSeekSession' and c.userId = @userId")
    List<DeepSeekSession> findByUserId(String userId);

    /**
     * 根据会话id查询会话信息
     * @param sessionId
     * @return
     */
    @Query("SELECT value ARRAY_SLICE(c.history, -3) FROM c where c.id = @sessionId and c.code = 'DeepSeekSession'")
    List<ChatSession.Message> findLatestMessage(String sessionId);

    /**
     * 根据用户id查询会话信息
     * @param userId
     * @return
     */
    @Query("select * from c where c.code = 'DeepSeekSession' and c.id = @userId")
    List<ChatSession> findCommentsById(String userId);



}
