package com.zhujizheng.IHome.chat.net; import com.zhujizheng.IHome.chat.dto.PYDeleteChatDTO; import com.zhujizheng.IHome.chat.service.PYSingleChatService; import com.zhujizheng.IHome.chat.vo.PYDeleteChatVO; import com.zhujizheng.IHome.chat.vo.PYSingleChatAckVO; import com.zhujizheng.IHome.generator.dao.PYMsgNotify; import com.zhujizheng.IHome.msgnotify.net.NetMsgNotifyAck; import com.zhujizheng.IHome.msgnotify.service.PYMsgNotifyServer; import com.zhujizheng.IHome.msgnotify.vo.PYMsgNotifyVO; import com.zhujizheng.IHome.websocket.Net.Base.NetProtocolBase; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.atomic.AtomicInteger; @Slf4j @Component public class NetDeleteChat extends NetProtocolBase { @Autowired private PYSingleChatService service; @Autowired private PYMsgNotifyServer msgNotifyServer; @Override public void dealWithJSONAndSid(JSONObject json, String sid) { super.dealWithJSONAndSid(json, sid); int userId = Integer.parseInt(sid); // 解析消息数据 PYDeleteChatDTO dto = (PYDeleteChatDTO)JSONObject.toBean(json, PYDeleteChatDTO.class); PYDeleteChatVO vo = new PYDeleteChatVO(); vo.setReceiveId(dto.getReceiveId()); // 设置回调结果的消息唯一标识符 vo.setMsgSeqArr(dto.getMsgSeqArr()); // 设置回调结果的消息状态默认为成功 vo.setStatus(1); // 更新数据库 AtomicInteger result = new AtomicInteger(1); for (String msgSeq : dto.getMsgSeqArr()) { result.set(service.deleteSingleChat(msgSeq)); if (result.get() == 0) { break; } } if (result.get() == 0) { log.info("发送状态更新失败"); // 设置回调结果的消息状态为失败 vo.setStatus(0); this.sendObject(vo, sid); } else { // 给自己回调发送的结果,走到这一步才能确保消息发送成功 this.sendObject(vo, sid); } PYSingleChatAckVO singleChatAckVO = service.getLastSingleChatVO(userId, dto.getReceiveId()); if (singleChatAckVO == null) return; NetMsgNotifyAck msgNotifyAck = new NetMsgNotifyAck(); PYMsgNotify msgNotify = PYMsgNotify.createChatMsgNotify(userId, singleChatAckVO.getReceiveId(), singleChatAckVO.getMsgContent(), singleChatAckVO.getSendTime()); PYMsgNotifyVO msgNotifyVO = msgNotifyServer.addOrUpdateMsgNotify(msgNotify); msgNotifyAck.sendObject(msgNotifyVO, sid); } }