package com.fs.chat.mapper;

import java.util.List;
import com.fs.chat.domain.ChatMsg;
import com.fs.chat.param.ChatMsgListAParam;
import com.fs.chat.param.ChatMsgListCParam;
import com.fs.chat.vo.ChatMsgListAVO;
import com.fs.chat.vo.ChatMsgListCVO;
import com.fs.chat.vo.ChatMsgVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 聊天消息记录Mapper接口
 *
 * @author fs
 * @date 2024-04-19
 */
public interface ChatMsgMapper
{
    /**
     * 查询聊天消息记录
     *
     * @param msgId 聊天消息记录主键
     * @return 聊天消息记录
     */
    public ChatMsg selectChatMsgByMsgId(Long msgId);

    /**
     * 查询聊天消息记录列表
     *
     * @param chatMsg 聊天消息记录
     * @return 聊天消息记录集合
     */
    public List<ChatMsg> selectChatMsgList(ChatMsg chatMsg);

    /**
     * 新增聊天消息记录
     *
     * @param chatMsg 聊天消息记录
     * @return 结果
     */
    public int insertChatMsg(ChatMsg chatMsg);

    /**
     * 修改聊天消息记录
     *
     * @param chatMsg 聊天消息记录
     * @return 结果
     */
    public int updateChatMsg(ChatMsg chatMsg);

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

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

    @Select({"<script> " +
            "select m.*,r.role_name,c.company_name  from chat_msg m " +
            " left join chat_role r on r.role_id=m.role_id" +
            " left join company c on c.company_id = m.company_id  " +
            "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<ChatMsgListCVO> selectChatMsgListCVO(@Param("maps")ChatMsgListCParam param);

    @Select("select ifnull(count(1),0) from chat_msg where ext_id=#{extId}")
    int checkMsg(String extId);

    @Select("select *  from chat_msg where user_id=#{userId} and role_id=#{roleId} order by msg_id desc")
    List<ChatMsg> selectAllChatMsgByUserIdAndRoleId(@Param("userId")String userId, @Param("roleId")Long roleId);

    @Select("select m.*,r.role_name  from chat_msg m left join chat_role r on r.role_id=m.role_id where msg_id=#{msgId}")
    ChatMsgVO selectChatMsgByMsgVOId(Long msgId);

    @Select("select content from 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 m.*  from chat_msg m  where m.session_id=#{sessionId} order by m.msg_id desc")
    List<ChatMsgVO> selectChatMsgBySessionId(@Param("sessionId")Long sessionId);


    @Select({"<script> " +
            "select m.*   from chat_msg m " +
            "where 1=1  " +
            "<if test = ' maps.roleId!=null  '> " +
            "and m.role_id = #{maps.roleId} " +
            "</if>" +
            "<if test = ' maps.userId!=null  '> " +
            "and m.user_id = #{maps.userId} " +
            "</if>" +
            " order by m.create_time desc "+
            "</script>"})
    List<ChatMsgListAVO> selectChatMsgListAVO(@Param("maps")ChatMsgListAParam param);
}
