vaccineSheet.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Imports\Order;
  3. use App\Models\Docter;
  4. use App\Models\Order;
  5. use App\Models\OrderPatient;
  6. use App\Models\OrderVaccine;
  7. use App\Models\Organization;
  8. use App\Models\Patient;
  9. use App\Models\Vaccine;
  10. use App\User;
  11. use Illuminate\Support\Collection;
  12. use Maatwebsite\Excel\Concerns\ToCollection;
  13. use Illuminate\Support\Facades\Log;
  14. class vaccineSheet implements ToCollection
  15. {
  16. /**
  17. * @param Collection $collection
  18. */
  19. public function collection(Collection $collection)
  20. {
  21. // todo 缺少患者信息导致患者信息无法确定,支付方式,无订单编号,无法去重,没机构,没用户如何处理
  22. $i = 1;
  23. foreach ($collection as $row) {
  24. if($row[0] == '就诊人姓名') continue;
  25. $user_id = $this->getValue(new User(),['phone'=>$row[1]],'id');
  26. if(empty($user_id)){
  27. echo $row[1].'用户缺失'.PHP_EOL;
  28. continue;
  29. }
  30. $org_id = $this->getValue(new Organization(),['name'=>$row[2]],'id');
  31. if(empty($org_id)){
  32. echo $row[2].'机构缺失'.PHP_EOL;
  33. //todo 没有机构直接跳过
  34. continue;
  35. $org_id = 0;
  36. }
  37. $vaccine_id = $this->getValue(new Vaccine(),['name'=>$row[3]],'id');
  38. //疫苗为空直接创建疫苗
  39. if(empty($vaccine_id)){
  40. $vaccine = ['name'=>$row[3],'org_id'=>$org_id,'type'=>1,'states'=>1];
  41. $vaccine_id = Vaccine::insertGetId($vaccine);
  42. }
  43. //患者信息
  44. $patientInfo = Patient::where(['name'=>$row[0]])->first();
  45. if(empty($patientInfo)){
  46. echo $row[0].'患者缺失'.PHP_EOL;
  47. continue;
  48. }
  49. if(empty($user_id)){
  50. Log::info('订单没有用户信息: '.$row[0].' 电话:'.$row[1].' 时间:'.$row[4].PHP_EOL);
  51. continue;
  52. }
  53. $status = Order::getStatus();
  54. //订单状态
  55. $order_status = array_search($row[5],$status);
  56. if(empty($org_id)) $org_id = 0;
  57. // $order_sn = build_sn($i);
  58. //todo 写入计免订单表,订单患者表
  59. $time = explode(' ',$row[4]);
  60. $appion_time = explode('-',$time[1]);
  61. $start =strtotime($time[0].$appion_time[0]);
  62. //预约时间大于今天
  63. ($start- 86400) > strtotime('today') ?$createTimer = strtotime('-1 days') : $createTimer = ($start- 86400) > strtotime('today');
  64. $created_at = date('Y-m-d H:i:s',$createTimer);
  65. $end = strtotime($time[0].$appion_time[1]);
  66. $orderInfo = [
  67. 'user_id'=>$user_id,
  68. 'patient_id'=>$patientInfo->id,
  69. 'docter_id'=>0,
  70. 'order_status'=>$order_status,
  71. 'organization_id'=>$org_id,
  72. 'product_type'=>4,
  73. 'created_at'=>$created_at
  74. ];
  75. $order = Order::create($orderInfo);
  76. $orderId = $order['id'];
  77. $order_sn = build_sn($order['id']);
  78. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  79. //没有患者名称不存数据
  80. if(empty($patientInfo)) continue;
  81. $orderPatient = [
  82. 'patient_id'=>$patientInfo->id,
  83. 'name'=>$row[0],
  84. 'order_id'=>$orderId,
  85. 'patient_id'=>$patientInfo->id,
  86. 'address'=>$patientInfo->address,
  87. 'sex'=>$patientInfo->sex,
  88. 'appoint_start_time'=>$start,
  89. 'appoint_end_time'=>$end,
  90. 'organization_id'=>$org_id,
  91. ];
  92. $orderPid = OrderPatient::insertGetId($orderPatient);
  93. $vaccineOrder = [
  94. 'vaccine_id'=>$vaccine_id,
  95. 'order_id'=>$orderId,
  96. 'order_patient_id'=>$orderPid,
  97. 'vaccine_name'=>$row[3],
  98. ];
  99. return OrderVaccine::insert($vaccineOrder);
  100. }
  101. }
  102. public function getValue($model,$where,$field)
  103. {
  104. return $model->where($where)->value($field);
  105. }
  106. }