CheckOrder.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // cancel
  42. case 0:
  43. if (strtotime($item->created_at) <= (time() - 1800)) {
  44. $item->status = 2;
  45. $item->save();
  46. }
  47. break;
  48. case 1:
  49. $this->orderHandle($item);
  50. break;
  51. }
  52. }
  53. }
  54. private function orderHandle ($order) {
  55. $user = User::find($order->user_id);
  56. return $this->buy($order, $user);
  57. }
  58. private function buy ($order, $user) {
  59. $plan = Plan::find($order->plan_id);
  60. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  61. $user->enable = 1;
  62. $user->plan_id = $plan->id;
  63. $user->group_id = $plan->group_id;
  64. $user->expired_at = $this->getTime($order->cycle, $user->expired_at);
  65. if ($user->save()) {
  66. $order->status = 3;
  67. $order->save();
  68. }
  69. }
  70. private function getTime ($str, $timestamp) {
  71. if ($timestamp < time()) {
  72. $timestamp = time();
  73. }
  74. switch ($str) {
  75. case 'month_price': return strtotime('+1 month', $timestamp);
  76. case 'quarter_price': return strtotime('+3 month', $timestamp);
  77. case 'half_year_price': return strtotime('+6 month', $timestamp);
  78. case 'year_price': return strtotime('+12 month', $timestamp);
  79. }
  80. }
  81. }