PatientExport.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Exports;
  3. use App\Repositories\Enums\SexEnum;
  4. use App\Repositories\Models\TCM\Category;
  5. use App\Repositories\Models\TCM\Prescription;
  6. use App\Repositories\Models\User\Student;
  7. use Illuminate\Support\Facades\Log;
  8. use Maatwebsite\Excel\Concerns\Exportable;
  9. use Maatwebsite\Excel\Concerns\FromQuery;
  10. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  11. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  12. use Maatwebsite\Excel\Concerns\WithHeadings;
  13. use Maatwebsite\Excel\Concerns\WithMapping;
  14. use Maatwebsite\Excel\Concerns\WithStyles;
  15. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  16. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  17. class PatientExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  18. {
  19. use Exportable;
  20. protected $rowNums = 0;
  21. protected $ids = [];
  22. protected $fields = [];
  23. protected $type = 0;
  24. public function __construct(array $fields, $type)
  25. {
  26. $this->type = $type;
  27. $this->fields = $fields;
  28. }
  29. public function forIds(array $ids)
  30. {
  31. $this->ids = $ids;
  32. return $this;
  33. }
  34. public function query()
  35. {
  36. switch ($this->type) {
  37. case 0:
  38. return Prescription::query()->with(['patient', 'medical_record'])->whereIn('patient_id', $this->ids);
  39. break;
  40. case 1:
  41. return Prescription::query()->with(['patient', 'medical_record'])->whereIn('medical_record_id', $this->ids);
  42. break;
  43. case 2:
  44. return Prescription::query()->with(['patient', 'medical_record'])->whereIn('id', $this->ids);
  45. break;
  46. }
  47. }
  48. public function headings(): array
  49. {
  50. $bases = [
  51. '序号',
  52. '姓名',
  53. '性别',
  54. '年龄',
  55. '就诊医院',
  56. '接诊医师',
  57. '就诊科室',
  58. '中医诊断',
  59. '处方类型',
  60. '处方',
  61. '处方剂量',
  62. '服用方式',
  63. '服用频次',
  64. '服用单次用量',
  65. '医嘱',
  66. ];
  67. // return $bases;
  68. // $data[] = '序号';
  69. foreach ($this->fields as $field) {
  70. $data[] = $bases[$field];
  71. }
  72. return $data;
  73. }
  74. public function columnWidths(): array
  75. {
  76. $bases = [
  77. 'A' => 8,
  78. 'B' => 30,
  79. 'C' => 10,
  80. 'D' => 30,
  81. 'E' => 15,
  82. 'F' => 40,
  83. 'G' => 40,
  84. 'H' => 30,
  85. 'I' => 30,
  86. 'J' => 10,
  87. 'K' => 15,
  88. 'L' => 15,
  89. 'M' => 15,
  90. 'N' => 15,
  91. 'O' => 15,
  92. 'P' => 15,
  93. // 'Q' => 30,
  94. // 'R' => 15,
  95. // 'S' => 8,
  96. // 'T' => 15,
  97. // 'U' => 20,
  98. // 'V' => 15,
  99. ];
  100. $data['A'] = 8;
  101. $values = array_values($bases);
  102. $keys = array_keys($bases);
  103. foreach ($this->fields as $field) {
  104. $key = $keys[$field];
  105. $data["{$key}"] = $values[$field];
  106. }
  107. return $data;
  108. }
  109. public function styles(Worksheet $sheet)
  110. {
  111. $cellRange = 'A1:P1';
  112. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  113. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  114. // 文字居中
  115. $lastrow = $sheet->getHighestRow();
  116. $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  117. $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  118. $sheet->getStyle('A1:P' . $lastrow)->getAlignment()->setWrapText(true);
  119. }
  120. public function map($row): array
  121. {
  122. $this->rowNums++;
  123. $drugs = [];
  124. foreach ($row['drugs'] as $drug) {
  125. $use_name = array_key_exists('use_name', $drug) ? $drug['use_name'] : '';
  126. $d = [
  127. $drug['name'],
  128. $drug['dose'],
  129. $drug['unit'],
  130. $use_name
  131. ];
  132. $drugs[] = arr2str($d, '/');
  133. }
  134. Log::error($row['medical_record']);
  135. $department = $row['medical_record'] ? Category::query()->where('id', (int)$row['medical_record']['department'])->value('title') : '--';
  136. Log::error($department);
  137. $bases = [
  138. $this->rowNums,
  139. $row['patient'] ? $row['patient']['name'] : '--',
  140. SexEnum::getDescription($row['patient'] ? $row['patient']['sex'] : 0),
  141. $row['patient']['age'],
  142. $row['medical_record'] ? $row['medical_record']['hospital'] : '--',
  143. $row['medical_record'] ? $row['medical_record']['doctor'] : '--',
  144. $department,
  145. $row['medical_record'] ? $row['medical_record']['chinese_tra_diagnosis'] : '--',
  146. Prescription::$eatMap[$row['eat_type']],
  147. arr2str($drugs, ','),
  148. $row['number'],
  149. Prescription::$adviceTypeMap[$row['advice_type']],
  150. Prescription::$frequencyMap[$row['advice_frequency']],
  151. $row['one_consumption'] . Prescription::$useUnitMap[$row['use_unit']],
  152. $row['advice']
  153. ];
  154. // $data[] = $this->rowNums;
  155. foreach ($this->fields as $field) {
  156. $data[] = $bases[$field];
  157. }
  158. return $data;
  159. }
  160. }