SendKeyword.php
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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);
}
}