OrderService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 create()
  34. {
  35. }
  36. public function setOrderType(User $user)
  37. {
  38. $order = $this->order;
  39. if ($order->cycle === 'reset_price') {
  40. $order->type = 4;
  41. }
  42. if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
  43. if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
  44. $order->type = 3;
  45. $this->getSurplusValue($user, $order);
  46. if ($order->surplus_amount >= $order->total_amount) {
  47. $order->refund_amount = $order->surplus_amount - $order->total_amount;
  48. $order->total_amount = 0;
  49. } else {
  50. $order->total_amount = $order->total_amount - $order->surplus_amount;
  51. }
  52. } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
  53. $order->type = 2;
  54. } else {
  55. $order->type = 1;
  56. }
  57. }
  58. public function setVipDiscount(User $user)
  59. {
  60. $order = $this->order;
  61. if ($user->discount) {
  62. $order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
  63. }
  64. $order->total_amount = $order->total_amount - $order->discount_amount;
  65. }
  66. public function setInvite(User $user)
  67. {
  68. $order = $this->order;
  69. if ($user->invite_user_id && $order->total_amount > 0) {
  70. $order->invite_user_id = $user->invite_user_id;
  71. $commissionFirstTime = (int)config('v2board.commission_first_time_enable', 1);
  72. if (!$commissionFirstTime || ($commissionFirstTime && !Order::where('user_id', $user->id)->where('status', 3)->first())) {
  73. $inviter = User::find($user->invite_user_id);
  74. if ($inviter && $inviter->commission_rate) {
  75. $order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
  76. } else {
  77. $order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
  78. }
  79. }
  80. }
  81. }
  82. private function getSurplusValue(User $user, Order $order)
  83. {
  84. if ($user->expired_at === NULL) {
  85. $this->getSurplusValueByOneTime($user, $order);
  86. } else {
  87. $this->getSurplusValueByCycle($user, $order);
  88. }
  89. }
  90. private function getSurplusValueByOneTime(User $user, Order $order)
  91. {
  92. $plan = Plan::find($user->plan_id);
  93. $trafficUnitPrice = $plan->onetime_price / $plan->transfer_enable;
  94. if ($user->discount && $trafficUnitPrice) {
  95. $trafficUnitPrice = $trafficUnitPrice - ($trafficUnitPrice * $user->discount / 100);
  96. }
  97. $notUsedTrafficPrice = $plan->transfer_enable - (($user->u + $user->d) / 1073741824);
  98. $result = $trafficUnitPrice * $notUsedTrafficPrice;
  99. $orderModel = Order::where('user_id', $user->id)->where('cycle', '!=', 'reset_price')->where('status', 3);
  100. $order->surplus_amount = $result > 0 ? $result : 0;
  101. $order->surplus_order_ids = json_encode(array_map(function ($v) { return $v['id'];}, $orderModel->get()->toArray()));
  102. }
  103. private function getSurplusValueByCycle(User $user, Order $order)
  104. {
  105. $strToMonth = [
  106. 'month_price' => 1,
  107. 'quarter_price' => 3,
  108. 'half_year_price' => 6,
  109. 'year_price' => 12,
  110. 'onetime_price' => 0
  111. ];
  112. $orderModel = Order::where('user_id', $user->id)
  113. ->where('cycle', '!=', 'reset_price')
  114. ->where('status', 3);
  115. $totalValue = $orderModel->sum('total_amount') + $orderModel->sum('balance_amount');
  116. if ($totalValue <= 0) {
  117. return;
  118. }
  119. $totalMonth = 0;
  120. foreach ($orderModel->get() as $item) {
  121. $totalMonth = $totalMonth + $strToMonth[$item->cycle];
  122. }
  123. $unitPrice = $totalValue / $totalMonth;
  124. $remainingMonth = ($user->expired_at - time()) / 2678400;
  125. $result = $unitPrice * $remainingMonth;
  126. $order->surplus_amount = $result > 0 ? $result : 0;
  127. $order->surplus_order_ids = json_encode(array_map(function ($v) { return $v['id'];}, $orderModel->get()->toArray()));
  128. }
  129. }