cqp 13 godzin temu
rodzic
commit
614effe6a2
1 zmienionych plików z 70 dodań i 9 usunięć
  1. 70 9
      app/Service/ExportFileService.php

+ 70 - 9
app/Service/ExportFileService.php

@@ -4,6 +4,7 @@ namespace App\Service;
 
 use App\Exports\ExportOrder;
 use App\Exports\MultiSheetExport;
+use App\Model\Employee;
 use App\Model\Quantization;
 use App\Model\RevenueCost;
 use Maatwebsite\Excel\Facades\Excel;
@@ -81,21 +82,81 @@ class ExportFileService extends Service
         if(! $status) return [false, $model];
 
         // 导出数据
-        $return = [];
-
         $header_default = $user['e_header_default'];
         $column = array_column($header_default,'key');
         $header = array_column($header_default,'value');
-        $model->chunk(500,function ($data) use(&$return,$column, $service, $user){
-            $data = $data->toArray();
-            $list['data'] = $data;
 
-            $list = $service->fillCreateData($list, $user);
-            //返回数据
-            $this->fillData($list['data'], $column, $return);
+        $detailKeys = ['a', 'b', 'c', 'd', 'e', 'f'];
+        $detailLabels = ['维度名称', '维度总分', '总得分', '维度详细名称', '分数', '打分比例'];
+
+        $fullColumn = array_merge($column, $detailKeys);
+        $fullHeader = array_merge($header, $detailLabels);
+
+        // 2. 导出逻辑(建议直接返回文件流或路径)
+        $exportData = [];
+
+        $employeeMap = Employee::pluck('emp_name', 'id')
+            ->toArray();
+
+        $model->with('details')->chunk(200, function ($items) use (&$exportData, $fullColumn, $employeeMap) {
+            foreach ($items as $item) {
+                $mainRow = $item->toArray();
+                $mainRow['crt_name'] = $employeeMap[$mainRow['crt_id']] ?? "";
+                $mainRow['crt_time'] =  $mainRow['crt_time'] ? date('Y-m-d H:i:s', $mainRow['crt_time']) : '';
+                // 如果该主行没有明细,也要保底导出一行
+                if ($item->details->isEmpty()) {
+                    $exportData[] = $this->mapRowToColumn($mainRow, $fullColumn);
+                    continue;
+                }
+
+                // 1. 先取出所有顶级父项 (parent_id == 0)
+                $parentDetails = $item->details->where('parent_id', 0);
+
+                foreach ($parentDetails as $parent) {
+                    // --- A. 写入父级行 ---
+                    $parentRow = $mainRow;
+                    $parentRow['a'] = $parent->title;
+                    $parentRow['b'] = $parent->quantization_score;
+                    $parentRow['c'] = $parent->score;
+                    $parentRow['d'] = '';
+                    $parentRow['e'] = '';
+                    $parentRow['f'] = '';
+
+                    $exportData[] = $this->mapRowToColumn($parentRow, $fullColumn);
+
+                    // --- B. 紧接着查找并写入该父级下的所有子级 ---
+                    $children = $item->details->where('parent_id', $parent->id);
+
+                    foreach ($children as $child) {
+                        $childRow = $mainRow;
+                        // 子级行:a, b, c 字段可以保留父级信息(方便对账),或者留空
+                        $childRow['a'] = $parent->title;
+                        $childRow['b'] = $parent->quantization_score;
+                        $childRow['c'] = $parent->score;
+
+                        // 填充子级特有信息到 d, e, f 列
+                        $childRow['d'] = $child->title;
+                        $childRow['e'] = $child->score;
+                        $childRow['f'] = $child->rate;
+
+                        $exportData[] = $this->mapRowToColumn($childRow, $fullColumn);
+                    }
+                }
+            }
         });
 
-        return [true, $this->saveExportData($return,$header)];
+        return [true, $this->saveExportData($exportData, $fullHeader)];
+    }
+
+    /**
+     * 辅助方法:确保每行数据字段顺序与表头完全一致
+     */
+    private function mapRowToColumn($data, $columns) {
+        $res = [];
+        foreach ($columns as $col) {
+            $res[$col] = $data[$col] ?? '';
+        }
+        return $res;
     }
 
     public function two($ergs, $user){