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

108 lines
4.1 KiB

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