phoneOrder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Organization;
  7. use App\Models\Patient;
  8. use App\User;
  9. use Illuminate\Support\Collection;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. class phoneOrder implements ToCollection
  12. {
  13. /**
  14. * @param Collection $collection
  15. */
  16. public function collection(Collection $collection)
  17. {
  18. foreach ($collection as $row){
  19. if ($row[0] == '订单编号') continue;
  20. // [
  21. // 0 => "MNC20210131110722347"
  22. // 1 => "云南运营主体"
  23. // 2 => "王洛染"
  24. // 3 => "18669008017"
  25. // 4 => "姚正"
  26. // 5 => "昆明儿科联盟"
  27. // 6 => "云南省昆明市昆明市西山区前卫佳湖社区卫生服务站"
  28. // 7 => "全科医学科"
  29. // 8 => "否"
  30. // 9 => "已完成"
  31. // 10 => "已评价"
  32. // 11 => "否"
  33. // 12 => "19.99"
  34. // 13 => "0.00"
  35. // 14 => "19.99"
  36. // 15 => "19.99"
  37. // 16 => "微信支付"
  38. // 17 => "自费"
  39. // 18 => "已付款"
  40. // 19 => "2021-01-31 11:07:23"
  41. // ];
  42. //检查是否有该订单
  43. $isHave = $this->getValue(new Order(), ['order_sn' => $row[0]], 'id');
  44. if ($isHave) return null;
  45. $user_id = $this->getValue(new User(), ['phone' => $row[3]], 'id');
  46. if (empty($user_id)) {
  47. echo $row[3] . '用户缺失' . PHP_EOL;
  48. return null;
  49. }
  50. //患者信息
  51. $patientInfo = Patient::where(['name' => $row[2]])->first();
  52. if (empty($patientInfo)) {
  53. echo $row[2] . '患者缺失' . PHP_EOL;
  54. continue;
  55. }
  56. $org_name = substr($row[6],18);
  57. $org_id = $this->getValue(new Organization(), ['name' => $org_name], 'id');
  58. if (empty($org_id)) {
  59. echo $row[6] . '机构缺失' . PHP_EOL;
  60. continue;
  61. }
  62. //如果没有用户不导入该订单
  63. if (empty($user_id)) {
  64. Log::info('订单没有用户信息,订单编号: ' . $row[0] . PHP_EOL);
  65. continue;
  66. }
  67. //订单状态
  68. $status = Order::getStatus();
  69. //订单状态
  70. $paystatus = Order::getPayStatus();
  71. $pay_status = array_search($row[18], $paystatus);
  72. $order_status = array_search($row[9], $status);
  73. $docter_id = $this->getValue(new Docter(), ['name' => $row[4]], 'id');
  74. $evaluate = $row[10] == '已评价' ? 1 : 0;
  75. $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
  76. //支付方式
  77. $payType = array_search($row[16], $pay_types) + 1;
  78. if ($payType == 1) $payType = 2;
  79. $orderInfo = [
  80. 'order_sn' => $row[0],
  81. 'patient_id' => $patientInfo->id,
  82. 'user_id' => $user_id,
  83. 'docter_id' => $docter_id,
  84. 'order_status' => $order_status,
  85. 'organization_id' => $org_id,
  86. 'is_evaluate' => $evaluate,
  87. 'is_discount' => $row[11],
  88. 'total_amount' => intval($row[12]) * 100,
  89. 'discount_amount' => intval($row[13]) * 100,
  90. 'payment_amount' => intval($row[15]) * 100,
  91. 'payment_type' => $payType,
  92. 'payment_status' => $pay_status,
  93. 'product_type' => 1,
  94. 'created_at' => $row[19],
  95. ];
  96. $order = Order::create($orderInfo);
  97. $orderId = $order['id'];
  98. $orderPatient = [
  99. 'patient_id' => $patientInfo->id,
  100. 'name' => $row[0],
  101. 'order_id' => $orderId,
  102. 'patient_id' => $patientInfo->id,
  103. 'address' => $patientInfo->address,
  104. 'sex' => $patientInfo->sex,
  105. 'organization_id' => $org_id,
  106. ];
  107. OrderPatient::insert($orderPatient);
  108. }
  109. }
  110. public function getValue($model,$where,$field)
  111. {
  112. return $model->where($where)->value($field);
  113. }
  114. }