SimilarAddressExport.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Exports\Manage;
  3. use App\Repositories\Enums\Manage\DealStatusEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Manage\Message;
  6. use App\Repositories\Models\Manage\PreMessage;
  7. use Maatwebsite\Excel\Concerns\Exportable;
  8. use Maatwebsite\Excel\Concerns\FromQuery;
  9. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  10. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  11. use Maatwebsite\Excel\Concerns\WithHeadings;
  12. use Maatwebsite\Excel\Concerns\WithMapping;
  13. use Maatwebsite\Excel\Concerns\WithStyles;
  14. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  15. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  16. class SimilarAddressExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  17. {
  18. use Exportable;
  19. const BaseFields = [
  20. [
  21. 'name' => '序号',
  22. 'width' => 8,
  23. ],
  24. [
  25. 'name' => '事项来源',
  26. 'width' => 20,
  27. ],
  28. [
  29. 'name' => '事项分类',
  30. 'width' => 30,
  31. ],
  32. [
  33. 'name' => '反应人',
  34. 'width' => 20,
  35. ],
  36. [
  37. 'name' => '联系方式',
  38. 'width' => 15,
  39. ],
  40. [
  41. 'name' => '地址',
  42. 'width' => 20,
  43. ],
  44. [
  45. 'name' => '处理单位',
  46. 'width' => 30,
  47. ],
  48. [
  49. 'name' => '受理时间',
  50. 'width' => 30,
  51. ],
  52. [
  53. 'name' => '事项内容',
  54. 'width' => 50,
  55. ],
  56. [
  57. 'name' => '办理结果',
  58. 'width' => 50,
  59. ],
  60. [
  61. 'name' => '办理进度',
  62. 'width' => 15,
  63. ],
  64. // [
  65. // 'name' => '聚合值',
  66. // 'width' => 10,
  67. // ],
  68. ];
  69. protected $rowNums = 0;
  70. protected $ids = [];
  71. protected $day = 0;
  72. protected $type = 0;
  73. protected $lastC = 'A';
  74. protected $fields = [];
  75. public function __construct($config)
  76. {
  77. $this->ids = $config['ids'];
  78. $fields = [];
  79. if (!array_key_exists('field_ids', $config)) {
  80. $fields = self::BaseFields;
  81. } else {
  82. $field_ids = $config['field_ids'];
  83. foreach ($field_ids as $field_id) {
  84. $fields[] = self::BaseFields[$field_id];
  85. }
  86. }
  87. $this->fields = $fields;
  88. }
  89. public function query()
  90. {
  91. $ids = $this->ids;
  92. $address_ids = Message::query()->whereIn('id', $ids)->pluck('address_id');
  93. return Message::query()
  94. ->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])
  95. ->where('status', ModelStatusEnum::OK)
  96. ->whereIn('address_id', $address_ids)
  97. // ->where('pid', 0)
  98. ->orderBy('id');
  99. }
  100. public function headingRow(): int
  101. {
  102. return 2;
  103. }
  104. public function headings(): array
  105. {
  106. $room_name = '聚合名单';
  107. $data[] = [$room_name];
  108. $data[] = array_column($this->fields, 'name');
  109. return $data;
  110. }
  111. public function columnWidths(): array
  112. {
  113. $i = 65;
  114. $data = [];
  115. foreach ($this->fields as $k => $field) {
  116. $s = intval($k / 26);
  117. if (!$s) $key = chr($i + $k);
  118. if ($s > 0) {
  119. $y = intval($k % 26);
  120. $key = chr($s + $i - 1);
  121. $key .= chr($y + $i);
  122. }
  123. $data["{$key}"] = $field['width'];
  124. $this->lastC = $key;
  125. }
  126. return $data;
  127. }
  128. public function styles(Worksheet $sheet)
  129. {
  130. $last = $this->lastC;
  131. $cellRange = "A1:{$last}1";
  132. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  133. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  134. // 文字居中
  135. $lastrow = $sheet->getHighestRow();
  136. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  137. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  138. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  139. $sheet->mergeCells("A1:{$last}1"); //合并
  140. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  141. }
  142. public function map($row): array
  143. {
  144. $row = $row->toArray();
  145. $this->rowNums++;
  146. // $jhi = $row['pid'];
  147. // if (!$jhi) $jhi = $row['id'];
  148. // $row = Message::query()->where('id', $row['message_id'])->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])->first();
  149. // if (!$row) return [];
  150. $data = [];
  151. foreach (array_column($this->fields, 'name') as $k => $field) {
  152. if ($field == '序号') $data[$k] = $this->rowNums;
  153. if ($field == '事项来源') $data[$k] = isset($row['type']['name']) ? $row['type']['name'] : '';
  154. if ($field == '事项分类') {
  155. $category = [];
  156. if ($row['category_1']) $category[] = $row['category_1']['name'];
  157. if ($row['category_2']) $category[] = $row['category_2']['name'];
  158. if ($row['category_3']) $category[] = $row['category_3']['name'];
  159. if ($row['category_4']) $category[] = $row['category_4']['name'];
  160. if ($row['category_5']) $category[] = $row['category_5']['name'];
  161. $data[$k] = arr2str($category, '/');
  162. }
  163. if ($field == '反应人') $data[$k] = $row['name'];
  164. if ($field == '联系方式') $data[$k] = $row['mobile'];
  165. if ($field == '地址') $data[$k] = $row['address_name'];
  166. if ($field == '处理单位') $data[$k] = isset($row['deal_department']['name']) ? $row['deal_department']['name'] : '';
  167. if ($field == '受理时间') $data[$k] = $row['complain_date'];
  168. if ($field == '事项内容') $data[$k] = $row['body'];
  169. if ($field == '办理结果') $data[$k] = $row['deal_idea'];
  170. // if ($field == '办理时限') $data[$k] = $row['name'];
  171. if ($field == '办理进度') {
  172. $deal_name = '';
  173. switch ($row['deal_status']) {
  174. case DealStatusEnum::WAIT:
  175. $deal_name = '未办理';
  176. break;
  177. case DealStatusEnum::IN:
  178. $deal_name = '正在办理';
  179. break;
  180. case DealStatusEnum::OK:
  181. $deal_name = '已办结';
  182. break;
  183. }
  184. $data[$k] = $deal_name;
  185. }
  186. // if ($field == '聚合值') $data[$k] = $jhi;
  187. // if ($field == '满意度') $data[$k] = $row['name'];
  188. }
  189. return $data;
  190. }
  191. }