InitRecipelsCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\Models\TCM\Combinations;
  4. use App\Repositories\Models\TCM\Prescription;
  5. use App\Repositories\Models\TCM\Recipels;
  6. use App\Services\CombinationService;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * Class PresenterCommand
  11. * @package Prettus\Repository\Generators\Commands
  12. * @author Anderson Andrade <contato@andersonandra.de>
  13. */
  14. class InitRecipelsCommand extends Command
  15. {
  16. /**
  17. * The name of command.
  18. *
  19. * @var string
  20. */
  21. protected $name = 'init:recipels';
  22. /**
  23. * The description of command.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Create a new recipels.';
  28. /**
  29. * The type of class being generated.
  30. *
  31. * @var string
  32. */
  33. protected $type = 'Recipels';
  34. /**
  35. * Execute the command.
  36. *
  37. * @return void
  38. * @see fire()
  39. */
  40. public function handle()
  41. {
  42. DB::table('tcm_recipels')->truncate();
  43. DB::table('tcm_combinations')->truncate();
  44. $prescriptions = Prescription::query()->get();
  45. foreach ($prescriptions as $prescription) {
  46. $this->init($prescription);
  47. }
  48. $this->line('ok');
  49. }
  50. public function init(Prescription $prescription)
  51. {
  52. Recipels::query()->where('prescription_id', $prescription->id)->delete();
  53. $data = [];
  54. $prescription->load(['patient', 'medical_record']);
  55. foreach ($prescription->drugs as $drug) {
  56. $d = [
  57. 'patient_id' => $prescription->patient_id,
  58. 'name' => $prescription->patient->name,
  59. 'sex' => $prescription->patient->sex,
  60. 'age' => $prescription->patient->age,
  61. 'medical_record_id' => $prescription->medical_record_id,
  62. 'chinese_tra_diagnosis' => $prescription->medical_record ? $prescription->medical_record->chinese_tra_diagnosis : '--',
  63. 'prescription_id' => $prescription->id,
  64. // 'drug_id' => isset($drug['drug']) ? $drug['drug']['id'] : 0,
  65. 'drug_id' => $drug['id'],
  66. 'drug' => $drug['name'],
  67. 'dose' => $drug['dose'],
  68. 'unit' => $drug['unit'],
  69. 'use' => isset($drug['use']) ? (int)$drug['use'] : 0,
  70. 'use_name' => isset($drug['use_name']) ? $drug['use_name'] : '',
  71. 'admin_id' => $prescription->admin_id
  72. ];
  73. $data[] = $d;
  74. }
  75. Recipels::query()->insert($data);
  76. //组合分析
  77. $names = array_column($prescription->drugs, 'name');
  78. $combinations = CombinationService::combinationALL($names);
  79. $cd = [];
  80. foreach ($combinations as $combination) {
  81. $cd[] = [
  82. 'name' => '-' . arr2str($combination, '-') . '-',
  83. 'count' => count($combination),
  84. 'prescription_id' => $prescription->id,
  85. 'admin_id' => $prescription->admin_id
  86. ];
  87. }
  88. Combinations::query()->insert($cd);
  89. }
  90. }