ImportService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\TableHeadExport;
  4. use App\Import\Import;
  5. use App\Import\ImportAll;
  6. use App\Model\BasicType;
  7. use App\Model\Customer;
  8. use App\Model\CustomerInfo;
  9. use App\Model\Depart;
  10. use App\Model\Employee;
  11. use App\Model\Product;
  12. use App\Model\ProductCategory;
  13. use App\Model\ProductPriceDetail;
  14. use App\Model\SalesOrder;
  15. use App\Model\SalesOrderInfo;
  16. use App\Model\SalesOrderProductInfo;
  17. use Illuminate\Support\Facades\DB;
  18. use Maatwebsite\Excel\Facades\Excel;
  19. use PhpOffice\PhpSpreadsheet\IOFactory;
  20. class ImportService extends Service
  21. {
  22. public static $type = [
  23. 'product', //产品
  24. 'customer', //客户
  25. 'salesOnline', //线上订单
  26. ];
  27. public function getTableTitleXls($data,$user){
  28. if(empty($data['type'])) return [false,'缺少类型'];
  29. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  30. //获取配置文件
  31. $fuc = $data['type'];
  32. list($status,$msg,$filename) = $this->$fuc($data,$user);
  33. if(!$status) return [false, $msg];
  34. $headers = array_column($msg,'value');
  35. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  36. return [true, ['file' => $filename]];
  37. }
  38. private function customer($data,$user){
  39. //生成下载文件
  40. $filename = "客户模板_" . time() . '.' . 'xlsx';
  41. //获取配置文件
  42. $config = "excel.customerTable";
  43. $config_array = config($config) ?? [];
  44. if(empty($config_array)) return [false, '配置文件不存在',''];
  45. return [true, $config_array,$filename];
  46. }
  47. private function product($data,$user){
  48. //获取配置文件
  49. $config = "excel.productTable";
  50. $config_array = config($config) ?? [];
  51. if(empty($config_array)) return [false, '配置文件不存在', ''];
  52. $result = (new BasicTypeService())->getMyBasicList($user, 22);
  53. if(! empty($result)){
  54. foreach ($result as $value){
  55. $config_array[] = [
  56. 'key' => 'table_id.' . $value['id'],
  57. 'value' => $value['title'],
  58. ];
  59. }
  60. }
  61. //生成下载文件
  62. $filename = "产品模板_" . time() . '.' . 'xlsx';
  63. return [true, $config_array,$filename];
  64. }
  65. private function salesOnline($data,$user){
  66. //生成下载文件
  67. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  68. //获取配置文件
  69. $config = "excel.salesOnlineTable";
  70. $config_array = config($config) ?? [];
  71. if(empty($config_array)) return [false, '配置文件不存在',''];
  72. return [true, $config_array,$filename];
  73. }
  74. //导入入口
  75. public function importAll($data,$user){
  76. // //不超时
  77. // ini_set('max_execution_time', 0);
  78. // //内存设置
  79. // ini_set('memory_limit', -1);
  80. // $reader = IOFactory::createReader('Xlsx');
  81. // $reader->setReadDataOnly(true); // 只读取有数据的单元格
  82. // $spreadsheet = $reader->load($data['file']);
  83. // dd($spreadsheet);
  84. // // 创建一个Reader对象
  85. // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader
  86. //
  87. //// 加载Excel文件
  88. // $spreadsheet = $reader->load($data['file']);
  89. //
  90. //// 获取第一个工作表
  91. // $worksheet = $spreadsheet->getActiveSheet();
  92. //
  93. //// 获取总行数
  94. // $totalRows = $worksheet->getHighestRow();dd($totalRows);
  95. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  96. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  97. if(empty($data['file'])) return [false,'导入文件不能为空'];
  98. try {
  99. $import = new ImportAll();
  100. //设置导入人id
  101. $import->setCrt($user['id']);
  102. $import->setUser($user);
  103. $import->setType($data['type']);
  104. //导入
  105. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  106. if($import->getMsg()) return [false, $import->getMsg()];
  107. }catch (\Throwable $exception) {
  108. return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
  109. }
  110. return [true, ''];
  111. }
  112. }