v2board/app/Console/Commands/CheckOrder.php

163 lines
4.4 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Console\Commands;
2020-03-17 20:00:46 +08:00
use App\Services\OrderService;
2019-10-29 15:33:36 +08:00
use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\User;
use App\Models\Plan;
use App\Utils\Helper;
2020-01-01 23:40:14 +08:00
use App\Models\Coupon;
2020-04-09 13:35:27 +08:00
use Illuminate\Support\Facades\DB;
2019-10-29 15:33:36 +08:00
class CheckOrder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:order';
/**
* The console command description.
*
* @var string
*/
protected $description = '订单检查任务';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2020-03-17 20:00:46 +08:00
$orders = Order::get();
foreach ($orders as $item) {
2019-10-29 15:33:36 +08:00
switch ($item->status) {
2019-11-25 00:27:36 +08:00
// cancel
2019-10-29 15:33:36 +08:00
case 0:
if (strtotime($item->created_at) <= (time() - 1800)) {
2020-03-17 20:00:46 +08:00
$orderService = new OrderService($item);
$orderService->cancel();
2019-10-29 15:33:36 +08:00
}
break;
case 1:
$this->orderHandle($item);
break;
}
2020-01-11 13:36:52 +08:00
2019-10-29 15:33:36 +08:00
}
}
2020-01-11 13:36:52 +08:00
2020-02-28 01:03:05 +08:00
private function orderHandle(Order $order)
2020-01-11 13:36:52 +08:00
{
2019-10-29 15:33:36 +08:00
$user = User::find($order->user_id);
2020-02-28 01:03:05 +08:00
$plan = Plan::find($order->plan_id);
2020-04-09 13:35:27 +08:00
if ($order->refund_amount) {
$user->balance = $user->balance + $order->refund_amount;
}
DB::beginTransaction();
if ($order->surplus_order_ids) {
try {
Order::whereIn('id', json_decode($order->surplus_order_ids))->update([
'status' => 4
]);
} catch (\Exception $e) {
DB::rollback();
abort(500, '开通失败');
}
}
2020-04-25 19:44:47 +08:00
switch ((string)$order->cycle) {
case 'onetime_price':
$this->buyByOneTime($order, $user, $plan);
break;
case 'reset_price':
$this->buyReset($user);
break;
default:
$this->buyByCycle($order, $user, $plan);
2020-04-09 13:35:27 +08:00
}
if (!$user->save()) {
DB::rollBack();
abort(500, '开通失败');
2020-02-28 01:03:05 +08:00
}
2020-04-09 13:35:27 +08:00
$order->status = 3;
if (!$order->save()) {
DB::rollBack();
abort(500, '开通失败');
}
DB::commit();
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
2020-04-25 19:44:47 +08:00
private function buyReset(User $user)
{
$user->u = 0;
$user->d = 0;
}
2020-02-28 01:03:05 +08:00
private function buyByCycle(Order $order, User $user, Plan $plan)
2020-01-11 13:36:52 +08:00
{
2020-02-08 23:03:48 +08:00
// change plan process
2020-03-17 01:50:50 +08:00
if ((int)$order->type === 3) {
2020-02-17 01:27:07 +08:00
$user->expired_at = time();
}
2019-10-29 15:33:36 +08:00
$user->transfer_enable = $plan->transfer_enable * 1073741824;
// 续费重置&类型=续费
if ((int)config('v2board.renew_reset_traffic_enable', 1) && $order->type === 2) $this->buyReset($user);
// 购买前用户过期为NULL一次性
if ($user->expired_at === NULL) $this->buyReset($user);
// 新购
if ($order->type === 1) $this->buyReset($user);
2019-10-29 15:33:36 +08:00
$user->plan_id = $plan->id;
$user->group_id = $plan->group_id;
$user->expired_at = $this->getTime($order->cycle, $user->expired_at);
2020-02-28 01:03:05 +08:00
}
2020-02-18 13:52:10 +08:00
2020-02-28 01:03:05 +08:00
private function buyByOneTime(Order $order, User $user, Plan $plan)
{
$user->transfer_enable = $plan->transfer_enable * 1073741824;
2020-03-02 00:27:06 +08:00
$user->u = 0;
$user->d = 0;
2020-02-28 01:03:05 +08:00
$user->plan_id = $plan->id;
$user->group_id = $plan->group_id;
2020-03-01 23:29:49 +08:00
$user->expired_at = NULL;
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
private function getTime($str, $timestamp)
{
2019-10-29 15:33:36 +08:00
if ($timestamp < time()) {
$timestamp = time();
}
switch ($str) {
2020-01-11 13:36:52 +08:00
case 'month_price':
return strtotime('+1 month', $timestamp);
case 'quarter_price':
return strtotime('+3 month', $timestamp);
case 'half_year_price':
return strtotime('+6 month', $timestamp);
case 'year_price':
return strtotime('+12 month', $timestamp);
2020-09-10 21:48:44 +08:00
case 'two_year_price':
return strtotime('+24 month', $timestamp);
case 'three_year_price':
return strtotime('+36 month', $timestamp);
2019-10-29 15:33:36 +08:00
}
}
}