package com.zhujizheng.IHome.applyfriend.net; import com.zhujizheng.IHome.applyfriend.dto.PYApplyFriendDTO; import com.zhujizheng.IHome.applyfriend.service.PYApplyFriendService; import com.zhujizheng.IHome.applyfriend.vo.PYApplyFriendVO; 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.user.service.PYUserService; import com.zhujizheng.IHome.applyfriend.vo.PYApplyFriendResulltVO; import com.zhujizheng.IHome.user.vo.PYUserVO; import com.zhujizheng.IHome.util.alipush.AliPushService; import com.zhujizheng.IHome.websocket.Net.Base.NetProtocolBase; import com.zhujizheng.IHome.websocket.server.WebSocketServer; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Slf4j @Component public class NetApplyFriend extends NetProtocolBase { @Autowired private PYUserService userService; @Autowired private PYApplyFriendService applyFriendService; @Autowired private AliPushService aliPushService; @Autowired private PYMsgNotifyServer msgNotifyServer; @Override public void dealWithJSONAndSid(JSONObject json, String sid) { super.dealWithJSONAndSid(json, sid); int friendId = json.getInt("friendId"); int selfId = Integer.parseInt(sid); PYUserVO userVO = userService.selectInfo(selfId); if (userVO == null) { log.error("申请失败"); this.sendServerError(sid); return; } PYApplyFriendResulltVO resultVO = new PYApplyFriendResulltVO(); // result 为0表示申请成功 resultVO.setResult(0); resultVO.setFriendId(friendId); if (userVO.getLoverId() != 0) { resultVO.setReason("你已经绑定情侣了,需要先取消绑定再申请"); // result为1表示自己已绑定 resultVO.setResult(1); this.sendObject(resultVO, sid); return; } PYUserVO friendVO = userService.selectInfo(friendId); if (friendVO == null) { log.error("申请失败"); this.sendServerError(sid); return; } if (friendVO.getLoverId() != 0) { resultVO.setReason("对方已经绑定情侣了"); // result为2表示对方已绑定 resultVO.setResult(2); this.sendObject(resultVO, sid); return; } // 更新好友申请表 PYApplyFriendDTO dto = PYApplyFriendDTO.create(selfId, friendId); PYApplyFriendVO vo = applyFriendService.applyFriend(dto); if (vo == null) { // result为3表示申请失败 resultVO.setResult(3); this.sendObject(resultVO, sid); return; } String desc = "'" + userVO.getUserName() + "'申请了与你绑定关系"; String ext = ""; PYMsgNotify msgNotify = PYMsgNotify.createApplyMsgNotify(friendId, selfId, desc, ext); PYMsgNotifyVO msgNotifyVO = msgNotifyServer.addOrUpdateMsgNotify(msgNotify); // 更新消息通知表 String selfDesc = "你申请与'" + friendVO.getUserName() + "'绑定关系"; String selfExt = ""; PYMsgNotify selfMsgNotify = PYMsgNotify.createApplyMsgNotify(selfId, friendId, selfDesc, selfExt); PYMsgNotifyVO selfMsgNotifyVO = msgNotifyServer.addOrUpdateMsgNotify(selfMsgNotify); // 下发好友申请数据 NetApplyFriendAck ack = new NetApplyFriendAck(); // 下发消息通知数据 NetMsgNotifyAck msgNotifyAck = new NetMsgNotifyAck(); // 通知对方 WebSocketServer item = WebSocketServer.getWebSocketMap().get(String.valueOf(friendId)); if (null == item) { // 离线推送 log.info("离线推送::friendId.toString() = " + friendId); aliPushService.pushRemoteNotify("新消息", desc, String.valueOf(friendId), 1); } else { // 通知对方收到系统消息 msgNotifyAck.sendObject(msgNotifyVO, String.valueOf(friendId)); // 通知对方好友申请状态已变更 ack.sendObject(vo, String.valueOf(friendId)); } // 通知自己收到系统消息 msgNotifyAck.sendObject(selfMsgNotifyVO, sid); log.info("msgNotifyAck 通知自己:sid = " + sid + ", msgNotifyVO = " + selfMsgNotifyVO); // 通知自己好友申请状态已变更 ack.sendObject(vo, sid); // 通知自己申请成功 this.sendObject(resultVO, sid); log.info("通知自己申请成功:sid = " + sid + ", resultVO = " + resultVO); } }