海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Exports\ProductExport;
  4. use App\Exports\ProductSpecExport;
  5. use App\Models\Product;
  6. use App\Models\ProductExportLog;
  7. use App\Models\ProductSpec;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldBeUnique;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Support\Facades\Storage;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. use RecursiveDirectoryIterator;
  17. use RecursiveIteratorIterator;
  18. use ZipArchive;
  19. /**
  20. * 导出全部产品
  21. */
  22. class ExportProductToExcel implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. private int $supplier_id;
  26. private string|array|null $_export_;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(int $supplier_id, string|array $_export_ = null)
  33. {
  34. $this->supplier_id = $supplier_id;
  35. $this->_export_ = $_export_;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $export_dir = "supplier/export/{$this->supplier_id}/" . date('Y-m') . '/';
  45. $product_file = $export_dir . '产品.xlsx';
  46. Storage::disk('public')->deleteDirectory($export_dir);
  47. if (!Excel::store(new ProductExport($this->supplier_id, $this->_export_, $export_dir), $product_file, 'public')) {
  48. return;
  49. }
  50. # 导出规格
  51. $ids = $this->_export_ == 'all' ?
  52. Product::where('supplier_id', $this->supplier_id)->get('id')->pluck('id') :
  53. Product::whereIn('id', $this->_export_)->where('supplier_id', $this->supplier_id)->get('id')->pluck('id');
  54. foreach ($ids as $id) {
  55. if (ProductSpec::where([['product_id', '=', $id], ['date', '>=', date('Y-m-d')]])->exists()) {
  56. Excel::store(new ProductSpecExport($id), $export_dir . "产品规格{$id}.xlsx", 'public');
  57. }
  58. }
  59. # 生成zip文件
  60. $zip = new ZipArchive();
  61. $zip_file = substr(rtrim($export_dir, '/'), 0, strrpos(rtrim($export_dir, '/'), '/')) . '/export_' . date('Y-m-d_H-i-s') . '.zip';
  62. if ($zip->open(Storage::disk('public')->path($zip_file),ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
  63. $files = new RecursiveIteratorIterator(
  64. new RecursiveDirectoryIterator(Storage::disk('public')->path($export_dir)),
  65. RecursiveIteratorIterator::LEAVES_ONLY
  66. );
  67. foreach ($files as $file) {
  68. if (!$file->isDir()) {
  69. $filePath = $file->getRealPath();
  70. $relativePath = substr($filePath, strlen(Storage::disk('public')->path($export_dir)));
  71. $zip->addFile($filePath, $relativePath);
  72. }
  73. }
  74. $zip->close();
  75. }
  76. Storage::disk('public')->deleteDirectory($export_dir);
  77. # 生成导出记录
  78. ProductExportLog::create([
  79. 'supplier_id' => $this->supplier_id,
  80. 'filename' => $zip_file,
  81. ]);
  82. }
  83. }