123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Exports;
- use App\Repositories\Enums\SexEnum;
- use App\Repositories\Models\TCM\Category;
- use App\Repositories\Models\TCM\Prescription;
- use App\Repositories\Models\User\Student;
- use Illuminate\Support\Facades\Log;
- 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 PatientExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- protected $rowNums = 0;
- protected $ids = [];
- protected $fields = [];
- protected $type = 0;
- public function __construct(array $fields, $type)
- {
- $this->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;
- }
- }
|