RemainDay.php 8.7 KB
<?php

namespace App\Console\Commands\Domain;

use App\Models\Geo\GeoCount;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use Illuminate\Console\Command;

/**
 * 剩余服务时长
 * Class Traffic
 * @package App\Console\Commands
 * @author zbj
 * @date 2023/5/18
 */
class RemainDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'remain_day';

    /**
     * @var 按优化时间统计
     */
    protected $projectId = [
        1434,1812,276,2414,2974,
    ];//需要单独处理的项目

    /**
     * @var int[]
     * 按照达标天数收费的项目(白帽)
     */
    protected $bm_projectId = [
        4247,4299,4310,4215,4038,4084,4148,4178,4405
    ];
    /**
     * @var
     * 优化项目,中途切换按服务时间长计时,单独计算
     */
    protected $projectSwitchId = [
        793,
    ];

    /**
     * @var 暂停的项目
     */
    protected $ceaseProjectId = [
        354, 378, 649, 1226, 1283, 1703, 1893, 2066, 2250, 2193, 2399, 1685, 3931,2273,3647,1934
    ];//需要单独处理的项目
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网站服务剩余时长';

    public $project_id = 0;

    /**
     * @return bool
     */
    public function handle()
    {
        $this->project = new Project();
        $this->_action();
        return true;
    }

    /**
     * @remark :计算剩余服务时常
     * @name   :_action
     * @author :lyh
     * @method :post
     * @time   :2025/11/4 10:59
     */
    public function _action(){
        $projectModel = new Project();
        $deployBuildModel = new DeployBuild();
        $list = $projectModel->list(['extend_type'=>Project::TYPE_ZERO,'type'=>['in',[Project::TYPE_TWO,Project::TYPE_THREE,Project::TYPE_FOUR,Project::TYPE_SIX]]],'id',['id','type','level','uptime','remain_day','version','is_remain_today','pause_days','finish_remain_day','bm_finish_remain_day','geo_qualify_num']);
        foreach ($list as $item){
            $deploy_build = $deployBuildModel->read(['project_id'=>$item['id']],['service_duration','seo_service_duration','plan','seo_plan']);
            $this->output('start->项目id:' . $item['id'],$item['id']);
            //todo::暂停的项目停止计时
            if(in_array($item['id'],$this->ceaseProjectId)){//暂停的项目
                $this->output('暂停的项目id:' . $item['id'],$item['id']);
                if(($item['type'] == Project::TYPE_TWO) && ($item['is_remain_today'] == 1)){
                    $pause_days = $item['pause_days'] + 1;
                }
                if($item['type'] != Project::TYPE_THREE){
                    $pause_days = $item['pause_days'] + 1;
                }
                $this->project->edit(['pause_days'=>$pause_days],['id'=>$item['id']]);
                continue;
            }
            //todo::优化项目中途达标计时切换为按天计时
            if(in_array($item['id'],$this->projectSwitchId)){
                $this->output('中途切换计时的项目id:' . $item['id'].',旧达标天数:'.$item['finish_remain_day'].',新达标天数:'.($item['finish_remain_day'] + 1),$item['id']);
                $this->output('中途切换计时的项目id:' . $item['id'].',当前剩余服务时常:'.$item['remain_day'].',剩余时常:'.($item['remain_day'] - 1),$item['id']);
                $this->project->edit(['remain_day'=>($item['remain_day'] - 1),'finish_remain_day'=>($item['finish_remain_day'] + 1)],['id'=>$item['id']]);
                continue;
            }
            //白帽版本单独计算
            $this->seoRemainDay($deploy_build,$item);
            //默认版本统计
            $this->remainDay($item,$deploy_build);
            $this->output('end->项目id:' . $item['id']);
        }
        return true;
    }



    /**
     * @remark :白帽版本单独计算
     * @name   :seoRemainDay
     * @author :lyh
     * @method :post
     * @time   :2025/4/25 14:24
     */
    public function seoRemainDay($deploy_build,$item){
        //白帽版本的系统
        if($deploy_build['seo_plan'] != 0){
            if($deploy_build['seo_service_duration'] != 0){
                $this->output('白帽计算剩余服务时常:'.$item['id'],$item['id']);
                if(in_array($item['id'],$this->bm_projectId) || (in_array( 19,$item['level']))){
                    $compliance_day = (int)$item['bm_finish_remain_day'];
                    $seo_remain_day = $deploy_build['seo_service_duration'] - (int)$item['bm_finish_remain_day'];
                    $this->output('按白帽达标天数统计:'.$item['id'].',bm达标天数:'.$item['bm_finish_remain_day'].',bm剩余白帽天数:'.$seo_remain_day,$item['id']);
                }else{
                    //按自然日统计
                    $diff = time() - strtotime($item['uptime']);
                    $compliance_day = floor($diff / (60 * 60 * 24));
                    $seo_remain_day = $deploy_build['seo_service_duration'] - $compliance_day;
                    $this->output('按白帽自然日天数统计:'.$item['id'].',bm达标天数:'.$compliance_day.',bm剩余白帽天数:'.$seo_remain_day,$item['id']);
                }
                $this->project->edit(['seo_remain_day'=>$seo_remain_day,'bm_finish_remain_day'=>$compliance_day ?? 0],['id'=>$item['id']]);
            }
        }
        return true;
    }

    /**
     * @remark :普通版本剩余服务时常
     * @name   :RemainDay
     * @author :lyh
     * @method :post
     * @time   :2025/4/25 14:31
     */
    public function remainDay($item,$deploy_build){
        $this->output('普通项目计算剩余服务时常:'.$item['id'],$item['id']);
        //默认版本统计
        if($deploy_build['service_duration'] == 0 || $deploy_build['plan'] == 0){
            $this->output('当前项目跳过:'.$item['id'],$item['id']);
            return false;
        }
        //默认版本计算剩余服务时常
        if($item['type'] == Project::TYPE_TWO || $item['type'] == Project::TYPE_FOUR){
            if(in_array($item['id'],$this->projectId)){//已开始优化的时间结算
                $optimizeModel = new DeployOptimize();
                $opInfo = $optimizeModel->read(['project_id'=>$item['id']],['start_date']);
                if($opInfo === false){
                    return true;
                }
                $diff = time() - strtotime($opInfo['start_date'] ?? $item['uptime']);
                $compliance_day = floor($diff / (60 * 60 * 24));
                $remain_day = $deploy_build['service_duration'] - $compliance_day;
                $this->output('已开始优化的时间结算:'.$item['id'].',剩余服务时常:'.$remain_day,$item['id']);
            }else{
                $compliance_day  = ($item['finish_remain_day'] ?? 0);
                //todo::7.5版本单独计算
                if($item['version'] == 7.5 && $deploy_build['seo_plan'] != 0 && $deploy_build['plan'] != 0){
                    $geoCountModel = new GeoCount();
                    $is_qualify = $geoCountModel->where('project_id', $item['id'])->orderBy('id', 'desc')->value('is_qualify');
                    $remain_day = $item['remain_day'];
                    if($item['is_remain_today'] > 0 && $is_qualify > 0){
                        $remain_day  = $item['remain_day'] - 1;
                    }
                    $this->output('7.5版本单独计算:'.$item['id'].',剩余服务时常:'.$remain_day,$item['id']);
                }else{
                    $remain_day = $deploy_build['service_duration'] - $compliance_day;
                    $this->output('优化项目:'.$item['id'].',剩余服务时常:'.$remain_day.',已达标天数:'.$compliance_day,$item['id']);
                }
            }
        }else{
            //普通建站项目
            if($item['uptime']){
                $diff = time() - strtotime($item['uptime']);
                $compliance_day = floor($diff / (60 * 60 * 24));
                $remain_day = $deploy_build['service_duration'] - $compliance_day;
            }else{
                $remain_day = $deploy_build['service_duration'];
            }
        }
        $extend_type = 0;
        $this->project->edit(['remain_day'=>$remain_day,'extend_type'=>$extend_type,'finish_remain_day'=>$compliance_day ?? 0],['id'=>$item['id']]);
        return true;
    }

    /**
     * 输入日志
     * @param $message
     * @return bool
     */
    public function output($message,$project_id = 1)
    {
        $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
        echo $message;
        file_put_contents(storage_path('logs/remain_day/') . $project_id . '.log', $message, FILE_APPEND);
        return true;
    }
}