Explorar o código

7天在线时长统计

yzc hai 1 ano
pai
achega
abd383b4aa

+ 5 - 0
hx-common/src/main/java/com/fjhx/common/entity/on/po/OnLineInfo.java

@@ -40,4 +40,9 @@ public class OnLineInfo extends BasePo {
      */
     private Date lastOnlineTime;
 
+    /**
+     * 租户id
+     */
+    private String tenantId;
+
 }

+ 17 - 2
hx-common/src/main/java/com/fjhx/common/service/on/impl/OnLineInfoServiceImpl.java

@@ -14,6 +14,7 @@ import com.fjhx.common.service.on.OnLineInfoService;
 import com.fjhx.socket.core.WebSocketServer;
 import com.fjhx.socket.core.event.WebSocketOnMessageEvent;
 import com.fjhx.socket.core.event.WebSocketOnOpenEvent;
+import com.ruoyi.common.annotation.TenantIgnore;
 import com.ruoyi.common.core.domain.entity.SysDept;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.system.service.ISysDeptService;
@@ -112,24 +113,32 @@ public class OnLineInfoServiceImpl extends ServiceImpl<OnLineInfoMapper, OnLineI
      * 用户连接websock监听
      */
     @EventListener
+    @TenantIgnore
     public void webSocketOnOpenEvent(WebSocketOnOpenEvent webSocketOnOpenEvent) {
         WebSocketServer webSocketServer = webSocketOnOpenEvent.getWebSocketServer();
         Long userId = webSocketServer.getUserId();
         //记录上线时间
+        DynamicDataSourceContextHolder.push(SourceConstant.COMMON);
         OnLineInfo onLineInfo = this.getOne(q -> q.eq(OnLineInfo::getUserId, userId).apply("DATE(on_line_date) = DATE(NOW())"));
-        if (ObjectUtil.isNotEmpty(onLineInfo)) {
+        DynamicDataSourceContextHolder.poll();
+        if (ObjectUtil.isEmpty(onLineInfo)) {
             onLineInfo = new OnLineInfo();
             onLineInfo.setOnLineDuration(0L);
             onLineInfo.setOnLineDate(new Date());
+            onLineInfo.setUserId(userId);
+            onLineInfo.setTenantId(webSocketServer.getTenantId());
         }
         onLineInfo.setLastOnlineTime(new Date());
+        DynamicDataSourceContextHolder.push(SourceConstant.COMMON);
         this.saveOrUpdate(onLineInfo);
+        DynamicDataSourceContextHolder.poll();
     }
 
     /**
      * 收到websock消息监听
      */
     @EventListener
+    @TenantIgnore
     public void webSocketOnMessageEvent(WebSocketOnMessageEvent webSocketOnMessageEvent) {
         WebSocketServer webSocketServer = webSocketOnMessageEvent.getWebSocketServer();
         Long userId = webSocketServer.getUserId();
@@ -138,11 +147,15 @@ public class OnLineInfoServiceImpl extends ServiceImpl<OnLineInfoMapper, OnLineI
         JSONObject json = JSONObject.parseObject(message);
         if (ObjectUtil.isNotEmpty(json.get("heartbeat"))) {
             //保存在线时长信息
+            DynamicDataSourceContextHolder.push(SourceConstant.COMMON);
             OnLineInfo onLineInfo = this.getOne(q -> q.eq(OnLineInfo::getUserId, userId).apply("DATE(on_line_date) = DATE(NOW())"));
-            if (ObjectUtil.isNotEmpty(onLineInfo)) {
+            DynamicDataSourceContextHolder.poll();
+            if (ObjectUtil.isEmpty(onLineInfo)) {
                 onLineInfo = new OnLineInfo();
                 onLineInfo.setOnLineDuration(0L);
                 onLineInfo.setOnLineDate(new Date());
+                onLineInfo.setUserId(userId);
+                onLineInfo.setTenantId(webSocketServer.getTenantId());
             }
 
 
@@ -154,7 +167,9 @@ public class OnLineInfoServiceImpl extends ServiceImpl<OnLineInfoMapper, OnLineI
             //记录在线时长
             onLineInfo.setOnLineDuration(onLineInfo.getOnLineDuration() + onLineTime);
             onLineInfo.setLastOnlineTime(endDate);
+            DynamicDataSourceContextHolder.push(SourceConstant.COMMON);
             this.saveOrUpdate(onLineInfo);
+            DynamicDataSourceContextHolder.poll();
         }
     }
 

+ 1 - 1
hx-common/src/main/resources/mapper/on/OnLineInfoMapper.xml

@@ -27,7 +27,7 @@
             </if>
         </where>
         GROUP BY
-        oli.id
+        oli.user_id
     </select>
 
 </mapper>