RankData.php 5.3 KB
<?php

namespace App\Console\Commands\RankData;

use App\Helper\Arr;
use App\Helper\QuanqiusouApi;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\RankData\RankData as GoogleRankModel;

/**
 * Class GoogleRank
 * @package App\Console\Commands
 * @author zbj
 * @date 2023/5/6
 */
class RankData extends BaseCommands
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rank_data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '谷歌排名数据';

    /**
     * @author zbj
     * @date 2023/5/6
     */
    public function do()
    {
        $error = 0;
        $api = new QuanqiusouApi();
        //有排名api编号的项目
        $list = DeployOptimize::where('api_no', '>' , 0)->select('api_no','minor_languages','project_id')->get();
        //当日所有站点谷歌收录数据
        $site_res = $api->getSiteRes();
        if(!$site_res){
            return false;
        }

        foreach ($list as $item){
            $model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '')->first();
            if (!$model || $model->updated_date == date('Y-m-d')) {
                $res = $api->getGoogleRank($item['api_no']);
                if(!$res){
                    $error++;
                    continue;
                }
                //收录数
                $indexed_pages_num = $site_res[$item['api_no']];

                $this->save_rank($item['project_id'], $res, $indexed_pages_num);
            }

            //有小语种的
            if($item['minor_languages']){
                $model = GoogleRankModel::where('project_id', $item['project_id'])->where('lang', '<>', '')->first();
                if (!$model || $model->updated_date == date('Y-m-d')) {
                    $res = $api->getGoogleRank($item['api_no'], 1);
                    if(!$res){
                        $error++;
                        continue;
                    }
                    $data = [];
                    //不同的小语种取出来
                    foreach ($res as $keyword => $v){
                        $data[Arr::last($v)['lang']][$keyword] = $v;
                    }
                    foreach ($data as $lang => $rank){
                        $this->save_rank($item['project_id'], $rank, 0, $lang);
                    }
                }
            }
        }
        return !$error;
    }

    /**
     * @param $project_id
     * @param int $indexed_pages_num
     * @param $data
     * @param string $lang
     * @author zbj
     * @date 2023/5/8
     */
    public function save_rank($project_id, $data, int $indexed_pages_num = 0, string $lang = ''){
        $without_project_ids = [];  //不用处理排名的项目

        $first_num = $first_page_num = $first_three_pages_num = $first_five_pages_num = $first_ten_pages_num = 0;

        if(!$lang){
            foreach ($data as &$ranks){
                foreach ($ranks as &$rank){
                    //处理排名
                    if(!in_array($project_id, $without_project_ids)){
                        if($rank['position'] >= 10){
                            $rank['position'] -= 5;
                        }
                        //todo 需要特殊处理排名的项目
                    }
                }
                $last = Arr::last($ranks);
                //第一名
                if($last['position'] == 1){
                    $first_num ++;
                }
                //排名第一页
                if($last['position'] > 0 && $last['position'] <= 10){
                    $first_page_num ++;
                }
                //排名前三页
                if($last['position'] > 0 && $last['position'] <= 30){
                    $first_three_pages_num ++;
                }
                //排名前五页
                if($last['position'] > 0 && $last['position'] <= 50){
                    $first_five_pages_num ++;
                }
                //排名前十页
                if($last['position'] > 0 && $last['position'] <= 100){
                    $first_ten_pages_num ++;
                }
            }
        }

        $where = [
            'project_id' => $project_id,
            'lang' => $lang
        ];
        $model = GoogleRankModel::where($where)->first();
        if(!$model){
            $model = new GoogleRankModel();
        }

        //关键词达标天数
        if($model->updated_date != date('Y-m-d')){
            //保证关键词数
            $keyword_num = DeployBuild::where('project_id', $project_id)->value('keyword_num');
            if($first_page_num >= $keyword_num){
                $model->compliance_day = $model->compliance_day + 1;
            }
        }

        $model->project_id = $project_id;
        $model->first_num = $first_num;
        $model->first_page_num = $first_page_num;
        $model->first_three_pages_num = $first_three_pages_num;
        $model->first_five_pages_num = $first_five_pages_num;
        $model->first_ten_pages_num = $first_ten_pages_num;
        $model->indexed_pages_num = $indexed_pages_num;
        $model->lang = $lang;
        $model->data = $data;
        $model->updated_date = date('Y-m-d');
        $model->save();
    }
}