海南旅游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.

110 lines
4.5 KiB

  1. <?php
  2. namespace App\Exports;
  3. use App\Common\ProductStatus;
  4. use App\Models\Product;
  5. use Illuminate\Support\Facades\Storage;
  6. use Maatwebsite\Excel\Concerns\FromQuery;
  7. use Maatwebsite\Excel\Concerns\WithColumnFormatting;
  8. use Maatwebsite\Excel\Concerns\WithHeadings;
  9. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  10. class ProductExport implements FromQuery, WithHeadings, WithColumnFormatting
  11. {
  12. private int $supplier_id;
  13. private string|array|null $_export_;
  14. private string $export_dir;
  15. public function __construct(int $supplier_id, string|array $_export_, string $export_dir)
  16. {
  17. $this->supplier_id = $supplier_id;
  18. $this->_export_ = $_export_;
  19. $this->export_dir = $export_dir;
  20. }
  21. public function query()
  22. {
  23. if ($this->_export_ == 'all') {
  24. return Product::with('category:id,name')->where('supplier_id', $this->supplier_id);
  25. } else if (!empty($this->_export_) && is_array($this->_export_)) {
  26. return Product::with('category:id,name')->where('supplier_id', $this->supplier_id)->whereIn('id', $this->_export_);
  27. } else {
  28. return Product::with('category:id,name')->where('supplier_id', $this->supplier_id)->limit(20);
  29. }
  30. }
  31. public function prepareRows($rows)
  32. {
  33. return $rows->transform(function ($row) {
  34. # 复制图片到导出目录
  35. if (is_array($row['pictures']) && !empty($row['pictures'])) {
  36. foreach ($row['pictures'] as $key => $picture) {
  37. try {
  38. Storage::disk('public')->copy($picture, $this->export_dir . '产品主图/' . $row->id . '/' . ($key + 1) . '.' . pathinfo($picture)['extension']);
  39. } catch (\Exception $exception) {
  40. continue;
  41. }
  42. }
  43. }
  44. return [
  45. 'id' => $row->id,
  46. 'status' => ProductStatus::array()[$row->status] ?? '',
  47. 'category_name' => $row->category?->name ?? '',
  48. 'title' => $row->title,
  49. 'price' => $row->price,
  50. 'original_price' => $row->original_price,
  51. 'stock' => $row->stock,
  52. 'know' => $row->know,
  53. 'content' => $row->content,
  54. 'verify_mobile' => $row->verify_mobile,
  55. 'diy_form_id' => $row->diy_form_id,
  56. '出发地' => $row->extends['field_0_departure_place'] ?? '',
  57. '出发地经度' => $row->extends['field_0_departure_place_longitude'] ?? '',
  58. '出发地纬度' => $row->extends['field_0_departure_place_latitude'] ?? '',
  59. '目的地' => $row->extends['field_0_destination'] ?? '',
  60. '目的地经度' => $row->extends['field_0_destination_longitude'] ?? '',
  61. '目的地纬度' => $row->extends['field_0_destination_latitude'] ?? '',
  62. '行程起始时间' => $row->extends['field_0_date?->start'] ?? '',
  63. '行程结束时间' => $row->extends['field_0_date?->end'] ?? '',
  64. '酒店名' => $row->extends['field_1_name'] ?? '',
  65. '酒店地址' => $row->extends['field_1_address'] ?? '',
  66. '酒店经度' => $row->extends['field_1_longitude'] ?? '',
  67. '酒店纬度' => $row->extends['field_1_latitude'] ?? '',
  68. '景区名' => $row->extends['field_2_name'] ?? '',
  69. '景区地址' => $row->extends['field_2_address'] ?? '',
  70. '景区经度' => $row->extends['field_2_longitude'] ?? '',
  71. '景区纬度' => $row->extends['field_2_latitude'] ?? '',
  72. '餐厅名' => $row->extends['field_3_name'] ?? '',
  73. '餐厅地址' => $row->extends['field_3_address'] ?? '',
  74. '餐厅经度' => $row->extends['field_3_longitude'] ?? '',
  75. '餐厅纬度' => $row->extends['field_3_latitude'] ?? '',
  76. '交通地址' => $row->extends['field_4_address'] ?? '',
  77. '交通经度' => $row->extends['field_4_longitude'] ?? '',
  78. '交通纬度' => $row->extends['field_4_latitude'] ?? '',
  79. '购物地址' => $row->extends['field_5_address'] ?? '',
  80. '购物经度' => $row->extends['field_5_longitude'] ?? '',
  81. '购物纬度' => $row->extends['field_5_latitude'] ?? '',
  82. ];
  83. });
  84. }
  85. public function headings(): array
  86. {
  87. return [
  88. '产品ID', '状态', '分类', '产品标题', '销售价', '市场价', '库存', '旅客须知', '产品详情', '核销手机号', '信息收集表单ID',
  89. '出发地', '出发地经度', '出发地纬度', '目的地', '目的地经度', '目的地纬度', '行程起始时间', '行程结束时间',
  90. '酒店名', '酒店地址', '酒店经度', '酒店纬度', '景区名', '景区地址', '景区经度', '景区纬度',
  91. '餐厅名', '餐厅地址', '餐厅经度', '餐厅纬度', '交通地址', '交通经度', '交通纬度', '购物地址', '购物经度', '购物纬度',
  92. ];
  93. }
  94. public function columnFormats(): array
  95. {
  96. for ($i = 65; $i <= 90; $i++) {
  97. $format[chr($i)] = NumberFormat::FORMAT_TEXT;
  98. }
  99. return $format;
  100. }
  101. }