123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Exports\Manage;
- use App\Repositories\Enums\Manage\DealStatusEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Manage\Message;
- use App\Repositories\Models\Manage\PreMessage;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromQuery;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnWidths;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithMapping;
- use Maatwebsite\Excel\Concerns\WithStyles;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- class SimilarAddressExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- const BaseFields = [
- [
- 'name' => '序号',
- 'width' => 8,
- ],
- [
- 'name' => '事项来源',
- 'width' => 20,
- ],
- [
- 'name' => '事项分类',
- 'width' => 30,
- ],
- [
- 'name' => '反应人',
- 'width' => 20,
- ],
- [
- 'name' => '联系方式',
- 'width' => 15,
- ],
- [
- 'name' => '地址',
- 'width' => 20,
- ],
- [
- 'name' => '处理单位',
- 'width' => 30,
- ],
- [
- 'name' => '受理时间',
- 'width' => 30,
- ],
- [
- 'name' => '事项内容',
- 'width' => 50,
- ],
- [
- 'name' => '办理结果',
- 'width' => 50,
- ],
- [
- 'name' => '办理进度',
- 'width' => 15,
- ],
- // [
- // 'name' => '聚合值',
- // 'width' => 10,
- // ],
- ];
- protected $rowNums = 0;
- protected $ids = [];
- protected $day = 0;
- protected $type = 0;
- protected $lastC = 'A';
- protected $fields = [];
- public function __construct($config)
- {
- $this->ids = $config['ids'];
- $fields = [];
- if (!array_key_exists('field_ids', $config)) {
- $fields = self::BaseFields;
- } else {
- $field_ids = $config['field_ids'];
- foreach ($field_ids as $field_id) {
- $fields[] = self::BaseFields[$field_id];
- }
- }
- $this->fields = $fields;
- }
- public function query()
- {
- $ids = $this->ids;
- $address_ids = Message::query()->whereIn('id', $ids)->pluck('address_id');
- return Message::query()
- ->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])
- ->where('status', ModelStatusEnum::OK)
- ->whereIn('address_id', $address_ids)
- // ->where('pid', 0)
- ->orderBy('id');
- }
- public function headingRow(): int
- {
- return 2;
- }
- public function headings(): array
- {
- $room_name = '聚合名单';
- $data[] = [$room_name];
- $data[] = array_column($this->fields, 'name');
- return $data;
- }
- public function columnWidths(): array
- {
- $i = 65;
- $data = [];
- foreach ($this->fields as $k => $field) {
- $s = intval($k / 26);
- if (!$s) $key = chr($i + $k);
- if ($s > 0) {
- $y = intval($k % 26);
- $key = chr($s + $i - 1);
- $key .= chr($y + $i);
- }
- $data["{$key}"] = $field['width'];
- $this->lastC = $key;
- }
- return $data;
- }
- public function styles(Worksheet $sheet)
- {
- $last = $this->lastC;
- $cellRange = "A1:{$last}1";
- $sheet->getStyle($cellRange)->getFont()->setSize(12);
- $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
- // 文字居中
- $lastrow = $sheet->getHighestRow();
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
- $sheet->mergeCells("A1:{$last}1"); //合并
- $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
- }
- public function map($row): array
- {
- $row = $row->toArray();
- $this->rowNums++;
- // $jhi = $row['pid'];
- // if (!$jhi) $jhi = $row['id'];
- // $row = Message::query()->where('id', $row['message_id'])->with(['type', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'deal_department'])->first();
- // if (!$row) return [];
- $data = [];
- foreach (array_column($this->fields, 'name') as $k => $field) {
- if ($field == '序号') $data[$k] = $this->rowNums;
- if ($field == '事项来源') $data[$k] = isset($row['type']['name']) ? $row['type']['name'] : '';
- if ($field == '事项分类') {
- $category = [];
- if ($row['category_1']) $category[] = $row['category_1']['name'];
- if ($row['category_2']) $category[] = $row['category_2']['name'];
- if ($row['category_3']) $category[] = $row['category_3']['name'];
- if ($row['category_4']) $category[] = $row['category_4']['name'];
- if ($row['category_5']) $category[] = $row['category_5']['name'];
- $data[$k] = arr2str($category, '/');
- }
- if ($field == '反应人') $data[$k] = $row['name'];
- if ($field == '联系方式') $data[$k] = $row['mobile'];
- if ($field == '地址') $data[$k] = $row['address_name'];
- if ($field == '处理单位') $data[$k] = isset($row['deal_department']['name']) ? $row['deal_department']['name'] : '';
- if ($field == '受理时间') $data[$k] = $row['complain_date'];
- if ($field == '事项内容') $data[$k] = $row['body'];
- if ($field == '办理结果') $data[$k] = $row['deal_idea'];
- // if ($field == '办理时限') $data[$k] = $row['name'];
- if ($field == '办理进度') {
- $deal_name = '';
- switch ($row['deal_status']) {
- case DealStatusEnum::WAIT:
- $deal_name = '未办理';
- break;
- case DealStatusEnum::IN:
- $deal_name = '正在办理';
- break;
- case DealStatusEnum::OK:
- $deal_name = '已办结';
- break;
- }
- $data[$k] = $deal_name;
- }
- // if ($field == '聚合值') $data[$k] = $jhi;
- // if ($field == '满意度') $data[$k] = $row['name'];
- }
- return $data;
- }
- }
|