From 902e4c9843f4d2a853948dfe005073572ce43f7d Mon Sep 17 00:00:00 2001 From: Yang Hanlin Date: Sun, 12 Jul 2020 11:08:13 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E4=B8=8E=E8=AE=BF=E5=AE=A2=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=9A=E8=AF=9D=E7=9A=84=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cloudservice/ccs/biz/ISessionBiz.java | 2 ++ .../com/cloudservice/ccs/biz/impl/SessionImpl.java | 9 +++++++++ .../cloudservice/ccs/controller/SessionController.java | 10 ++++++++-- .../com/cloudservice/ccs/mapper/SessionMapper.java | 2 ++ src/main/resources/mapper/SessionMapper.xml | 8 ++++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cloudservice/ccs/biz/ISessionBiz.java b/src/main/java/com/cloudservice/ccs/biz/ISessionBiz.java index dd4987a..7ddc374 100644 --- a/src/main/java/com/cloudservice/ccs/biz/ISessionBiz.java +++ b/src/main/java/com/cloudservice/ccs/biz/ISessionBiz.java @@ -24,4 +24,6 @@ public interface ISessionBiz { List getActiveByCustomerServiceId(Integer customerServiceId); List getHistoryByCustomerServiceId(Integer customerServiceId); + + Session getByCustomerServiceAndVisitor(Integer customerServiceId, String visitorIp); } diff --git a/src/main/java/com/cloudservice/ccs/biz/impl/SessionImpl.java b/src/main/java/com/cloudservice/ccs/biz/impl/SessionImpl.java index a86da07..04ea014 100644 --- a/src/main/java/com/cloudservice/ccs/biz/impl/SessionImpl.java +++ b/src/main/java/com/cloudservice/ccs/biz/impl/SessionImpl.java @@ -110,4 +110,13 @@ public class SessionImpl implements ISessionBiz { public List getHistoryByCustomerServiceId(Integer customerServiceId) { return sessionMapper.getWithActiveStatusWithLatestMessage(customerServiceId, false); } + + @Override + public Session getByCustomerServiceAndVisitor(Integer customerServiceId, String visitorIp) { + Session session = sessionMapper.getByCustomerServiceAndVisitor(customerServiceId, visitorIp); + if (session == null) { + throw new SessionException("指定的会话不存在"); + } + return session; + } } diff --git a/src/main/java/com/cloudservice/ccs/controller/SessionController.java b/src/main/java/com/cloudservice/ccs/controller/SessionController.java index f9b41ed..6874601 100644 --- a/src/main/java/com/cloudservice/ccs/controller/SessionController.java +++ b/src/main/java/com/cloudservice/ccs/controller/SessionController.java @@ -68,16 +68,22 @@ public class SessionController { } @GetMapping("/customer-service/{customerServiceId}/active") - public ApiResult queryActive(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) { + public ApiResult queryActiveByCustomerServiceId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) { PageHelper.startPage(page, limit); List result = sessionBiz.getActiveByCustomerServiceId(customerServiceId); return ApiResult.successWithData(PageData.fromPage((Page) result)); } @GetMapping("/customer-service/{customerServiceId}/history") - public ApiResult queryHistory(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) { + public ApiResult queryHistoryByCustomerServiceId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) { PageHelper.startPage(page, limit); List result = sessionBiz.getHistoryByCustomerServiceId(customerServiceId); return ApiResult.successWithData(PageData.fromPage((Page) result)); } + + @GetMapping("/customer-service/{customerServiceId}/visitor/{visitorIp}") + public ApiResult queryByCustomerServiceAndVisitor(@PathVariable Integer customerServiceId, @PathVariable String visitorIp) { + Session session = sessionBiz.getByCustomerServiceAndVisitor(customerServiceId, visitorIp); + return ApiResult.successWithData(session); + } } diff --git a/src/main/java/com/cloudservice/ccs/mapper/SessionMapper.java b/src/main/java/com/cloudservice/ccs/mapper/SessionMapper.java index e133f66..1791f63 100644 --- a/src/main/java/com/cloudservice/ccs/mapper/SessionMapper.java +++ b/src/main/java/com/cloudservice/ccs/mapper/SessionMapper.java @@ -28,4 +28,6 @@ public interface SessionMapper { List getWithLatestMessage(Integer customerServiceId); List getWithActiveStatusWithLatestMessage(Integer customerServiceId, Boolean activeStatus); + + Session getByCustomerServiceAndVisitor(Integer customerServiceId, String visitorIp); } diff --git a/src/main/resources/mapper/SessionMapper.xml b/src/main/resources/mapper/SessionMapper.xml index 8f531d5..c197078 100644 --- a/src/main/resources/mapper/SessionMapper.xml +++ b/src/main/resources/mapper/SessionMapper.xml @@ -88,4 +88,12 @@ ) OR session_message.sendTime is NULL) ORDER BY session_message.sendTime DESC + + \ No newline at end of file -- Gitee From 04853549f0250002f9a9bae6ced93654fa25f19e Mon Sep 17 00:00:00 2001 From: Yang Hanlin Date: Sun, 12 Jul 2020 11:20:46 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20POST=20/visitor/=20?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/CustomerServiceDesignationImpl.java | 1 + .../ccs/biz/impl/VisitorInfoImpl.java | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/cloudservice/ccs/biz/impl/CustomerServiceDesignationImpl.java b/src/main/java/com/cloudservice/ccs/biz/impl/CustomerServiceDesignationImpl.java index 3415cb3..34a84f2 100644 --- a/src/main/java/com/cloudservice/ccs/biz/impl/CustomerServiceDesignationImpl.java +++ b/src/main/java/com/cloudservice/ccs/biz/impl/CustomerServiceDesignationImpl.java @@ -49,6 +49,7 @@ public class CustomerServiceDesignationImpl implements ICustomerServiceDesignati public Integer createSession(VisitorInfo visitorInfo) { Session session = getSession(visitorInfo); sessionBiz.save(session); + visitorInfo.setCustomerServiceId(session.getCustomerServiceId()); return session.getSessionId(); } } diff --git a/src/main/java/com/cloudservice/ccs/biz/impl/VisitorInfoImpl.java b/src/main/java/com/cloudservice/ccs/biz/impl/VisitorInfoImpl.java index 7fe72c3..a561d4d 100644 --- a/src/main/java/com/cloudservice/ccs/biz/impl/VisitorInfoImpl.java +++ b/src/main/java/com/cloudservice/ccs/biz/impl/VisitorInfoImpl.java @@ -11,7 +11,6 @@ import com.cloudservice.ccs.mapper.VisitorInfoMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.Date; import java.util.List; import java.util.Map; @@ -28,14 +27,24 @@ public class VisitorInfoImpl implements IVisitorInfoBiz { @Override public Integer save(VisitorInfo visitorInfo) { - int result = visitorInfoMapper.save(visitorInfo); - if (result == 0) { - throw new VisitorInfoException("Create VisitorInfo entity error"); + boolean originallyExisting = true; + VisitorInfo foundVisitorInfo = visitorInfoMapper.getOne(visitorInfo.getIp()); + if (foundVisitorInfo == null) { + int result = visitorInfoMapper.save(visitorInfo); + if (result == 0) { + throw new VisitorInfoException("创建访客信息失败"); + } + originallyExisting = false; + foundVisitorInfo = visitorInfoMapper.getOne(visitorInfo.getIp()); } try { - return customerServiceDesignationBiz.createSession(visitorInfo); + Integer sessionId = customerServiceDesignationBiz.createSession(foundVisitorInfo); + visitorInfoMapper.update(foundVisitorInfo.getIp(), foundVisitorInfo); + return sessionId; } catch (Exception e) { - visitorInfoMapper.delete(visitorInfo.getIp()); + if (!originallyExisting) { + visitorInfoMapper.delete(visitorInfo.getIp()); + } throw e; } } -- Gitee From 15d5b72364ef80b169a8c49749715d277c3d7252 Mon Sep 17 00:00:00 2001 From: Yang Hanlin Date: Sun, 12 Jul 2020 11:25:26 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=20:warning:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yyyy-MM-dd -> yyyy-MM-dd HH:mm:ss 可能是 breaking change --- src/main/resources/application.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ad4d011..44c2e79 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,9 +4,8 @@ spring: url: jdbc:mysql://bank.accelerator.wtf/ccs?serverTimezone=Asia%2FShanghai username: misaka jackson: - # date-format: 'yyyy-MM-dd HH:mm:ss' # 尚不更改 + date-format: 'yyyy-MM-dd HH:mm:ss' time-zone: 'Asia/Shanghai' - date-format: 'yyyy-MM-dd' servlet: multipart: max-file-size: 50MB -- Gitee From 80764218b1ac6781ff6bd43a3e87425aa3af0d61 Mon Sep 17 00:00:00 2001 From: Yang Hanlin Date: Sun, 12 Jul 2020 12:12:05 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20WebSocket=20=E4=B8=8B?= =?UTF-8?q?=E5=8F=91=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloudservice/ccs/biz/impl/SessionMessageImpl.java | 3 ++- .../java/com/cloudservice/ccs/entity/SessionMessage.java | 1 + .../cloudservice/ccs/model/SessionWithLatestMessage.java | 3 +++ .../com/cloudservice/ccs/util/WebSocketSessionPool.java | 9 +++++++++ .../com/cloudservice/ccs/websocket/SessionServer.java | 3 ++- 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cloudservice/ccs/biz/impl/SessionMessageImpl.java b/src/main/java/com/cloudservice/ccs/biz/impl/SessionMessageImpl.java index b95cfdb..cc2494b 100644 --- a/src/main/java/com/cloudservice/ccs/biz/impl/SessionMessageImpl.java +++ b/src/main/java/com/cloudservice/ccs/biz/impl/SessionMessageImpl.java @@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; @@ -39,7 +40,7 @@ public class SessionMessageImpl implements ISessionMessageBiz { if (sessionId != null) { try { SessionMessageServer.getCustomerServiceSessionPool().sendMessage(sessionId, ApiResult.successWithData(sessionMessage)); - SessionMessageServer.getVisitorSessionPool().sendMessage(sessionId, ApiResult.successWithData(sessionMessage)); + SessionMessageServer.getVisitorSessionPool().sendMessage(sessionId, ApiResult.successWithData(sessionMessage), new SimpleDateFormat("HH:mm:ss")); } catch (IOException e) { if (logger.isWarnEnabled()) { logger.warn("Error when notifying new message: " + e.getClass().getName() + ": " + e.getMessage()); diff --git a/src/main/java/com/cloudservice/ccs/entity/SessionMessage.java b/src/main/java/com/cloudservice/ccs/entity/SessionMessage.java index 255d369..c52736e 100644 --- a/src/main/java/com/cloudservice/ccs/entity/SessionMessage.java +++ b/src/main/java/com/cloudservice/ccs/entity/SessionMessage.java @@ -1,6 +1,7 @@ package com.cloudservice.ccs.entity; import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; diff --git a/src/main/java/com/cloudservice/ccs/model/SessionWithLatestMessage.java b/src/main/java/com/cloudservice/ccs/model/SessionWithLatestMessage.java index 46eedf9..18ff3ef 100644 --- a/src/main/java/com/cloudservice/ccs/model/SessionWithLatestMessage.java +++ b/src/main/java/com/cloudservice/ccs/model/SessionWithLatestMessage.java @@ -1,7 +1,9 @@ package com.cloudservice.ccs.model; import com.cloudservice.ccs.entity.Session; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; @@ -10,6 +12,7 @@ public class SessionWithLatestMessage extends Session { private String latestMessageContent; + @JsonFormat(pattern = "MM-dd HH:mm") private Date latestMessageTime; public static SessionWithLatestMessage fromSession(Session session) { diff --git a/src/main/java/com/cloudservice/ccs/util/WebSocketSessionPool.java b/src/main/java/com/cloudservice/ccs/util/WebSocketSessionPool.java index 8598281..8962846 100644 --- a/src/main/java/com/cloudservice/ccs/util/WebSocketSessionPool.java +++ b/src/main/java/com/cloudservice/ccs/util/WebSocketSessionPool.java @@ -2,9 +2,11 @@ package com.cloudservice.ccs.util; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; import javax.websocket.Session; import java.io.IOException; +import java.text.DateFormat; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @@ -94,6 +96,13 @@ public class WebSocketSessionPool { sendMessage(session, new ObjectMapper().writeValueAsString(message)); } + public void sendMessage(T receiver, Object message, DateFormat dateFormat) throws IOException { + Session session = onlineSessions.get(receiver); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setDateFormat(dateFormat); + sendMessage(session, objectMapper.writeValueAsString(message)); + } + private void sendMessage(Session session, String message) throws IOException { if (session != null) { synchronized (session) { diff --git a/src/main/java/com/cloudservice/ccs/websocket/SessionServer.java b/src/main/java/com/cloudservice/ccs/websocket/SessionServer.java index 7b56091..3add453 100644 --- a/src/main/java/com/cloudservice/ccs/websocket/SessionServer.java +++ b/src/main/java/com/cloudservice/ccs/websocket/SessionServer.java @@ -16,6 +16,7 @@ import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -69,7 +70,7 @@ public class SessionServer { public static void sendSessionList(Integer id, SessionMapper sessionMapper) throws IOException { List activeSessionsWithLatestMessage = sessionMapper.getWithActiveStatusWithLatestMessage(id, true); - sessionPool.sendMessage(id, ApiResult.successWithData(activeSessionsWithLatestMessage)); + sessionPool.sendMessage(id, ApiResult.successWithData(activeSessionsWithLatestMessage), new SimpleDateFormat("MM-dd HH:mm")); } private void logError(Throwable throwable) { -- Gitee