OrderService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/9/26 11:11
  7. */
  8. namespace App\Http\Services\Frontend\Client\Service;
  9. use App\Exceptions\ApiException;
  10. use App\Http\Services\Service;
  11. use App\Models\Coach\User;
  12. use App\Models\Service\Order;
  13. use Illuminate\Support\Facades\Auth;
  14. class OrderService extends Service
  15. {
  16. protected array $baseColumn = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
  17. function buildOrderSN(): string
  18. {
  19. $random = rand(1, 999999);
  20. return date('YmdHis') . str_repeat('0', 6 - strlen($random)) . $random;
  21. }
  22. /**
  23. * @throws ApiException
  24. */
  25. public function createOrder(array $params)
  26. {
  27. $params['user_id'] = Auth::id();
  28. // 用户地址
  29. $params['address'] = '默认地址';
  30. // $address_model = new Address();
  31. $address = ['lat' => 0, 'lng' => 0, 'area_id' => 0];
  32. // 优惠卷
  33. // $coupon_record_model = new CouponRecord();
  34. // 渠道
  35. // $channel_model = new UserChannel();
  36. // 用户渠道
  37. // $input['channel_id'] = $channel_model->getChannelId($this->_user['id']);
  38. // 订单号
  39. $order_id = !empty($params['order_id']) ? $params['order_id'] : 0;
  40. // 使用余额
  41. $use_balance = $params['use_balance'] ?? 0;
  42. // 出行方式
  43. $car_type = $params['car_type'] ?? 0;
  44. // 技师
  45. $coach_id = $params['coach_id'] ?? 0;
  46. $coach_model = new User();
  47. $coach_info = $coach_model->info($coach_id);
  48. !empty($coach_info) && $coach_info['is_work'] == 0 && self::error('该技师未上班');
  49. !empty($coach_info) && ($coach_info['auth_status'] !== 2 || $coach_info['status'] !== 1) && self::error('该技师已下架');
  50. // 优惠卷ID
  51. $coupon_id = !empty($params['coupon_id']) ? $params['coupon_id'] : 0;
  52. // 加钟订单
  53. if ($order_id) {
  54. } else {
  55. // $address = $address_model->info($params['address_id']);
  56. // if(empty($address)){
  57. // $this->errorMsg('请添加地址');
  58. // }
  59. }
  60. $orderModel = new Order();
  61. $orderPayInfo = $orderModel->buildPayInfo($params['user_id'], $params['project_id'], $address['area_id'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id);
  62. //默认微信
  63. $payType = $params['pay_type'] ?? 1;
  64. $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0);
  65. $orderData = [
  66. 'order_sn' => $this->buildOrderSN(),
  67. 'user_id' => $params['user_id'],
  68. 'pay_type' => $payType,
  69. ...$orderPayInfo,
  70. //备注
  71. // 'text' => $params['text'] ?: '',
  72. // 'car_type' => $car_type,
  73. // 'channel_id' => $params['channel_id'] ?: 0,
  74. //目的地地址
  75. //加钟
  76. // 'add_pid' => $order_id,
  77. // 'is_add' => $order_id ? 1 : 0,
  78. ];
  79. // 创建订单
  80. return $orderModel->newQuery()->create($orderData)->id;
  81. }
  82. public function getOrderPage(array $data)
  83. {
  84. $user_id = Auth::id();
  85. $status = intval($data['status'] ?? 0);
  86. $where = ['user_id' => $user_id, 'user_del' => 0];
  87. $statusRange = match ($status) {
  88. 1 => [0, 1, 2, 3],
  89. 2 => [4, 5],
  90. 3 => [5],
  91. 4 => [6, 7],
  92. default => [0, 1, 2, 3, 4, 5, 6, 7]
  93. };
  94. // $select = ['id', 'project_name', 'project_icon', 'duration', 'paid_type', 'price', 'real_address', 'status', 'created_at', 'refunded_at'];
  95. $select = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
  96. $orderPage = Order::query()->where($where)->whereIn('status', $statusRange)->select($select)->latest()->paginate();
  97. return ['list' => $orderPage->items(), 'total' => $orderPage->total()];
  98. }
  99. public function getOrder(int $order_id)
  100. {
  101. $user_id = Auth::id();
  102. return Order::query()->where('user_id', $user_id)->select($this->baseColumn)->find($order_id);
  103. }
  104. /**
  105. * @throws ApiException
  106. */
  107. public function updateOrder(array $data, int $order_id): void
  108. {
  109. $order = Order::query()->find($order_id);
  110. $status = $data['status'];
  111. switch ($status) {
  112. // 结束订单
  113. case 6:
  114. $order->status !== 5 && self::error('订单状态有误');
  115. // 查询是否存在加钟订单
  116. // 判断加钟服务是否完成
  117. $order->user_end = 1;
  118. $order->end_time = time();
  119. break;
  120. case 9:
  121. !in_array($order->status, [0, 1, 2, 3]) && self::error('订单状态有误');
  122. break;
  123. }
  124. $order->status = $status;
  125. $order->save();
  126. }
  127. public function delOrder(int $order_id): void
  128. {
  129. $order = Order::query()->find($order_id);
  130. $order->user_del = 1;
  131. $order->save();
  132. }
  133. }