cqp 1 kuukausi sitten
vanhempi
sitoutus
3818efcbd4
1 muutettua tiedostoa jossa 31 lisäystä ja 10 poistoa
  1. 31 10
      app/Service/StatisticsService.php

+ 31 - 10
app/Service/StatisticsService.php

@@ -56,10 +56,11 @@ class StatisticsService extends Service
             $model->whereIn('a.data_id', $data_id);
         }
 
-        return [true, [$model, [
+        $time = $this->getMonthsFirstDayTimestamps([
             0 => $start_time,
             1 => $end_time,
-        ]]];
+        ]);
+        return [true, [$model, $time]];
     }
 
     public function statisticsEmployee($data,$user){
@@ -74,10 +75,8 @@ class StatisticsService extends Service
     }
 
     public function statisticsEmployeeFillData($data, $ergs){
-        $employee_id = array_unique(array_column($data,'data_id'));
         $employee_hours_map = EmployeeDetails::where('del_time', 0)
-            ->whereIn('employee_id', $employee_id)
-            ->whereBetween('time', $ergs)
+            ->whereIn('time', $ergs)
             ->selectRaw('employee_id, SUM(total_hours) as total_hours')
             ->groupBy('employee_id')
             ->pluck('total_hours', 'employee_id')
@@ -192,10 +191,11 @@ class StatisticsService extends Service
             $model->whereIn('a.data_id', $data_id);
         }
 
-        return [true, [$model, [
+        $time = $this->getMonthsFirstDayTimestamps([
             0 => $start_time,
             1 => $end_time,
-        ]]];
+        ]);
+        return [true, [$model, $time]];
     }
 
     public function statisticsDevice($data,$user){
@@ -211,10 +211,8 @@ class StatisticsService extends Service
     }
 
     public function statisticsDeviceFillData($data, $ergs){
-        $employee_id = array_unique(array_column($data,'data_id'));
         $device_hours_map = DeviceDetails::where('del_time', 0)
-            ->whereIn('device_id', $employee_id)
-            ->whereBetween('time', $ergs)
+            ->whereIn('time', $ergs)
             ->selectRaw('device_id, SUM(total_hours) as total_hours')
             ->groupBy('device_id')
             ->pluck('total_hours', 'device_id')
@@ -286,4 +284,27 @@ class StatisticsService extends Service
 
         return $device;
     }
+
+    function getMonthsFirstDayTimestamps(array $ergs)
+    {
+        if (empty($ergs) || count($ergs) < 2) return [];
+
+        $start = (int)$ergs[0];
+        $end   = (int)$ergs[1];
+
+        $timestamps = [];
+
+        // 从开始时间创建 DateTime 对象
+        $current = (new \DateTime())->setTimestamp($start)->modify('first day of this month')->setTime(0, 0, 0);
+
+        // 结束时间
+        $endDate = (new \DateTime())->setTimestamp($end)->modify('first day of this month')->setTime(0, 0, 0);
+
+        while ($current <= $endDate) {
+            $timestamps[] = $current->getTimestamp();
+            $current->modify('+1 month'); // 下个月第一天
+        }
+
+        return $timestamps;
+    }
 }