Files
ProxyPanel/app/Console/Commands/ServiceTimer.php
兔姬桑 8f06a74b48 修复与规范日期有关代码
1. 修复原版本中节点/用户每日流量日志记录错误;
原版:在30号看到的29号的总流量,其实是28号当天产生的流量;依次类推全部流量都错位1天;
现:日流量表精确到当天,天流量表精确到当前;
2. 修复原流量折线图的日期-流量配对错误;
原版:并不是按照记录日期,而是按照有的记录;
现:按照记录日期排序;
----- 以上皆为SSRPanel中遗留的问题代码-----
3. 简化,规范化日期转换
4. 清理多余代码;
2020-08-05 03:51:29 +08:00

92 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Console\Commands;
use App\Components\Helpers;
use App\Http\Controllers\ServiceController;
use App\Models\Order;
use App\Models\User;
use DB;
use Exception;
use Illuminate\Console\Command;
use Log;
class ServiceTimer extends Command {
protected $signature = 'serviceTimer';
protected $description = '服务计时器';
public function handle(): void {
$jobStartTime = microtime(true);
// 扣减用户到期商品的流量
$this->decGoodsTraffic();
$jobEndTime = microtime(true);
$jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
}
// 扣减用户到期商品的流量
private function decGoodsTraffic(): void {
//获取失效的套餐
$orderList = Order::query()
->with(['goods'])
->whereStatus(2)
->whereIsExpire(0)
->whereHas('goods', static function($q) {
$q->whereType(2);
})
->where('expired_at', '<=', date('Y-m-d H:i:s'))
->get();
if($orderList->isNotEmpty()){
try{
DB::beginTransaction();
foreach($orderList as $order){
// 过期本订单
Order::query()->whereOid($order->oid)->update(['is_expire' => 1]);
// 过期生效中的加油包
Order::query()
->with(['goods'])
->whereUserId($order->user_id)
->whereStatus(2)
->whereIsExpire(0)
->whereHas('goods', static function($q) {
$q->whereType(1);
})
->update(['is_expire' => 1]);
if(empty($order->user) || empty($order->goods)){
continue;
}
// 清理全部流量,重置重置日期和等级
User::query()->whereId($order->user_id)->update([
'u' => 0,
'd' => 0,
'transfer_enable' => 0,
'reset_time' => null,
'level' => 0
]);
Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $order->user->transfer_enable, 0,
'[定时任务]用户所购商品到期,扣减商品对应的流量');
// 检查该订单对应用户是否有预支付套餐
$prepaidOrder = Order::query()->whereUserId($order->user_id)->whereStatus(3)->oldest()->first();
if($prepaidOrder){
(new ServiceController)->activePrepaidOrder($prepaidOrder->oid);
}
}
DB::commit();
}catch(Exception $e){
Log::error($this->description.''.$e);
DB::rollBack();
}
}
}
}