SendKeyword.php 6.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2025/3/11
 * Time: 14:13
 */
namespace App\Console\Commands\Product;

use App\Models\Product\Keyword;
use App\Models\Project\OnlineCheck;
use App\Models\Project\Project;
use App\Models\RankData\RankData;
use App\Repositories\ToolRepository;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '未达标项目 自动推送拼接关键词 到监控系统';

    /**
     * SendKeyword constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * TODO 查询不达标优化项目, 按照规则推送拼接关键词到监控系统
     * http://zentao.globalso.com/index.php?m=task&f=view&taskID=185918
     * @return bool
     */
    public function handle()
    {
        $field = ['gl_project.id', 'gl_project.company', 'gl_project.main_lang_id', 'b.is_auto_keywords', 'b.start_date', 'b.api_no', 'd.domain'];
        $projects = Project::select($field)->leftJoin('gl_project_deploy_optimize as b', 'gl_project.id', '=', 'b.project_id')
            ->leftJoin('gl_project_online_check as c', 'gl_project.id', '=', 'c.project_id')
            ->leftJoin('gl_domain_info as d', 'gl_project.id', '=', 'd.project_id')
            ->where('gl_project.type', Project::TYPE_TWO)
            ->where('gl_project.extend_type', 0) // 是否续费是由extend_type字段控制
            ->where('gl_project.delete_status', Project::IS_DEL_FALSE)
            ->where('gl_project.main_lang_id', '<>', 8) // 不需要俄语站点项目
            ->where(function ($subQuery) {
                $subQuery->orwhere('c.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)->orwhere('gl_project.is_upgrade', Project::IS_UPGRADE_TRUE);
            })
            ->get();
        $time = time();
        $send_num = [30 => 1000, 60 => 3000, 90 => 6000];
        $ban_domain = $this->banDomain();
        foreach ($projects as $item) {
            if (empty($item->is_auto_keywords))
                continue;
            if (in_array($item->domain, $ban_domain))
                continue;
            if (empty($item->api_no)) {
                $this->output('项目ID:' . $item->id . ', api_no为空;');
                continue;
            }
            if (empty($item->start_date)) {
                $this->output('项目ID:' . $item->id . ', 推广开始时间为空;');
                continue;
            }
            // 按照间隔时间发送不同关键词数量
            $start_time = strtotime($item->start_date);
            $day = intval(($time - $start_time) / 86400);
            if (empty($send_num[$day])) {
                $this->output('项目ID:' . $item->id . ', 推广天数:' . $day . ', 不需要推送;');
                continue;
            }

            // 项目是否达标
            $compliance = RankData::where(['project_id' => $item->id, 'api_no' => $item->api_no, 'lang' => ''])->value('is_compliance');
            if (FALSE == empty($compliance))
                return false;

            $project = ProjectServer::useProject($item->id);
            if (empty($project)) {
                DB::disconnect('custom_mysql');
                continue;
            }

            $send = $this->extendKeyword($item, $send_num[$day]);
            DB::disconnect('custom_mysql');
            // 数据成功后推送同志
            if ($send) {
                $item = $item->toArray();
                unset($item['main_lang_id']);
                unset($item['is_auto_keywords']);
                $item['day'] = $day;
                $this->sendNotice($item);
            }
        }
        return true;
    }

    /**
     * 截取关键词
     * @param $item
     * @param int $num
     * @return bool
     */
    public function extendKeyword($item, $num = 1000)
    {
        $keywords = Keyword::extendKeyword($item->id);
        if (empty($keywords))
            return false;
        $keywords = array_filter(array_unique($keywords));
        $sort = [];
        foreach ($keywords as $keyword) {
            $sort[] = strlen($keyword);
        }
        array_multisort($sort, SORT_ASC, $keywords);
        $result = array_slice($keywords, 0, $num);
        file_put_contents(storage_path('data/send_product_tag_keyword/' . $item->id . '.json'), json_encode($result, 256));
        return true;
    }

    /**
     * 通知
     * @param $item
     * @return bool
     */
    public function sendNotice($item)
    {
        try {
            $query = http_build_query($item);
            $url = 'http://api.quanqiusou.cn/google-rank/api/v6_add_extend_keywords.php?' . $query;
            app(ToolRepository::class)->curlRequest($url, [], 'GET');
            // TODO 推送通知 待定
        } catch (\Exception $e) {
            $this->output('推送通知失败,项目ID:' . $item['id']);
        }
        return true;
    }

    /**
     * 禁用
     * @return array
     */
    public function banDomain()
    {
        $array = [
            'www.venttopurifier.com',
            'www.gwxpcsheet.com',
            'www.bizzyboi.com',
            'www.wholesaleladyclothing.com',
            'www.chuanbokeji.com',
            'www.goalwhite.com',
            'www.yusunheating.com',
            'www.bretonprecision.com',
            'www.chinarendering.com',
            'www.centurybeauty.com',
            'www.vazpocable.com',
            'www.tengda-smart.com',
            'www.kayne-bm.com',
            'www.aimazingbag.com',
            'www.alpha-lifetech.com',
            'www.junyi-laser.com',
            'www.unionfurnish.com',
            'www.zphydraulic.com',
            'www.younisicleaning.com',
            'www.shibaodeprecision.com',
            'www.semmcocoa.com',
            'www.sbdmold.com',
            'www.utsfabrictester.com',
            'www.tlgantryrobot.com',
            'www.slabyte.com',
            'www.hysum.com',
            'www.rsecobag.com',
            'www.yuchomachine.com',
            'www.hidinosaurs.com',
            'www.medigauze.com',
            'www.shtangkefresh.com',
            'www.fkcrane.com',
            'www.zkmparts.com',
            'www.zhishuogroup.com',
            'www.yunlinplushtoys.com',
        ];
        return $array;
    }

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