importMain($data,$user); if(! $status) return [false, $msg]; return [true, '']; } //主方法 public function importMain($data,$user){ //获取配置文件 $config = "excel." . $data['type']; $config_array = config($config) ?? []; if(empty($config_array)) return [false, '配置文件不存在']; //(特殊 额外的表头数据) $config_array = $this->getTableTitle($config_array,$user,$data); //获取合并单元格范围 $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径 $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件 $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表 $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围 // 需要导入的公用数据 $msg['user_id'] = $user['id']; $msg['depart_id'] = $this->getDepart($user); $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0; //导入 $import = new Import(); $import->setConfig($config_array, $mergedCells,$msg); \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']); //异常提示报错 if($import->getMsg()) return [false, $import->getMsg()]; return [true, '']; } //表头入口 public function getTableTitle($config_array,$user,$data){ if(! empty($config_array['dynamics_field'])){ $func = $config_array['dynamics_field']['func']; return $this->$func($config_array,$user,$data); } return $config_array; } //产品导入的额外表头 public function productTitle($config_array,$user,$data){ $model = BasicType::TopClear($user,$data); $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray(); if(! empty($result)){ foreach ($result as $value){ $config_array['field'][$value['title']] = [ "key" => $config_array['dynamics_field']['name'], "key_array" => [ "basic_type_id" => $value['id'], "price" => 0, ], "rule" => "", "other_rule" => "is_numeric", "multiple" => true, "map" => [ $value['title'] => "price", ], ]; } } return $config_array; } //产品导入的额外数据 public function fillInsertProductData($time){ $last_insert_data = Product::where('crt_time',$time) ->where('del_time',0) ->select("id","product_category_id") ->get()->toArray(); if(empty($last_insert_data)) return; $list = ProductCategory::where('del_time',0) ->select('id','parent_id') ->get()->toArray(); foreach ($last_insert_data as $value){ $parentsId = $this->findParentIds($value['product_category_id'], $list); array_unshift($parentsId, $value['product_category_id']); $result = array_reverse($parentsId); Product::where('id',$value['id'])->update([ 'product_category' => json_encode($result) ]); } } }