'序号', 'width' => 8, ], [ 'name' => '姓名', 'width' => 15, ], [ 'name' => '性别', 'width' => 8, ], [ 'name' => '身份证号', 'width' => 30, ], [ 'name' => '年龄', 'width' => 8, ], [ 'name' => '住址', 'width' => 50, ], [ 'name' => '供应商', 'width' => 30, ], [ 'name' => '派遣单位', 'width' => 30, ], [ 'name' => '派遣工种', 'width' => 15, ], [ 'name' => '派遣日期', 'width' => 20, ], [ 'name' => '车费类型', 'width' => 15, ], [ 'name' => '车费', 'width' => 10, ], [ 'name' => '派遣包', 'width' => 30, ], ]; protected $rowNums = 0; protected $package_id = 0; protected $day = 0; protected $type = 0; protected $lastC = 'A'; protected $fields = []; protected $where = false; public function __construct($package_id = 0, $day = false, $type, $field_ids = [], $where = false) { $this->package_id = $package_id; $this->day = $day; $this->type = $type; $this->where = $where; $fields = []; if (is_string($field_ids)) { $fields = self::BaseFields; } else { foreach ($field_ids as $field_id) { $fields[] = self::BaseFields[$field_id]; } } $this->fields = $fields; } public function query() { $package_id = $this->package_id; $day = $this->day; $type = $this->type; $where = $this->where; return Dispatch::query() ->with(['human', 'factory', 'supplier', 'package']) ->where('status', ModelStatusEnum::OK) ->where('type', $type) ->when($package_id, function ($query) use ($package_id) { return $query->where('package_id', $package_id); })->when($day, function ($query) use ($day) { return $query->whereDate('dispatch_date', $day); }) ->when($where, function ($query) use ($where) { return $query->where($where); }) ->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++; $data = []; foreach (array_column($this->fields, 'name') as $k => $field) { if ($field == '序号') $data[$k] = $this->rowNums; if ($field == '姓名') { $data[$k] = $row['human'] ? $row['human']['name'] : ''; } if ($field == '身份证号') { $data[$k] = $row['human'] ? $row['human']['card_id'] . ' ' : ''; } if ($field == '性别') { if ($row['human']) { if ($row['human']['sex'] == 1) $data[$k] = '男'; if ($row['human']['sex'] == 2) $data[$k] = '女'; } else { $data[$k] = '未知'; } } if ($field == '年龄') { $data[$k] = $row['human'] ? $row['human']['age'] : ''; } if ($field == '住址') { $data[$k] = $row['human'] ? $row['human']['address'] : ''; } if ($field == '供应商') { $data[$k] = $row['supplier'] ? $row['supplier']['name'] : ''; } if ($field == '派遣单位') { $data[$k] = $row['factory'] ? $row['factory']['name'] : ''; } if ($field == '派遣工种') { $data[$k] = Dict::byCodeAndIdGetDict(Position::JOB_CODE, $row['job_position']); } if ($field == '派遣日期') $data[$k] = $row['dispatch_date']; if ($field == '车费类型') { if ($row['car_type'] == 1) $data[$k] = '免费'; if ($row['car_type'] == 2) $data[$k] = '已收'; if ($row['car_type'] == 3) $data[$k] = '未付'; } if ($field == '车费') { $data[$k] = $row['car_money']; } if ($field == '派遣包') $data[$k] = $row['package'] ? $row['package']['name'] : ''; // if ($field == '单价') $data[$k] = $row['human']['card_id']; // if ($field == '提成价') $data[$k] = $row['human']['card_id']; } return $data; } }