作者 zhl

不达标项目推送同志

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2025/3/11
  6 + * Time: 14:13
  7 + */
  8 +namespace App\Console\Commands\Product;
  9 +
  10 +use App\Models\Product\Keyword;
  11 +use App\Models\Project\OnlineCheck;
  12 +use App\Models\Project\Project;
  13 +use App\Models\RankData\RankData;
  14 +use App\Services\ProjectServer;
  15 +use Illuminate\Console\Command;
  16 +use Illuminate\Support\Facades\DB;
  17 +
  18 +/**
  19 + * Class SendKeyword
  20 + * @package App\Console\Commands\Product
  21 + */
  22 +class SendKeyword extends Command
  23 +{
  24 + /**
  25 + * The name and signature of the console command.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $signature = 'send_product_tag_keyword';
  30 +
  31 + /**
  32 + * The console command description.
  33 + *
  34 + * @var string
  35 + */
  36 + protected $description = '未达标项目 自动推送拼接关键词 到监控系统';
  37 +
  38 + /**
  39 + * SendKeyword constructor.
  40 + */
  41 + public function __construct()
  42 + {
  43 + parent::__construct();
  44 + }
  45 +
  46 + /**
  47 + * TODO 查询不达标优化项目, 按照规则推送拼接关键词到监控系统
  48 + * http://zentao.globalso.com/index.php?m=task&f=view&taskID=185918
  49 + * @return bool
  50 + */
  51 + public function handle()
  52 + {
  53 + $field = ['gl_project.id', 'gl_project.company', 'gl_project.main_lang_id', 'gl_project.is_upgrade', 'b.start_date', 'b.api_no', 'd.domain'];
  54 + $projects = Project::select($field)->leftJoin('gl_project_deploy_optimize as b', 'gl_project.id', '=', 'b.project_id')
  55 + ->leftJoin('gl_project_online_check as c', 'gl_project.id', '=', 'c.project_id')
  56 + ->leftJoin('gl_domain_info as d', 'gl_project.id', '=', 'd.project_id')
  57 + ->where('gl_project.type', Project::TYPE_TWO)
  58 + ->where('gl_project.extend_type', 0) // 是否续费是由extend_type字段控制
  59 + ->where('gl_project.delete_status', Project::IS_DEL_FALSE)
  60 + ->where('gl_project.main_lang_id', '<>', 8) // 不需要俄语站点项目
  61 + ->where(function ($subQuery) {
  62 + $subQuery->orwhere('c.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)->orwhere('gl_project.is_upgrade', Project::IS_UPGRADE_TRUE);
  63 + })
  64 + ->get();
  65 + $time = time();
  66 + $send_num = [30 => 1000, 60 => 3000, 90 => 6000];
  67 + foreach ($projects as $item) {
  68 + if (empty($item->api_no)) {
  69 + $this->output('项目ID:' . $item->id . ', api_no为空;');
  70 + continue;
  71 + }
  72 + if (empty($item->start_date)) {
  73 + $this->output('项目ID:' . $item->id . ', 推广开始时间为空;');
  74 + continue;
  75 + }
  76 + // 按照间隔时间发送不同关键词数量
  77 + $start_time = strtotime($item->start_date);
  78 + $day = intval(($time - $start_time) / 86400);
  79 + if (empty($send_num[$day])) {
  80 + $this->output('项目ID:' . $item->id . ', 推广天数:' . $day . ', 不需要推送;');
  81 + continue;
  82 + }
  83 +
  84 + // 项目是否达标
  85 + $compliance = RankData::where(['project_id' => $item->id, 'api_no' => $item->api_no, 'lang' => ''])->value('is_compliance');
  86 + if (FALSE == empty($compliance))
  87 + return false;
  88 +
  89 + $project = ProjectServer::useProject($item->id);
  90 + if (empty($project)) {
  91 + DB::disconnect('custom_mysql');
  92 + continue;
  93 + }
  94 +
  95 + $send = $this->extendKeyword($item, $send_num[$day]);
  96 + DB::disconnect('custom_mysql');
  97 + // 数据成功后推送同志
  98 + if ($send) {
  99 + $this->sendNotice($item);
  100 + }
  101 + }
  102 + return true;
  103 + }
  104 +
  105 + /**
  106 + * 截取关键词
  107 + * @param $item
  108 + * @param int $num
  109 + * @return bool
  110 + */
  111 + public function extendKeyword($item, $num = 1000)
  112 + {
  113 + $keywords = Keyword::extendKeyword($item->id);
  114 + if (empty($keywords))
  115 + return false;
  116 + $keywords = array_filter(array_unique($keywords));
  117 + $sort = [];
  118 + foreach ($keywords as $keyword) {
  119 + $sort[] = strlen($keyword);
  120 + }
  121 + array_multisort($sort, SORT_ASC, $keywords);
  122 + $result = array_slice($keywords, 0, $num);
  123 + file_put_contents(storage_path('data/send_product_tag_keyword/' . $item->id . '.json'), json_encode($result, 256));
  124 + return true;
  125 + }
  126 +
  127 + public function sendNotice($item)
  128 + {
  129 + $itme = $item->toArray();
  130 + try {
  131 + // TODO 推送通知 待定
  132 + } catch (\Exception $e) {
  133 + $this->output('推送通知失败,项目ID:' . $item['id']);
  134 + }
  135 + return true;
  136 + }
  137 +
  138 + /**
  139 + * @param $message
  140 + */
  141 + public function output($message)
  142 + {
  143 + $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  144 + echo $message;
  145 + file_put_contents(storage_path('data/send_product_tag_keyword.log'), $message, FILE_APPEND);
  146 + }
  147 +}
@@ -50,6 +50,19 @@ class Keyword extends Base @@ -50,6 +50,19 @@ class Keyword extends Base
50 ]; 50 ];
51 51
52 /** 52 /**
  53 + * 聚合页按照首字母归类
  54 + * @param $string
  55 + * @return int
  56 + */
  57 + public function getFirstWord($string) {
  58 + $first_string = strtolower(mb_substr($string, 0, 1));
  59 + if (is_numeric($first_string))
  60 + return 0;
  61 + $string_key = array_search($first_string, $this->firstNumWord);
  62 + return $string_key ?: 27;
  63 + }
  64 +
  65 + /**
53 * @remark :视频 66 * @remark :视频
54 * @name :getKeywordVideoAttribute 67 * @name :getKeywordVideoAttribute
55 * @author :lyh 68 * @author :lyh
@@ -97,4 +110,43 @@ class Keyword extends Base @@ -97,4 +110,43 @@ class Keyword extends Base
97 return Keyword::where("project_id",$project->id)->where("status",1)->inRandomOrder()->take(10)->get(); 110 return Keyword::where("project_id",$project->id)->where("status",1)->inRandomOrder()->take(10)->get();
98 } 111 }
99 112
  113 + /**
  114 + * 拆解关键词, 拼接成为长尾关键词
  115 + * @param $project_id
  116 + * @return array
  117 + */
  118 + public static function extendKeyword($project_id)
  119 + {
  120 + $result = [];
  121 + $keywords = self::where(['project_id' => $project_id])->pluck('seo_title', 'title')->toArray();
  122 + if (empty($keywords))
  123 + return $result;
  124 + // $item:前缀 + 关键词 + 后缀
  125 + foreach ($keywords as $keyword => $item) {
  126 + try {
  127 + if (empty($keyword) || empty($item))
  128 + continue;
  129 +
  130 + // 如果title数据无法正常解析, 就将数据完整填充
  131 + $tmp = explode($keyword, $item);
  132 + if (count($tmp) <= 1) {
  133 + array_push($result, $item);
  134 + continue;
  135 + }
  136 +
  137 + // 正常数据 一个关键词会拆分成为 3或者5个关键词
  138 + // 拆分规则:前缀 + 关键词; 关键词 + 后缀;前缀 + 关键词 + 后缀;
  139 + $prefix = $tmp[0];
  140 + $suffix_array = explode(',', $tmp[1]);
  141 + array_push($result, trim($prefix . $keyword));
  142 + foreach ($suffix_array as $suffix) {
  143 + array_push($result, trim($keyword . $suffix));
  144 + array_push($result, trim($prefix . $keyword . $suffix));
  145 + }
  146 + } catch (\Exception $e) {
  147 + continue;
  148 + }
  149 + }
  150 + return $result;
  151 + }
100 } 152 }