gogs 2 месяцев назад
Родитель
Сommit
df959af31a
1 измененных файлов с 18 добавлено и 12 удалено
  1. 18 12
      app/Service/StatisticService.php

+ 18 - 12
app/Service/StatisticService.php

@@ -56,31 +56,37 @@ class StatisticService extends Service
         $item = Item::Clear($user, $data);
         $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
         $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
+        $collect = collect($month_employee_list);
+
+        $employee_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['employee_id'] . '_' . $item['order_date'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+        $employee_work_count = $collect->groupBy(fn($item) => $item['employee_id'] . '_' . $item['order_date'])
+            ->map(fn($group) => round($group->sum('total_work') / 60, 2))
+            ->toArray();
         $collection = collect($month_employee_list);
-        $count = $collection->count();
-        $total_minutes = $collection->sum('total_work');
-        // 算出目标总工时(绝对标准)
-        $absolute_total_hours = round($total_minutes / 60, 2);
-
         // 注意:这里使用 transform,参数名建议写准确
         // 关键点:use 后面加了 & 符号
-        $month_employee_list = $collection->transform(function ($item, $index) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$absolute_total_hours, $count) {
+        $month_employee_list = $collection->transform(function ($item, $index) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_count) {
 
             $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
             $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
 
             // 如果不是最后一条
-            if ($index < $count - 1) {
+            $key = $item['employee_id'].'_'.$item['order_date'];
+            if(--$employee_count[$key] > 0){
                 $current_hours = round($item['total_work'] / 60, 2);
                 $item['total_work_hours'] = $current_hours;
                 // 关键:递减外部的 $absolute_total_hours
-                $absolute_total_hours = round($absolute_total_hours - $current_hours, 2);
-            } else {
-                // 最后一条:直接拿剩下的“余额”
-                $item['total_work_hours'] = $absolute_total_hours;
-            }
+                $employee_work_count[$key] -= $current_hours;
 
+            }else{
+                $item['total_work_hours'] =   $employee_work_count[$key];
+            }
             return $item;
         })->all();
         return [true, $month_employee_list];