package com.fs.fastGpt.mapper;

import com.fs.fastGpt.domain.FastGptChatSession;
import com.fs.fastGpt.param.FastGptChatSessionParam;
import com.fs.fastGpt.vo.FastGptChatSessionCVO;
import com.fs.fastGpt.vo.FastGptChatSessionListCVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 对话关系Mapper接口
 *
 * @author fs
 * @date 2024-10-10
 */
public interface FastGptChatSessionMapper
{
    /**
     * 查询对话关系
     *
     * @param sessionId 对话关系主键
     * @return 对话关系
     */
    public FastGptChatSession selectFastGptChatSessionBySessionId(Long sessionId);

    /**
     * 查询对话关系列表
     *
     * @param fastGptChatSession 对话关系
     * @return 对话关系集合
     */
    public List<FastGptChatSession> selectFastGptChatSessionList(FastGptChatSession fastGptChatSession);

    /**
     * 新增对话关系
     *
     * @param fastGptChatSession 对话关系
     * @return 结果
     */
    public int insertFastGptChatSession(FastGptChatSession fastGptChatSession);

    /**
     * 修改对话关系
     *
     * @param fastGptChatSession 对话关系
     * @return 结果
     */
    public int updateFastGptChatSession(FastGptChatSession fastGptChatSession);

    /**
     * 删除对话关系
     *
     * @param sessionId 对话关系主键
     * @return 结果
     */
    public int deleteFastGptChatSessionBySessionId(Long sessionId);

    /**
     * 批量删除对话关系
     *
     * @param sessionIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFastGptChatSessionBySessionIds(Long[] sessionIds);

    @Select({"<script> " +
            "select s.*,r.role_name,c.company_name,qu.qw_user_name  from fastgpt_chat_session s " +
            " left join fastgpt_role r on r.role_id = s.kf_id " +
            " left join company c on c.company_id = s.company_id " +
            " left join qw_user qu on qu.id = s.qw_user_id  " +
            "where 1=1 " +
            "<if test = ' maps.nickName!=null and maps.nickName != \"\"  '> " +
            "and s.nick_name like concat('%', #{maps.nickName}, '%') " +
            "</if>" +
            "<if test = ' maps.qwUserName!=null and maps.qwUserName != \"\"  '> " +
            "and qu.qw_user_name like concat('%', #{maps.qwUserName}, '%') " +
            "</if>" +
            "<if test = ' maps.roleName!=null and maps.roleName != \"\"  '> " +
            "and r.role_name like concat('%', #{maps.roleName}, '%') " +
            "</if>" +
            "<if test = ' maps.chatId!=null and maps.chatId != \"\"  '> " +
            "and s.chat_id = #{maps.chatId}  " +
            "</if>" +
            "<if test = ' maps.companyId!=null  '> " +
            "and s.company_id = #{maps.companyId} " +
            "</if>" +
            "<if test = ' maps.kfId!=null and maps.kfId != \"\" '> " +
            "and s.kf_id = #{maps.kfId} " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and s.status = #{maps.status} " +
            "</if>" +
            "<if test = ' maps.isArtificial!=null  '> " +
            "and s.is_artificial = #{maps.isArtificial} " +
            "</if>" +
            "<if test = ' maps.isLook!=null  '> " +
            "and s.is_look = #{maps.isLook} " +
            "</if>" +
            "<if test=' maps.beginTime != null and maps.beginTime != \"\" '>" +
            "AND date_format(s.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')  " +
            "</if>" +
            "<if test='maps.endTime != null and maps.endTime != \"\" '>" +
            "AND date_format(s.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')  " +
            "</if>" +
            " order by s.session_id desc "+
            "</script>"})
    List<FastGptChatSessionListCVO> selectFastGptChatSessionListCVO(@Param("maps") FastGptChatSessionParam param);


    @Select("select s.*,r.role_name  from fastgpt_chat_session s  " +
            " left join fastgpt_role r on r.kf_id = s.kf_id  where  s.session_id = #{sessionId}")
    FastGptChatSessionCVO selectFastGptChatSessionCVOBySessionId(Long sessionId);


    @Select("select * from fastgpt_chat_session where user_id=#{userId} and qw_user_id=#{qwUserId} and status=1 order by session_id desc limit 1 ")
    FastGptChatSession selectFastGptChatSessionByUserIdAndKfId(@Param("userId") String userId, @Param("qwUserId")Long qwUserId);
    @Select("select * from fastgpt_chat_session where remind_status=1 and remind_time < NOW() ")
    List<FastGptChatSession> selectFastGptChatSessionByRemind();
    @Select("select * from fastgpt_chat_session where qw_ext_id=#{extId} and qw_user_id=#{qwUser} and status=1 limit 1")
    FastGptChatSession selectFastGptChatSessionByQwExternalContactsAndUserId(@Param("extId") Long extId,@Param("qwUser")  Long qwUser);

    @Update("UPDATE fastgpt_chat_session  SET is_artificial = 0 WHERE is_artificial = 1  AND TIMESTAMPDIFF(MINUTE, last_time, NOW()) > 10;")
    void updateFastGptChatSessionByIsReply();

}
