Commit c3f9539c authored by Shen's avatar Shen

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

parent 06b844fa
......@@ -7,7 +7,10 @@ import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
......@@ -18,16 +21,23 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Slf4j
@Component
@ServerEndpoint("/webSocket")
@ServerEndpoint("/webSocket/{orgId}")
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
private String orgId;
@OnOpen
public void opOpen(Session session) {
public void opOpen(@PathParam("orgId") String orgId, Session session) {
this.session = session;
this.orgId = orgId;
log.info("【WebSocket】机构上线:{}", orgId);
clients.put(orgId, this);
webSocketSet.add(this);
log.info("【WebSocket】有一个新的连接,总数:{}", webSocketSet.size());
}
......@@ -35,6 +45,7 @@ public class WebSocket {
@OnClose
public void onClose() {
webSocketSet.remove(this);
clients.remove(orgId);
log.info("【WebSocket】连接断开,总数:{}", webSocketSet.size());
}
......@@ -44,13 +55,23 @@ public class WebSocket {
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) {
for (WebSocket webSocket : webSocketSet) {
log.info("【WebSocket】广播消息,message={}", message);
try {
webSocket.session.getBasicRemote().sendText(message);
}catch (Exception e) {
e.printStackTrace();
// e.printStackTrace();
}
}
}
......
......@@ -23,4 +23,6 @@ public interface PensionOrganizationServiceDao extends BaseDao<PensionOrganizati
Long getUserOrgId(Long userId);
Long findOrgIdByServiceId(Long orgId);
}
......@@ -22,4 +22,6 @@ public interface PensionOrganizationService extends CrudService<PensionOrganizat
PensionOrganizationDTO getUserOrg(Long userId);
Long findOrgIdByServiceId(Long orderId);
}
......@@ -59,7 +59,6 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe
return wrapper;
}
@Override
public PageData<PensionOrderDTO> getOrganizationOrder(Map<String, Object> params, Long userId) {
//登录->查询登录人员所属机构ID->机构ID查询服务ID(List)->服务ID + 状态查询订单(List)
......
......@@ -60,4 +60,9 @@ public class PensionOrganizationServiceImpl extends CrudServiceImpl<PensionOrgan
public PensionOrganizationDTO getUserOrg(Long userId) {
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;
import io.hmit.common.utils.WebSocket;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.service.PensionOrderService;
import io.hmit.modules.serviceOrder.service.PensionOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -25,10 +26,14 @@ public class WebSocketController {
@Autowired
private PensionOrderService pensionOrderService;
@Autowired
private PensionOrganizationService pensionOrganizationService;
@GetMapping("notify")
public String doNotify(@RequestParam("orderId") Long orderId) {
PensionOrderDTO pensionOrderDTO = pensionOrderService.get(orderId);
webSocket.sendMessage("新订单-服务id:" + pensionOrderDTO.getServiceId());
// PensionOrderDTO pensionOrderDTO = pensionOrderService.get(orderId);
Long orgId = pensionOrganizationService.findOrgIdByServiceId(orderId);
webSocket.sendMessageToOrg(orderId.toString(), orgId.toString());
return "success";
}
}
......@@ -21,4 +21,9 @@
FROM pension_organization_user WHERE user_id = #{userId}
</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>
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