CheckOrder.php 4.1 KB

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