CoachFlowService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Services\Coach;
  3. use App\Models\MemberUser;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class CoachFlowService
  7. {
  8. /**
  9. * 创建投流订单
  10. *
  11. * @param int $userId 用户ID
  12. * @param array $params 订单参数
  13. * @return array
  14. */
  15. public function createOrder(int $userId, array $params)
  16. {
  17. try {
  18. return DB::transaction(function () use ($params, $userId) {
  19. // 查询用户信息
  20. $user = MemberUser::findOrFail($userId);
  21. // 验证用户是否为教练
  22. abort_if(! $user->coach, 403, '非教练用户无法创建投流订单');
  23. // 验证时间段参数
  24. abort_if(! in_array($params['time_slot'], [1, 2, 3]), 422, '无效的时间段');
  25. // 验证天数参数
  26. $validDays = [1, 7, 15, 30, 90, 180, 365];
  27. abort_if(! in_array($params['days'], $validDays), 422, '无效的天数');
  28. // 验证位置类型
  29. abort_if(! in_array($params['position_type'], [1, 2]), 422, '无效的位置类型');
  30. // 计算订单金额
  31. $amount = $this->calculateAmount($params['time_slot'], $params['days']);
  32. // 生成订单号
  33. $orderNo = $this->generateOrderNo();
  34. // 创建订单
  35. $order = $user->coach->flowOrders()->create([
  36. 'order_no' => $orderNo,
  37. 'coach_id' => $user->id,
  38. 'position_type' => $params['position_type'],
  39. 'time_slot' => $params['time_slot'],
  40. 'days' => $params['days'],
  41. 'amount' => $amount,
  42. 'state' => 0,
  43. 'start_time' => null,
  44. 'end_time' => null,
  45. ]);
  46. Log::info('创建投流订单成功', [
  47. 'order_id' => $order->id,
  48. 'order_no' => $orderNo,
  49. 'user_id' => $userId,
  50. 'amount' => $amount,
  51. 'params' => $params,
  52. ]);
  53. return [
  54. 'order_id' => $order->id,
  55. 'order_no' => $orderNo,
  56. 'amount' => number_format($amount, 2),
  57. ];
  58. });
  59. } catch (\Exception $e) {
  60. Log::error('创建投流订单失败', [
  61. 'message' => $e->getMessage(),
  62. 'user_id' => $userId,
  63. 'params' => $params,
  64. 'trace' => $e->getTraceAsString(),
  65. ]);
  66. throw new \Exception('创建订单失败:'.$e->getMessage());
  67. }
  68. }
  69. /**
  70. * 生成订单号
  71. */
  72. private function generateOrderNo(): string
  73. {
  74. return 'FL'.date('YmdHis').mt_rand(1000, 9999);
  75. }
  76. /**
  77. * 获取价格配置
  78. */
  79. public function getPriceConfig()
  80. {
  81. return [
  82. 'time_slots' => [
  83. ['type' => 1, 'name' => '上午', 'time' => '6:00~12:00', 'price' => 4],
  84. ['type' => 2, 'name' => '下午', 'time' => '12:00~18:00', 'price' => 6],
  85. ['type' => 3, 'name' => '晚上', 'time' => '18:00~6:00', 'price' => 8],
  86. ],
  87. 'days' => [
  88. ['days' => 1, 'price' => 12],
  89. ['days' => 7, 'price' => 53],
  90. ['days' => 15, 'price' => 99],
  91. ['days' => 30, 'price' => 160],
  92. ['days' => 90, 'price' => 430],
  93. ['days' => 180, 'price' => 760],
  94. ['days' => 365, 'price' => 1360],
  95. ],
  96. ];
  97. }
  98. /**
  99. * 计算订单金额
  100. */
  101. private function calculateAmount($timeSlot, $days)
  102. {
  103. $dayPrice = 0;
  104. switch ($timeSlot) {
  105. case 1:
  106. $dayPrice = 4;
  107. break;
  108. case 2:
  109. $dayPrice = 6;
  110. break;
  111. case 3:
  112. $dayPrice = 8;
  113. break;
  114. }
  115. $totalPrice = 0;
  116. switch ($days) {
  117. case 1:
  118. $totalPrice = 12;
  119. break;
  120. case 7:
  121. $totalPrice = 53;
  122. break;
  123. case 15:
  124. $totalPrice = 99;
  125. break;
  126. case 30:
  127. $totalPrice = 160;
  128. break;
  129. case 90:
  130. $totalPrice = 430;
  131. break;
  132. case 180:
  133. $totalPrice = 760;
  134. break;
  135. case 365:
  136. $totalPrice = 1360;
  137. break;
  138. }
  139. return $totalPrice;
  140. }
  141. }