CheckOrder.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\OrderService;
  4. use Illuminate\Console\Command;
  5. use App\Models\Order;
  6. use App\Models\User;
  7. use App\Models\Plan;
  8. use Illuminate\Support\Facades\DB;
  9. class CheckOrder extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'check:order';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '订单检查任务';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $orders = Order::get();
  40. foreach ($orders as $item) {
  41. switch ($item->status) {
  42. // cancel
  43. case 0:
  44. if (strtotime($item->created_at) <= (time() - 1800)) {
  45. $orderService = new OrderService($item);
  46. $orderService->cancel();
  47. }
  48. break;
  49. case 1:
  50. $this->orderHandle($item);
  51. break;
  52. }
  53. }
  54. }
  55. private function orderHandle(Order $order)
  56. {
  57. $user = User::find($order->user_id);
  58. $plan = Plan::find($order->plan_id);
  59. if ($order->refund_amount) {
  60. $user->balance = $user->balance + $order->refund_amount;
  61. }
  62. DB::beginTransaction();
  63. if ($order->surplus_order_ids) {
  64. try {
  65. Order::whereIn('id', json_decode($order->surplus_order_ids))->update([
  66. 'status' => 4
  67. ]);
  68. } catch (\Exception $e) {
  69. DB::rollback();
  70. abort(500, '开通失败');
  71. }
  72. }
  73. switch ((string)$order->cycle) {
  74. case 'onetime_price':
  75. $this->buyByOneTime($user, $plan);
  76. break;
  77. case 'reset_price':
  78. $this->buyByResetTraffic($user);
  79. break;
  80. default:
  81. $this->buyByCycle($order, $user, $plan);
  82. }
  83. if (!$user->save()) {
  84. DB::rollBack();
  85. abort(500, '开通失败');
  86. }
  87. $order->status = 3;
  88. if (!$order->save()) {
  89. DB::rollBack();
  90. abort(500, '开通失败');
  91. }
  92. DB::commit();
  93. }
  94. private function buyByResetTraffic(User $user)
  95. {
  96. $user->u = 0;
  97. $user->d = 0;
  98. }
  99. private function buyByCycle(Order $order, User $user, Plan $plan)
  100. {
  101. // change plan process
  102. if ((int)$order->type === 3) {
  103. $user->expired_at = time();
  104. }
  105. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  106. // 续费重置&类型=续费
  107. if ((int)config('v2board.renew_reset_traffic_enable', 1) && $order->type === 2) $this->buyByResetTraffic($user);
  108. // 购买前用户过期为NULL(一次性)
  109. if ($user->expired_at === NULL) $this->buyByResetTraffic($user);
  110. // 新购
  111. if ($order->type === 1) $this->buyByResetTraffic($user);
  112. $user->plan_id = $plan->id;
  113. $user->group_id = $plan->group_id;
  114. $user->expired_at = $this->getTime($order->cycle, $user->expired_at);
  115. }
  116. private function buyByOneTime(User $user, Plan $plan)
  117. {
  118. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  119. $user->u = 0;
  120. $user->d = 0;
  121. $user->plan_id = $plan->id;
  122. $user->group_id = $plan->group_id;
  123. $user->expired_at = NULL;
  124. }
  125. private function getTime($str, $timestamp)
  126. {
  127. if ($timestamp < time()) {
  128. $timestamp = time();
  129. }
  130. switch ($str) {
  131. case 'month_price':
  132. return strtotime('+1 month', $timestamp);
  133. case 'quarter_price':
  134. return strtotime('+3 month', $timestamp);
  135. case 'half_year_price':
  136. return strtotime('+6 month', $timestamp);
  137. case 'year_price':
  138. return strtotime('+12 month', $timestamp);
  139. case 'two_year_price':
  140. return strtotime('+24 month', $timestamp);
  141. case 'three_year_price':
  142. return strtotime('+36 month', $timestamp);
  143. }
  144. }
  145. }