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. use App\Models\Coupon;
  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. $order = Order::get();
  40. foreach ($order as $item) {
  41. switch ($item->status) {
  42. // cancel
  43. case 0:
  44. if (strtotime($item->created_at) <= (time() - 1800)) {
  45. $item->status = 2;
  46. $item->save();
  47. }
  48. break;
  49. case 1:
  50. $this->orderHandle($item);
  51. break;
  52. }
  53. }
  54. }
  55. private function orderHandle ($order) {
  56. $user = User::find($order->user_id);
  57. return $this->buy($order, $user);
  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. }