GoogleSearchKeyword.php 3.7 KB
<?php
/**
 * @remark :
 * @name   :GoogleSearchKeyword.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/31 11:29
 */

namespace App\Console\Commands\GoogleSearch;

use App\Models\Com\NoticeLog;
use App\Models\Domain\DomainInfo;
use App\Models\GoogleSearch\GoogleSearch;
use App\Models\GoogleSearch\GoogleSearchDetail;
use App\Models\Project\Project;
use App\Services\GoogleSearchService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * @remark :客户搜索词
 * @name   :GoogleSearchKeyword
 * @author :lyh
 * @method :post
 * @time   :2025/3/31 11:29
 */
class GoogleSearchKeyword extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'search_keyword';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'google搜索词';

    /**
     * @remark :拉取google搜索数据
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2025/3/31 11:37
     */
    public function handle(){
        $projectModel = new Project();
        $lists = $projectModel->list(['delete_status' => 0,'type'=>['!=',$projectModel::TYPE_ONE]], 'id', ['id']);
        $domainModel = new DomainInfo();
        foreach ($lists as $val) {
            echo date('Y-m-d H:i:s') . '开始--项目的id:'. $val['id'] . PHP_EOL;
            $domainInfo = $domainModel->read(['project_id'=>$val['id']]);
            if($domainInfo === false){
                echo '域名不存在。'.PHP_EOL;
                continue;
            }
            $typeData = [1=>'date', 2=>'query', 3=>'page', 4=>'device', 5=>'country'];
            foreach ($typeData as $valT){
                $this->_action($domainInfo['domain'],$valT,$val['id']);
            }
        }
        return true;
    }

    /**
     * @remark :执行的方法
     * @name   :_action
     * @author :lyh
     * @method :post
     * @time   :2025/3/31 11:30
     */
    public function _action($domain,$type,$project_id){
        $googleService = new GoogleSearchService();
        $data = $googleService->googleSearch($domain,$type);
        if(empty($data)){
            echo '域名:'.$domain.'拉取数据为空.'.PHP_EOL;
            return false;
        }
        //保存一条主记录诗句
        $searchModel = new GoogleSearch();
        $searchModel->addReturnId(['date'=>date('Y-m-d'),'project_id'=>$project_id,'type'=>$type]);
        $saveData = [];
        $clicksNum = array_sum(array_column($data, 'clicks'));
        $impressionsNum = array_sum(array_column($data, 'impressions'));
        foreach ($data as $val){
            $saveData[] = [
                'date'=>date('Y-m-d'),
                'project_id'=>$project_id,
                'type'=>$type,
                'keys'=>$val['keys'][0],
                'clicks'=>$val['clicks'],
                'click_rate'=>number_format($val['clicks'] / $clicksNum, 2),
                'impressions'=>$val['impressions'],
                'impressions_rate'=>number_format($val['impressions'] / $impressionsNum, 2),
                'ctr'=>$val['ctr'],
                'position'=>$val['position'],
            ];
        }
        if(!empty($saveData)){
            DB::beginTransaction();
            try {
                //清空以前的数据
                $detailModel = new GoogleSearchDetail();
                $detailModel->del(['project_id'=>$project_id,'type'=>$type]);
                $detailModel->insertAll($saveData);
                DB::commit();
            }catch (\Exception $e){
                DB::rollBack();
                echo '重新添加数据失败:project_id'.$project_id.PHP_EOL;
            }
        }
        return true;
    }
}