فهرست منبع

简化代码和重构业务代码

oyq28 3 سال پیش
والد
کامیت
f1f0f55ba1

+ 31 - 52
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/mq/TaskWork.java

@@ -19,6 +19,7 @@ import com.github.zuihou.business.productionReadyCenter.entity.AAutoNodeLog;
 import com.github.zuihou.business.productionReadyCenter.entity.BomProcedureProgramGroup;
 import com.github.zuihou.business.productionReadyCenter.service.AutoNodeService;
 import com.github.zuihou.business.util.DynamicRabbitMq;
+import com.github.zuihou.business.util.MsgUtil;
 import com.github.zuihou.business.websocket.WebSocketServer;
 import com.github.zuihou.common.constant.BizConstant;
 import com.github.zuihou.common.constant.CacheKey;
@@ -26,6 +27,7 @@ import com.github.zuihou.common.constant.ParameterKey;
 import com.github.zuihou.common.util.StringUtil;
 import com.github.zuihou.context.BaseContextHandler;
 import com.github.zuihou.database.mybatis.conditions.Wraps;
+import com.google.common.collect.Maps;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -35,10 +37,7 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
 
 import java.text.NumberFormat;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Component
 public class TaskWork {
@@ -51,8 +50,6 @@ public class TaskWork {
     @Autowired
     private PlanMapper planMapper;
     @Autowired
-    private AutoNodeMapper autoNodeMapper;
-    @Autowired
     private TTaskMapper taskMapper;
     @Autowired
     private AutoNodeLogMapper autoNodeLogMapper;
@@ -69,8 +66,7 @@ public class TaskWork {
 
     @Autowired
     private TaskNodeMapper taskNodeMapper;
-    @Autowired
-    private WebSocketServer webSocketServer;
+
     @Autowired
     private DynamicRabbitMq dynamicRabbitMq;
 
@@ -80,6 +76,9 @@ public class TaskWork {
     //总控端口
     private String ZK_port="4840";
 
+    @Autowired
+    private MsgUtil msgUtil;
+
     private static Logger logger = LoggerFactory.getLogger(TaskWork.class);
     //@Scheduled(cron="*/9 * * * * ?")  //9秒执行一次
     public void updateTaskStatusJob1(String taskId){
@@ -104,8 +103,7 @@ public class TaskWork {
                     //把本次待执行任务全部改成进行中
                     //taskMapper.updateTaskBegin();
                     if("1".equals(task.getStatus())){//下发的数据才更新状态
-                        task.setStatus("2");
-                        task.setStartTime(new Date());
+                        task.setStatus("2").setStartTime(new Date());
                         taskMapper.updateById(task);
                         if(!planMap.containsKey(String.valueOf(task.getPlanId()))){
                             //设置计划开始
@@ -119,8 +117,8 @@ public class TaskWork {
                             OrderMap.put(String.valueOf(task.getOrderId()),String.valueOf(task.getOrderId()));
                         }
                     }
-                    redisTemplate.opsForValue().set(CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId(), task.getId());
-                    redisTemplate.opsForValue().set(CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId(), task);
+                    msgUtil.redis_set(CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId(), task.getId());
+                    msgUtil.redis_set(CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId(), task);
                     boolean f = false;
                     //执行任务对应的自动化程序
                     /*AutoNode model = new AutoNode();
@@ -146,7 +144,7 @@ public class TaskWork {
                             }
 
                             //把当前节点存放缓存
-                            redisTemplate.opsForValue().set(CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId(), taskNode.getAutoNode().getId());
+                            msgUtil.redis_set(CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId(), taskNode.getAutoNode().getId());
 //                            String code = taskNode.getAutoNodeCode();
 //                            autoProgrameUrl+="?func="+code;
 //                            autoProgrameUrl+="&ip="+task.getIp()+"&port="+task.getPort();
@@ -159,13 +157,9 @@ public class TaskWork {
                             //插入执行日志
                             AAutoNodeLog log = new AAutoNodeLog();
                             if(autoNodeLogList==null||autoNodeLogList.size()==0){
-                                log.setTaskNodeId(taskNode.getId());
-                                log.setAutoNodeId(taskNode.getAutoNode().getTaskId());
-                                log.setOrderId(task.getOrderId());
-                                log.setTaskId(task.getId());
-                                log.setExeStatus("2");
-                                log.setStartTime(new Date());
-                                log.setFeedbackFile(fileName);
+                                log.setTaskNodeId(taskNode.getId()).setAutoNodeId(taskNode.getAutoNode().getTaskId())
+                                   .setOrderId(task.getOrderId()).setTaskId(task.getId()).setExeStatus("2")
+                                   .setStartTime(new Date()).setFeedbackFile(fileName);
                                 autoNodeLogMapper.insert(log);
                             }else{
                                 log = autoNodeLogList.get(0);
@@ -204,7 +198,7 @@ public class TaskWork {
 //                            if(1==1){
                                 //判断值是否需要循环自检
                                 if(!"0".equals(taskNode.getNodeType())){//人工上下料或质检
-                                    redisTemplate.opsForValue().set(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId(), "0");
+                                    msgUtil.redis_set(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId(), "0");
                                     String b = checkLoading(taskNode);
                                 }else{//调用机器
                                     if("1".equals(taskNode.getSelfExamine())){//需要自检的,需要停顿一会,避免取数不及时
@@ -240,28 +234,23 @@ public class TaskWork {
                                 //插入执行日志
                                 log.setExeStatus("3");
                                 if(r.getIsSuccess()){
-                                    log.setExeResult("1");
-                                    log.setEndTime(new Date());
+                                    log.setExeResult("1").setEndTime(new Date());
                                     autoNodeLogMapper.updateAllById(log);
                                 }else{
-                                    log.setExeResult("0");
-                                    log.setFeedback(r.getMsg());
-                                    log.setEndTime(new Date());
+                                    log.setExeResult("0").setFeedback(r.getMsg()).setEndTime(new Date());
                                     autoNodeLogMapper.updateAllById(log);
                                     f = true;
                                     break;
                                 }
 
                                 //更新节点任务
-                                taskNode.setExeStatus("3");
-                                taskNode.setExeResult("1");
-                                taskNode.setEndTime(new Date());
+                                taskNode.setExeStatus("3").setExeResult("1").setEndTime(new Date());
                                 taskNodeMapper.updateAllById(taskNode);
 
                                 //把当前完成的节点放入缓存,用于推送任务完成的百分比
                                 NumberFormat numberFormat = NumberFormat.getInstance();
                                 numberFormat.setMaximumFractionDigits(2);
-                                redisTemplate.opsForValue().set(CacheKey.TASK_CURRENT_NODE_PERCENT+"_"+task.getResourceId(), numberFormat.format(100*(float)(i+1) /(float)taskNodeList.size()));
+                                msgUtil.redis_set(CacheKey.TASK_CURRENT_NODE_PERCENT+"_"+task.getResourceId(), numberFormat.format(100*(float)(i+1) /(float)taskNodeList.size()));
                                 task.setProcess(Double.parseDouble(numberFormat.format(100*(float)(i+1) /(float)taskNodeList.size())));
                                 if(i==taskNodeList.size()-1){
                                     task.setStatus("3");
@@ -269,9 +258,7 @@ public class TaskWork {
                                     taskMapper.updateById(task);
                                     //推送
                                     //删除正在执行的缓存
-                                    redisTemplate.delete(CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId());
-                                    redisTemplate.delete(CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId());
-                                    redisTemplate.delete(CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId());
+                                    msgUtil.redis_del(Arrays.asList(new String[] {CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId(),CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId(),CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId()}));
                                     this.pushTask(task);
                                 }else{//主要更新完成比例
                                     taskMapper.updateById(task);
@@ -305,8 +292,7 @@ public class TaskWork {
                             }
                         }
                     }else{//没有节点直接改成已完成
-                        task.setStatus("3");
-                        task.setEndTime(new Date());
+                        task.setStatus("3").setEndTime(new Date());
                         taskMapper.updateById(task);
                     }
                     if(f){
@@ -317,9 +303,7 @@ public class TaskWork {
                     logger.info("===============================接口返回",e);
                 }finally {
                     //删除正在执行的缓存
-                    redisTemplate.delete(CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId());
-                    redisTemplate.delete(CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId());
-                    redisTemplate.delete(CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId());
+                    msgUtil.redis_del(Arrays.asList(new String[] {CacheKey.PRESOURCE_CURRENT_TASK+"_"+task.getResourceId(),CacheKey.PRESOURCE_CURRENT_TASK_OBJECT+"_"+task.getResourceId(),CacheKey.TASK_CURRENT_NODE+"_"+task.getResourceId()}));
                 }
             }
             //查询出下一个任务,并推送到设备上
@@ -337,7 +321,7 @@ public class TaskWork {
     public String checkLoading(TaskNode taskNode)throws Exception{
         String redisData = "";
         try {
-            redisData =   redisTemplate.opsForValue().get(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId()).toString();
+            redisData =   msgUtil.redis_get(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId()).toString();
         }catch (Exception e){
             Thread.sleep(1000);
             logger.error("缓存获取异常",e);
@@ -348,7 +332,7 @@ public class TaskWork {
             Thread.sleep(1000);
             return checkLoading(taskNode);
         }else{
-            redisTemplate.delete(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId());
+            msgUtil.redis_del(CacheKey.TASK_CURRENT_NODE_STATUS+"_"+taskNode.getId());
             return "1";
         }
     }
@@ -362,7 +346,7 @@ public class TaskWork {
         }
         String redisData = "";
         try {
-            redisData =   redisTemplate.opsForValue().get(ip+":"+port).toString();
+            redisData =   msgUtil.redis_get(ip+":"+port).toString();
         }catch (Exception e){
             Thread.sleep(1000);
             logger.error("缓存获取异常",e);
@@ -471,19 +455,14 @@ public class TaskWork {
 
 
     public void pushTask(TTask task){
-        Map<String,Object>rmap = new HashMap<>();
+        Map<String,Object> rmap = Maps.newHashMap();
         rmap.put("id",task.getResourceId());
         Map<String,Object> pmap = taskService.getExeTaskByResourceList(rmap);
-        try {
-                Object o = pmap.get("orderList");
-                Map<String,Object >pushMap = new HashMap<>();
-                pushMap.put("type",WebSocketServer.PUSH_TYPE_RESOURCE_TASK);
-                pushMap.put(String.valueOf(task.getResourceId()),pmap);
-                String pushMsg = JSONObject.toJSONString(pushMap);
-                webSocketServer.BroadCastInfo(pushMsg);
-        }catch (Exception e){
-            logger.error("推送异常======"+logger);
-        }
+        Object o = pmap.get("orderList");
+        Map<String,Object >pushMap = new HashMap<>();
+        pushMap.put("type",WebSocketServer.PUSH_TYPE_RESOURCE_TASK);
+        pushMap.put(String.valueOf(task.getResourceId()),pmap);
+        msgUtil.pushTask(task, pushMap);
     }
 
     /**

+ 41 - 18
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/util/MsgUtil.java

@@ -25,6 +25,7 @@ import javax.annotation.Resource;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 @Component
 public class MsgUtil implements ApplicationContextAware {
@@ -48,7 +49,7 @@ public class MsgUtil implements ApplicationContextAware {
     }
 
     //获取容器泛型对象
-    public  static <T> T getBean(Class<T> clazz){
+    public static <T> T getBean(Class<T> clazz){
        return  applicationContext.getBean(clazz);
     }
     //获取容器对象
@@ -58,29 +59,30 @@ public class MsgUtil implements ApplicationContextAware {
 
     public void pushTask(TTask task) {
         //查出所有的线边库数据
+        StockInfoService stockInfoService = (StockInfoService)getBean("StockInfoService");
+        Map<String, Map<String, List<Map>>> stockMap = stockInfoService.demoLineAllStock();
+        Map<String, Object> pushMap = new HashMap<>();
+        pushMap.put("type", WebSocketServer.PUSH_TYPE_STORGE);
+        pushMap.put("data", stockMap);
+        this.pushTask(task, pushMap);
+
+        ZZoneService zoneService = (ZZoneService)getBean("ZZoneService");
+        Map resourceMap = zoneService.demoLineAllResource();
+        pushMap.clear();
+        pushMap.put("type", WebSocketServer.PUSH_TYPE_RESOURCE);
+        pushMap.put("data", resourceMap);
+        this.pushTask(task, pushMap);
+    }
+
+    public void pushTask(TTask task, Map<String, Object> pushMap) {
         try {
-            StockInfoService stockInfoService = (StockInfoService)getBean("StockInfoService");
-            Map<String, Map<String, List<Map>>> stockMap = stockInfoService.demoLineAllStock();
-            Map<String, Object> pushMap = new HashMap<>();
-            pushMap.put("type", WebSocketServer.PUSH_TYPE_STORGE);
-            pushMap.put("data", stockMap);
             String pushMsg = JSONObject.toJSONString(pushMap);
             webSocketServer.BroadCastInfo(pushMsg);
-
-            ZZoneService zoneService = (ZZoneService)getBean("ZZoneService");
-            Map resourceMap = zoneService.demoLineAllResource();
-            pushMap.clear();
-            pushMap.put("type", WebSocketServer.PUSH_TYPE_RESOURCE);
-            pushMap.put("data", resourceMap);
-            pushMsg = JSONObject.toJSONString(pushMap);
-            webSocketServer.BroadCastInfo(pushMsg);
-
-        } catch (Exception e) {
-            logger.error("推送异常======" + logger);
+        }catch (Exception e){
+            logger.error("推送异常======"+logger);
         }
     }
 
-
     /**
      *
      * @param taskNode
@@ -157,4 +159,25 @@ public class MsgUtil implements ApplicationContextAware {
         }
     }
 
+    public void redis_set(String key, Object value){
+        redisTemplate.opsForValue().set(key, value);
+    }
+
+    public void redis_set(String key, Object value, long l, TimeUnit time){
+        redisTemplate.opsForValue().set(key, value, l, time);
+    }
+
+    public Object redis_get(String key){
+        return redisTemplate.opsForValue().get(key);
+    }
+
+    public void redis_del(List<String> keys){
+        redisTemplate.delete(keys);
+    }
+
+    public void redis_del(String key){
+        if(redisTemplate.hasKey(key)){
+            redisTemplate.delete(key);
+        }
+    }
 }