package com.zhujizheng.IHome.chat.service; import com.zhujizheng.IHome.chat.dto.PYSingleChatDTO; import com.zhujizheng.IHome.chat.vo.PYSingleChatAckVO; import com.zhujizheng.IHome.generator.dao.PYSingleChat; import com.zhujizheng.IHome.generator.mapper.PYSingleChatMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @Slf4j @Service @Transactional(rollbackFor = RuntimeException.class) public class PYSingleChatServiceImpl implements PYSingleChatService { @Autowired private PYSingleChatMapper singleChatMapper; private static final int MAX_REVOKE_TIME = 2 * 60 * 1000; // 2分钟 @Override public PYSingleChatAckVO pushSingleChatWithDTO(PYSingleChatDTO pushDTO) { log.info("pushDTO = " + pushDTO); if (pushDTO == null) { return null; } if (pushDTO.getMsgSeq() == null) { return null; } PYSingleChat singleChat = singleChatMapper.selectSingleChatWithMsgSeq(pushDTO.getMsgSeq()); if (singleChat == null) { PYSingleChat newSingleChat = PYSingleChat.createSingleChatWithDTO(pushDTO); int result = singleChatMapper.insertSingleChat(newSingleChat); if (result == 0) { log.info("插入聊天信息失败"); return null; } else { return PYSingleChatAckVO.createVOWithSingleChat(newSingleChat); } } if (singleChat.getStatus() != 1) { singleChat.setStatus(1); } return PYSingleChatAckVO.createVOWithSingleChat(singleChat); } @Override public PYSingleChatAckVO getLastSingleChatVO(int sendId, int receiveId) { PYSingleChat singleChat = singleChatMapper.selectLastSingleChat(sendId, receiveId); return PYSingleChatAckVO.createVOWithSingleChat(singleChat); } @Override public List pullOfflineSingleChatVO(Integer sendId, Integer receiveId, Long latestTime) { List voList = new ArrayList<>(); List list = singleChatMapper.selectOfflineSingleChat(sendId, receiveId, latestTime); for (PYSingleChat singleChat : list) { PYSingleChatAckVO vo = PYSingleChatAckVO.createVOWithSingleChat(singleChat); if (vo != null) { voList.add(vo); } else { log.info("获取离线消息为空" + singleChat); } } return voList; } @Override public int succeedSingleChat(String msgSeq) { log.info("消息发送成功seq = " + msgSeq); return singleChatMapper.updateStatus(1, msgSeq); } @Override public int hideSingleChat(String msgSeq) { log.info("隐藏消息seq= " + msgSeq); return singleChatMapper.hideMsg(msgSeq); } @Override public int revokeSingleChat(String msgSeq) { log.info("撤回消息seq = " + msgSeq); return singleChatMapper.revokeMsg(msgSeq); } @Override public int deleteSingleChat(String msgSeq) { log.info("删除消息seq = " + msgSeq); return singleChatMapper.deleteMsg(msgSeq); } @Override public int updateOffline(String msgSeq) { log.info("更新消息离线状态,seq = " + msgSeq); return singleChatMapper.updateOffline(msgSeq); } @Override public List syncChatMsg(Integer sendId, Integer receiveId) { List voList = new ArrayList<>(); List list = singleChatMapper.selectAllChatMsg(sendId, receiveId); for (PYSingleChat singleChat : list) { PYSingleChatAckVO vo = PYSingleChatAckVO.createVOWithSingleChat(singleChat); if (vo != null) { voList.add(vo); } else { log.info("获取所有消息为空" + singleChat); } } return voList; } @Override public PYSingleChatAckVO getChatMsgBySeq(String msgSeq) { PYSingleChat singleChat = singleChatMapper.selectSingleChatWithMsgSeq(msgSeq); return PYSingleChatAckVO.createVOWithSingleChat(singleChat); } }