RankWeek.php 1.8 KB
<?php

namespace App\Console\Commands\RankData;


use App\Helper\Arr;
use App\Helper\QuanqiusouApi;
use App\Models\Project\DeployOptimize;
use App\Models\RankData\RankWeek as RankWeekModel;
use App\Utils\LogUtils;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '排名数据-每周排名数据';

    /**
     * @author zbj
     * @date 2023/5/6
     */
    public function do()
    {
        $error = 0;

        //获取每周排名数据
        $api = new QuanqiusouApi();

        $res = $api->getGoogleRankWeek();
        if (!$res) {
            return false;
        }

        $res = Arr::s2a($res);
        //有排名api编号的项目
        $list = DeployOptimize::where('api_no', '>', 0)->pluck('api_no', 'project_id')->toArray();
        LogUtils::info('start rank_data_week:' . count($list));
        foreach ($list as $project_id => $api_no) {
            $rank_week = RankWeekModel::where('project_id', $project_id)->first();
            if ($rank_week && $rank_week->updated_date >= getThisWeekStarDate()) {
                //本周数据已更新
                continue;
            }

            if (!$rank_week) {
                $rank_week = new RankWeekModel();
            }

            $rank_week->project_id = $project_id;
            $rank_week->data = $res['data'][$api_no];
            $rank_week->date = $res['date'];
            $rank_week->updated_date = date('Y-m-d');
            $rank_week->save();
        }

        return !$error;
    }
}