ExternalLinkMake.php 3.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2025/9/15
 * Time: 10:09
 */
namespace App\Console\Commands\Product;

use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Project\Project;
use App\Models\RankData\RankDataLog;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

/**
 * Class ExternalLinkMake
 * @package App\Console\Commands\Product
 */
class ExternalLinkMake extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'external_link_make';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计不达标项目,500条 聚合页, 作为外链提交';

    /**
     * @return bool
     */
    public function handle()
    {
        $date = date('Y-m-d H:i:s', strtotime('-75 day'));
        $projects = Project::where(['type' => 2, 'delete_status' => 0, 'site_status' => 0, 'extend_type' => 0, 'is_remain_today' => 0])->get();

        foreach ($projects as $project) {
            if ($project->id <= 1)
                continue;
            $this->output('项目开始: ' . $project->id);

            $log = DB::table('gl_project_external_link_make')->where(['project_id' => $project->id])->first();
            if ($log) {
                $this->output('项目已推送过: ' . $project->id . ', 跳过');
                continue;
            }

            $yesterday = RankDataLog::where(['project_id' => $project->id, 'date' => date('Y-m-d', '-1 day')])->first();
            if (FALSE == empty($yesterday) && $yesterday->is_compliance == 1){
                $this->output('项目昨日达标: ' . $project->id . ', 跳过');
                continue;
            }

            if(empty($project->deploy_optimize['start_date']) || $project->deploy_optimize['start_date'] > $date){
                $this->output('项目推广不到75天: ' . $project->id . ', 推广开始时间:' . $project->deploy_optimize['start_date']);
                continue;
            }

            $domain = DomainInfo::where(['project_id' => $project->id, 'status' => DomainInfo::STATUS_ONE])->first();
            if (empty($domain)) {
                $this->output('项目未找到域名: ' . $project->id);
                continue;
            }
            $host = 'https://' . $domain->domain . '/';
            ProjectServer::useProject($project->id);
            $keywords = Keyword::select(['id', 'title', 'route', 'seo_title'])->where('route', '<>', null)->orderBy('id', 'desc')->limit(500)->get();
            $updated_at = $created_at = date('Y-m-d H:i:s');
            $data = [];
            foreach ($keywords as $keyword) {
                $data[] = [
                    'project_id' => $project->id,
                    'keyword_id' => $keyword->id,
                    'keyword' => $keyword->title,
                    'route' => $keyword->route,
                    'seo_title' => $keyword->seo_title,
                    'external_link' => $host . $keyword->route . '/{' . $keyword->seo_title . '}',
                    'updated_at' => $updated_at,
                    'created_at' => $created_at
                ];
            }
            DB::table('gl_project_external_link_make')->insert($data);
            DB::disconnect('custom_mysql');
        }

        return true;
    }

    /**
     * 输出message
     * @param $message
     */
    public function output($message)
    {
        $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
        file_put_contents(storage_path('logs/external_link_make.log'), $message, FILE_APPEND);
        echo $message;
    }
}