nurseSheet.php 4.3 KB

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