type = $type; $this->fields = $fields; } public function forIds(array $ids) { $this->ids = $ids; return $this; } public function query() { switch ($this->type) { case 0: return Prescription::query()->with(['patient', 'medical_record'])->whereIn('patient_id', $this->ids); break; case 1: return Prescription::query()->with(['patient', 'medical_record'])->whereIn('medical_record_id', $this->ids); break; case 2: return Prescription::query()->with(['patient', 'medical_record'])->whereIn('id', $this->ids); break; } } public function headings(): array { $bases = [ '序号', '姓名', '性别', '年龄', '就诊医院', '接诊医师', '就诊科室', '中医诊断', '处方类型', '处方', '处方剂量', '服用方式', '服用频次', '服用单次用量', '医嘱', ]; // return $bases; // $data[] = '序号'; foreach ($this->fields as $field) { $data[] = $bases[$field]; } return $data; } public function columnWidths(): array { $bases = [ 'A' => 8, 'B' => 30, 'C' => 10, 'D' => 30, 'E' => 15, 'F' => 40, 'G' => 40, 'H' => 30, 'I' => 30, 'J' => 10, 'K' => 15, 'L' => 15, 'M' => 15, 'N' => 15, 'O' => 15, 'P' => 15, // 'Q' => 30, // 'R' => 15, // 'S' => 8, // 'T' => 15, // 'U' => 20, // 'V' => 15, ]; $data['A'] = 8; $values = array_values($bases); $keys = array_keys($bases); foreach ($this->fields as $field) { $key = $keys[$field]; $data["{$key}"] = $values[$field]; } return $data; } public function styles(Worksheet $sheet) { $cellRange = 'A1:P1'; $sheet->getStyle($cellRange)->getFont()->setSize(12); $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高 // 文字居中 $lastrow = $sheet->getHighestRow(); $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中 $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setWrapText(true); } public function map($row): array { $this->rowNums++; $drugs = []; foreach ($row['drugs'] as $drug) { $use_name = array_key_exists('use_name', $drug) ? $drug['use_name'] : ''; $d = [ $drug['name'], $drug['dose'], $drug['unit'], $use_name ]; $drugs[] = arr2str($d, '/'); } Log::error($row['medical_record']); $department = $row['medical_record'] ? Category::query()->where('id', (int)$row['medical_record']['department'])->value('title') : '--'; Log::error($department); $bases = [ $this->rowNums, $row['patient'] ? $row['patient']['name'] : '--', SexEnum::getDescription($row['patient'] ? $row['patient']['sex'] : 0), $row['patient']['age'], $row['medical_record'] ? $row['medical_record']['hospital'] : '--', $row['medical_record'] ? $row['medical_record']['doctor'] : '--', $department, $row['medical_record'] ? $row['medical_record']['chinese_tra_diagnosis'] : '--', Prescription::$eatMap[$row['eat_type']], arr2str($drugs, ','), $row['number'], Prescription::$adviceTypeMap[$row['advice_type']], Prescription::$frequencyMap[$row['advice_frequency']], $row['one_consumption'] . Prescription::$useUnitMap[$row['use_unit']], $row['advice'] ]; // $data[] = $this->rowNums; foreach ($this->fields as $field) { $data[] = $bases[$field]; } return $data; } }