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