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

100 lines
3.6 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. public function __construct(int $supplier_id)
  13. {
  14. $this->supplier_id = $supplier_id;
  15. }
  16. public function query()
  17. {
  18. return Product::with('category:id,name')->where('supplier_id', $this->supplier_id);
  19. }
  20. public function prepareRows($rows)
  21. {
  22. return $rows->transform(function ($row) {
  23. return [
  24. 'id' => $row->id,
  25. 'status' => ProductStatus::array()[$row->status] ?? '',
  26. 'category_name' => $row->category?->name ?? '',
  27. 'title' => $row->title,
  28. 'price' => $row->price,
  29. 'original_price' => $row->original_price,
  30. 'stock' => $row->stock,
  31. 'know' => $row->know,
  32. 'content' => $row->content,
  33. 'verify_mobile' => $row->verify_mobile,
  34. 'diy_form_id' => $row->diy_form_id,
  35. '出发地' => $row->extends['field_0_departure_place'] ?? '',
  36. '出发地经度' => $row->extends['field_0_departure_place_longitude'] ?? '',
  37. '出发地纬度' => $row->extends['field_0_departure_place_latitude'] ?? '',
  38. '目的地' => $row->extends['field_0_destination'] ?? '',
  39. '目的地经度' => $row->extends['field_0_destination_longitude'] ?? '',
  40. '目的地纬度' => $row->extends['field_0_destination_latitude'] ?? '',
  41. '行程起始时间' => $row->extends['field_0_date?->start'] ?? '',
  42. '行程结束时间' => $row->extends['field_0_date?->end'] ?? '',
  43. '酒店名' => $row->extends['field_1_name'] ?? '',
  44. '酒店地址' => $row->extends['field_1_address'] ?? '',
  45. '酒店经度' => $row->extends['field_1_longitude'] ?? '',
  46. '酒店纬度' => $row->extends['field_1_latitude'] ?? '',
  47. '景区名' => $row->extends['field_2_name'] ?? '',
  48. '景区地址' => $row->extends['field_2_address'] ?? '',
  49. '景区经度' => $row->extends['field_2_longitude'] ?? '',
  50. '景区纬度' => $row->extends['field_2_latitude'] ?? '',
  51. '餐厅名' => $row->extends['field_3_name'] ?? '',
  52. '餐厅地址' => $row->extends['field_3_address'] ?? '',
  53. '餐厅经度' => $row->extends['field_3_longitude'] ?? '',
  54. '餐厅纬度' => $row->extends['field_3_latitude'] ?? '',
  55. '交通地址' => $row->extends['field_4_address'] ?? '',
  56. '交通经度' => $row->extends['field_4_longitude'] ?? '',
  57. '交通纬度' => $row->extends['field_4_latitude'] ?? '',
  58. '购物地址' => $row->extends['field_5_address'] ?? '',
  59. '购物经度' => $row->extends['field_5_longitude'] ?? '',
  60. '购物纬度' => $row->extends['field_5_latitude'] ?? '',
  61. ];
  62. });
  63. }
  64. public function map($row): array
  65. {
  66. return [
  67. $row->id,
  68. $row->category->name,
  69. ];
  70. }
  71. public function headings(): array
  72. {
  73. return [
  74. 'ID', '状态', '分类', '产品标题', '销售价', '市场价', '库存', '旅客须知', '产品详情', '核销手机号', '信息收集表单ID',
  75. '出发地', '出发地经度', '出发地纬度', '目的地', '目的地经度', '目的地纬度', '行程起始时间', '行程结束时间',
  76. '酒店名', '酒店地址', '酒店经度', '酒店纬度',
  77. '景区名', '景区地址', '景区经度', '景区纬度',
  78. '餐厅名', '餐厅地址', '餐厅经度', '餐厅纬度',
  79. '交通地址', '交通经度', '交通纬度',
  80. '购物地址', '购物经度', '购物纬度',
  81. ];
  82. }
  83. public function columnFormats(): array
  84. {
  85. for ($i = 65; $i <= 90; $i++) {
  86. $format[chr($i)] = NumberFormat::FORMAT_TEXT;
  87. }
  88. return $format;
  89. }
  90. }