OrderService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Order;
  4. use App\Models\Plan;
  5. use App\Models\User;
  6. use Illuminate\Support\Facades\DB;
  7. class OrderService
  8. {
  9. public $order;
  10. public function __construct(Order $order)
  11. {
  12. $this->order = $order;
  13. }
  14. public function cancel():bool
  15. {
  16. $order = $this->order;
  17. DB::beginTransaction();
  18. $order->status = 2;
  19. if (!$order->save()) {
  20. DB::rollBack();
  21. return false;
  22. }
  23. if ($order->balance_amount) {
  24. $userService = new UserService();
  25. if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
  26. DB::rollBack();
  27. return false;
  28. }
  29. }
  30. DB::commit();
  31. return true;
  32. }
  33. public function setOrderType(User $user)
  34. {
  35. $order = $this->order;
  36. if ($order->cycle === 'reset_price') {
  37. $order->type = 4;
  38. } else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
  39. if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
  40. $order->type = 3;
  41. $this->getSurplusValue($user, $order);
  42. if ($order->surplus_amount >= $order->total_amount) {
  43. $order->refund_amount = $order->surplus_amount - $order->total_amount;
  44. $order->total_amount = 0;
  45. } else {
  46. $order->total_amount = $order->total_amount - $order->surplus_amount;
  47. }
  48. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { // 用户订阅未过期且购买订阅与当前订阅相同 === 续费
  49. $order->type = 2;
  50. } else { // 新购
  51. $order->type = 1;
  52. }
  53. }
  54. public function setVipDiscount(User $user)
  55. {
  56. $order = $this->order;
  57. if ($user->discount) {
  58. $order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
  59. }
  60. $order->total_amount = $order->total_amount - $order->discount_amount;
  61. }
  62. public function setInvite(User $user)
  63. {
  64. $order = $this->order;
  65. if ($user->invite_user_id && $order->total_amount > 0) {
  66. $order->invite_user_id = $user->invite_user_id;
  67. $commissionFirstTime = (int)config('v2board.commission_first_time_enable', 1);
  68. if (!$commissionFirstTime || ($commissionFirstTime && !Order::where('user_id', $user->id)->where('status', 3)->first())) {
  69. $inviter = User::find($user->invite_user_id);
  70. if ($inviter && $inviter->commission_rate) {
  71. $order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
  72. } else {
  73. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  74. }
  75. }
  76. }
  77. }
  78. private function getSurplusValue(User $user, Order $order)
  79. {
  80. if ($user->expired_at === NULL) {
  81. $this->getSurplusValueByOneTime($user, $order);
  82. } else {
  83. $this->getSurplusValueByCycle($user, $order);
  84. }
  85. }
  86. private function getSurplusValueByOneTime(User $user, Order $order)
  87. {
  88. $plan = Plan::find($user->plan_id);
  89. $trafficUnitPrice = $plan->onetime_price / $plan->transfer_enable;
  90. if ($user->discount && $trafficUnitPrice) {
  91. $trafficUnitPrice = $trafficUnitPrice - ($trafficUnitPrice * $user->discount / 100);
  92. }
  93. $notUsedTraffic = $plan->transfer_enable - (($user->u + $user->d) / 1073741824);
  94. $result = $trafficUnitPrice * $notUsedTraffic;
  95. $orderModel = Order::where('user_id', $user->id)->where('cycle', '!=', 'reset_price')->where('status', 3);
  96. $order->surplus_amount = $result > 0 ? $result : 0;
  97. $order->surplus_order_ids = json_encode(array_column($orderModel->get()->toArray(), 'id'));
  98. }
  99. private function getSurplusValueByCycle(User $user, Order $order)
  100. {
  101. $strToMonth = [
  102. 'month_price' => 1,
  103. 'quarter_price' => 3,
  104. 'half_year_price' => 6,
  105. 'year_price' => 12
  106. ];
  107. $orderModel = Order::where('user_id', $user->id)
  108. ->where('cycle', '!=', 'reset_price')
  109. ->where('status', 3);
  110. $orderSurplusMonth = 0;
  111. $orderSurplusAmount = 0;
  112. $userSurplusMonth = ($user->expired_at - time()) / 2678400;
  113. foreach ($orderModel->get() as $item) {
  114. // 兼容历史余留问题
  115. if ($item->cycle === 'onetime_price') continue;
  116. $orderSurplusMonth = $orderSurplusMonth + $strToMonth[$item->cycle];
  117. $orderSurplusAmount = $orderSurplusAmount + ($item['total_amount'] + $item['balance_amount']);
  118. }
  119. if (!$orderSurplusMonth || !$orderSurplusAmount) return;
  120. $monthUnitPrice = $orderSurplusAmount / $orderSurplusMonth;
  121. // 如果用户过期月大于订单过期月
  122. if ($userSurplusMonth > $orderSurplusMonth) {
  123. $orderSurplusAmount = $orderSurplusMonth * $monthUnitPrice;
  124. } else {
  125. $orderSurplusAmount = $userSurplusMonth * $monthUnitPrice;
  126. }
  127. if (!$orderSurplusAmount) {
  128. return;
  129. }
  130. $order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
  131. $order->surplus_order_ids = json_encode(array_column($orderModel->get()->toArray(), 'id'));
  132. }
  133. public function success(string $callbackNo)
  134. {
  135. $order = $this->order;
  136. if ($order->status !== 0) {
  137. return true;
  138. }
  139. $order->status = 1;
  140. $order->callback_no = $callbackNo;
  141. return $order->save();
  142. }
  143. }