CheckOrder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 App\Utils\Helper;
  9. use App\Models\Coupon;
  10. class CheckOrder extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'check:order';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '订单检查任务';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $orders = Order::get();
  41. foreach ($orders as $item) {
  42. switch ($item->status) {
  43. // cancel
  44. case 0:
  45. if (strtotime($item->created_at) <= (time() - 1800)) {
  46. $orderService = new OrderService($item);
  47. $orderService->cancel();
  48. }
  49. break;
  50. case 1:
  51. $this->orderHandle($item);
  52. break;
  53. }
  54. }
  55. }
  56. private function orderHandle(Order $order)
  57. {
  58. $user = User::find($order->user_id);
  59. $plan = Plan::find($order->plan_id);
  60. if ((string)$order->cycle === 'onetime_price') {
  61. return $this->buyByOneTime($order, $user, $plan);
  62. }
  63. return $this->buyByCycle($order, $user, $plan);
  64. }
  65. private function buyByCycle(Order $order, User $user, Plan $plan)
  66. {
  67. // change plan process
  68. if ((int)$order->type === 3) {
  69. $user->expired_at = time();
  70. }
  71. if ($order->refund_amount) {
  72. $user->balance = $user->balance + $order->refund_amount;
  73. }
  74. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  75. if ((int)config('v2board.renew_reset_traffic_enable', 1)) {
  76. $user->u = 0;
  77. $user->d = 0;
  78. }
  79. $user->plan_id = $plan->id;
  80. $user->group_id = $plan->group_id;
  81. $user->expired_at = $this->getTime($order->cycle, $user->expired_at);
  82. if ($user->save()) {
  83. $order->status = 3;
  84. $order->save();
  85. }
  86. }
  87. private function buyByOneTime(Order $order, User $user, Plan $plan)
  88. {
  89. if ($order->refund_amount) {
  90. $user->balance = $user->balance + $order->refund_amount;
  91. }
  92. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  93. $user->u = 0;
  94. $user->d = 0;
  95. $user->plan_id = $plan->id;
  96. $user->group_id = $plan->group_id;
  97. $user->expired_at = NULL;
  98. if ($user->save()) {
  99. $order->status = 3;
  100. $order->save();
  101. }
  102. }
  103. private function getTime($str, $timestamp)
  104. {
  105. if ($timestamp < time()) {
  106. $timestamp = time();
  107. }
  108. switch ($str) {
  109. case 'month_price':
  110. return strtotime('+1 month', $timestamp);
  111. case 'quarter_price':
  112. return strtotime('+3 month', $timestamp);
  113. case 'half_year_price':
  114. return strtotime('+6 month', $timestamp);
  115. case 'year_price':
  116. return strtotime('+12 month', $timestamp);
  117. }
  118. }
  119. }