作者 zhl

不达标项目推送同志

<?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\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', 'gl_project.is_upgrade', '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];
foreach ($projects as $item) {
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) {
$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;
}
public function sendNotice($item)
{
$itme = $item->toArray();
try {
// TODO 推送通知 待定
} catch (\Exception $e) {
$this->output('推送通知失败,项目ID:' . $item['id']);
}
return true;
}
/**
* @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);
}
}
\ No newline at end of file
... ...
... ... @@ -50,6 +50,19 @@ class Keyword extends Base
];
/**
* 聚合页按照首字母归类
* @param $string
* @return int
*/
public function getFirstWord($string) {
$first_string = strtolower(mb_substr($string, 0, 1));
if (is_numeric($first_string))
return 0;
$string_key = array_search($first_string, $this->firstNumWord);
return $string_key ?: 27;
}
/**
* @remark :视频
* @name :getKeywordVideoAttribute
* @author :lyh
... ... @@ -97,4 +110,43 @@ class Keyword extends Base
return Keyword::where("project_id",$project->id)->where("status",1)->inRandomOrder()->take(10)->get();
}
/**
* 拆解关键词, 拼接成为长尾关键词
* @param $project_id
* @return array
*/
public static function extendKeyword($project_id)
{
$result = [];
$keywords = self::where(['project_id' => $project_id])->pluck('seo_title', 'title')->toArray();
if (empty($keywords))
return $result;
// $item:前缀 + 关键词 + 后缀
foreach ($keywords as $keyword => $item) {
try {
if (empty($keyword) || empty($item))
continue;
// 如果title数据无法正常解析, 就将数据完整填充
$tmp = explode($keyword, $item);
if (count($tmp) <= 1) {
array_push($result, $item);
continue;
}
// 正常数据 一个关键词会拆分成为 3或者5个关键词
// 拆分规则:前缀 + 关键词; 关键词 + 后缀;前缀 + 关键词 + 后缀;
$prefix = $tmp[0];
$suffix_array = explode(',', $tmp[1]);
array_push($result, trim($prefix . $keyword));
foreach ($suffix_array as $suffix) {
array_push($result, trim($keyword . $suffix));
array_push($result, trim($prefix . $keyword . $suffix));
}
} catch (\Exception $e) {
continue;
}
}
return $result;
}
}
... ...