Browse Source

发送用户任务数

yzc 1 year ago
parent
commit
f6e1c5f885

+ 3 - 1
hx-common/src/main/java/com/fjhx/common/enums/PushBusinessTypeEnum.java

@@ -61,7 +61,9 @@ public enum PushBusinessTypeEnum {
     /**
      * 新合同
      */
-    NEW_CONTRACT(11);
+    NEW_CONTRACT(11),
+
+    TASK_COUNT(12);
 
     private final int type;
 

+ 4 - 0
hx-mes/src/main/java/com/fjhx/mes/mapper/production/ProductionTaskDetailMapper.java

@@ -40,5 +40,9 @@ public interface ProductionTaskDetailMapper extends BaseMapper<ProductionTaskDet
      */
     ProductionTaskDetailVo getDetail(@Param("ew") IWrapper<ProductionTaskDetail> wrapper);
 
+    /**
+     * 获取用户任务数
+     */
+    Long getUserTaskCount(@Param("ew") IWrapper<ProductionTaskDetail> wrapper);
 
 }

+ 58 - 0
hx-mes/src/main/java/com/fjhx/mes/service/production/impl/ProductionTaskDetailServiceImpl.java

@@ -3,9 +3,13 @@ package com.fjhx.mes.service.production.impl;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson2.JSONObject;
+import com.alibaba.fastjson2.JSONWriter;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.common.constant.SourceConstant;
 import com.fjhx.common.enums.PushBusinessTypeEnum;
 import com.fjhx.common.utils.Assert;
 import com.fjhx.customer.entity.customer.po.Customer;
@@ -33,19 +37,25 @@ import com.fjhx.mes.service.work.WorkOrderProductionProcessesService;
 import com.fjhx.mes.service.work.WorkOrderService;
 import com.fjhx.socket.core.PushTypeEnum;
 import com.fjhx.socket.core.WebSocketPush;
+import com.fjhx.socket.core.WebSocketServer;
+import com.fjhx.socket.core.event.WebSocketOnMessageEvent;
+import com.fjhx.socket.core.event.WebSocketOnOpenEvent;
 import com.fjhx.wms.entity.stock.emums.StockWaitType;
 import com.fjhx.wms.entity.stock.po.StockWait;
 import com.fjhx.wms.entity.stock.po.StockWaitDetails;
 import com.fjhx.wms.service.stock.StockWaitDetailsService;
 import com.fjhx.wms.service.stock.StockWaitService;
+import com.ruoyi.common.annotation.TenantIgnore;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.ruoyi.common.utils.wrapper.SqlField;
 import com.ruoyi.system.utils.UserUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.function.Function;
@@ -648,4 +658,52 @@ public class ProductionTaskDetailServiceImpl extends ServiceImpl<ProductionTaskD
         return productionTaskDetailVo;
     }
 
+
+    /**
+     * 用户连接websock监听
+     */
+    @EventListener
+    @TenantIgnore
+    public void webSocketOnOpenEvent(WebSocketOnOpenEvent webSocketOnOpenEvent) {
+        WebSocketServer webSocketServer = webSocketOnOpenEvent.getWebSocketServer();
+        sendTaskCount(webSocketServer);
+    }
+
+    /**
+     * 用户收到websock消息监听
+     */
+    @EventListener
+    @TenantIgnore
+    public void webSocketOnMessageEvent(WebSocketOnMessageEvent webSocketOnMessageEvent) {
+        WebSocketServer webSocketServer = webSocketOnMessageEvent.getWebSocketServer();
+        String message = webSocketOnMessageEvent.getMessage();
+        JSONObject json = JSONObject.parseObject(message);
+        if (ObjectUtil.isNotEmpty(json.get("getTaskCount"))) {
+            sendTaskCount(webSocketServer);
+        }
+    }
+
+    /**
+     * 发送用户任务数
+     */
+    void sendTaskCount(WebSocketServer webSocketServer) {
+        Long userId = webSocketServer.getUserId();
+
+        IWrapper<ProductionTaskDetail> wrapper = IWrapper.getWrapper();
+        wrapper.eq("ptpd.user_id", userId);
+        wrapper.eq("ptd.finish_status", 0);
+
+        DynamicDataSourceContextHolder.push(SourceConstant.MES);
+        Long userTaskCount = baseMapper.getUserTaskCount(wrapper);
+        DynamicDataSourceContextHolder.clear();
+
+        JSONObject msg = new JSONObject();
+        msg.put("taskCount", userTaskCount);
+        try {
+            webSocketServer.getSession().getBasicRemote().sendText(msg.toJSONString(JSONWriter.Feature.WriteLongAsString));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 }

+ 9 - 0
hx-mes/src/main/resources/mapper/production/ProductionTaskDetailMapper.xml

@@ -148,5 +148,14 @@
                 AND sc.del_flag =0
             ${ew.customSqlSegment}
     </select>
+    <select id="getUserTaskCount" resultType="java.lang.Long">
+        SELECT
+            count( ptd.id )
+        FROM
+            production_task_detail ptd
+                LEFT JOIN production_task_processes_detail ptpd ON ptpd.production_task_id = ptd.production_task_id
+                AND ptpd.production_processes_id = ptd.production_processes_id
+            ${ew.customSqlSegment}
+    </select>
 
 </mapper>