OrderService.php 4.8 KB

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