BikeCodeExport.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Exports\BikeManage;
  3. use App\Repositories\Enums\BikeManage\BindingStatusEnum;
  4. use App\Repositories\Models\BikeManage\Bike;
  5. use Maatwebsite\Excel\Concerns\Exportable;
  6. use Maatwebsite\Excel\Concerns\FromQuery;
  7. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  8. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  9. use Maatwebsite\Excel\Concerns\WithHeadings;
  10. use Maatwebsite\Excel\Concerns\WithMapping;
  11. use Maatwebsite\Excel\Concerns\WithStyles;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  14. class BikeCodeExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  15. {
  16. use Exportable;
  17. const BaseFields = [
  18. [
  19. 'name' => '序号',
  20. 'width' => 8,
  21. ],
  22. [
  23. 'name' => '校内牌照',
  24. 'width' => 15,
  25. ],
  26. [
  27. 'name' => '车辆牌照',
  28. 'width' => 15,
  29. ],
  30. [
  31. 'name' => '二维码数据',
  32. 'width' => 50,
  33. ],
  34. [
  35. 'name' => '是否绑定',
  36. 'width' => 30,
  37. ],
  38. [
  39. 'name' => '车主姓名',
  40. 'width' => 15,
  41. ],
  42. [
  43. 'name' => '车主手机号',
  44. 'width' => 15,
  45. ],
  46. [
  47. 'name' => '车主班级',
  48. 'width' => 10,
  49. ],
  50. ];
  51. protected $rowNums = 0;
  52. protected $ids = [];
  53. protected $lastC = 'A';
  54. protected $fields = [];
  55. public function __construct($ids, $field_ids = [])
  56. {
  57. $this->ids = $ids;
  58. $fields = [];
  59. if (!count($field_ids)) {
  60. $fields = self::BaseFields;
  61. } else {
  62. foreach ($field_ids as $field_id) {
  63. $fields[] = self::BaseFields[$field_id];
  64. }
  65. }
  66. $this->fields = $fields;
  67. }
  68. public function query()
  69. {
  70. return Bike::query()
  71. ->with(['user'])
  72. ->whereIn('id', $this->ids)
  73. ->orderByDesc('id');
  74. }
  75. public function map($row): array
  76. {
  77. $row = $row->toArray();
  78. $this->rowNums++;
  79. $data = [];
  80. foreach (array_column($this->fields, 'name') as $k => $field) {
  81. if ($field == '序号') $data[$k] = $this->rowNums;
  82. if ($field == '校内牌照') $data[$k] = $row['school_no'];
  83. if ($field == '车辆牌照') $data[$k] = $row['bike_no'];
  84. if ($field == '是否绑定') $data[$k] = BindingStatusEnum::getDescription($row['is_bind']);
  85. if ($field == '二维码数据') $data[$k] = $row['qr_cord'];
  86. if ($field == '车主姓名') $data[$k] = $row['user'] ? $row['user']['name'] : '';
  87. if ($field == '车主手机号') $data[$k] = $row['user'] ? $row['user']['mobile'] : '';
  88. if ($field == '车主班级') $data[$k] = $row['user'] ? $row['user']['class_name'] : '';
  89. }
  90. return $data;
  91. }
  92. public function headings(): array
  93. {
  94. $room_name = '车辆列表';
  95. $data[] = [$room_name];
  96. $data[] = array_column($this->fields, 'name');
  97. return $data;
  98. }
  99. public function headingRow(): int
  100. {
  101. return 2;
  102. }
  103. public function columnWidths(): array
  104. {
  105. $i = 65;
  106. $data = [];
  107. foreach ($this->fields as $k => $field) {
  108. $s = intval($k / 26);
  109. if (!$s) $key = chr($i + $k);
  110. if ($s > 0) {
  111. $y = intval($k % 26);
  112. $key = chr($s + $i - 1);
  113. $key .= chr($y + $i);
  114. }
  115. $data["{$key}"] = $field['width'];
  116. $this->lastC = $key;
  117. }
  118. return $data;
  119. }
  120. public function styles(Worksheet $sheet)
  121. {
  122. $last = $this->lastC;
  123. $cellRange = "A1:{$last}1";
  124. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  125. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  126. // 文字居中
  127. $lastrow = $sheet->getHighestRow();
  128. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  129. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  130. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  131. $sheet->mergeCells("A1:{$last}1"); //合并
  132. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  133. }
  134. }