Commit c3f9539c authored by Shen's avatar Shen

1. 对新订单进行WebSocket对应机构的通知

parent 06b844fa
...@@ -7,7 +7,10 @@ import javax.websocket.OnClose; ...@@ -7,7 +7,10 @@ import javax.websocket.OnClose;
import javax.websocket.OnMessage; import javax.websocket.OnMessage;
import javax.websocket.OnOpen; import javax.websocket.OnOpen;
import javax.websocket.Session; import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
/** /**
...@@ -18,16 +21,23 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -18,16 +21,23 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Slf4j @Slf4j
@Component @Component
@ServerEndpoint("/webSocket") @ServerEndpoint("/webSocket/{orgId}")
public class WebSocket { public class WebSocket {
private Session session; private Session session;
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>(); private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
private String orgId;
@OnOpen @OnOpen
public void opOpen(Session session) { public void opOpen(@PathParam("orgId") String orgId, Session session) {
this.session = session; this.session = session;
this.orgId = orgId;
log.info("【WebSocket】机构上线:{}", orgId);
clients.put(orgId, this);
webSocketSet.add(this); webSocketSet.add(this);
log.info("【WebSocket】有一个新的连接,总数:{}", webSocketSet.size()); log.info("【WebSocket】有一个新的连接,总数:{}", webSocketSet.size());
} }
...@@ -35,6 +45,7 @@ public class WebSocket { ...@@ -35,6 +45,7 @@ public class WebSocket {
@OnClose @OnClose
public void onClose() { public void onClose() {
webSocketSet.remove(this); webSocketSet.remove(this);
clients.remove(orgId);
log.info("【WebSocket】连接断开,总数:{}", webSocketSet.size()); log.info("【WebSocket】连接断开,总数:{}", webSocketSet.size());
} }
...@@ -44,13 +55,23 @@ public class WebSocket { ...@@ -44,13 +55,23 @@ public class WebSocket {
log.info("【WebSocket】收到客户端发来的消息:{}", message); log.info("【WebSocket】收到客户端发来的消息:{}", message);
} }
public void sendMessageToOrg(String message, String orgId) {
message = "您有新的订单:" + message;
for (WebSocket webSocket : clients.values()) {
if (webSocket.orgId.equals(orgId) ) {
webSocket.session.getAsyncRemote().sendText(message);
break;
}
}
}
public void sendMessage(String message) { public void sendMessage(String message) {
for (WebSocket webSocket : webSocketSet) { for (WebSocket webSocket : webSocketSet) {
log.info("【WebSocket】广播消息,message={}", message); log.info("【WebSocket】广播消息,message={}", message);
try { try {
webSocket.session.getBasicRemote().sendText(message); webSocket.session.getBasicRemote().sendText(message);
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} }
} }
} }
......
...@@ -23,4 +23,6 @@ public interface PensionOrganizationServiceDao extends BaseDao<PensionOrganizati ...@@ -23,4 +23,6 @@ public interface PensionOrganizationServiceDao extends BaseDao<PensionOrganizati
Long getUserOrgId(Long userId); Long getUserOrgId(Long userId);
Long findOrgIdByServiceId(Long orgId);
} }
...@@ -22,4 +22,6 @@ public interface PensionOrganizationService extends CrudService<PensionOrganizat ...@@ -22,4 +22,6 @@ public interface PensionOrganizationService extends CrudService<PensionOrganizat
PensionOrganizationDTO getUserOrg(Long userId); PensionOrganizationDTO getUserOrg(Long userId);
Long findOrgIdByServiceId(Long orderId);
} }
...@@ -59,7 +59,6 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe ...@@ -59,7 +59,6 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe
return wrapper; return wrapper;
} }
@Override @Override
public PageData<PensionOrderDTO> getOrganizationOrder(Map<String, Object> params, Long userId) { public PageData<PensionOrderDTO> getOrganizationOrder(Map<String, Object> params, Long userId) {
//登录->查询登录人员所属机构ID->机构ID查询服务ID(List)->服务ID + 状态查询订单(List) //登录->查询登录人员所属机构ID->机构ID查询服务ID(List)->服务ID + 状态查询订单(List)
......
...@@ -60,4 +60,9 @@ public class PensionOrganizationServiceImpl extends CrudServiceImpl<PensionOrgan ...@@ -60,4 +60,9 @@ public class PensionOrganizationServiceImpl extends CrudServiceImpl<PensionOrgan
public PensionOrganizationDTO getUserOrg(Long userId) { public PensionOrganizationDTO getUserOrg(Long userId) {
return ConvertUtils.sourceToTarget(pensionOrganizationDao.getOrgByUserId(userId), PensionOrganizationDTO.class); return ConvertUtils.sourceToTarget(pensionOrganizationDao.getOrgByUserId(userId), PensionOrganizationDTO.class);
} }
@Override
public Long findOrgIdByServiceId(Long orderId) {
return pensionOrganizationServiceDao.findOrgIdByServiceId(orderId);
}
} }
...@@ -3,6 +3,7 @@ package io.hmit.modules.sys.controller; ...@@ -3,6 +3,7 @@ package io.hmit.modules.sys.controller;
import io.hmit.common.utils.WebSocket; import io.hmit.common.utils.WebSocket;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO; import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.service.PensionOrderService; import io.hmit.modules.serviceOrder.service.PensionOrderService;
import io.hmit.modules.serviceOrder.service.PensionOrganizationService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -25,10 +26,14 @@ public class WebSocketController { ...@@ -25,10 +26,14 @@ public class WebSocketController {
@Autowired @Autowired
private PensionOrderService pensionOrderService; private PensionOrderService pensionOrderService;
@Autowired
private PensionOrganizationService pensionOrganizationService;
@GetMapping("notify") @GetMapping("notify")
public String doNotify(@RequestParam("orderId") Long orderId) { public String doNotify(@RequestParam("orderId") Long orderId) {
PensionOrderDTO pensionOrderDTO = pensionOrderService.get(orderId); // PensionOrderDTO pensionOrderDTO = pensionOrderService.get(orderId);
webSocket.sendMessage("新订单-服务id:" + pensionOrderDTO.getServiceId()); Long orgId = pensionOrganizationService.findOrgIdByServiceId(orderId);
webSocket.sendMessageToOrg(orderId.toString(), orgId.toString());
return "success"; return "success";
} }
} }
...@@ -21,4 +21,9 @@ ...@@ -21,4 +21,9 @@
FROM pension_organization_user WHERE user_id = #{userId} FROM pension_organization_user WHERE user_id = #{userId}
</select> </select>
<select id="findOrgIdByServiceId" resultType="Long">
SELECT pos.organization_id FROM pension_order po, pension_organization_service pos
WHERE po.service_id = pos.service_id AND po.id = #{orderId}
</select>
</mapper> </mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment