123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace App\Imports;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\TCM\Category;
- use App\Repositories\Models\TCM\Drug;
- use App\Repositories\Models\TCM\MedicalRecord;
- use App\Repositories\Models\TCM\Patient;
- use App\Repositories\Models\TCM\Prescription;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Maatwebsite\Excel\Concerns\OnEachRow;
- use Maatwebsite\Excel\Row;
- use Overtrue\LaravelPinyin\Facades\Pinyin;
- class PrescriptionsImport implements OnEachRow
- {
- public function onRow(Row $row)
- {
- if ($row->getIndex() <= 1) return false;
- if (empty($row[1])) return false;
- 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;
- $sex = 0;
- switch ($sex_text) {
- case "男":
- case 'man':
- $sex = 1;
- break;
- case '女':
- case 'women':
- $sex = 2;
- break;
- }
- if (empty($name)) return false;
- $patient = [
- 'name' => $name,
- 'sex' => $sex,
- 'birth_date' => Carbon::now()->addYears(-$age)->toDateString(),
- ];
- $department_id = Category::query()->firstOrCreate(['title' => $department, 'parent_id' => 11], ['value' => $department]);
- $medical_record = [
- 'hospital' => $hospital,
- 'doctor' => $doctor,
- 'department' => $department_id->id,
- 'chinese_tra_diagnosis' => $chinese_tra_diagnosis,
- ];
- $eat_type = Prescription::EAT_TJ;
- switch ($eat) {
- case Prescription::$eatMap[Prescription::EAT_TJ]:
- $eat_type = Prescription::EAT_TJ;
- break;
- case Prescription::$eatMap[Prescription::EAT_WJ]:
- $eat_type = Prescription::EAT_WJ;
- break;
- }
- $advice_type = Prescription::ADVICE_TYPE_NF;
- switch ($advice_f) {
- case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_NF]:
- $advice_type = Prescription::ADVICE_TYPE_NF;
- break;
- case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_WF]:
- $advice_type = Prescription::ADVICE_TYPE_WF;
- break;
- case Prescription::$adviceTypeMap[Prescription::ADVICE_TYPE_GC]:
- $advice_type = Prescription::ADVICE_TYPE_GC;
- break;
- }
- $advice_frequency = array_search($advice_t, Prescription::$frequencyMap);
- list($one_consumption, $use_unit_t) = str2arr($one_consumption_T, '/');
- $use_unit = array_search($use_unit_t, Prescription::$useUnitMap);
- $drugsD = str2arr($drugs_t, ',');
- $drugs = [];
- foreach ($drugsD as $d) {
- list($dname, $ddose, $dunit) = str2arr($d, '/');
- $letter = Pinyin::abbr($dname);
- $drug = Drug::query()->firstOrCreate(['name' => $dname], [
- 'letter' => $letter,
- 'unit' => $dunit,
- 'initial' => substr($letter, 1),
- ]);
- $drug['source'] = 0;
- $drug['drug_type'] = $drug->type;
- $drug['dose'] = $ddose;
- $drug['use_name'] = '';
- $drug['use'] = '';
- // $drugs[] = [
- // 'source' => 0,
- // 'drug_type' => $drug ? $drug->type : 0,
- // 'name' => $dname,
- // 'dose' => $ddose,
- // 'use' => '',
- // 'use_name' => '',
- // 'unit' => $dunit,
- // 'drug' => $drug,
- // ];
- $drugs[] = $drug;
- }
- $ps[] = [
- 'eat_type' => $eat_type,
- 'number' => $nums,
- 'advice_type' => $advice_type,
- 'advice_frequency' => $advice_frequency,
- 'one_consumption' => $one_consumption,
- 'advice' => $advice,
- 'use_unit' => $use_unit,
- 'drugs' => $drugs,
- ];
- // foreach ($prescriptions as $prescription) {
- //// $ps[] = collect($prescription)->only(['eat_type', 'drug_type', 'number', 'advice_type', 'advice_frequency', 'one_consumption', 'use_unit', 'advice', 'drugs'])->toArray();
- // $ps[] = [
- //
- // ];
- // }
- $user_id = login_admin_id();
- DB::transaction(function () use ($patient, $medical_record, $ps, $user_id) {
- $patient['admin_id'] = $user_id;
- $medical_record['admin_id'] = $user_id;
- $patient_m = Patient::query()->create($patient);
- $medical_record['chief_complaint'] = $medical_record['chinese_tra_diagnosis'];
- $medical_record['patient_id'] = $patient_m->id;
- $medical_record_m = MedicalRecord::query()->create($medical_record);
- foreach ($ps as $p) {
- $p['admin_id'] = $user_id;
- $p['patient_id'] = $patient_m->id;
- $p['medical_record_id'] = $medical_record_m->id;
- Prescription::query()->create($p);
- }
- });
- return true;
- }
- }
|