From 54849757852f6ab40eb17afbd03d1d839b60a38d Mon Sep 17 00:00:00 2001
From: guoshilong <123456>
Date: Mon, 13 Nov 2023 17:09:15 +0800
Subject: [PATCH] 重复定时和连续执行

---
 src/components/task/CreatePlan.vue |  268 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 185 insertions(+), 83 deletions(-)

diff --git a/src/components/task/CreatePlan.vue b/src/components/task/CreatePlan.vue
index b53d588..6fc96bd 100644
--- a/src/components/task/CreatePlan.vue
+++ b/src/components/task/CreatePlan.vue
@@ -105,41 +105,26 @@
         <a-form-item label="执行日期"
                      v-if="planBody.task_type === TaskType.Repeat || planBody.task_type === TaskType.Continuous"
                      name="time_range" :labelCol="{ span: 23 }">
-          <a-range-picker v-model:value="planBody.time_range" format="YYYY-MM-DD" placeholder="请选择日期"/>
+          <a-range-picker v-model:value="planBody.time_range" format="YYYY-MM-DD"
+                          :placeholder="['开始时间','结束时间']"/>
         </a-form-item>
 
         <!-- 执行时间 (重复定时)-->
-        <a-form-item label="执行时间" v-if="planBody.task_type === TaskType.Repeat" name="execute_time_arr"
-                     :labelCol="{ span: 23 }">
-          <div class="execute-time-arr-item" v-for="(item,index) in executeTimeRepeatArr" :key="index">
-            <a-time-picker v-model:value="item.value" format="HH:mm"/>
-            <a-button @click="addTime" type="default" shape="circle" :size="componentOptions.size">
-              <template #icon>
-                <PlusCircleOutlined/>
-              </template>
-            </a-button>
-
-            <a-button @click="deleteTime(item)" type="default" shape="circle" :size="componentOptions.size">
-              <template #icon>
-                <MinusCircleOutlined/>
-              </template>
-            </a-button>
-          </div>
-        </a-form-item>
-
-        <!-- 执行时间 (连续执行)-->
-        <a-form-item label="执行时间" v-if="planBody.task_type === TaskType.Continuous" :labelCol="{ span: 23 }">
-          <div class="execute-time-arr-continuous-item" v-for="(item,index) in executeTimeContinuousArr" :key="index">
-            <div class="time-select">
-              <div class="time-item">
-                <a-time-picker style="width: 100px;" v-model:value="item.value[0]" format="HH:mm"/>
-              </div>
-              <div style="color: white">-</div>
-              <div class="time-item">
-                <a-time-picker style="width: 100px;" v-model:value="item.value[1]" format="HH:mm"/>
-              </div>
-            </div>
-            <div class="btn-group">
+        <div v-if="planBody.task_type === TaskType.Repeat">
+          <div class="execute-time-arr-item">
+            <a-form-item
+                v-for="(item, index) in planBody.executeTimeRepeatArr"
+                :key="item.index"
+                :label="index === 0 ? '执行时间' : ''"
+                :name="item.index+'repValue'"
+                :labelCol="{ span: 23 }"
+                :rules="[{
+                required: true,
+                trigger: ['change','blur'],
+                validator: validateRepeatExecuteTime
+              }]"
+            >
+              <a-time-picker v-model:value="item.value" @change="onRepeatChange(item)" format="HH:mm"/>
               <a-button @click="addTime" type="default" shape="circle" :size="componentOptions.size">
                 <template #icon>
                   <PlusCircleOutlined/>
@@ -150,19 +135,63 @@
                   <MinusCircleOutlined/>
                 </template>
               </a-button>
-            </div>
+            </a-form-item>
           </div>
-        </a-form-item>
+        </div>
 
-        <!-- 重复频率-->
+        <!-- 执行时间 (连续执行)-->
+        <div v-if="planBody.task_type === TaskType.Continuous">
+          <a-form-item
+              v-for="(item, index) in planBody.executeTimeContinuousArr"
+              :key="item.index"
+              :label="index === 0 ? '执行时间' : ''"
+              :name="item.index+'conValue'"
+              :labelCol="{ span: 23 }"
+              :rules="{
+                required: true,
+                trigger: ['change', 'blur'],
+               validator: validateContinuousExecuteTime
+              }"
+          >
+
+            <div class="execute-time-arr-continuous-item">
+              <div class="time-select">
+                <div class="time-item">
+                  <a-time-picker style="width: 100px;" v-model:value="item.value[0]" @change="onContinuousChange(item)"
+                                 format="HH:mm"/>
+                </div>
+                <div style="color: white">-</div>
+                <div class="time-item">
+                  <a-time-picker style="width: 100px;" v-model:value="item.value[1]" @change="onContinuousChange(item)"
+                                 format="HH:mm"/>
+                </div>
+              </div>
+              <div class="btn-group">
+                <a-button @click="addTime" type="default" shape="circle" :size="componentOptions.size">
+                  <template #icon>
+                    <PlusCircleOutlined/>
+                  </template>
+                </a-button>
+                <a-button @click="deleteTime(item)" type="default" shape="circle" :size="componentOptions.size">
+                  <template #icon>
+                    <MinusCircleOutlined/>
+                  </template>
+                </a-button>
+              </div>
+            </div>
+          </a-form-item>
+        </div>
+
+        <!-- 任务开始执行的电量-->
         <a-form-item label="任务开始执行的电量" v-if=" planBody.task_type === TaskType.Continuous"
-                     name="battery_capacity" :labelCol="{ span: 23 }">
-          <a-input style="background: black;" v-model:value="planBody.battery_capacity"/>
+                     name="min_battery_capacity" :labelCol="{ span: 23 }">
+          <a-input style="background: black;" v-model:value="planBody.min_battery_capacity"/>
         </a-form-item>
 
         <!-- 重复频率-->
         <a-form-item label="重复频率"
                      v-if="planBody.task_type === TaskType.Repeat || planBody.task_type === TaskType.Continuous"
+                     name="rep_fre_val"
                      :labelCol="{ span: 23 }">
           <div class="rep-fre-item">
             <div>每</div>
@@ -186,7 +215,7 @@
           </a-button>
         </a-form-item>
 
-        <!-- 重复频率-->
+        <!-- 重复规则-->
         <a-form-item label="重复规则"
                      v-if="(planBody.task_type === TaskType.Repeat || planBody.task_type === TaskType.Continuous) && planBody.rep_fre_type === FrequencyType.month"
                      :labelCol="{ span: 23 }">
@@ -254,32 +283,32 @@
 </template>
 
 <script lang="ts" setup>
-import { computed, onMounted, onUnmounted, reactive, ref, toRaw, UnwrapRef } from 'vue'
+import { computed, reactive, ref } from 'vue'
 import {
-  CloseOutlined,
-  RocketOutlined,
   CameraFilled,
-  UserOutlined,
+  CloseOutlined,
+  MinusCircleOutlined,
   PlusCircleOutlined,
-  MinusCircleOutlined
+  RocketOutlined,
+  UserOutlined
 } from '@ant-design/icons-vue'
 import { ELocalStorageKey, ERouterName } from '/@/types'
 import { useMyStore } from '/@/store'
-import { WaylineType, WaylineFile } from '/@/types/wayline'
+import { WaylineFile } from '/@/types/wayline'
 import { Device, EDeviceType } from '/@/types/device'
-import { createPlan, CreatePlan } from '/@/api/wayline'
+import { createPlan, CreatePlan, createPlanCondition } from '/@/api/wayline'
 import { getRoot } from '/@/root'
 import {
-  TaskType,
-  OutOfControlActionOptions,
-  OutOfControlAction,
-  TaskTypeOptions,
-  FrequencyTypeOptions,
   FrequencyType,
-  RepeatRuleTypeOptions,
+  FrequencyTypeOptions,
+  OutOfControlAction,
+  OutOfControlActionOptions,
   RepeatRuleType,
-  WhichWeekOptions,
-  WhichDayOptions
+  RepeatRuleTypeOptions,
+  TaskType,
+  TaskTypeOptions,
+  WhichDayOptions,
+  WhichWeekOptions
 } from '/@/types/task'
 import moment, { Moment } from 'moment'
 import { RuleObject } from 'ant-design-vue/es/form/interface'
@@ -318,14 +347,35 @@
   rep_fre_val: 1,
   rep_fre_type: 3,
   rep_rule_type: 1,
+
+  executeTimeContinuousArr: [{ index: 1, value: [] }],
+  executeTimeRepeatArr: [{ index: 1, value: '' }]
 })
 
-const executeTimeRepeatArr = ref([
-  { index: 1, value: '' }
-])
-const executeTimeContinuousArr = ref([
-  { index: 1, value: [] }
-])
+watch(
+  planBody.executeTimeRepeatArr,
+  (newVal) => {
+    if (newVal.length) {
+      newVal.forEach(item => {
+        planBody[`${item.index}repValue`] = item.value
+      })
+    }
+  },
+  { deep: true, immediate: true }
+)
+
+watch(
+  planBody.executeTimeContinuousArr,
+  (newVal) => {
+    if (newVal.length) {
+      newVal.forEach(item => {
+        planBody[`${item.index}conValue`] = item.value
+      })
+    }
+  },
+  { deep: true, immediate: true }
+)
+
 // 重复规则为按星期时
 const repeatRuleValueWeek = ref([1, 1])
 // 重复规则为按日期时
@@ -385,35 +435,74 @@
       },
     }
   ],
-  battery_capacity: [
+  min_battery_capacity: [
     {
       validator: async (rule: RuleObject, value: string) => {
         if (!/^[0-9]{1,}$/.test(value)) {
-          planBody.battery_capacity = ''
+          planBody.min_battery_capacity = ''
           throw new Error('最小执行电量不能为空')
         } else if (value < 50) {
-          planBody.battery_capacity = 50
-        } else if (value > 100) {
-          planBody.battery_capacity = 100
+          planBody.min_battery_capacity = 50
+        } else if (value > 90) {
+          planBody.min_battery_capacity = 90
         }
       },
       trigger: 'blur'
     }
   ],
   out_of_control_action: [{ required: true, message: '请选择航线飞行中失联' }],
+  rep_fre_val: [
+    {
+      validator: async (rule: RuleObject, value: string) => {
+        if (!/^[0-9]{1,}$/.test(value)) {
+          planBody.rep_fre_val = ''
+          throw new Error('重复频率不能为空')
+        }
+      },
+      trigger: ['blur', 'change']
+    }
+  ]
+}
+
+function validateContinuousExecuteTime (rule: RuleObject, value: Moment[]) {
+  if (!value[0]) {
+    return Promise.reject(new Error('请选择执行时间'))
+  } else if (value.length < 2) {
+    return Promise.reject(new Error('请选择执行结束时间'))
+  } else if (value[1].isBefore(value[0])) {
+    return Promise.reject(new Error('执行结束时间不能小于执行开始时间'))
+  } else {
+    return Promise.resolve()
+  }
+}
+
+function validateRepeatExecuteTime (rule: RuleObject, value: any) {
+  if (!value) {
+    return Promise.reject(new Error('请选择执行时间'))
+  } else {
+    return Promise.resolve()
+  }
+}
+
+function onRepeatChange (item: any) {
+  planBody[`${item.index}repValue`] = item.value
+}
+
+function onContinuousChange (item: any) {
+  planBody[`${item.index}conValue`] = item.value
 }
 
 // 重复定时添加执行时间
 function addTime () {
   if (planBody.task_type === TaskType.Repeat) {
-    const index = executeTimeRepeatArr.value[executeTimeRepeatArr.value.length - 1].index
-    executeTimeRepeatArr.value.push({
+    const index = planBody.executeTimeRepeatArr[planBody.executeTimeRepeatArr.length - 1].index
+    planBody.executeTimeRepeatArr.push({
       index: index + 1,
       value: ''
     })
   } else if (planBody.task_type === TaskType.Continuous) {
-    const index = executeTimeContinuousArr.value[executeTimeContinuousArr.value.length - 1].index
-    executeTimeContinuousArr.value.push({
+    const index = planBody.executeTimeContinuousArr[planBody.executeTimeContinuousArr.length - 1].index
+    planBody.executeTimeContinuousArr.push({
       index: index + 1,
       value: []
     })
@@ -423,9 +512,9 @@
 // 删除执行时间
 function deleteTime (item: any) {
   if (planBody.task_type === TaskType.Repeat) {
-    executeTimeRepeatArr.value = executeTimeRepeatArr.value.filter(e => e.index !== item.index)
+    planBody.executeTimeRepeatArr = planBody.executeTimeRepeatArr.filter(e => e.index !== item.index)
   } else if (planBody.task_type === TaskType.Continuous) {
-    executeTimeContinuousArr.value = executeTimeContinuousArr.value.filter(e => e.index !== item.index)
+    planBody.executeTimeContinuousArr = planBody.executeTimeContinuousArr.filter(e => e.index !== item.index)
   }
 }
 
@@ -448,9 +537,7 @@
         createPlanBody.task_days = [moment(planBody.time_range[0]).unix(), moment(planBody.time_range[1]).unix()]
         createPlanBody.task_periods = []
         // 执行时间
-        executeTimeRepeatArr.value.forEach(item => {
-          console.log(item, '------')
-
+        planBody.executeTimeRepeatArr.forEach(item => {
           createPlanBody.task_periods.push([moment(item.value).unix()])
         })
 
@@ -466,11 +553,10 @@
         } else if (planBody.rep_fre_type === FrequencyType.week) {
           createPlanBody.rep_rule_val = repeatFrequencyValueWeek.value
         }
-        console.log(createPlanBody, '*******************')
 
         break
       case TaskType.Continuous:
-        createPlanBody.task_type = 2
+        // createPlanBody.task_type = 2
         createPlanBody.task_days = [moment(planBody.time_range[0]).unix(), moment(planBody.time_range[1]).unix()]
 
         if (planBody.rep_fre_type === FrequencyType.month) {
@@ -487,7 +573,7 @@
         }
         createPlanBody.task_periods = []
         // 执行时间
-        executeTimeContinuousArr.value.forEach(item => {
+        planBody.executeTimeContinuousArr.forEach(item => {
           createPlanBody.task_periods.push([moment(item.value[0]).unix(), moment(item.value[1]).unix()])
         })
 
@@ -499,14 +585,30 @@
       createPlanBody.wayline_type = wayline.value.template_types[0]
     }
 
-    // getFlyTimeList(createPlanBody)
+    if (planBody.task_type === TaskType.Immediate || planBody.task_type === TaskType.Timed) {
+      createPlan(workspaceId, createPlanBody)
+        .then(res => {
+          disabled.value = false
+        }).finally(() => {
+          closePlan()
+        })
+    } else {
+      createPlanBody.execute_start_time_arr = getFlyTimeList(createPlanBody)
+      console.log(createPlanBody, '-----------------------')
 
-    createPlan(workspaceId, createPlanBody)
-      .then(res => {
-        disabled.value = false
-      }).finally(() => {
-        closePlan()
-      })
+      if (planBody.task_type === TaskType.Repeat) {
+        createPlanBody.task_type = 1
+      } else if (planBody.task_type === TaskType.Continuous) {
+        createPlanBody.task_type = 2
+      }
+
+      createPlanCondition(workspaceId, createPlanBody)
+        .then(res => {
+          disabled.value = false
+        }).finally(() => {
+          closePlan()
+        })
+    }
   }).catch((e: any) => {
     console.log('validate err', e)
   })

--
Gitblit v1.9.3