*/ class InitRecipelsCommand extends Command { /** * The name of command. * * @var string */ protected $name = 'init:recipels'; /** * The description of command. * * @var string */ protected $description = 'Create a new recipels.'; /** * The type of class being generated. * * @var string */ protected $type = 'Recipels'; /** * Execute the command. * * @return void * @see fire() */ public function handle() { DB::table('tcm_recipels')->truncate(); DB::table('tcm_combinations')->truncate(); $prescriptions = Prescription::query()->get(); foreach ($prescriptions as $prescription) { $this->init($prescription); } $this->line('ok'); } public function init(Prescription $prescription) { Recipels::query()->where('prescription_id', $prescription->id)->delete(); $data = []; $prescription->load(['patient', 'medical_record']); foreach ($prescription->drugs as $drug) { $d = [ 'patient_id' => $prescription->patient_id, 'name' => $prescription->patient->name, 'sex' => $prescription->patient->sex, 'age' => $prescription->patient->age, 'medical_record_id' => $prescription->medical_record_id, 'chinese_tra_diagnosis' => $prescription->medical_record ? $prescription->medical_record->chinese_tra_diagnosis : '--', 'prescription_id' => $prescription->id, // 'drug_id' => isset($drug['drug']) ? $drug['drug']['id'] : 0, 'drug_id' => $drug['id'], 'drug' => $drug['name'], 'dose' => $drug['dose'], 'unit' => $drug['unit'], 'use' => isset($drug['use']) ? (int)$drug['use'] : 0, 'use_name' => isset($drug['use_name']) ? $drug['use_name'] : '', 'admin_id' => $prescription->admin_id ]; $data[] = $d; } Recipels::query()->insert($data); //组合分析 $names = array_column($prescription->drugs, 'name'); $combinations = CombinationService::combinationALL($names); $cd = []; foreach ($combinations as $combination) { $cd[] = [ 'name' => '-' . arr2str($combination, '-') . '-', 'count' => count($combination), 'prescription_id' => $prescription->id, 'admin_id' => $prescription->admin_id ]; } Combinations::query()->insert($cd); } }