package com.fs.chat.mapper;

import com.fs.chat.domain.ChatSession;
import com.fs.chat.param.ChatSessionListCParam;
import com.fs.chat.vo.ChatSessionCVO;
import com.fs.chat.vo.ChatSessionListCVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 会话Mapper接口
 *
 * @author fs
 * @date 2024-05-11
 */
public interface ChatSessionMapper
{
    /**
     * 查询会话
     *
     * @param sessionId 会话主键
     * @return 会话
     */
    public ChatSession selectChatSessionBySessionId(Long sessionId);

    /**
     * 查询会话列表
     *
     * @param chatSession 会话
     * @return 会话集合
     */
    public List<ChatSession> selectChatSessionList(ChatSession chatSession);

    /**
     * 新增会话
     *
     * @param chatSession 会话
     * @return 结果
     */
    public int insertChatSession(ChatSession chatSession);

    /**
     * 修改会话
     *
     * @param chatSession 会话
     * @return 结果
     */
    public int updateChatSession(ChatSession chatSession);

    /**
     * 删除会话
     *
     * @param sessionId 会话主键
     * @return 结果
     */
    public int deleteChatSessionBySessionId(Long sessionId);

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


    @Select({"<script> " +
            "select s.*,u.nickname,r.role_name,c.company_name  from chat_session s " +
            " left join chat_user u on u.external_userid = s.user_id " +
            " left join chat_role r on r.kf_id = s.kf_id " +
            " left join company c on c.company_id = s.company_id " +
            "where 1=1 " +
            "<if test = ' maps.nickName!=null and maps.nickName != \"\"  '> " +
            "and u.nickname like concat('%', #{maps.nickName}, '%') " +
            "</if>" +
            "<if test = ' maps.sessionCode!=null and maps.sessionCode != \"\"  '> " +
            "and s.session_code = #{maps.sessionCode}  " +
            "</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.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<ChatSessionListCVO> selectChatSessionListCVO(@Param("maps") ChatSessionListCParam chatSession);


    @Select("select s.*,u.nickname,r.role_name  from chat_session s left join chat_user u on u.external_userid = s.user_id " +
            " left join chat_role r on r.kf_id = s.kf_id  where  s.session_id = #{sessionId}")
    ChatSessionCVO selectChatSessionCVOBySessionId(Long sessionId);
    @Select("select * from chat_session where status=1 and user_id=#{userId} and kf_id=#{kfId} order by session_id desc limit 1")
    ChatSession selectChatSessionByIds(@Param("userId")String userId, @Param("kfId")String kfId);
}
