SimilarBodyExport.php 6.7 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 SimilarBodyExport 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. return Message::query()
  93. ->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])
  94. ->where('status', ModelStatusEnum::OK)
  95. ->whereIn('id', $ids)
  96. // ->where('pid', 0)
  97. ->orderBy('id');
  98. }
  99. public function headingRow(): int
  100. {
  101. return 2;
  102. }
  103. public function headings(): array
  104. {
  105. $room_name = '聚合名单';
  106. $data[] = [$room_name];
  107. $data[] = array_column($this->fields, 'name');
  108. return $data;
  109. }
  110. public function columnWidths(): array
  111. {
  112. $i = 65;
  113. $data = [];
  114. foreach ($this->fields as $k => $field) {
  115. $s = intval($k / 26);
  116. if (!$s) $key = chr($i + $k);
  117. if ($s > 0) {
  118. $y = intval($k % 26);
  119. $key = chr($s + $i - 1);
  120. $key .= chr($y + $i);
  121. }
  122. $data["{$key}"] = $field['width'];
  123. $this->lastC = $key;
  124. }
  125. return $data;
  126. }
  127. public function styles(Worksheet $sheet)
  128. {
  129. $last = $this->lastC;
  130. $cellRange = "A1:{$last}1";
  131. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  132. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  133. // 文字居中
  134. $lastrow = $sheet->getHighestRow();
  135. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  136. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  137. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  138. $sheet->mergeCells("A1:{$last}1"); //合并
  139. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  140. }
  141. public function map($row): array
  142. {
  143. $row = $row->toArray();
  144. $this->rowNums++;
  145. // $jhi = $row['pid'];
  146. // if (!$jhi) $jhi = $row['id'];
  147. // $row = Message::query()->where('id', $row['message_id'])->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])->first();
  148. // if (!$row) return [];
  149. $data = [];
  150. foreach (array_column($this->fields, 'name') as $k => $field) {
  151. if ($field == '序号') $data[$k] = $this->rowNums;
  152. if ($field == '事项来源') $data[$k] = isset($row['type']['name']) ? $row['type']['name'] : '';
  153. if ($field == '事项分类') {
  154. $category = [];
  155. if ($row['category_1']) $category[] = $row['category_1']['name'];
  156. if ($row['category_2']) $category[] = $row['category_2']['name'];
  157. if ($row['category_3']) $category[] = $row['category_3']['name'];
  158. if ($row['category_4']) $category[] = $row['category_4']['name'];
  159. if ($row['category_5']) $category[] = $row['category_5']['name'];
  160. $data[$k] = arr2str($category, '/');
  161. }
  162. if ($field == '反应人') $data[$k] = $row['name'];
  163. if ($field == '联系方式') $data[$k] = $row['mobile'];
  164. if ($field == '地址') $data[$k] = $row['address_name'];
  165. if ($field == '处理单位') $data[$k] = isset($row['deal_department']['name']) ? $row['deal_department']['name'] : '';
  166. if ($field == '受理时间') $data[$k] = $row['complain_date'];
  167. if ($field == '事项内容') $data[$k] = $row['body'];
  168. if ($field == '办理结果') $data[$k] = $row['deal_idea'];
  169. // if ($field == '办理时限') $data[$k] = $row['name'];
  170. if ($field == '办理进度') {
  171. $deal_name = '';
  172. switch ($row['deal_status']) {
  173. case DealStatusEnum::WAIT:
  174. $deal_name = '未办理';
  175. break;
  176. case DealStatusEnum::IN:
  177. $deal_name = '正在办理';
  178. break;
  179. case DealStatusEnum::OK:
  180. $deal_name = '已办结';
  181. break;
  182. }
  183. $data[$k] = $deal_name;
  184. }
  185. // if ($field == '聚合值') $data[$k] = $jhi;
  186. // if ($field == '满意度') $data[$k] = $row['name'];
  187. }
  188. return $data;
  189. }
  190. }