Explorar o código

瑞都日报填报初始化计算时长

Zimo hai 2 semanas
pai
achega
fe08ecbd05
Modificáronse 1 ficheiros con 35 adicións e 28 borrados
  1. 35 28
      pages/ruiDu/compontents/report-form.vue

+ 35 - 28
pages/ruiDu/compontents/report-form.vue

@@ -424,20 +424,41 @@ const getDefaultReportDate = () =>
 const getDefaultEndDateTime = (reportDate) =>
   dayjs(reportDate).add(1, "day").startOf("day").valueOf();
 
+function getReportDetailDuration(row) {
+  if (!row.reportDate || !row.endDateTime || !row.startTime || !row.endTime) {
+    return 0;
+  }
+
+  const reportDate = dayjs(row.reportDate).format("YYYY-MM-DD");
+  const endDateTime = dayjs(row.endDateTime).format("YYYY-MM-DD");
+  const start = dayjs(`${reportDate} ${row.startTime}`);
+  const end = dayjs(`${endDateTime} ${row.endTime}`);
+
+  let diffMinutes = end.diff(start, "minute");
+
+  if (diffMinutes < 0) {
+    diffMinutes += 1440;
+  }
+
+  return Number((diffMinutes / 60).toFixed(2));
+}
+
 const addReportDetailRow = () => {
   if (!form.reportDetails) {
     form.reportDetails = [];
   }
   const reportDate = getDefaultReportDate();
-  form.reportDetails.push({
+  const row = {
     reportDate,
     endDateTime: getDefaultEndDateTime(reportDate),
     startTime: "08:00",
     endTime: "08:00",
     duration: 0,
-    constructionDetail: "",
-  });
-};
+    constructionDetail: "",
+  };
+  row.duration = getReportDetailDuration(row);
+  form.reportDetails.push(row);
+};
 
 const removeReportDetailRow = (index) => {
   if (index === 0) {
@@ -515,17 +536,19 @@ const formDataFormat = () => {
 
   form.reportDetails = (props.reportData.reportDetails || []).map((item) => {
     const reportDate = formatDateTimestamp(item.reportDate);
-    return {
-      duration: item.duration || 0,
+    const row = {
+      duration: 0,
       constructionDetail: item.constructionDetail || "",
       reportDate,
       endDateTime: item.endDateTime
         ? formatDateTimestamp(item.endDateTime)
         : getDefaultEndDateTime(reportDate),
-      startTime: formatT(item.startTime),
-      endTime: formatT(item.endTime),
+      startTime: formatT(item.startTime) || "08:00",
+      endTime: formatT(item.endTime) || "08:00",
     };
-  });
+    row.duration = getReportDetailDuration(row);
+    return row;
+  });
 
   if (!form.reportDetails.length) {
     addReportDetailRow();
@@ -626,25 +649,9 @@ const handleClickTimeRangeItem = (index) => {
   reportDetailsTimeRangeRef.value.open();
 };
 
-const calculateDuration = (row) => {
-  if (!row.reportDate || !row.endDateTime || !row.startTime || !row.endTime) {
-    row.duration = 0;
-    return;
-  }
-
-  const reportDate = dayjs(row.reportDate).format("YYYY-MM-DD");
-  const endDateTime = dayjs(row.endDateTime).format("YYYY-MM-DD");
-  const start = dayjs(`${reportDate} ${row.startTime}`);
-  const end = dayjs(`${endDateTime} ${row.endTime}`);
-
-  let diffMinutes = end.diff(start, "minute");
-
-  if (diffMinutes < 0) {
-    diffMinutes += 1440;
-  }
-
-  row.duration = Number((diffMinutes / 60).toFixed(2));
-};
+const calculateDuration = (row) => {
+  row.duration = getReportDetailDuration(row);
+};
 
 const handleReportDetailDateChange = (index) => {
   calculateDuration(form.reportDetails[index]);