PrescriptionsImport.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Imports;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\TCM\Category;
  5. use App\Repositories\Models\TCM\Drug;
  6. use App\Repositories\Models\TCM\MedicalRecord;
  7. use App\Repositories\Models\TCM\Patient;
  8. use App\Repositories\Models\TCM\Prescription;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\DB;
  11. use Maatwebsite\Excel\Concerns\OnEachRow;
  12. use Maatwebsite\Excel\Row;
  13. use Overtrue\LaravelPinyin\Facades\Pinyin;
  14. class PrescriptionsImport implements OnEachRow
  15. {
  16. public function onRow(Row $row)
  17. {
  18. if ($row->getIndex() <= 1) return false;
  19. if (empty($row[1])) return false;
  20. list($index, $name, $sex_text, $age, $hospital, $doctor, $department, $chinese_tra_diagnosis, $eat, $drugs_t, $nums, $advice_t, $advice_f, $one_consumption_T, $advice) = $row;
  21. $sex = 0;
  22. switch ($sex_text) {
  23. case "男":
  24. case 'man':
  25. $sex = 1;
  26. break;
  27. case '女':
  28. case 'women':
  29. $sex = 2;
  30. break;
  31. }
  32. if (empty($name)) return false;
  33. $patient = [
  34. 'name' => $name,
  35. 'sex' => $sex,
  36. 'birth_date' => Carbon::now()->addYears(-$age)->toDateString(),
  37. ];
  38. $department_id = Category::query()->firstOrCreate(['title' => $department, 'parent_id' => 11], ['value' => $department]);
  39. $medical_record = [
  40. 'hospital' => $hospital,
  41. 'doctor' => $doctor,
  42. 'department' => $department_id->id,
  43. 'chinese_tra_diagnosis' => $chinese_tra_diagnosis,
  44. ];
  45. $eat_type = Prescription::EAT_TJ;
  46. switch ($eat) {
  47. case Prescription::$eatMap[Prescription::EAT_TJ]:
  48. $eat_type = Prescription::EAT_TJ;
  49. break;
  50. case Prescription::$eatMap[Prescription::EAT_WJ]:
  51. $eat_type = Prescription::EAT_WJ;
  52. break;
  53. }
  54. $advice_type = Prescription::ADVICE_TYPE_NF;
  55. switch ($advice_f) {
  56. case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_NF]:
  57. $advice_type = Prescription::ADVICE_TYPE_NF;
  58. break;
  59. case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_WF]:
  60. $advice_type = Prescription::ADVICE_TYPE_WF;
  61. break;
  62. case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_GC]:
  63. $advice_type = Prescription::ADVICE_TYPE_GC;
  64. break;
  65. }
  66. $advice_frequency = array_search($advice_t, Prescription::$frequencyMap);
  67. list($one_consumption, $use_unit_t) = str2arr($one_consumption_T, '/');
  68. $use_unit = array_search($use_unit_t, Prescription::$useUnitMap);
  69. $drugsD = str2arr($drugs_t, ',');
  70. $drugs = [];
  71. foreach ($drugsD as $d) {
  72. list($dname, $ddose, $dunit) = str2arr($d, '/');
  73. $letter = Pinyin::abbr($dname);
  74. $drug = Drug::query()->firstOrCreate(['name' => $dname], [
  75. 'letter' => $letter,
  76. 'unit' => $dunit,
  77. 'initial' => substr($letter, 1),
  78. ]);
  79. $drug['source'] = 0;
  80. $drug['drug_type'] = $drug->type;
  81. $drug['dose'] = $ddose;
  82. $drug['use_name'] = '';
  83. $drug['use'] = '';
  84. // $drugs[] = [
  85. // 'source' => 0,
  86. // 'drug_type' => $drug ? $drug->type : 0,
  87. // 'name' => $dname,
  88. // 'dose' => $ddose,
  89. // 'use' => '',
  90. // 'use_name' => '',
  91. // 'unit' => $dunit,
  92. // 'drug' => $drug,
  93. // ];
  94. $drugs[] = $drug;
  95. }
  96. $ps[] = [
  97. 'eat_type' => $eat_type,
  98. 'number' => $nums,
  99. 'advice_type' => $advice_type,
  100. 'advice_frequency' => $advice_frequency,
  101. 'one_consumption' => $one_consumption,
  102. 'advice' => $advice,
  103. 'use_unit' => $use_unit,
  104. 'drugs' => $drugs,
  105. ];
  106. // foreach ($prescriptions as $prescription) {
  107. //// $ps[] = collect($prescription)->only(['eat_type', 'drug_type', 'number', 'advice_type', 'advice_frequency', 'one_consumption', 'use_unit', 'advice', 'drugs'])->toArray();
  108. // $ps[] = [
  109. //
  110. // ];
  111. // }
  112. $user_id = login_admin_id();
  113. DB::transaction(function () use ($patient, $medical_record, $ps, $user_id) {
  114. $patient['admin_id'] = $user_id;
  115. $medical_record['admin_id'] = $user_id;
  116. $patient_m = Patient::query()->create($patient);
  117. $medical_record['chief_complaint'] = $medical_record['chinese_tra_diagnosis'];
  118. $medical_record['patient_id'] = $patient_m->id;
  119. $medical_record_m = MedicalRecord::query()->create($medical_record);
  120. foreach ($ps as $p) {
  121. $p['admin_id'] = $user_id;
  122. $p['patient_id'] = $patient_m->id;
  123. $p['medical_record_id'] = $medical_record_m->id;
  124. Prescription::query()->create($p);
  125. }
  126. });
  127. return true;
  128. }
  129. }