瀏覽代碼

异步功能处理

oyq28 1 天之前
父節點
當前提交
6735cef35d

+ 6 - 0
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/productionResourceCenter/service/AsyncService.java

@@ -0,0 +1,6 @@
+package com.github.zuihou.business.productionResourceCenter.service;
+
+public interface AsyncService {
+
+    Boolean asyncTaskSchedule();
+}

+ 203 - 0
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/productionResourceCenter/service/impl/AsyncServiceImpl.java

@@ -0,0 +1,203 @@
+package com.github.zuihou.business.productionResourceCenter.service.impl;
+
+import cn.hutool.core.collection.CollectionUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.github.zuihou.business.DemoLine.DemoLineConstant;
+import com.github.zuihou.business.DemoLine.YunjianConstant;
+import com.github.zuihou.business.edgeLibrary.entity.StockInfo;
+import com.github.zuihou.business.edgeLibrary.entity.Storge;
+import com.github.zuihou.business.edgeLibrary.service.StockInfoService;
+import com.github.zuihou.business.edgeLibrary.service.StorgeService;
+import com.github.zuihou.business.operationManagementCenter.entity.TTask;
+import com.github.zuihou.business.operationManagementCenter.entity.TaskNode;
+import com.github.zuihou.business.operationManagementCenter.service.TaskNodeService;
+import com.github.zuihou.business.operationManagementCenter.service.TaskService;
+import com.github.zuihou.business.productionReadyCenter.entity.BomProcedureProductionresource;
+import com.github.zuihou.business.productionReadyCenter.service.BomProcedureProductionresourceService;
+import com.github.zuihou.business.productionResourceCenter.dao.ProductionresourceBizMapper;
+import com.github.zuihou.business.productionResourceCenter.entity.Productionresource;
+import com.github.zuihou.business.productionResourceCenter.service.AsyncService;
+import com.github.zuihou.business.productionResourceCenter.service.ProductionresourcePositionService;
+import com.github.zuihou.business.util.MsgUtil;
+import com.github.zuihou.common.constant.DictionaryKey;
+import com.github.zuihou.common.util.DateUtil;
+import com.github.zuihou.database.mybatis.conditions.query.LbqWrapper;
+import com.google.common.collect.Maps;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.compress.utils.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+public class AsyncServiceImpl implements AsyncService {
+
+   @Autowired
+   private MsgUtil msgUtil;
+
+   @Autowired
+   private ProductionresourceBizMapper productionresourceBizMapper;
+
+   @Autowired
+   private StorgeService  storgeService;
+
+   @Autowired
+   private StockInfoService stockInfoService;
+
+   @Autowired
+   private TaskNodeService taskNodeService;
+
+   @Autowired
+   private TaskService taskService;
+
+   @Autowired
+   private ProductionresourcePositionService productionresourcePositionService;
+
+   @Autowired
+   private BomProcedureProductionresourceService bomProcedureProductionresourceService;
+
+    @Override
+    @Async
+    public Boolean asyncTaskSchedule() {
+        log.warn("[线边库加工设备重调度]");
+        LbqWrapper<Productionresource> lbqWrapper = new LbqWrapper<com.github.zuihou.business.productionResourceCenter.entity.Productionresource>();
+        lbqWrapper.eq(com.github.zuihou.business.productionResourceCenter.entity.Productionresource::getStatus, "1").ne(com.github.zuihou.business.productionResourceCenter.entity.Productionresource::getOnlineStatus, "0").isNotNull(com.github.zuihou.business.productionResourceCenter.entity.Productionresource::getModeSpecification);
+        //获取全部加工在线的加工设备
+        List<Productionresource> macProductionResources = productionresourceBizMapper.selectList(lbqWrapper);
+        //获取未处于加工状态的加工设备
+        List<com.github.zuihou.business.productionResourceCenter.entity.Productionresource> bizProductionResources = macProductionResources.stream().filter(item -> item.getOnlineStatus().equals("1")).collect(Collectors.toList());
+
+        if (bizProductionResources.size() > 0) {
+            List<Long> macResourceIds = macProductionResources.stream().map(com.github.zuihou.business.productionResourceCenter.entity.Productionresource::getId).collect(Collectors.toList());
+            //获取线边库加工任务列表(模糊匹配舱体和框体线边库)
+            List<Long> storgeIds = storgeService.list(new LbqWrapper<Storge>().eq(Storge::getLockStatus, "1").like(Storge::getName, "T_库位")).stream().map(Storge::getId).collect(Collectors.toList());
+            //获取线边库零件编号
+            List<String> completeBatchNos = stockInfoService.list(new LbqWrapper<StockInfo>().eq(StockInfo::getGoodsType, "4").in(StockInfo::getStorgeId, storgeIds)).stream().map(StockInfo::getCompleteBatchNo).collect(Collectors.toList());
+            //获取线边库满足条件的节点
+            List<TaskNode> taskNodeList = completeBatchNos.size() > 0 ? taskNodeService.list(new LbqWrapper<TaskNode>().eq(TaskNode::getExeStatus, "2").in(TaskNode::getTargetResourceId, macResourceIds).in(TaskNode::getCompleteBatchNo, completeBatchNos).eq(TaskNode::getNodeName, "放本序设备").orderByAsc(TaskNode::getStartTime)) : Lists.newArrayList();
+            //获得线边库加工设备列表
+            //Map<Long, List<TaskNode>> taskNodeMap = taskNodeList.stream().filter(item-> resourceIds.contains(item.getTargetResourceId())).collect(Collectors.groupingBy(TaskNode::getResourceId,  LinkedHashMap::new, Collectors.toList()));
+
+            if (taskNodeList != null && taskNodeList.size() > 0) {
+                //判断任务是否已经变更(线边库同类型加工设备只允许变更一次)
+                List<Long> ids = taskNodeList.stream().map(TaskNode::getTaskId).distinct().collect(Collectors.toList());
+
+                Map paramMap = Maps.newHashMap();
+                String strIds = CollectionUtil.isNotEmpty(ids) ? CollectionUtil.join(ids, ",") : "";
+                paramMap.put("ids", strIds);
+                //判断节点处于执行阶段且未放入加工设备
+                List<Map> taskLists = taskService.getXbkChangeStatus(paramMap);
+                Map<Integer, String> taskMaps = null;
+                if (taskLists.size() > 0) {
+                    taskMaps = taskLists.stream().collect(Collectors.toMap(item -> (Integer) item.get("isReload"), item -> item.get("cnt").toString()));
+                } else {
+                    taskMaps = Maps.newHashMap();
+                }
+                //taskMaps.entrySet().stream().forEach(entry-> System.out.println("key="+entry.getKey()+", value="+entry.getValue()));
+                //获取可执行的设备类型
+                Map<Integer, String> finalTaskMaps = taskMaps;
+                List<String> deviceTypes = DictionaryKey.YJ_DEVICE_TYPE.entrySet().stream().filter(map -> !finalTaskMaps.containsKey(map.getValue())).distinct().map(Map.Entry::getKey).collect(Collectors.toList());
+                if (finalTaskMaps.entrySet().size() >= DictionaryKey.YJ_DEVICE_TYPE.size() || deviceTypes.size() == 0) {
+                    //获取已调度任务进行重刷判断处理
+                    List<Integer> reloadLists = finalTaskMaps.keySet().stream().collect(Collectors.toList());
+                    List<Long> taskIdList = taskService.list(new LbqWrapper<TTask>().eq(TTask::getStatus, "2").in(TTask::getIsReload, reloadLists)).stream().filter(item -> {
+                        com.github.zuihou.business.productionResourceCenter.entity.Productionresource productionresource = productionresourceBizMapper.selectById(item.getResourceId());
+                        return productionresource.getOnlineStatus().equals("2") || productionresource.getOnlineStatus().equals("3");
+                    }).map(TTask::getId).collect(Collectors.toList());
+                    if (taskIdList.size() > 0) {
+                        LambdaUpdateWrapper<TTask> updateWrapper = new LambdaUpdateWrapper<TTask>();
+                        updateWrapper.set(TTask::getIsReload, 0).in(TTask::getId, taskIdList);
+                        taskService.update(null, updateWrapper);
+                        log.warn("线边库任务{}二次调度", StringUtils.join(taskIdList, ","));
+                    }
+                    return true;
+                }
+
+                List<TaskNode> updatedTaskNodeList = taskNodeList.stream().filter(item -> bizProductionResources.stream().anyMatch(productionresource -> {
+                    //线边库节点的目标设备
+                    com.github.zuihou.business.productionResourceCenter.entity.Productionresource device = productionresourceBizMapper.selectById(item.getTargetResourceId());
+                    List targetList = productionresourcePositionService.getFreeProductionresourcePositionByIds(new String[]{productionresource.getId().toString()});
+                    //判断任务节点已被动态变更
+                    TTask task = taskService.getById(item.getTaskId());
+                    //限定加工设备在工艺配置设备的范围内
+                    List<BomProcedureProductionresource> bomProcedureProductionresourceList = bomProcedureProductionresourceService.list(new LbqWrapper<BomProcedureProductionresource>().eq(BomProcedureProductionresource::getProcedureId, task.getProcedureId()));
+                    List<Long> productionresourceIds = bomProcedureProductionresourceList.stream().map(BomProcedureProductionresource::getResourceId).distinct().collect(Collectors.toList());
+                    //判断加工设备类型和当前点位可用
+                    //判断过滤原设备和已被锁定变更的设备
+                    //判断设备的类型是否已变更
+                    if (device.getModeSpecification().equals(productionresource.getModeSpecification()) && targetList.size() > 0 && !device.getId().equals(productionresource.getId()) && task.getIsReload().equals(0) && productionresourceIds.contains(productionresource.getId())
+                            && deviceTypes.contains(productionresource.getModeSpecification())) {
+                        item.setTargetResourceId(productionresource.getId());
+                        return true;
+                    }
+                    return false;
+                })).collect(Collectors.toList());
+
+                // 执行任务和节点设备更新
+                if (updatedTaskNodeList.size() > 0) {
+                    TaskNode updatedNode = updatedTaskNodeList.stream().sorted(Comparator.comparing(TaskNode::getStartTime)).collect(Collectors.toList()).get(0);
+                    //只默认取当前第一条任务
+                    TTask updatedTask = taskService.getById(updatedNode.getTaskId());
+                    com.github.zuihou.business.productionResourceCenter.entity.Productionresource targetDevice = productionresourceBizMapper.selectById(updatedNode.getTargetResourceId());
+                    //更新缓存中哈默上传文件路径IP信息
+                    if (null != targetDevice && targetDevice.getModeSpecification().equals("HEIDENHAIN")) {
+                        String uploadInfo = msgUtil.redis_get(DemoLineConstant.DEMOLINE_HEIDENHAIN_FILE_URL + "_" + updatedTask.getId()) != null ? msgUtil.redis_get(DemoLineConstant.DEMOLINE_HEIDENHAIN_FILE_URL + "_" + updatedTask.getId()).toString() : null;
+                        if (StringUtils.isNotEmpty(uploadInfo)) {
+                            JSONObject jsonObject = JSONObject.parseObject(uploadInfo);
+                            jsonObject.put("url", targetDevice.getIp());
+                            msgUtil.redis_set(DemoLineConstant.DEMOLINE_HEIDENHAIN_FILE_URL + "_" + updatedTask.getId(), jsonObject.toJSONString(), 30, TimeUnit.DAYS);
+                        }
+                    } else if (null == targetDevice) {
+                        log.warn("节点{}目标设备不存在", updatedNode);
+                        msgUtil.redis_del(YunjianConstant.YUNJIAN_XBK_SCHEDULE);
+                        return false;
+                    }
+
+                    // 执行任务和节点设备更新
+                    if (updatedTask != null && updatedTask.getResourceId() != null) {
+                        //设定任务加工设备状态变更
+                        int isReload = DictionaryKey.YJ_DEVICE_TYPE.containsKey(targetDevice.getModeSpecification()) ? DictionaryKey.YJ_DEVICE_TYPE.get(targetDevice.getModeSpecification()) : 0;
+                        updatedTask.setResourceId(updatedNode.getTargetResourceId()).setIsReload(isReload);
+                        JSONObject dyChangedInfo = new JSONObject();
+                        List<TaskNode> taskNodes = taskNodeService.list(new LbqWrapper<TaskNode>().eq(TaskNode::getTaskId, updatedTask.getId()));
+                        //更新节点目的设备
+                        taskNodes.stream().forEach(taskNode -> {
+                            if (null != taskNode.getTargetResourceId()) {
+                                if (taskNode.getResourceId().equals(taskNode.getTargetResourceId())) {
+                                    if (!dyChangedInfo.containsKey("fromResource")) {
+                                        dyChangedInfo.put("fromResource", taskNode.getResourceId());
+                                    }
+                                    taskNode.setResourceId(updatedTask.getResourceId());
+                                }
+                                taskNode.setTargetResourceId(updatedTask.getResourceId());
+                            }
+                        });
+                        // 更新零件地址
+                        taskService.updateById(updatedTask);
+                        taskNodeService.updateBatchById(taskNodes);
+
+                        dyChangedInfo.put("toResource", updatedTask.getResourceId());
+                        dyChangedInfo.put("time", DateUtil.formatTime(new Date()));
+
+                        // 缓存数据更新
+                        //TaskNode currTaskNode = taskNodeService.getOne(new LbqWrapper<TaskNode>().eq(TaskNode::getTaskId,updatedTask.getId()).eq(TaskNode::getExeStatus,"2"));
+                        msgUtil.redis_set(DemoLineConstant.DEMOLINE_RESOURCE_DY_CHANGE + "_" + updatedTask.getId(), dyChangedInfo.toJSONString(), 1, TimeUnit.DAYS);
+                        msgUtil.redis_del(YunjianConstant.YUNJIAN_XBK_SCHEDULE);
+                    }
+
+                }
+            }
+        }
+        return true;
+    }
+}