OrderService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\Site;
  12. use App\Models\Coach\User;
  13. use App\Models\Member\Address;
  14. use App\Models\Member\Benefit;
  15. use App\Models\Service\Order;
  16. use App\Models\Service\Project;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. class OrderService extends Service
  21. {
  22. protected array $baseColumn = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
  23. function buildOrderSN(): string
  24. {
  25. $random = rand(1, 999999);
  26. return date('YmdHis') . str_repeat('0', 6 - strlen($random)) . $random;
  27. }
  28. /**
  29. * @throws ApiException
  30. */
  31. public function createOrder(array $params)
  32. {
  33. $data = ['user_id' => Auth::id()];
  34. // 用户地址
  35. $address = Address::query()->where('user_id', $data['user_id'])->find($params['addressId']);
  36. !$address && self::error('地址信息错误');
  37. $data['address'] = $address['address'];
  38. $data['real_address'] = $address['addressInfo'];
  39. $data['address_lat'] = $address['lat'];
  40. $data['address_lng'] = $address['lng'];
  41. $data['area_id'] = 0;
  42. // 获取项目信息
  43. $project = Project::query()->find($params['projectId']);
  44. $data['project_name'] = $project->title;
  45. $data['project_icon'] = $project->cover;
  46. // 优惠卷
  47. // if ($params['couponId']) $data['coupon_id'] = $params['couponId'];
  48. // $coupon_record_model = new CouponRecord();
  49. // 渠道
  50. // $channel_model = new UserChannel();
  51. // 用户渠道
  52. // $input['channel_id'] = $channel_model->getChannelId($this->_user['id']);
  53. // 订单号
  54. $order_id = $params['orderId'] ?? null;
  55. // 使用余额
  56. $use_balance = $params['useBalance'] ?? 0;
  57. // 出行方式
  58. $car_type = $params['carType'] ?? 0;
  59. // 技师
  60. $coach_id = $params['coachId'] ?? null;
  61. if ($coach_id) {
  62. $coach_model = new User();
  63. $coach_info = $coach_model->info($coach_id);
  64. !$coach_info && self::error('技师不存在');
  65. !empty($coach_info) && $coach_info['is_work'] == 0 && self::error('该技师未上班');
  66. !empty($coach_info) && ($coach_info['auth_status'] !== 2 || $coach_info['status'] !== 1) && self::error('该技师已下架');
  67. }
  68. // 优惠卷ID
  69. $coupon_id = $params['couponId'] ?? 0;
  70. // 加钟订单
  71. if ($order_id) {
  72. } else {
  73. // $address = $address_model->info($params['address_id']);
  74. // if(empty($address)){
  75. // $this->errorMsg('请添加地址');
  76. // }
  77. }
  78. $orderModel = new Order();
  79. DB::beginTransaction();
  80. try {
  81. $memberQuery = \App\Models\Member\User::query();
  82. $member = $memberQuery->lockForUpdate()->find($data['user_id']);
  83. $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);
  84. //默认微信
  85. $payType = $params['pay_type'] ?? 1;
  86. $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0);
  87. // 订单状态
  88. $orderStatus = $use_balance && !$orderPayInfo['pay_price'] ? 1 : 0;
  89. $orderData = [
  90. 'order_sn' => $this->buildOrderSN(),
  91. 'pay_type' => $payType,
  92. 'status' => $orderStatus,
  93. ...$data,
  94. ...$orderPayInfo,
  95. //备注
  96. // 'text' => $params['text'] ?: '',
  97. // 'car_type' => $car_type,
  98. // 'channel_id' => $params['channel_id'] ?: 0,
  99. //目的地地址
  100. //加钟
  101. // 'add_pid' => $order_id,
  102. // 'is_add' => $order_id ? 1 : 0,
  103. ];
  104. // 创建订单
  105. $orderId = $orderModel->newQuery()->create($orderData)->id;
  106. // 变更用户余额
  107. if ($use_balance && $orderPayInfo['balance_price']) {
  108. Benefit::query()->create([
  109. 'user_id' => $data['user_id'],
  110. 'order_id' => $orderId,
  111. 'type' => 1,
  112. 'benefit' => $orderPayInfo['balance_price'] * -1,
  113. 'balance' => $member['balance']
  114. ]);
  115. $member->decrement('balance', $orderPayInfo['balance_price']);
  116. }
  117. DB::commit();
  118. return $orderId;
  119. } catch (\Exception $e) {
  120. DB::rollBack();
  121. Log::error('Transaction failed: ' . $e->getMessage());
  122. self::error('执行错误!');
  123. }
  124. }
  125. public function getOrderPage(array $data)
  126. {
  127. $user_id = Auth::id();
  128. $status = intval($data['status'] ?? 0);
  129. $where = ['user_id' => $user_id, 'user_del' => 0];
  130. $statusRange = match ($status) {
  131. 1 => [0, 1, 2, 3],
  132. 2 => [4, 5],
  133. 3 => [5],
  134. 4 => [6, 7],
  135. default => [0, 1, 2, 3, 4, 5, 6, 7]
  136. };
  137. // $select = ['id', 'project_name', 'project_icon', 'duration', 'paid_type', 'price', 'real_address', 'status', 'created_at', 'refunded_at'];
  138. $select = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
  139. $orderPage = Order::query()->where($where)->whereIn('status', $statusRange)->select($select)->latest()->paginate();
  140. return ['list' => $orderPage->items(), 'total' => $orderPage->total()];
  141. }
  142. public function getOrder(int $order_id)
  143. {
  144. $user_id = Auth::id();
  145. return Order::query()->where('user_id', $user_id)->select($this->baseColumn)->find($order_id);
  146. }
  147. /**
  148. * @throws ApiException
  149. */
  150. public function updateOrder(array $data, int $order_id): void
  151. {
  152. $order = Order::query()->find($order_id);
  153. $status = $data['status'];
  154. switch ($status) {
  155. // 结束订单
  156. case 6:
  157. $order->status !== 5 && self::error('订单状态有误');
  158. // 查询是否存在加钟订单
  159. // 判断加钟服务是否完成
  160. $order->user_end = 1;
  161. $order->end_time = time();
  162. break;
  163. case 9:
  164. !in_array($order->status, [0, 1, 2, 3]) && self::error('订单状态有误');
  165. break;
  166. }
  167. $order->status = $status;
  168. $order->save();
  169. }
  170. public function delOrder(int $order_id): void
  171. {
  172. $order = Order::query()->find($order_id);
  173. $order->user_del = 1;
  174. $order->save();
  175. }
  176. public function confirmOrder(array $params)
  177. {
  178. $user_id = Auth::id();
  179. $projectId = $params['projectId'];
  180. $addressId = $params['addressId'];
  181. $coachId = $params['coachId'] ?? 0;
  182. // 查询地区项目
  183. $projectSelect = ['title', 'sub_title', 'cover', 'price', 'init_price', 'total_sale', 'time_long', 'is_store', 'is_door'];
  184. $project = Project::query()->select($projectSelect)->find($projectId);
  185. // 查询用户余额
  186. $balance = \App\Models\Member\User::query()->where('id', $user_id)->pluck('balance');
  187. // 车费
  188. // 判断技师是否收取车费
  189. // $coach = User::query()->find($coachId);
  190. // 获取技师定位
  191. $coachSite = null;
  192. if ($coachId) {
  193. $coachSelect = ['latitude', 'longitude'];
  194. $coachSite = Site::query()->select($coachSelect)->where('coach_id', $coachId)->first();
  195. }
  196. return [
  197. 'balance' => $balance,
  198. 'project' => $project,
  199. 'coach' => $coachSite
  200. ];
  201. }
  202. /**
  203. * @throws ApiException
  204. */
  205. public function computePrice(array $params): array
  206. {
  207. $user_id = Auth::id();
  208. $addressId = $params['addressId'];
  209. $use_balance = $params['useBalance'] ?? 0;
  210. $coach_id = $params['coachId'] ?? 0;
  211. $distance = $params['distance'] ?? 0;
  212. $car_type = $params['carType'] ?? 0;
  213. $coupon_id = $params['couponId'] ?? 0;
  214. // $member = $memberQuery->find($user_id);
  215. // 获取地址
  216. $address = Address::query()->where('user_id', $user_id)->find($addressId);
  217. !$address && self::error('地址数据错误');
  218. $orderModel = new Order();
  219. $orderPayInfo = $orderModel->buildPayInfo($user_id, $params['projectId'], $use_balance, $coach_id, $car_type, $coupon_id, $distance);
  220. return $orderPayInfo;
  221. }
  222. }