'序号', 'width' => 8, ], [ 'name' => '类型', 'width' => 8, ], [ 'name' => '姓名', 'width' => 15, ], [ 'name' => '身份证号', 'width' => 30, ], [ 'name' => '派遣单位', 'width' => 30, ], [ 'name' => '派遣工种', 'width' => 15, ], [ 'name' => '入职日期/离职日期', 'width' => 20, ], [ 'name' => '商业保险金额', 'width' => 15, ], [ 'name' => '状态', 'width' => 15, ] ]; protected $rowNums = 0; protected $id = 0; protected $day = 0; protected $type = 0; protected $lastC = 'A'; protected $fields = []; public function __construct($id = 0, $field_ids = []) { $this->id = $id; $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() { $id = $this->id; return InsuranceRecord::query() ->with(['human', 'factory', 'staff']) ->where('status', ModelStatusEnum::OK) ->where(function ($query) use ($id) { return $query->orWhere('insurance_id', $id)->orWhere('out_insurance_id', $id); }) ->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['out_insurance_id'] ? '离职' : '入职'; } if ($field == '姓名') { $data[$k] = $row['human'] ? $row['human']['name'] : ''; } if ($field == '身份证号') { $data[$k] = $row['human'] ? $row['human']['card_id'] . ' ' : ''; } 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['money']; if ($row['out_insurance_id']) { //离职状态 if ($field == '入职日期/离职日期') $data[$k] = $row['staff'] ? $row['staff']['quit_date'] : ''; if ($field == '状态') { if ($row['insurance_status'] == StaffInsuranceStatusEnum::WAIT) $data[$k] = '待购买'; if ($row['insurance_status'] == StaffInsuranceStatusEnum::BUY_OK) $data[$k] = '待退订'; if ($row['insurance_status'] == StaffInsuranceStatusEnum::OUT_OK) $data[$k] = '已退订'; } } else { if ($field == '入职日期/离职日期') $data[$k] = $row['staff'] ? $row['staff']['induction_date'] : ''; if ($field == '状态') { if ($row['insurance_status'] == StaffInsuranceStatusEnum::WAIT) $data[$k] = '待购买'; if ($row['insurance_status'] == StaffInsuranceStatusEnum::BUY_OK) $data[$k] = '已购买'; if ($row['insurance_status'] == StaffInsuranceStatusEnum::OUT_OK) $data[$k] = '已退订'; } } } return $data; } }