chatOrder.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Imports\Order;
  3. use AlibabaCloud\Gpdb\V20160503\DeleteDatabase;
  4. use App\Models\Docter;
  5. use App\Models\Order;
  6. use App\Models\OrderPatient;
  7. use App\Models\Organization;
  8. use App\Models\Patient;
  9. use App\User;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\Log;
  12. use Maatwebsite\Excel\Concerns\ToCollection;
  13. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  14. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  15. use Maatwebsite\Excel\Concerns\WithChunkReading;
  16. use Maatwebsite\Excel\Concerns\WithProgressBar;
  17. class chatOrder implements ToCollection,WithBatchInserts,WithChunkReading
  18. {
  19. /**
  20. * @param array $row
  21. *
  22. * @return \Illuminate\Database\Eloquent\Model|null
  23. */
  24. public function collection(Collection $collection)
  25. {
  26. foreach ($collection as $row) {
  27. if ($row[0] == '订单编号') continue;
  28. //检查是否有该订单
  29. $isHave = $this->getValue(new Order(), ['order_sn' => $row[0]], 'id');
  30. if ($isHave) continue;
  31. $user_id = $this->getValue(new User(), ['phone' => $row[3]], 'id');
  32. if (empty($user_id)) {
  33. echo $row[3] . '用户缺失' . PHP_EOL;
  34. continue;
  35. }
  36. //患者信息
  37. $patientInfo = Patient::where(['name' => $row[2]])->first();
  38. if (empty($patientInfo)) {
  39. // $pp = Patient::where('user_id',$user_id)->get()->toArray();
  40. echo $row[2] . '患者缺失' . PHP_EOL;
  41. continue;
  42. }
  43. $org_name = substr($row[6],18);
  44. $org_id = $this->getValue(new Organization(), ['name' => $org_name], 'id');
  45. if (empty($org_id)) {
  46. echo $row[6] . '机构缺失' . PHP_EOL;
  47. continue;
  48. }
  49. //如果没有用户不导入该订单
  50. if (empty($user_id)) {
  51. Log::info('订单没有用户信息,订单编号: ' . $row[0] . PHP_EOL);
  52. continue;
  53. }
  54. //订单状态
  55. $status = Order::getStatus();
  56. //订单状态
  57. $paystatus = Order::getPayStatus();
  58. $pay_status = array_search($row[18], $paystatus);
  59. $order_status = array_search($row[9], $status);
  60. if($row[9] == '已过期') $order_status = 6;
  61. if($row[18] == '未付款' || empty($row[18])) $pay_status = 1;
  62. $docter_id = $this->getValue(new Docter(), ['name' => $row[4]], 'id');
  63. $evaluate = $row[10] == '已评价' ? 1 : 0;
  64. $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
  65. //支付方式
  66. $payType = array_search($row[16], $pay_types) + 1;
  67. if ($payType == 1) $payType = 2;
  68. $orderInfo = [
  69. 'order_sn' => $row[0],
  70. 'patient_id' => $patientInfo->id,
  71. 'user_id' => $user_id,
  72. 'docter_id' => $docter_id,
  73. 'order_status' => $order_status,
  74. 'organization_id' => $org_id,
  75. 'is_evaluate' => $evaluate,
  76. 'is_discount' => $row[11],
  77. 'total_amount' => $row[12] * 100,
  78. 'discount_amount' => $row[13] * 100,
  79. 'payment_amount' => $row[15] * 100,
  80. 'payment_type' => $payType,
  81. 'payment_status' => $pay_status,
  82. 'product_type' => 2,
  83. 'created_at' => $row[19],
  84. ];
  85. $order = Order::create($orderInfo);
  86. $orderId = $order['id'];
  87. $orderPatient = [
  88. 'patient_id' => $patientInfo->id,
  89. 'name' => $row[0],
  90. 'order_id' => $orderId,
  91. 'patient_id' => $patientInfo->id,
  92. 'address' => $patientInfo->address,
  93. 'sex' => $patientInfo->sex,
  94. 'organization_id' => $org_id,
  95. ];
  96. OrderPatient::insert($orderPatient);
  97. }
  98. return null;
  99. }
  100. public function startRow(): int
  101. {
  102. return 2;
  103. }
  104. public function batchSize(): int
  105. {
  106. return 1000;
  107. }
  108. public function chunkSize(): int
  109. {
  110. return 1000;
  111. }
  112. public function getValue($model,$where,$field)
  113. {
  114. return $model->where($where)->value($field);
  115. }
  116. }