OrderService.php 6.2 KB

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