cqp 3 месяцев назад
Родитель
Сommit
36618937ee

+ 130 - 0
app/Exports/TableHeadExport.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace App\Exports;
+
+use Illuminate\Support\Collection;
+use Maatwebsite\Excel\Concerns\FromCollection;
+use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
+use Maatwebsite\Excel\Concerns\WithHeadings;
+use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
+use Maatwebsite\Excel\Concerns\WithEvents;
+use Maatwebsite\Excel\Events\AfterSheet;
+use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
+use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
+
+class TableHeadExport extends DefaultValueBinder implements
+    WithCustomValueBinder,
+    FromCollection,
+    WithStrictNullComparison,
+    WithHeadings,
+    WithEvents
+{
+    protected $data;
+    protected $headers;
+    protected $headerComments; // 表头批注:键=表头名,值=批注内容
+
+    public function __construct($data, $headers = [], $headerComments = [], $enums = [])
+    {
+        $this->data = $data;
+        $this->headers = $headers;
+        $this->headerComments = $headerComments;
+        $this->enums = $enums; // 例如:['性别' => ['男', '女'], '状态' => ['启用', '禁用']]
+    }
+
+    public function collection()
+    {
+        return new Collection($this->createData());
+    }
+
+    public function createData()
+    {
+        return $this->export();
+    }
+
+    public function headings(): array
+    {
+        return $this->headers;
+    }
+
+    private function export()
+    {
+        $list = [];
+        foreach ($this->data as $v) {
+            $list[] = $v;
+        }
+        return $list;
+    }
+
+    public function registerEvents(): array
+    {
+        return [
+            AfterSheet::class => function(AfterSheet $event) {
+                if (empty($this->headerComments)) {
+                    return; // 不传就不加批注
+                }
+
+                $sheet = $event->sheet->getDelegate();
+
+                // 1. 获取总列数
+                $columnCount = count($this->headers);
+
+                // 2. 循环设置每一列的宽度
+                for ($i = 1; $i <= $columnCount; $i++) {
+                    $columnLetter = Coordinate::stringFromColumnIndex($i);
+
+                    // 设置固定宽度,例如 20
+                    $sheet->getColumnDimension($columnLetter)->setWidth(25);
+
+                    // 或者设置为自动宽度(根据内容自适应)
+                    // $sheet->getColumnDimension($columnLetter)->setAutoSize(true);
+                }
+
+                // --- 2. 核心:添加下拉枚举 (新逻辑) ---
+                foreach ($this->headers as $index => $headerName) {
+                    if (!isset($this->enums[$headerName])) continue;
+
+                    $columnLetter = Coordinate::stringFromColumnIndex($index + 1);
+                    $options = $this->enums[$headerName];
+
+                    // 设置下拉范围,例如从第 2 行到第 1000 行
+                    $range = "{$columnLetter}2:{$columnLetter}1000";
+
+                    $validation = $sheet->getDataValidation($range);
+                    $validation->setType(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
+                    $validation->setErrorStyle(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_STOP);
+                    $validation->setAllowBlank(true);
+                    $validation->setShowInputMessage(true);
+                    $validation->setShowErrorMessage(true);
+                    $validation->setShowDropDown(true);
+                    $validation->setErrorTitle('输入错误');
+                    $validation->setError('请从下拉列表中选择有效选项');
+                    $validation->setPromptTitle('选择提示');
+                    $validation->setPrompt('请选择一个预设值');
+
+                    // 将数组转为 Excel 识别的格式:"项1,项2,项3"
+                    $validation->setFormula1('"' . implode(',', $options) . '"');
+                }
+
+                foreach ($this->headers as $index => $headerName) {
+                    if (!isset($this->headerComments[$headerName])) {
+                        continue; // 该表头没设置批注
+                    }
+
+                    $columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1);
+                    $cell = $columnLetter . '1';
+
+                    $commentText = $this->headerComments[$headerName];
+
+                    // 创建批注
+                    $comment = $sheet->getComment($cell);
+                    $comment->getText()->createTextRun($commentText);
+                    $comment->setAuthor('系统提示');
+
+                    // 设置批注框大小(可调整数值)
+                    $comment->setWidth('200pt');   // 宽度
+                    $comment->setHeight('100pt');  // 高度
+                }
+            }
+        ];
+    }
+}

+ 68 - 0
app/Import/ImportAll.php

@@ -0,0 +1,68 @@
+<?php
+
+
+namespace  App\Import;
+
+use App\Service\ImportService;
+use Maatwebsite\Excel\Concerns\ToArray;
+use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
+
+class ImportAll implements ToArray,WithCalculatedFormulas {
+
+    private $msg = '';
+    public $crt_id = 0;
+    public $type = "";
+    public $user = [];
+    public $is_long_text = false;
+    public $other_param = [];
+
+    public function array (array $array){
+        $this->handleData($array);
+    }
+
+    public function setCrt($crt_id){
+        $this->crt_id = $crt_id;
+    }
+
+    public function setType($type){
+        $this->type = $type;
+    }
+
+    public function setUser($user){
+        $this->user = $user;
+    }
+
+    public function setOtherParam($param){
+        $this->other_param = $param;
+    }
+
+    public function getMsg(){
+        return $this->msg;
+    }
+
+    public function setMsg($msg){
+        $this->msg = $msg;
+    }
+
+    public function setIsLongText($bool = true){
+        $this->is_long_text = $bool;
+    }
+
+    public function getIsLongText(){
+        return $this->is_long_text;
+    }
+
+    public function handleData (array $array) {
+        $func = $this->type . "Import";
+        if(! $func) {
+            $this->setMsg("意外错误");
+            return;
+        }
+
+        list($status,$msg) = (new ImportService())->$func($array,$this->user,$this->other_param);
+        if(empty($status)) {
+            if($status === 0) $this->setIsLongText();
+            $this->setMsg($msg);
+        }
+    }
+}

+ 0 - 45
app/Service/ExportFileService.php

@@ -139,51 +139,6 @@ class ExportFileService extends Service
         return [true, $this->saveExportData($return,$header)];
     }
 
-    public function two($ergs,$user){
-        // 导出数据
-        $return = [];
-
-        $header_default = $user['e_header_default'];
-        $column = array_column($header_default,'key');
-        $header = array_column($header_default,'value');
-
-        $service = new ItemService();
-        $model = $service->itemCommon($ergs, $user);
-        $model->chunk(500,function ($data) use(&$return,$column,$service){
-            $data = $data->toArray();
-
-            $list['data'] = $data;
-            $list = $service->fillData($list);
-
-            $this->fillData($list['data'], $column, $return);
-        });
-
-        return [true, $this->saveExportData($return,$header)];
-    }
-
-    public function three($ergs,$user){
-        // 导出数据
-        $return = [];
-        $header_default = $user['e_header_default'];
-        $column = array_column($header_default,'key');
-        $header = array_column($header_default,'value');
-
-        $service = new DeviceService();
-        $model = $service->deviceCommon($ergs, $user);
-
-        $model->chunk(500,function ($data) use(&$return, $service, $column){
-            $data = $data->toArray();
-            $list['data'] = $data;
-
-            //订单数据
-            $list = $service->fillData($list);
-            //返回数据
-            $this->fillData($list['data'], $column, $return);
-        });
-
-        return [true, $this->saveExportData($return,$header)];
-    }
-
     public function four($ergs,$user){
         // 导出数据
         $return = [];

+ 5 - 4
app/Service/ItemService.php

@@ -279,16 +279,17 @@ class ItemService extends Service
         if(empty($data['data'])) return $data;
 
         $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
-        $map = $this->getDetailsMap(array_column($data['data'],'id'));
+        $map = [];
+        if($is_export) $map = $this->getDetailsMap(array_column($data['data'],'id'));
         foreach ($data['data'] as $key => $value){
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
             $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
             $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
             $data['data'][$key]['state_title'] = Item::State_Type[$value['state']] ?? "";
-            $tmp = $map[$value['id']];
-            $data['data'][$key]['man_list'] = $tmp['man'];
-            $data['data'][$key]['device_list'] = $tmp['device'];
+            $tmp = $map[$value['id']] ?? [];
+            $data['data'][$key]['man_list'] = $tmp['man'] ?? "";
+            $data['data'][$key]['device_list'] = $tmp['device'] ?? "";
         }
 
         return $data;