|
@@ -1,26 +1,23 @@
|
|
|
package com.fjhx.listener;
|
|
|
|
|
|
-import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.cron.CronUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fjhx.entity.MessageEntity;
|
|
|
-import com.fjhx.entity.material.Material;
|
|
|
-import com.fjhx.entity.stock.StockDetail;
|
|
|
-import com.fjhx.entity.stock.StockTag;
|
|
|
import com.fjhx.event.WebSocketOnCloseEvent;
|
|
|
import com.fjhx.event.WebSocketOnMessageEvent;
|
|
|
import com.fjhx.event.WebSocketOnOpenEvent;
|
|
|
import com.fjhx.material.service.MaterialService;
|
|
|
import com.fjhx.service.WebSocketServer;
|
|
|
-import com.fjhx.stock.service.StockDetailService;
|
|
|
-import com.fjhx.stock.service.StockTagService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
@Slf4j
|
|
@@ -28,12 +25,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
public class WebSocketEventListener {
|
|
|
|
|
|
@Autowired
|
|
|
- private StockTagService stockTagService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private StockDetailService stockDetailService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
private MaterialService materialService;
|
|
|
|
|
|
/**
|
|
@@ -68,17 +59,6 @@ public class WebSocketEventListener {
|
|
|
|
|
|
private final Map<String, MessageEntity> map = new ConcurrentHashMap<>();
|
|
|
|
|
|
-// /**
|
|
|
-// * 启动定时任务
|
|
|
-// */
|
|
|
-// @PostConstruct
|
|
|
-// public void startCronUtil() {
|
|
|
-// // 支持秒级别定时任务
|
|
|
-//
|
|
|
-// CronUtil.start();
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
@EventListener
|
|
|
public void onMessageListener(WebSocketOnOpenEvent event) {
|
|
|
WebSocketServer source = event.getSource();
|
|
@@ -112,9 +92,21 @@ public class WebSocketEventListener {
|
|
|
|
|
|
@EventListener
|
|
|
public void onCloseListener(WebSocketOnCloseEvent event) {
|
|
|
- String sessionId = event.getSource().getSessionId();
|
|
|
- map.remove(sessionId);
|
|
|
+ WebSocketServer source = event.getSource();
|
|
|
+ String sessionId = source.getSessionId();
|
|
|
+ String userId = source.getUserId();
|
|
|
+
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ data.put(FORWARD_USER_ID, userId);
|
|
|
+ data.put(FORWARD_SESSION_ID, sessionId);
|
|
|
+ data.put("type", 9);
|
|
|
+
|
|
|
+ // 关闭rfid扫描
|
|
|
+ WebSocketServer.sendInfo(WEB_STOCK_PROGRAM, 2, data);
|
|
|
+ // 关闭定时任务
|
|
|
CronUtil.remove(sessionId);
|
|
|
+ // 删除数据
|
|
|
+ map.remove(sessionId);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -144,62 +136,64 @@ public class WebSocketEventListener {
|
|
|
*/
|
|
|
private void warehousingAndIssue(MessageEntity sendEntity, Integer businessType) {
|
|
|
JSONObject data = sendEntity.getData();
|
|
|
+ String sessionId = sendEntity.getSessionId();
|
|
|
String rfid = data.getString("rfid");
|
|
|
- String stockHouseId = data.getString("stockHouseId");
|
|
|
|
|
|
MessageEntity messageEntity;
|
|
|
synchronized (businessType == 1 ? warehousingLock : issueLock) {
|
|
|
- messageEntity = this.map.get(sendEntity.getSessionId());
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ messageEntity = this.map.get(sessionId);
|
|
|
|
|
|
if (messageEntity == null) {
|
|
|
- messageEntity = BeanUtil.copyProperties(sendEntity, MessageEntity.class);
|
|
|
+ messageEntity = sendEntity;
|
|
|
messageEntity.getData().remove("rfid");
|
|
|
- messageEntity.getData().put("rfidData", Collections.synchronizedList(new ArrayList<>()));
|
|
|
- this.map.put(sendEntity.getSessionId(), messageEntity);
|
|
|
-
|
|
|
- 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());
|
|
|
- });
|
|
|
- CronUtil.setMatchSecond(true);
|
|
|
- CronUtil.start();
|
|
|
+ messageEntity.getData().put("rfidList", Collections.singletonList(rfid));
|
|
|
+ this.map.put(sessionId, messageEntity);
|
|
|
+
|
|
|
+ // 执行定时任务
|
|
|
+ executeTask(sessionId, businessType);
|
|
|
+ } else {
|
|
|
+ List<String> rfidList = Convert.toList(String.class, messageEntity.getData().get("rfidList"));
|
|
|
+ rfidList.add(rfid);
|
|
|
+ messageEntity.getData().put("rfidList", rfidList);
|
|
|
}
|
|
|
+
|
|
|
+ long l2 = System.currentTimeMillis();
|
|
|
+ System.out.println(l2 - l);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行定时任务
|
|
|
+ */
|
|
|
+ private void executeTask(String sessionId, Integer businessType) {
|
|
|
+ CronUtil.schedule(sessionId, "0/3 * * * * *", () -> {
|
|
|
+ MessageEntity entity = map.get(sessionId);
|
|
|
+ JSONObject data = entity.getData();
|
|
|
+
|
|
|
+ log.info("======================> executeTask:" + JSONObject.toJSONString(entity));
|
|
|
+
|
|
|
+ List<String> rfidList = Convert.toList(String.class, data.get("rfidList"));
|
|
|
+ List<String> lastRfidList = Convert.toList(String.class, data.get("lastRfidList"));
|
|
|
+ if (rfidList.size() == lastRfidList.size()) return;
|
|
|
+
|
|
|
+ data.put("lastRfidList", new ArrayList<>(rfidList));
|
|
|
+
|
|
|
+ List<Map<String, Object>> rfidData;
|
|
|
+
|
|
|
+ if (businessType.equals(1)) {
|
|
|
+ rfidData = materialService.getInRfidData(rfidList);
|
|
|
+ } else {
|
|
|
+ rfidData = materialService.getOutRfidData(rfidList, data.getString("stockHouseId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ data.put("rfidData", rfidData);
|
|
|
|
|
|
- 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;
|
|
|
-
|
|
|
- long count = stockDetailService.count(Wrappers.<StockDetail>lambdaQuery()
|
|
|
- .eq(businessType == 2, StockDetail::getStockhouseid, stockHouseId)
|
|
|
- .eq(StockDetail::getRfidcode, rfid)
|
|
|
- .eq(StockDetail::getIsdelete, 0)
|
|
|
- .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());
|
|
|
- map.put("materialCode", material.getCode());
|
|
|
- map.put("materialName", material.getName());
|
|
|
- map.put("quantity", stockTag.getQuantity());
|
|
|
- map.put("rfid", rfid);
|
|
|
- rfidData.add(map);
|
|
|
+ log.info("======================> sendData:" + JSONObject.toJSONString(data));
|
|
|
+ WebSocketServer.sendInfo(entity.getUserId(), entity.getSessionId(), entity.getType(), data);
|
|
|
+ });
|
|
|
+ CronUtil.setMatchSecond(true);
|
|
|
+ CronUtil.start();
|
|
|
}
|
|
|
|
|
|
}
|