package com.iplatform.chat.util; import com.iplatform.chat.ChatSession; import com.iplatform.model.po.ChatDialog; import com.iplatform.model.vo.ChatViewVo; import com.iplatform.model.vo.ChatVo; import com.walker.infrastructure.utils.DateUtils; import com.walker.infrastructure.utils.StringUtils; import com.walker.jdbc.mongo.ChatRecord; public class ChatUtils { public static final String SESSION_ID_PREFIX = "CS"; /** * 把mongo数据库中记录转换成平台聊天记录。 * @param record * @param me 我的ID,我的聊天表 * @param isCustomerService 我是客服吗? * @return * @date 2023-07-17 */ public static final ChatVo acquireChatVo(ChatRecord record, long me, boolean isCustomerService){ ChatVo vo = new ChatVo(); // vo.setSessionId(); vo.setBizId(record.getBizId()); vo.setMessage(record.getMessage()); vo.setCreateTime(record.getCreateDate()); vo.setMsgType(record.getMsgType()); vo.setId(record.getId()); vo.setIsMeMsg(record.getType() == ChatRecord.TYPE_SEND? 1:0); // 是否我发送的消息,0表示我接收的 if(isCustomerService){ // 我是客服 vo.setCustomerService(me); vo.setMe(record.getUserId()); vo.setCustomerServiceSend(record.getType() == ChatRecord.TYPE_SEND? true:false); } else { // 我是C端用户 vo.setMe(me); vo.setCustomerService(record.getUserId()); vo.setCustomerServiceSend(record.getType() == ChatRecord.TYPE_SEND? false:true); } return vo; } public static final ChatViewVo acquireChatViewVo(ChatVo chatVo){ ChatViewVo viewVo = new ChatViewVo(); viewVo.setIsMeMsg(chatVo.getIsMeMsg() == 1? 0:1); viewVo.setCreateTime(DateUtils.toShowDate(chatVo.getCreateTime())); viewVo.setBizId(chatVo.getBizId()); viewVo.setMsgType(chatVo.getMsgType()); viewVo.setMessage(chatVo.getMessage()); viewVo.setNickName(chatVo.getNickName()); viewVo.setMsgUserAvatar(chatVo.getMsgUserAvatar()); viewVo.setSessionId(chatVo.getSessionId()); if(chatVo.isCustomerServiceSend()){ viewVo.setCustomer(0); viewVo.setSendId(chatVo.getCustomerService()); viewVo.setReceiverId(chatVo.getMe()); } else { viewVo.setCustomer(1); viewVo.setSendId(chatVo.getMe()); viewVo.setReceiverId(chatVo.getCustomerService()); } viewVo.setChatType(chatVo.getChatType()); return viewVo; } public static final ChatVo acquireChatVo(ChatViewVo chatViewVo){ ChatVo chatVo = new ChatVo(); chatVo.setCreateTime(DateUtils.getDateTimeNumber()); chatVo.setNickName(chatViewVo.getNickName()); chatVo.setMsgUserAvatar(chatViewVo.getMsgUserAvatar()); chatVo.setMsgType(chatViewVo.getMsgType()); chatVo.setMessage(chatViewVo.getMessage()); chatVo.setBizId(chatViewVo.getBizId()); chatVo.setSessionId(chatViewVo.getSessionId()); chatVo.setIsMeMsg(chatViewVo.getIsMeMsg()); if(chatViewVo.getCustomer() == 1){ // 用户 发送的消息 chatVo.setMe(chatViewVo.getSendId()); chatVo.setCustomerService(chatViewVo.getReceiverId()); chatVo.setCustomerServiceSend(false); } else { // 客服 发送的消息 chatVo.setMe(chatViewVo.getReceiverId()); chatVo.setCustomerService(chatViewVo.getSendId()); chatVo.setCustomerServiceSend(true); } chatVo.setChatType(chatViewVo.getChatType()); return chatVo; } public static final ChatSession acquireChatSession(ChatDialog dialog){ ChatSession session = new ChatSession(dialog.getId(), dialog.getUserId(), dialog.getCustomerService(), true); session.setBizId(dialog.getBizId()); session.setEndTime(dialog.getEndTime()); return session; } /** * 获得对话的唯一ID,算法如下: *
* (客服id + 用户id + 业务id).hashCode(); ** @param customerService * @param userId * @param bizId * @return * @date 2023-07-12 */ public static final long acquireChatDialogKey(long customerService, long userId, String bizId){ return new StringBuilder().append(customerService) .append(StringUtils.DEFAULT_SPLIT_SEPARATOR).append(userId) .append(StringUtils.DEFAULT_SPLIT_SEPARATOR).append(bizId).toString().hashCode(); } }