package me.zhengjie.application.admin.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.corundumstudio.socketio.SocketIOClient; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.zhengjie.annotation.rest.AnonymousPostMapping; import me.zhengjie.base.AppBaseResponse; import me.zhengjie.base.ResultCode; import me.zhengjie.base.util.ApplicationContextUtil; import me.zhengjie.base.util.FileUploadUtil; import me.zhengjie.base.websocket.AppPcBindSocketIo; import me.zhengjie.base.websocket.AppSocketClientCache; import me.zhengjie.base.websocket.PcSocketClientCache; import me.zhengjie.dao.mybatis.entity.OrderRoomIdEntity; import me.zhengjie.dao.mybatis.mapper.OrderRoomIdMapper; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Set; import java.util.UUID; @RestController @Validated @RequestMapping("/orderRoom") @Slf4j @RequiredArgsConstructor public class OrderRoomIdController { private final OrderRoomIdMapper orderRoomIdMapper; private final ApplicationContextUtil contextUtil; private final AppPcBindSocketIo appRelatePc; private final PcSocketClientCache pcSocketClientCache; private final AppSocketClientCache appSocketClientCache; /** * 查询视频 * * @return */ @RequestMapping("/getVideo") public AppBaseResponse getVideo(@RequestBody String json) { AppBaseResponse rep = new AppBaseResponse<>(); JSONObject jsonObj = JSONObject.parseObject(json); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("business_no", jsonObj.getString("businessNo")); queryWrapper.orderByDesc("update_time"); List orderRoomList = orderRoomIdMapper.selectList(queryWrapper); if (orderRoomList != null && orderRoomList.size() > 0) { OrderRoomIdEntity orderRoomId = orderRoomList.get(0); // 处理 String appUrl = orderRoomId.getAppVideoUrl(); if (StringUtils.isNotBlank(appUrl)) { appUrl = FileUploadUtil.getFileUrl(appUrl); orderRoomId.setAppVideoUrl(appUrl); } String pcUrl = orderRoomId.getPcVideoUrl(); if (StringUtils.isNotBlank(pcUrl)) { pcUrl = FileUploadUtil.getFileUrl(pcUrl); orderRoomId.setPcVideoUrl(pcUrl); } rep.setResult(orderRoomId); } return rep; } /** * 查询 房间号 * * @return */ // @RequestMapping @AnonymousPostMapping("/getRooId") public AppBaseResponse getRooId(@RequestBody String json) { log.info("全部的消息:" + json); JSONObject jsonObj = JSONObject.parseObject(json); String socketId = jsonObj.getString("socketId"); JSONObject socketObj = isPushUserName(socketId); if (socketObj != null) { String accountId = socketObj.getString("accountId"); String businessNo = socketObj.getString("businessNo"); Set userNames = appRelatePc.getBindNotary(businessNo); for (String userName : userNames) { HashMap userClient = pcSocketClientCache.getUserClient(userName); if (userClient != null) { for (SocketIOClient client : userClient.values()) { if (!userName.equalsIgnoreCase(accountId)) client.sendEvent("closeDialog", "closeDialog"); } } } List bindNotarys = new ArrayList<>(); jsonObj = new JSONObject(); jsonObj.put("userName", socketObj.getString("accountId")); bindNotarys.add(jsonObj); String notaryJson = JSONObject.toJSONString(bindNotarys); appRelatePc.setBusinessNo(businessNo, notaryJson, 60 * 30); // 如果确定的,关闭其他弹框 return AppBaseResponse.succ(socketObj); } return AppBaseResponse.error(ResultCode.NOT_EXISTS); } /** * 包含推送的用户 * * @param socketId * @return */ private JSONObject isPushUserName(String socketId) { List data = appRelatePc.socketIoTime(socketId); // try String userName = contextUtil.getCurrentUsername(); if (data != null && data.size() > 0) { for (JSONObject socketObj : data) { if (socketObj.getString("accountId").equalsIgnoreCase(userName)) { return socketObj; } } } return null; } /** * 取消 * * @param json * @return */ @AnonymousPostMapping("/cancel") public AppBaseResponse cancel(@RequestBody String json) { JSONObject jsonObj = JSONObject.parseObject(json); String socketId = jsonObj.getString("socketId"); JSONObject socketObj = isPushUserName(socketId); if (socketObj == null) { return AppBaseResponse.success(); } String businessNo = socketObj.getString("businessNo"); log.info("pc端取消公证员绑定人员:{} ",businessNo); appRelatePc.removeNotary(businessNo, socketObj.getString("accountId")); // Set notarys = appRelatePc.getBindNotary(businessNo); // if (CollectionUtils.isEmpty(notarys) || notarys.size() == 1) { // // 直接删除这条数据 // appRelatePc.delBusinessNo(businessNo); // // TDOO 发送给app。取消了视频通话 // sendAppMsg(businessNo); // return AppBaseResponse.success(); // } // notarys.remove(socketObj.getString("accountId")); // // 保存没有取消的时间 // appRelatePc.setNoCancelNotary(notarys, businessNo); return AppBaseResponse.success(); } private void sendAppMsg(String businessNo) { HashMap appSocketIo = appSocketClientCache.getUserClient(businessNo); if (MapUtils.isNotEmpty(appSocketIo)) { appSocketIo.forEach((uuid, socketIOClient) -> { socketIOClient.sendEvent("cancelEvent", "cancel"); }); } } /** * 结束视频 * * @return */ @RequestMapping("/endVideo") public AppBaseResponse endVideo(@RequestBody String json) { JSONObject jsonObj = JSONObject.parseObject(json); String businessNo = jsonObj.getString("businessNo"); appRelatePc.delBusinessNo(businessNo); return AppBaseResponse.success(); } }