chatOrder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // dd($row);
  29. // [
  30. // 0 => "im20210118173813522",
  31. // 1 => "云南运营主体",
  32. // 2 => "熊振宇",
  33. // 3 => "13708872753",
  34. // 4 => "文菊焱",
  35. // 5 => "",
  36. // 6 => "云南省昆明市昆明市盘龙区联盟街道金康园社区卫生服务站",
  37. // 7 => "妇保科",
  38. // 8 => "否",
  39. // 9 => "进行中",
  40. // 10 => "",
  41. // 11 => "否",
  42. // 12 => "1.99",
  43. // 13 => "0.00",
  44. // 14 => "1.99",
  45. // 15 => "1.99",
  46. // 16 => "微信支付",
  47. // 17 => "单次图文咨询",
  48. // 18 => "已付款",
  49. // 19 => "2021-01-18 17:38:13",
  50. // ];
  51. //检查是否有该订单
  52. $isHave = $this->getValue(new Order(), ['order_sn' => $row[0]], 'id');
  53. if ($isHave) return null;
  54. $user_id = $this->getValue(new User(), ['phone' => $row[3]], 'id');
  55. if (empty($user_id)) {
  56. echo $row[3] . '用户缺失' . PHP_EOL;
  57. return null;
  58. }
  59. //患者信息
  60. $patientInfo = Patient::where(['name' => $row[4]])->first();
  61. if (empty($patientInfo)) {
  62. echo $row[4] . '患者缺失' . PHP_EOL;
  63. continue;
  64. }
  65. $org_id = $this->getValue(new Organization(), ['name' => $row[6]], 'id');
  66. if (empty($org_id)) {
  67. echo $row[6] . '机构缺失' . PHP_EOL;
  68. continue;
  69. }
  70. //如果没有用户不导入该订单
  71. if (empty($user_id)) {
  72. Log::info('订单没有用户信息,订单编号: ' . $row[0] . PHP_EOL);
  73. continue;
  74. }
  75. // 1 => "未支付"
  76. // 2 => "待支付"
  77. // 3 => "进行中"
  78. // 4 => "已完成"
  79. // 5 => "已取消"
  80. // 6 => "已超时"
  81. // 7 => "已预约"
  82. //订单状态
  83. $status = Order::getStatus();
  84. //订单状态
  85. $paystatus = Order::getPayStatus();
  86. $pay_status = array_search($row[18], $paystatus);
  87. $order_status = array_search($row[9], $status);
  88. $docter_id = $this->getValue(new Docter(), ['name' => $row[4]], 'id');
  89. $org_id = $this->getValue(new Organization(), ['name' => $row[6]], 'id');
  90. $evaluate = $row[10] == '已评价' ? 1 : 0;
  91. $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
  92. //支付方式
  93. $payType = array_search($row[16], $pay_types) + 1;
  94. if ($payType == 1) $payType = 2;
  95. $orderInfo = [
  96. 'order_sn' => $row[0],
  97. 'patient_id' => $patientInfo->id,
  98. 'user_id' => $user_id,
  99. 'docter_id' => $docter_id,
  100. 'order_status' => $order_status,
  101. 'organization_id' => $org_id,
  102. 'is_evaluate' => $evaluate,
  103. 'is_discount' => $row[11],
  104. 'total_amount' => $row[12] * 100,
  105. 'discount_amount' => $row[13] * 100,
  106. 'payment_amount' => $row[15] * 100,
  107. 'payment_type' => $payType,
  108. 'payment_status' => $pay_status,
  109. 'product_type' => 2,
  110. 'created_at' => $row[19],
  111. ];
  112. $order = Order::create($orderInfo);
  113. $orderId = $order['id'];
  114. $orderPatient = [
  115. 'patient_id' => $patientInfo->id,
  116. 'name' => $row[0],
  117. 'order_id' => $orderId,
  118. 'patient_id' => $patientInfo->id,
  119. 'address' => $patientInfo->address,
  120. 'sex' => $patientInfo->sex,
  121. 'organization_id' => $org_id,
  122. ];
  123. OrderPatient::insert($orderPatient);
  124. }
  125. return null;
  126. }
  127. public function startRow(): int
  128. {
  129. return 2;
  130. }
  131. public function batchSize(): int
  132. {
  133. return 1000;
  134. }
  135. public function chunkSize(): int
  136. {
  137. return 1000;
  138. }
  139. public function getValue($model,$where,$field)
  140. {
  141. return $model->where($where)->value($field);
  142. }
  143. }