vaccineSheet.php 4.1 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. }
  36. $vaccine_id = $this->getValue(new Vaccine(),['name'=>$row[3]],'id');
  37. //疫苗为空直接创建疫苗
  38. if(empty($vaccine_id)){
  39. $vaccine = ['name'=>$row[3],'org_id'=>$org_id,'type'=>1,'states'=>1];
  40. $vaccine_id = Vaccine::insertGetId($vaccine);
  41. }
  42. //患者信息
  43. $patientInfo = Patient::where(['name'=>$row[0]])->first();
  44. if(empty($patientInfo)){
  45. echo $row[0].'患者缺失'.PHP_EOL;
  46. continue;
  47. }
  48. if(empty($user_id)){
  49. echo $row[1].'用户不存在'.PHP_EOL;
  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. // $order_sn = build_sn($i);
  57. //todo 写入计免订单表,订单患者表
  58. $time = explode(' ',$row[4]);
  59. $appion_time = explode('-',$time[1]);
  60. $start =strtotime($time[0].$appion_time[0]);
  61. //预约时间大于今天
  62. ($start- 86400) > strtotime('today') ?$createTimer = strtotime('-1 days') : $createTimer = ($start- 86400) > strtotime('today');
  63. $created_at = date('Y-m-d H:i:s',$createTimer);
  64. $end = strtotime($time[0].$appion_time[1]);
  65. $orderInfo = [
  66. 'user_id'=>$user_id,
  67. 'patient_id'=>$patientInfo->id,
  68. 'docter_id'=>0,
  69. 'order_status'=>$order_status,
  70. 'organization_id'=>$org_id,
  71. 'product_type'=>4,
  72. 'created_at'=>$created_at
  73. ];
  74. $order = Order::create($orderInfo);
  75. $orderId = $order['id'];
  76. $order_sn = build_sn($order['id']);
  77. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  78. //没有患者名称不存数据
  79. if(empty($patientInfo)) continue;
  80. $orderPatient = [
  81. 'patient_id'=>$patientInfo->id,
  82. 'name'=>$row[0],
  83. 'order_id'=>$orderId,
  84. 'patient_id'=>$patientInfo->id,
  85. 'address'=>$patientInfo->address,
  86. 'sex'=>$patientInfo->sex,
  87. 'appoint_start_time'=>$start,
  88. 'appoint_end_time'=>$end,
  89. 'organization_id'=>$org_id,
  90. ];
  91. $orderPid = OrderPatient::insertGetId($orderPatient);
  92. $vaccineOrder = [
  93. 'vaccine_id'=>$vaccine_id,
  94. 'order_id'=>$orderId,
  95. 'order_patient_id'=>$orderPid,
  96. 'vaccine_name'=>$row[3],
  97. ];
  98. OrderVaccine::insert($vaccineOrder);
  99. }
  100. return null;
  101. }
  102. public function getValue($model,$where,$field)
  103. {
  104. return $model->where($where)->value($field);
  105. }
  106. }