ImportService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\TableHeadExport;
  4. use App\Import\ImportAll;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. use Maatwebsite\Excel\Facades\Excel;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use PhpOffice\PhpSpreadsheet\Shared\Date;
  10. class ImportService extends Service
  11. {
  12. public static $type = [
  13. 'product', //存货
  14. ];
  15. public function getTableTitleXls($data,$user){
  16. if(empty($data['type'])) return [false,'缺少类型'];
  17. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  18. //获取配置文件
  19. $fuc = $data['type'];
  20. list($status,$return) = $this->$fuc($data,$user);
  21. list($msg,$filename) = $return;
  22. if(! $status) return [false, $msg];
  23. $headers = array_column($msg,'value');
  24. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  25. return [true, ['file' => $filename]];
  26. }
  27. private function getTableConfig($type = ""){
  28. if(empty($type)) return [];
  29. //获取配置文件
  30. $config = "excel." . $type . "Table";
  31. return config($config) ?? [];
  32. }
  33. private function product($data,$user){
  34. $config_array = $this->getTableConfig($data['type']);
  35. if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
  36. //生成下载文件
  37. $filename = "存货导入模板_" . time() . '.' . 'xlsx';
  38. return [true, [$config_array, $filename]];
  39. }
  40. //导入入口
  41. public function importAll($data,$user){
  42. // //不超时
  43. // ini_set('max_execution_time', 0);
  44. // //内存设置
  45. // ini_set('memory_limit', -1);
  46. // $reader = IOFactory::createReader('Xlsx');
  47. // $reader->setReadDataOnly(true); // 只读取有数据的单元格
  48. // $spreadsheet = $reader->load($data['file']);
  49. // dd($spreadsheet);
  50. // // 创建一个Reader对象
  51. // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader
  52. //
  53. //// 加载Excel文件
  54. // $spreadsheet = $reader->load($data['file']);
  55. //
  56. //// 获取第一个工作表
  57. // $worksheet = $spreadsheet->getActiveSheet();
  58. //
  59. //// 获取总行数
  60. // $totalRows = $worksheet->getHighestRow();dd($totalRows);
  61. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  62. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  63. if(empty($data['file'])) return [false,'导入文件不能为空'];
  64. try {
  65. $import = new ImportAll();
  66. //设置导入人id
  67. $import->setCrt($user['id']);
  68. $import->setUser($user);
  69. $import->setType($data['type']);
  70. $other = $data;
  71. unset($other['file']);
  72. $import->setOtherParam($other);
  73. //导入
  74. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  75. if($import->getMsg()) {
  76. $bool = $import->getIsLongText();
  77. if($bool) {
  78. return [0, $import->getMsg()];
  79. }else{
  80. return [false, $import->getMsg()];
  81. }
  82. }
  83. }catch (\Throwable $exception) {
  84. return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
  85. }
  86. return [true, ''];
  87. }
  88. public function productImport($array, $user, $other_param){
  89. $upload = $array[0];
  90. list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
  91. if(! $status) return [false, $msg];
  92. $table_config = $msg;
  93. // 去除表头
  94. unset($array[0]);
  95. if(empty($array)) return [false, '导入数据不能为空'];
  96. list($array, $error) = $this->checkProduct($array, $table_config);
  97. if(empty($array)) return [false, '导入数据不能为空'];
  98. try{
  99. DB::commit();
  100. }catch (\Exception $e){
  101. DB::rollBack();
  102. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  103. }
  104. return [true, ''];
  105. }
  106. private function checkProduct($array, $table_config){
  107. $error = [];
  108. foreach ($array as $key => $value){
  109. $line = '第' . $key . '行';
  110. $rowData = array_filter($value);
  111. if (empty($rowData)) {
  112. unset($array[$key]);
  113. } else{
  114. foreach ($value as $k => $v){
  115. $row = '第' . $k . '列';
  116. $tmp_v = trim($v);
  117. if(! isset($table_config[$k])){
  118. $error[] = $line . $row . "数据不存在";
  119. }else{
  120. $table_tmp = $table_config[$k] ?? [];
  121. if(! empty($table_tmp['required']) && empty($tmp_v)) $error[] = $line . $table_tmp['value'] . "必填";
  122. if(! empty($table_tmp['unique']))
  123. }
  124. $value[$k] = trim($v);
  125. }
  126. }
  127. }
  128. }
  129. //模板校验
  130. private function compareTableAndReturn($upload, $param){
  131. if(empty($upload)) return [false, '表头不能为空'];
  132. $config_array = $this->getTableConfig($param['type']);
  133. if(empty($config_array)) return [false, '导入配置表头文件不存在'];
  134. foreach ($config_array as $key => $value){
  135. $key_position = $key + 1;
  136. if(! isset($upload[$key])) return [false, "第" . $key_position . "列表头缺失"];
  137. $tmp_v = trim($upload[$key]);
  138. if($tmp_v != $value['value']) return [false, "第" . $key_position . "列表头与模板不符合,请重新下载模板"];
  139. }
  140. return [true, $config_array];
  141. }
  142. }