123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace App\Exports\Ant;
- use App\Repositories\Models\Ant\Record;
- 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 RecordExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- const BaseFields = [
- [
- 'name' => '序号',
- 'width' => 8,
- ],
- [
- 'name' => '省(州)',
- 'width' => 15,
- ],
- [
- 'name' => '市',
- 'width' => 15,
- ],
- [
- 'name' => '县',
- 'width' => 15,
- ],
- [
- 'name' => '工程名称',
- 'width' => 30,
- ],
- [
- 'name' => '工程地址',
- 'width' => 30,
- ],
- [
- 'name' => '工程等级',
- 'width' => 20,
- ],
- [
- 'name' => '工程类型(坝型或堤形)',
- 'width' => 20,
- ],
- [
- 'name' => '单元',
- 'width' => 20,
- ],
- [
- 'name' => '害堤动物种类',
- 'width' => 20,
- ],
- [
- 'name' => '蚁患区面积',
- 'width' => 20,
- ],
- [
- 'name' => '蚁源区面积',
- 'width' => 20,
- ],
- // [
- // 'name' => '贯穿性议道',
- // 'width' => 20,
- // ],
- [
- 'name' => '对工程的影响',
- 'width' => 20,
- ],
- [
- 'name' => '检测到的蚁种',
- 'width' => 20,
- ],
- [
- 'name' => '危害等级',
- 'width' => 20,
- ],
- ];
- protected $rowNums = 0;
- protected $ids = [];
- protected $lastC = 'A';
- protected $fields = [];
- public function __construct($ids, $field_ids = [])
- {
- $this->ids = $ids;
- $fields = [];
- if (!count($field_ids)) {
- $fields = self::BaseFields;
- } else {
- foreach ($field_ids as $field_id) {
- $fields[] = self::BaseFields[$field_id];
- }
- }
- $this->fields = $fields;
- }
- public function query()
- {
- return Record::query()
- ->with(['user', 'company'])
- ->whereIn('id', $this->ids)
- ->orderByDesc('id');
- }
- public function map($row): array
- {
- $row = $row->toArray();
- $this->rowNums++;
- $data = [];
- foreach (array_column($this->fields, 'name') as $k => $field) {
- if ($field == '序号') $data[$k] = $this->rowNums;
- if ($field == '省(州)') $data[$k] = $row['province'];
- if ($field == '市') $data[$k] = $row['city'];
- if ($field == '县') $data[$k] = $row['area'];
- if ($field == '工程名称') $data[$k] = $row['project_name'];
- if ($field == '工程地址') $data[$k] = $row['province'] . $row['city'] . $row['area'] . $row['zhen'] . $row['cun'];
- if ($field == '工程等级') $data[$k] = $row['project_level_name'];
- if ($field == '工程类型(坝型或堤形)') $data[$k] = $row['project_dam_type_name'];
- if ($field == '单元') $data[$k] = $row['danger_cell_code'];
- if ($field == '害堤动物种类') {
- $danger_animal_extend = $row['danger_animal_extend'];
- $msg = [];
- foreach ($danger_animal_extend as $item) {
- if (array_key_exists('isWrite', $item) && $item['isWrite']) {
- $msg[] = $item['name'];
- }
- }
- $data[$k] = arr2str($msg);
- }
- if ($field == '蚁患区面积') $data[$k] = $row['danger_area'];
- if ($field == '蚁源区面积') $data[$k] = $row['area_area'];
- if ($field == '贯穿性议道') $data[$k] = $row['danger_is_penetrate'] ? '是' : '否';
- if ($field == '对工程的影响') $data[$k] = $row['is_project_danger'];
- if ($field == '检测到的蚁种') {
- $extend = $row['danger_types'];
- $msg = [];
- foreach ($extend['select'] as $item) {
- if (array_key_exists('checked', $item) && $item['checked']) {
- $msg[] = $item['name'];
- }
- }
- if ($extend['others']) $msg[] = $extend['others'];
- $data[$k] = arr2str($msg);
- }
- if ($field == '危害等级') $data[$k] = $row['danger_level'];
- }
- return $data;
- }
- public function headings(): array
- {
- $room_name = '白蚁普查台账';
- $data[] = [$room_name];
- $data[] = array_column($this->fields, 'name');
- return $data;
- }
- public function headingRow(): int
- {
- return 2;
- }
- 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);
- }
- }
|