ImportService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Service;
  3. use App\Import\Import;
  4. use App\Model\BasicType;
  5. use App\Model\Product;
  6. use App\Model\ProductCategory;
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. class ImportService extends Service
  9. {
  10. public static $type = [
  11. 'product', //产品
  12. ];
  13. //导入入口
  14. public function import($data,$user){
  15. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  16. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  17. if(empty($data['file'])) return [false,'导入文件不能为空'];
  18. //导入的数据并且校验写入
  19. list($status,$msg) = $this->importMain($data,$user);
  20. if(! $status) return [false, $msg];
  21. return [true, ''];
  22. }
  23. //主方法
  24. public function importMain($data,$user){
  25. //获取配置文件
  26. $config = "excel." . $data['type'];
  27. $config_array = config($config) ?? [];
  28. if(empty($config_array)) return [false, '配置文件不存在'];
  29. //(特殊 额外的表头数据)
  30. $config_array = $this->getTableTitle($config_array,$user,$data);
  31. //获取合并单元格范围
  32. $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径
  33. $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件
  34. $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表
  35. $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围
  36. // 需要导入的公用数据
  37. $msg['user_id'] = $user['id'];
  38. $msg['depart_id'] = $this->getDepart($user);
  39. $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0;
  40. //导入
  41. $import = new Import();
  42. $import->setConfig($config_array, $mergedCells,$msg);
  43. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  44. //异常提示报错
  45. if($import->getMsg()) return [false, $import->getMsg()];
  46. return [true, ''];
  47. }
  48. //表头入口
  49. public function getTableTitle($config_array,$user,$data){
  50. if(! empty($config_array['dynamics_field'])){
  51. $func = $config_array['dynamics_field']['func'];
  52. return $this->$func($config_array,$user,$data);
  53. }
  54. return $config_array;
  55. }
  56. //产品导入的额外表头
  57. public function productTitle($config_array,$user,$data){
  58. $model = BasicType::TopClear($user,$data);
  59. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  60. if(! empty($result)){
  61. foreach ($result as $value){
  62. $config_array['field'][$value['title']] = [
  63. "key" => $config_array['dynamics_field']['name'],
  64. "key_array" => [
  65. "basic_type_id" => $value['id'],
  66. "price" => 0,
  67. ],
  68. "rule" => "",
  69. "other_rule" => "is_numeric",
  70. "multiple" => true,
  71. "map" => [
  72. $value['title'] => "price",
  73. ],
  74. ];
  75. }
  76. }
  77. return $config_array;
  78. }
  79. //产品导入的额外数据
  80. public function fillInsertProductData($time){
  81. $last_insert_data = Product::where('crt_time',$time)
  82. ->where('del_time',0)
  83. ->select("id","product_category_id")
  84. ->get()->toArray();
  85. if(empty($last_insert_data)) return;
  86. $list = ProductCategory::where('del_time',0)
  87. ->select('id','parent_id')
  88. ->get()->toArray();
  89. foreach ($last_insert_data as $value){
  90. $parentsId = $this->findParentIds($value['product_category_id'], $list);
  91. array_unshift($parentsId, $value['product_category_id']);
  92. $result = array_reverse($parentsId);
  93. Product::where('id',$value['id'])->update([
  94. 'product_category' => json_encode($result)
  95. ]);
  96. }
  97. }
  98. }