OrderService.php 5.4 KB

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