|
@@ -1,18 +1,42 @@
|
|
|
package com.fjhx.listener;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+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 javax.annotation.PostConstruct;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class WebSocketEventListener {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private StockTagService stockTagService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StockDetailService stockDetailService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MaterialService materialService;
|
|
|
+
|
|
|
/**
|
|
|
* 上位机userId
|
|
|
*/
|
|
@@ -43,6 +67,18 @@ public class WebSocketEventListener {
|
|
|
*/
|
|
|
final int PUSH_RFID = 3;
|
|
|
|
|
|
+ private final Map<String, MessageEntity> map = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动定时任务
|
|
|
+ */
|
|
|
+ @PostConstruct
|
|
|
+ public void startCronUtil() {
|
|
|
+ // 支持秒级别定时任务
|
|
|
+ CronUtil.setMatchSecond(true);
|
|
|
+ CronUtil.start();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@EventListener
|
|
|
public void onMessageListener(WebSocketOnOpenEvent event) {
|
|
@@ -51,14 +87,10 @@ public class WebSocketEventListener {
|
|
|
source.sendMessage(ON_OPEN_TYPE, jsonObject);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@EventListener
|
|
|
public void onMessageListener(WebSocketOnMessageEvent event) {
|
|
|
WebSocketServer webSocketServer = event.getSource();
|
|
|
String message = event.getMessage();
|
|
|
-
|
|
|
- System.err.println(message);
|
|
|
-
|
|
|
MessageEntity sendEntity = JSONObject.parseObject(message, MessageEntity.class);
|
|
|
|
|
|
Integer type = sendEntity.getType();
|
|
@@ -72,11 +104,169 @@ public class WebSocketEventListener {
|
|
|
break;
|
|
|
|
|
|
case PUSH_RFID:
|
|
|
- WebSocketServer.sendInfo(sendEntity.getUserId(), sendEntity.getSessionId(), type, data);
|
|
|
+ businessHandle(sendEntity);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @EventListener
|
|
|
+ public void onCloseListener(WebSocketOnCloseEvent event) {
|
|
|
+ map.remove(event.getSource().getSessionId());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 业务处理
|
|
|
+ */
|
|
|
+ private void businessHandle(MessageEntity sendEntity) {
|
|
|
+ JSONObject data = sendEntity.getData();
|
|
|
+
|
|
|
+ switch (data.getInteger("businessType")) {
|
|
|
+ // 入库
|
|
|
+ case 1:
|
|
|
+ warehousing(sendEntity);
|
|
|
+ break;
|
|
|
+ // 出库
|
|
|
+ case 2:
|
|
|
+ issue(sendEntity);
|
|
|
+ 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) {
|
|
|
+ JSONObject data = sendEntity.getData();
|
|
|
+ String rfid = data.getString("rfid");
|
|
|
+ String stockHouseId = data.getString("stockHouseId");
|
|
|
+
|
|
|
+ MessageEntity messageEntity;
|
|
|
+ synchronized (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<>()));
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|