From e35963c6c457a46c11309ddac7458481b586737e Mon Sep 17 00:00:00 2001
From: songshankun <songshankun@foxmail.com>
Date: 星期四, 26 十月 2023 11:51:31 +0800
Subject: [PATCH] feat: 工序运行时间取值调整

---
 src/views/visualization.vue |  109 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 36 deletions(-)

diff --git a/src/views/visualization.vue b/src/views/visualization.vue
index 4e950da..86e6442 100644
--- a/src/views/visualization.vue
+++ b/src/views/visualization.vue
@@ -53,7 +53,7 @@
         >
         <template v-if="taskData">
           <div class="gong-date">
-            宸ュ簭杩愯鏃堕棿锛歿{ getFormatTime(taskData.Procedure.startTime) }}
+            宸ュ簭杩愯鏃堕棿锛歿{ getTaskRunningTime(taskData.Procedure.realStartTime,taskData.Procedure.realEndTime) }}
           </div>
           <div class="statelist">
             <span class="title-item"
@@ -145,9 +145,9 @@
                   {{ taskData.Procedure.procedure.workHours || "--" }}
                 </el-descriptions-item>
                 <el-descriptions-item label="璁″垝鏃堕棿"
-                  >{{ formatDate(taskData.Order.startTime) || "--" }}
+                  >{{ formatDate(taskData.Procedure.startTime) || "--" }}
                   -
-                  {{ formatDate(taskData.Order.endTime) }}</el-descriptions-item
+                  {{ formatDate(taskData.Procedure.endTime) }}</el-descriptions-item
                 >
                 <el-descriptions-item label="閫氶亾">
                   {{ channelNameConfig[taskData.Channel] || "--" }}
@@ -803,6 +803,18 @@
     }
   },
   methods: {
+    newTaskPlaceholder(channel){
+      return {
+        Order: {},
+        Procedure: {
+          procedure: { inputMaterials: [], outputMaterials: [], workers: [] },
+        },
+        Channel:channel,
+        inputMaterials: [],
+        outputMaterials: [],
+        procedureList: [],
+      }
+    },
     /**
      * 灞曠ず宸ュ簭鐘舵��
      * 濡傛灉PLC鐨勭姸鎬佹槸寰呮満瑕佽窡闅廝LC鐘舵��
@@ -890,7 +902,7 @@
               }
             }
             this.resResult[i]=res;
-            console.log( this.resResult[i],'===res111')
+            // console.log( this.resResult[i],'===res111')
           })
         }
       }
@@ -1046,23 +1058,55 @@
       let now = new Date();
       this.formatTime2 = this.getDateObj(now, "yyyy骞碝M鏈坉d鏃�");
     },
-    getFormatTime(date){
-      const getCha=()=>{
-        let now=new Date().getTime()
-        let String=now-date*1000;
-        let seconds=Math.floor(String/1000)
-        let minutes=Math.floor(seconds/60)
-
-        let days=Math.floor(String/1000/60/60/24)
-        let hours=Math.floor(minutes/60)-days*24
-        let m=minutes-days*24*60-hours*60
-        return days+'澶�'+hours+'鏃�'+m+'鍒�'
+    /**
+     * 璁$畻鏃堕棿娈�, 娉ㄦ剰鍙傛暟瑕佹眰鏃堕棿鎴充负 JS Date 鐨�13浣嶆椂闂存埑
+     * @param {number} startTime
+     * @param {number} endTime
+     * @return {string}
+     */
+    calcRunningDuration(startTime,endTime){
+      let timeDuration=endTime-startTime;
+      if (timeDuration<0){
+        return '0澶�0鏃�0鍒�'
       }
-      let d= getCha()
+
+      let seconds=Math.floor(timeDuration/1000)
+      let minutes=Math.floor(seconds/60)
+
+      let days=Math.floor(timeDuration/1000/60/60/24)
+      let hours=Math.floor(minutes/60)-days*24
+      let m=minutes-days*24*60-hours*60
+      return `${days}澶�${hours}鏃�${m}鍒哷
+    },
+
+    /**
+     * 宸ュ簭杩愯鏃堕棿
+     * 鏍规嵁鎺ュ彛杩斿洖 realStartTime realEndTime 鍒ゆ柇濡備綍灞曠ず
+     * 濡傛灉 realEndTime 涓� 0 鍒� 鐢ㄥ綋鍓嶆椂闂� - realStartTime
+     * 濡傛灉 瀛樺湪 realEndTime, 鍒� realEndTime - realStartTime
+     * 娉ㄦ剰 realStartTime realEndTime 鏄帴鍙h繑鍥炵殑10浣嶆椂闂存埑
+     * @param realStartTime
+     * @param realEndTime
+     * @return {string}
+     */
+    getTaskRunningTime(realStartTime,realEndTime){
+      const getInfo = ()=>{
+        if (!realStartTime && !realEndTime){
+          return '--'
+        }
+        if(!realEndTime){
+          const now = Math.floor(new Date().getTime()/1000) * 1000
+          return this.calcRunningDuration(realStartTime*1000,now)
+        }
+
+        return this.calcRunningDuration(realStartTime*1000,realEndTime*1000)
+      }
+
+      let result = getInfo()
       setInterval(() => {
-       d= getCha();
+        result = getInfo();
       }, 6000);
-      return date?d:'--';
+      return result;
     },
     setInterCard(value, name, i) {
       if (this.TasksCopy[i].Procedure.procedure[value]!=null) {
@@ -1146,29 +1190,22 @@
       this.TasksCopy=[]
       getTaskInfo({taskMode:2}).then((res) => {
         if (res.code == 200&&res.data) {
-          // let arr=[]
-          if(res.data.Tasks){
-            res.data.Tasks.sort(function(a,b){
-            return (a.Channel - b.Channel)
-           })
-          }
-          this.TasksCopy = res.data.Tasks ? res.data.Tasks : [];
+          this.TasksCopy = res.data?.Tasks ?? [];
           // this.safeProduce=res.data.Prompt.safeProduce?res.data.Prompt.safeProduce:''
-          this.plcNotConnected=res.data.Prompt.plcNotConnected?res.data.Prompt.plcNotConnected:''
-          this.ChannelAmount=res.data.ChannelAmount?res.data.ChannelAmount:1
+          this.plcNotConnected=res.data.Prompt.plcNotConnected ?? ''
+          this.ChannelAmount=res.data.ChannelAmount ?? 1
           // if(this.ChannelAmount>1 &&this.TasksCopy.length<2){
           //   this.TasksCopy.push(this.object)
           // }
-          this.TaskCount=res.data.TaskCount?res.data.TaskCount:0
+          this.TaskCount=res.data.TaskCount ?? 0
 
-          if ((this.TasksCopy.length ==0 )||!this.TasksCopy) {
-            if (this.activeName == 1||this.activeName == 3) {
-              this.TasksCopy.push(this.object);
-            } else {
-              this.TasksCopy.push(this.object);
-              this.TasksCopy.push(this.object);
-            }
-          }
+          // 鎵惧嚭缂哄皯浠诲姟鏁版嵁鐨勯�氶亾, 浣跨敤鍗犱綅鏁版嵁鍗犱綇瀵瑰簲閫氶亾
+          const channelNumbers = this.TasksCopy.map(task=>task.Channel)
+          const allChannels = [0,1];
+          const lackChannels = _.chain(allChannels).difference(channelNumbers).value()
+          lackChannels.forEach(channel => this.TasksCopy.push(this.newTaskPlaceholder(channel)))
+          this.TasksCopy.sort((a,b)=>a.Channel - b.Channel)
+
           this.workers=res.data.workers?res.data.workers:[]
 
           // 灏嗗�肩彮浜烘牴鎹�氶亾鍙峰垎缁�

--
Gitblit v1.8.0