Browse Source

杰生重构

home 2 năm trước cách đây
mục cha
commit
42062b99d8

+ 72 - 72
hx-service/ding/src/main/java/com/fjhx/utils/DingUtil.java

@@ -1,76 +1,76 @@
-package com.fjhx.utils;
-
-//import com.dingtalk.api.DefaultDingTalkClient;
-//import com.dingtalk.api.DingTalkClient;
-//import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
-//import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
-//import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
-//import com.dingtalk.api.request.OapiV2UserGetRequest;
-//import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
-//import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
-//import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
-//import com.dingtalk.api.response.OapiV2UserGetResponse;
-//import com.taobao.api.ApiException;
-
-public class DingUtil {
-
-//    private static final String accessKey = "suitepgu4shlatgllyiqp";
-//    private static final String accessSecret = "52-_2aKc3WGd8qVX5FRFmR6MnkeAOi6seUKYxjMWBCeiXZJ0l01_YaDfABIP-hmk";
+//package com.fjhx.utils;
 //
-//    public static void main(String[] args) throws ApiException {
-//        OapiServiceGetCorpTokenResponse corpToken = getCorpToken("ding53d823e205604918a39a90f97fcb1e09",
-//                "R9lsfWBExszmJuiJwF8gd3LQ31YUnUaPRQ0L0W4CsslEsyM1L5GVfLXPJExnhbqRsLntMnFp7KYCha9msIjRCY");
+////import com.dingtalk.api.DefaultDingTalkClient;
+////import com.dingtalk.api.DingTalkClient;
+////import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
+////import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
+////import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
+////import com.dingtalk.api.request.OapiV2UserGetRequest;
+////import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
+////import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
+////import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
+////import com.dingtalk.api.response.OapiV2UserGetResponse;
+////import com.taobao.api.ApiException;
 //
-//        if (corpToken.getErrcode().equals(0L)) {
-//            String accessToken = corpToken.getAccessToken();
-//            getDeptList(accessToken, 1L);
+//public class DingUtil {
 //
-//        }
-//    }
+////    private static final String accessKey = "suitepgu4shlatgllyiqp";
+////    private static final String accessSecret = "52-_2aKc3WGd8qVX5FRFmR6MnkeAOi6seUKYxjMWBCeiXZJ0l01_YaDfABIP-hmk";
+////
+////    public static void main(String[] args) throws ApiException {
+////        OapiServiceGetCorpTokenResponse corpToken = getCorpToken("ding53d823e205604918a39a90f97fcb1e09",
+////                "R9lsfWBExszmJuiJwF8gd3LQ31YUnUaPRQ0L0W4CsslEsyM1L5GVfLXPJExnhbqRsLntMnFp7KYCha9msIjRCY");
+////
+////        if (corpToken.getErrcode().equals(0L)) {
+////            String accessToken = corpToken.getAccessToken();
+////            getDeptList(accessToken, 1L);
+////
+////        }
+////    }
+////
+////    /**
+////     * 获取token
+////     */
+////    public static OapiServiceGetCorpTokenResponse getCorpToken(String authCorpid, String suiteTicket) throws ApiException {
+////
+////        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
+////        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
+////        req.setAuthCorpid(authCorpid);
+////
+////        return client.execute(req, accessKey, accessSecret, suiteTicket);
+////    }
+////
+////    public static void getDeptList(String accessToken, Long deptId) throws ApiException {
+////        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
+////        OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
+////        req.setDeptId(deptId);
+////        OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);
+////
+////        List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rsp.getResult();
+////
+////        for (OapiV2DepartmentListsubResponse.DeptBaseResponse deptBaseResponse : result) {
+////            getDeptList(accessToken, deptBaseResponse.getDeptId());
+////
+////            getDeptDetails(accessToken,deptBaseResponse.getDeptId());
+////        }
+////
+////    }
+////
+////    public static void getDeptDetails(String accessToken, Long deptId) throws ApiException {
+////        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
+////        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
+////        req.setDeptId(deptId);
+////        OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
+////        System.out.println(rsp.getBody());
+////    }
+////
+////
+////    public static void getUser(String accessToken, String userId) throws ApiException {
+////        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
+////        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
+////        req.setUserid(userId);
+////        OapiV2UserGetResponse rsp = client.execute(req, accessToken);
+////        System.out.println(rsp.getBody());
+////    }
 //
-//    /**
-//     * 获取token
-//     */
-//    public static OapiServiceGetCorpTokenResponse getCorpToken(String authCorpid, String suiteTicket) throws ApiException {
-//
-//        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
-//        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
-//        req.setAuthCorpid(authCorpid);
-//
-//        return client.execute(req, accessKey, accessSecret, suiteTicket);
-//    }
-//
-//    public static void getDeptList(String accessToken, Long deptId) throws ApiException {
-//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
-//        OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
-//        req.setDeptId(deptId);
-//        OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);
-//
-//        List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rsp.getResult();
-//
-//        for (OapiV2DepartmentListsubResponse.DeptBaseResponse deptBaseResponse : result) {
-//            getDeptList(accessToken, deptBaseResponse.getDeptId());
-//
-//            getDeptDetails(accessToken,deptBaseResponse.getDeptId());
-//        }
-//
-//    }
-//
-//    public static void getDeptDetails(String accessToken, Long deptId) throws ApiException {
-//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
-//        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
-//        req.setDeptId(deptId);
-//        OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
-//        System.out.println(rsp.getBody());
-//    }
-//
-//
-//    public static void getUser(String accessToken, String userId) throws ApiException {
-//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
-//        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
-//        req.setUserid(userId);
-//        OapiV2UserGetResponse rsp = client.execute(req, accessToken);
-//        System.out.println(rsp.getBody());
-//    }
-
-}
+//}

+ 37 - 104
hx-service/storage/src/main/java/com/fjhx/listener/WebSocketEventListener.java

@@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Component;
 
-import javax.annotation.PostConstruct;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -69,15 +68,15 @@ public class WebSocketEventListener {
 
     private final Map<String, MessageEntity> map = new ConcurrentHashMap<>();
 
-    /**
-     * 启动定时任务
-     */
-    @PostConstruct
-    public void startCronUtil() {
-        // 支持秒级别定时任务
-        CronUtil.setMatchSecond(true);
-        CronUtil.start();
-    }
+//    /**
+//     * 启动定时任务
+//     */
+//    @PostConstruct
+//    public void startCronUtil() {
+//        // 支持秒级别定时任务
+//
+//        CronUtil.start();
+//    }
 
 
     @EventListener
@@ -113,7 +112,9 @@ public class WebSocketEventListener {
 
     @EventListener
     public void onCloseListener(WebSocketOnCloseEvent event) {
-        map.remove(event.getSource().getSessionId());
+        String sessionId = event.getSource().getSessionId();
+        map.remove(sessionId);
+        CronUtil.remove(sessionId);
     }
 
 
@@ -123,142 +124,74 @@ public class WebSocketEventListener {
     private void businessHandle(MessageEntity sendEntity) {
         JSONObject data = sendEntity.getData();
 
-        switch (data.getInteger("businessType")) {
-            // 入库
-            case 1:
-                warehousing(sendEntity);
-                break;
-            // 出库
-            case 2:
-                issue(sendEntity);
+        Integer businessType = data.getInteger("businessType");
+        switch (businessType) {
+            case 1:  // 入库
+            case 2:  // 出库
+                warehousingAndIssue(sendEntity, businessType);
                 break;
         }
-
     }
 
     // 入库锁
     private final Object warehousingLock = new Object();
 
-    /**
-     * 入库
-     */
-    private void warehousing(MessageEntity sendEntity) {
-        JSONObject data = sendEntity.getData();
-        String rfid = data.getString("rfid");
-        String stockHouseId = data.getString("stockHouseId");
-
-        MessageEntity messageEntity;
-        synchronized (warehousingLock) {
-            messageEntity = this.map.get(sendEntity.getSessionId());
-
-            if (messageEntity == null) {
-                messageEntity = BeanUtil.copyProperties(sendEntity, MessageEntity.class);
-                messageEntity.getData().remove("rfid");
-                messageEntity.getData().put("rfidData", Collections.synchronizedList(new ArrayList<>()));
-
-                CronUtil.schedule(sendEntity.getSessionId(), "*/2 * * * *", () -> {
-                    MessageEntity item = map.get(sendEntity.getSessionId());
-                    WebSocketServer.sendInfo(item.getUserId(), item.getSessionId(), item.getType(), item.getData());
-                });
-
-                this.map.put(sendEntity.getSessionId(), messageEntity);
-            }
-        }
-
-        List<Map<String, Object>> rfidData = (List) messageEntity.getData().get("rfidData");
-
-        StockTag stockTag = stockTagService.getOne(Wrappers.<StockTag>lambdaQuery()
-                .select(StockTag::getMaterialCode, StockTag::getQuantity)
-                .eq(StockTag::getRfidCode, rfid)
-                .eq(StockTag::getIsDelete, 0)
-                .last("limit 1")
-        );
-
-        if (stockTag == null) {
-            return;
-        }
-
-        long count = stockDetailService.count(Wrappers.<StockDetail>lambdaQuery()
-                .eq(StockDetail::getStockhouseid, stockHouseId)
-                .eq(StockDetail::getRfidcode, rfid)
-                .eq(StockDetail::getIsdelete, 0)
-                .last("limit 1")
-        );
-
-        // 在库不查
-        if (count > 0) {
-            return;
-        }
-
-        Material material = materialService.getOne(Wrappers.<Material>lambdaQuery()
-                .eq(Material::getCode, stockTag.getMaterialCode()));
-
-        HashMap<String, Object> map = new HashMap<>();
-        map.put("materialId", material.getId());
-        map.put("materialCode", material.getCode());
-        map.put("materialName", material.getName());
-        map.put("quantity", stockTag.getQuantity());
-        map.put("rfid", rfid);
-        rfidData.add(map);
-    }
-
     // 出库锁
     private final Object issueLock = new Object();
 
     /**
-     * 出库
+     * 入库和出库逻辑
      */
-    private void issue(MessageEntity sendEntity) {
+    private void warehousingAndIssue(MessageEntity sendEntity, Integer businessType) {
         JSONObject data = sendEntity.getData();
         String rfid = data.getString("rfid");
         String stockHouseId = data.getString("stockHouseId");
 
         MessageEntity messageEntity;
-        synchronized (issueLock) {
+        synchronized (businessType == 1 ? warehousingLock : issueLock) {
             messageEntity = this.map.get(sendEntity.getSessionId());
 
             if (messageEntity == null) {
                 messageEntity = BeanUtil.copyProperties(sendEntity, MessageEntity.class);
                 messageEntity.getData().remove("rfid");
                 messageEntity.getData().put("rfidData", Collections.synchronizedList(new ArrayList<>()));
+                this.map.put(sendEntity.getSessionId(), messageEntity);
 
-                CronUtil.schedule(sendEntity.getSessionId(), "*/2 * * * *", () -> {
+                CronUtil.schedule(sendEntity.getSessionId(), "0/2 * * * * *", () -> {
                     MessageEntity item = map.get(sendEntity.getSessionId());
+                    if (item == null) {
+                        map.remove(sendEntity.getSessionId());
+                        return;
+                    }
                     WebSocketServer.sendInfo(item.getUserId(), item.getSessionId(), item.getType(), item.getData());
                 });
-
-                this.map.put(sendEntity.getSessionId(), messageEntity);
+                CronUtil.setMatchSecond(true);
+                CronUtil.start();
             }
         }
 
         List<Map<String, Object>> rfidData = (List) messageEntity.getData().get("rfidData");
 
+        // 没有此rfid标签信息跳过
         StockTag stockTag = stockTagService.getOne(Wrappers.<StockTag>lambdaQuery()
                 .select(StockTag::getMaterialCode, StockTag::getQuantity)
                 .eq(StockTag::getRfidCode, rfid)
                 .eq(StockTag::getIsDelete, 0)
-                .last("limit 1")
-        );
-
-        // 没有找到标签信息
-        if (stockTag == null) {
-            return;
-        }
+                .last("limit 1"));
+        if (stockTag == null) return;
 
         long count = stockDetailService.count(Wrappers.<StockDetail>lambdaQuery()
-                .eq(StockDetail::getStockhouseid, stockHouseId)
+                .eq(businessType == 2, StockDetail::getStockhouseid, stockHouseId)
                 .eq(StockDetail::getRfidcode, rfid)
                 .eq(StockDetail::getIsdelete, 0)
-                .last("limit 1")
-        );
-
-        // 不在库
-        if (count == 0) {
-            return;
-        }
+                .last("limit 1"));
+        // 如果入库,库存中有此标签则跳过; 如果出库,库存中没有此标签则跳过
+        if ((businessType == 1 ? count > 0 : count == 0)) return;
 
+        // 查询物料信息
         Material material = materialService.getOne(Wrappers.<Material>lambdaQuery()
                 .eq(Material::getCode, stockTag.getMaterialCode()));
+        if (material == null) return;
 
         HashMap<String, Object> map = new HashMap<>();
         map.put("materialId", material.getId());