| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | 
							- <?php
 
- namespace App\Service;
 
- use App\Import\Import;
 
- use App\Model\BasicType;
 
- use App\Model\Product;
 
- use App\Model\ProductCategory;
 
- use PhpOffice\PhpSpreadsheet\IOFactory;
 
- class ImportService extends Service
 
- {
 
-     public static $type = [
 
-         'product', //产品
 
-     ];
 
-     //导入入口
 
-     public function import($data,$user){
 
-         if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
 
-         if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
 
-         if(empty($data['file'])) return [false,'导入文件不能为空'];
 
-         //导入的数据并且校验写入
 
-         list($status,$msg) = $this->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)
 
-             ]);
 
-         }
 
-     }
 
- }
 
 
  |