phoneOrder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $isHave = $this->getValue(new Order(), ['order_sn' => $row[0]], 'id');
  22. if ($isHave) continue;
  23. //支付状态(1.待付款 2.已付款 3.退款中 4.已退款 5.待退款)
  24. //如果没有用户不导入该订单
  25. $user_id = $this->getValue(new User(), ['phone' => $row[3]], 'id');
  26. if (empty($user_id)) {
  27. echo $row[3] . '用户缺失' . PHP_EOL;
  28. continue;
  29. }
  30. //患者信息
  31. $patientInfo = Patient::where(['name' => $row[2]])->first();
  32. if (empty($patientInfo)) {
  33. echo $row[2] . '患者缺失' . PHP_EOL;
  34. continue;
  35. }
  36. $org_name = substr($row[6],18);
  37. $org_id = $this->getValue(new Organization(), ['name' => $org_name], 'id');
  38. if (empty($org_id)) {
  39. echo $row[6] . '机构缺失' . PHP_EOL;
  40. continue;
  41. }
  42. //订单状态
  43. $status = Order::getStatus();
  44. //订单状态
  45. $paystatus = Order::getPayStatus();
  46. $pay_status = array_search($row[18], $paystatus);
  47. $order_status = array_search($row[9], $status);
  48. if( $row[18] == '已退款') $pay_status = 4;
  49. if($row[9] == '已过期') $order_status = 6;
  50. $docter_id = $this->getValue(new Docter(), ['name' => $row[4]], 'id');
  51. $evaluate = $row[10] == '已评价' ? 1 : 0;
  52. $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
  53. //支付方式
  54. $payType = array_search($row[16], $pay_types) + 1;
  55. if ($payType == 1) $payType = 2;
  56. $orderInfo = [
  57. 'order_sn' => $row[0],
  58. 'patient_id' => $patientInfo->id,
  59. 'user_id' => $user_id,
  60. 'docter_id' => $docter_id,
  61. 'order_status' => $order_status,
  62. 'organization_id' => $org_id,
  63. 'is_evaluate' => $evaluate,
  64. 'is_discount' => $row[11],
  65. 'total_amount' => intval($row[12]) * 100,
  66. 'discount_amount' => intval($row[13]) * 100,
  67. 'payment_amount' => intval($row[15]) * 100,
  68. 'payment_type' => $payType,
  69. 'payment_status' => $pay_status,
  70. 'product_type' => 1,
  71. 'created_at' => $row[19],
  72. ];
  73. $order = Order::create($orderInfo);
  74. $orderId = $order['id'];
  75. $orderPatient = [
  76. 'patient_id' => $patientInfo->id,
  77. 'name' => $row[2],
  78. 'order_id' => $orderId,
  79. 'patient_id' => $patientInfo->id,
  80. 'address' => $patientInfo->address,
  81. 'sex' => $patientInfo->sex,
  82. 'organization_id' => $org_id,
  83. ];
  84. OrderPatient::insert($orderPatient);
  85. }
  86. return null;
  87. }
  88. public function getValue($model,$where,$field)
  89. {
  90. return $model->where($where)->value($field);
  91. }
  92. }