RecordExport.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Exports\Ant;
  3. use App\Repositories\Models\Ant\Record;
  4. use Maatwebsite\Excel\Concerns\Exportable;
  5. use Maatwebsite\Excel\Concerns\FromQuery;
  6. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  7. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  8. use Maatwebsite\Excel\Concerns\WithHeadings;
  9. use Maatwebsite\Excel\Concerns\WithMapping;
  10. use Maatwebsite\Excel\Concerns\WithStyles;
  11. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  12. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  13. class RecordExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  14. {
  15. use Exportable;
  16. const BaseFields = [
  17. [
  18. 'name' => '序号',
  19. 'width' => 8,
  20. ],
  21. [
  22. 'name' => '省(州)',
  23. 'width' => 15,
  24. ],
  25. [
  26. 'name' => '市',
  27. 'width' => 15,
  28. ],
  29. [
  30. 'name' => '县',
  31. 'width' => 15,
  32. ],
  33. [
  34. 'name' => '工程名称',
  35. 'width' => 30,
  36. ],
  37. [
  38. 'name' => '工程地址',
  39. 'width' => 30,
  40. ],
  41. [
  42. 'name' => '工程等级',
  43. 'width' => 20,
  44. ],
  45. [
  46. 'name' => '工程类型(坝型或堤形)',
  47. 'width' => 20,
  48. ],
  49. [
  50. 'name' => '单元',
  51. 'width' => 20,
  52. ],
  53. [
  54. 'name' => '害堤动物种类',
  55. 'width' => 20,
  56. ],
  57. [
  58. 'name' => '蚁患区面积',
  59. 'width' => 20,
  60. ],
  61. [
  62. 'name' => '蚁源区面积',
  63. 'width' => 20,
  64. ],
  65. // [
  66. // 'name' => '贯穿性议道',
  67. // 'width' => 20,
  68. // ],
  69. [
  70. 'name' => '对工程的影响',
  71. 'width' => 20,
  72. ],
  73. [
  74. 'name' => '检测到的蚁种',
  75. 'width' => 20,
  76. ],
  77. [
  78. 'name' => '危害等级',
  79. 'width' => 20,
  80. ],
  81. ];
  82. protected $rowNums = 0;
  83. protected $ids = [];
  84. protected $lastC = 'A';
  85. protected $fields = [];
  86. public function __construct($ids, $field_ids = [])
  87. {
  88. $this->ids = $ids;
  89. $fields = [];
  90. if (!count($field_ids)) {
  91. $fields = self::BaseFields;
  92. } else {
  93. foreach ($field_ids as $field_id) {
  94. $fields[] = self::BaseFields[$field_id];
  95. }
  96. }
  97. $this->fields = $fields;
  98. }
  99. public function query()
  100. {
  101. return Record::query()
  102. ->with(['user', 'company'])
  103. ->whereIn('id', $this->ids)
  104. ->orderByDesc('id');
  105. }
  106. public function map($row): array
  107. {
  108. $row = $row->toArray();
  109. $this->rowNums++;
  110. $data = [];
  111. foreach (array_column($this->fields, 'name') as $k => $field) {
  112. if ($field == '序号') $data[$k] = $this->rowNums;
  113. if ($field == '省(州)') $data[$k] = $row['province'];
  114. if ($field == '市') $data[$k] = $row['city'];
  115. if ($field == '县') $data[$k] = $row['area'];
  116. if ($field == '工程名称') $data[$k] = $row['project_name'];
  117. if ($field == '工程地址') $data[$k] = $row['province'] . $row['city'] . $row['area'] . $row['zhen'] . $row['cun'];
  118. if ($field == '工程等级') $data[$k] = $row['project_level_name'];
  119. if ($field == '工程类型(坝型或堤形)') $data[$k] = $row['project_dam_type_name'];
  120. if ($field == '单元') $data[$k] = $row['danger_cell_code'];
  121. if ($field == '害堤动物种类') {
  122. $danger_animal_extend = $row['danger_animal_extend'];
  123. $msg = [];
  124. foreach ($danger_animal_extend as $item) {
  125. if (array_key_exists('isWrite', $item) && $item['isWrite']) {
  126. $msg[] = $item['name'];
  127. }
  128. }
  129. $data[$k] = arr2str($msg);
  130. }
  131. if ($field == '蚁患区面积') $data[$k] = $row['danger_area'];
  132. if ($field == '蚁源区面积') $data[$k] = $row['area_area'];
  133. if ($field == '贯穿性议道') $data[$k] = $row['danger_is_penetrate'] ? '是' : '否';
  134. if ($field == '对工程的影响') $data[$k] = $row['is_project_danger'];
  135. if ($field == '检测到的蚁种') {
  136. $extend = $row['danger_types'];
  137. $msg = [];
  138. foreach ($extend['select'] as $item) {
  139. if (array_key_exists('checked', $item) && $item['checked']) {
  140. $msg[] = $item['name'];
  141. }
  142. }
  143. if ($extend['others']) $msg[] = $extend['others'];
  144. $data[$k] = arr2str($msg);
  145. }
  146. if ($field == '危害等级') $data[$k] = $row['danger_level'];
  147. }
  148. return $data;
  149. }
  150. public function headings(): array
  151. {
  152. $room_name = '白蚁普查台账';
  153. $data[] = [$room_name];
  154. $data[] = array_column($this->fields, 'name');
  155. return $data;
  156. }
  157. public function headingRow(): int
  158. {
  159. return 2;
  160. }
  161. public function columnWidths(): array
  162. {
  163. $i = 65;
  164. $data = [];
  165. foreach ($this->fields as $k => $field) {
  166. $s = intval($k / 26);
  167. if (!$s) $key = chr($i + $k);
  168. if ($s > 0) {
  169. $y = intval($k % 26);
  170. $key = chr($s + $i - 1);
  171. $key .= chr($y + $i);
  172. }
  173. $data["{$key}"] = $field['width'];
  174. $this->lastC = $key;
  175. }
  176. return $data;
  177. }
  178. public function styles(Worksheet $sheet)
  179. {
  180. $last = $this->lastC;
  181. $cellRange = "A1:{$last}1";
  182. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  183. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  184. // 文字居中
  185. $lastrow = $sheet->getHighestRow();
  186. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  187. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  188. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  189. $sheet->mergeCells("A1:{$last}1"); //合并
  190. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  191. }
  192. }