chatOrder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Models\UserBalanceLog;
  10. use App\User;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Facades\Log;
  13. use Maatwebsite\Excel\Concerns\ToCollection;
  14. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  15. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  16. use Maatwebsite\Excel\Concerns\WithChunkReading;
  17. use Maatwebsite\Excel\Concerns\WithProgressBar;
  18. class chatOrder implements ToCollection,WithBatchInserts,WithChunkReading
  19. {
  20. /**
  21. * @param array $row
  22. *
  23. * @return \Illuminate\Database\Eloquent\Model|null
  24. */
  25. public function collection(Collection $collection)
  26. {
  27. foreach ($collection as $row) {
  28. if ($row[0] == '订单编号') continue;
  29. //订单状态
  30. $status = Order::getStatus();
  31. //订单状态
  32. $paystatus = Order::getPayStatus();
  33. $pay_status = array_search($row[18], $paystatus);
  34. $order_status = array_search($row[9], $status);
  35. if($row[9] == '已过期') $order_status = 6;
  36. if($row[18] == '未付款' || empty($row[18])) $pay_status = 1;
  37. $pay_types = [1 => '微信支付', 2 => '余额支付', 3 => '服务包支付'];
  38. //支付方式
  39. $payType = array_search($row[16], $pay_types) + 1;
  40. if ($payType == 1) $payType = 2;
  41. $order = Order::where('order_sn',$row[0])->first();
  42. if(!empty($order)){
  43. $orderInfo = [
  44. 'order_sn' => $row[0],
  45. 'order_status' => $order_status,
  46. 'order_notes' => '',
  47. 'payment_status'=>$pay_status,
  48. ];
  49. $old_order_status = intval($order->order_status);
  50. $old_notice = intval($order->order_notes);
  51. if($old_order_status == 5 && ($old_notice == '医生超时未接单自动取消' || $old_notice == '医生超时未接单自动取消') ){
  52. $log = UserBalanceLog::where(['relation_id'=>$order->id])->first();
  53. if(!empty($log)){
  54. $user = \App\Models\User::where('id',$order->user_id)->first();
  55. if($user->balance >0){
  56. User::where('id',$order->user_id)->decrement('balance',$log->change_balance);
  57. UserBalanceLog::where('id',$log->id)->delete();
  58. }
  59. }
  60. Order::where('id',$order->id)->update($orderInfo);
  61. }
  62. } else {
  63. continue;
  64. }
  65. }
  66. return null;
  67. }
  68. public function startRow(): int
  69. {
  70. return 2;
  71. }
  72. public function batchSize(): int
  73. {
  74. return 1000;
  75. }
  76. public function chunkSize(): int
  77. {
  78. return 1000;
  79. }
  80. public function getValue($model,$where,$field)
  81. {
  82. return $model->where($where)->value($field);
  83. }
  84. }