CheckOrder.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Order;
  5. use App\Models\User;
  6. use App\Models\Plan;
  7. use App\Utils\Helper;
  8. class CheckOrder extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'check:order';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '订单检查任务';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $order = Order::get();
  39. foreach ($order as $item) {
  40. switch ($item->status) {
  41. case 0:
  42. if (strtotime($item->created_at) <= (time() - 1800)) {
  43. $item->status = 2;
  44. $item->save();
  45. }
  46. break;
  47. case 1:
  48. $this->orderHandle($item);
  49. break;
  50. }
  51. }
  52. }
  53. private function orderHandle ($order) {
  54. $user = User::find($order->user_id);
  55. if (!$user->plan_id || $order->plan_id == $user->plan_id) {
  56. return $this->buy($order, $user);
  57. }
  58. }
  59. private function buy ($order, $user) {
  60. $plan = Plan::find($order->plan_id);
  61. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  62. $user->enable = 1;
  63. $user->plan_id = $plan->id;
  64. $user->group_id = $plan->group_id;
  65. $user->expired_at = $this->getTime($order->cycle, $user->expired_at);
  66. if ($user->save()) {
  67. $order->status = 3;
  68. $order->save();
  69. }
  70. }
  71. private function getTime ($str, $timestamp) {
  72. if ($timestamp < time()) {
  73. $timestamp = time();
  74. }
  75. switch ($str) {
  76. case 'month_price': return strtotime('+1 month', $timestamp);
  77. case 'quarter_price': return strtotime('+3 month', $timestamp);
  78. case 'half_year_price': return strtotime('+6 month', $timestamp);
  79. case 'year_price': return strtotime('+12 month', $timestamp);
  80. }
  81. }
  82. }