package com.fs.fastGpt.mapper;

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.fastGpt.domain.FastGptChatMsg;
import com.fs.fastGpt.param.FastGptChatMsgListCParam;
import com.fs.fastGpt.vo.FastGptChatMsgCVO;
import com.fs.fastGpt.vo.FastGptChatMsgListCVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 聊天记录Mapper接口
 *
 * @author fs
 * @date 2024-10-10
 */

public interface FastGptChatMsgMapper
{
    /**
     * 查询聊天记录
     *
     * @param msgId 聊天记录主键
     * @return 聊天记录
     */

    public FastGptChatMsg selectFastGptChatMsgByMsgId(Long msgId);


    /**
     * 查询聊天记录列表
     *
     * @param fastGptChatMsg 聊天记录
     * @return 聊天记录集合
     */
    public List<FastGptChatMsg> selectFastGptChatMsgList(FastGptChatMsg fastGptChatMsg);

    /**
     * 新增聊天记录
     *
     * @param fastGptChatMsg 聊天记录
     * @return 结果
     */

    public int insertFastGptChatMsg(FastGptChatMsg fastGptChatMsg);

    /**
     * 修改聊天记录
     *
     * @param fastGptChatMsg 聊天记录
     * @return 结果
     */
    public int updateFastGptChatMsg(FastGptChatMsg fastGptChatMsg);

    /**
     * 删除聊天记录
     *
     * @param msgId 聊天记录主键
     * @return 结果
     */
    public int deleteFastGptChatMsgByMsgId(Long msgId);

    /**
     * 批量删除聊天记录
     *
     * @param msgIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFastGptChatMsgByMsgIds(Long[] msgIds);

    @Select("select m.*  from fastgpt_chat_msg m  where m.session_id=#{sessionId} and msg_type=2 order by m.msg_id desc")
    public List<FastGptChatMsgCVO> selectFastGptChatMsgCVOBySessionId(@Param("sessionId")Long sessionId);


    @Select({"<script> " +
            "select m.*  from fastgpt_chat_msg m " +
            "where 1=1  " +
            "<if test = ' maps.nickName!=null and maps.nickName != \"\"  '> " +
            "and m.nick_name like concat('%', #{maps.nickName}, '%') " +
            "</if>" +
            "<if test = ' maps.companyId!=null  '> " +
            "and m.company_id = #{maps.companyId} " +
            "</if>" +
            "<if test = ' maps.roleId!=null  '> " +
            "and m.role_id = #{maps.roleId} " +
            "</if>" +
            "<if test = ' maps.msgType!=null  '> " +
            "and m.msg_type = #{maps.msgType} " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and m.status = #{maps.status} " +
            "</if>" +
            "<if test=' maps.params.beginTime != null and maps.params.beginTime != \"\" '>" +
            "AND date_format(m.create_time,'%y%m%d') &gt;= date_format(#{maps.params.beginTime},'%y%m%d')  " +
            "</if>" +
            "<if test='maps.params.endTime != null and maps.params.endTime != \"\" '>" +
            "AND date_format(m.create_time,'%y%m%d') &lt;= date_format(#{maps.params.endTime},'%y%m%d')  " +
            "</if>" +
            " order by m.create_time desc "+
            "</script>"})
    List<FastGptChatMsgListCVO> selectFastGptChatMsgListCVO(@Param("maps") FastGptChatMsgListCParam param);

    @Select("select m.*  from fastgpt_chat_msg m  where msg_id=#{msgId}")
    FastGptChatMsgCVO selectChatMsgByMsgVOId(Long msgId);

    @Select("select content from fastgpt_chat_msg where msg_id < #{msgId} and user_id = #{userId} and role_id = #{roleId} and send_type = 1  ORDER BY msg_id desc limit 1")
    String selectUserContent(@Param("msgId") Long msgId,@Param("userId") String userId,@Param("roleId") Long roleId);
    @Select("select * from fastgpt_chat_msg where  session_id =#{sessionId} and msg_type=1 ORDER BY msg_id DESC  limit 20 ")
    List<FastGptChatMsg> selectFastGptChatMsgByMsgSessionId(Long sessionId);


}
