VideoTask.php
7.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2024/02/26
* Time: 10:13
*/
namespace App\Console\Commands\KeywordInVideo;
use App\Console\Commands\Model;
use App\Console\Commands\TaskSub;
use App\Enums\Common\Code;
use App\Models\Com\KeywordVideoTask;
use App\Models\Com\KeywordVideoTaskLog;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class VideoTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video_task';
/**
* The console command description.
*
* @var string
*/
protected $description = '视频推广任务';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @var int 最大子任务
*/
public $max_sub_task = 800;
/**
* @return bool
*/
public function handle()
{
Log::info('开始视频推广任务');
$this->createSubTask();
$this->sendSubTask();
Log::info('结束视频推广任务');
return true;
}
/**
* 创建子任务
* TODO 获取需要生成子任务的项目,获取项目中未生成视频的关键词,通过关键词生成初始化子任务
* @return bool
*/
public function createSubTask()
{
$sub_task_num = $this->max_sub_task;
while (true) {
if ($sub_task_num <= 0){
break;
}
$task_project = KeywordVideoTask::where(['status' => KeywordVideoTask::STATUS_OPEN])->orderBy('sort', 'desc')->first();
if (empty($task_project)){
break;
}
ProjectServer::useProject($task_project->project_id);
$keyword = $this->getProjectKeyword();
// 已经没有需要生成视频的关键词
if (FALSE == $keyword->isEmpty()) {
$task_project->status = KeywordVideoTask::STATUS_CLOSE;
$task_project->save();
continue;
}
foreach ($keyword as $val) {
$log = KeywordVideoTaskLog::where(['project_id' => $task_project->project_id, 'keyword_id' => $val->id])->first();
if ($log){
continue;
}
$keywordInfo = $this->getKeywordImage($val->id,$task_project->project_id);
$array = [
'project_id' => $task_project->project_id,
'keyword_id' => $val->id,
'keyword' => $val->title,
'data' => json_encode(['url' => $keywordInfo['url'],'title' => $keywordInfo['title'], 'description' => $keywordInfo['keyword_content'], 'images' => $keywordInfo['product_list'], 'keywords' => $keywordInfo['keyword_list']]),
'status' => KeywordVideoTaskLog::STATUS_INIT,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s'),
];
KeywordVideoTaskLog::insert($array);
$sub_task_num--;
}
$task_project->status = KeywordVideoTask::STATUS_CLOSE;
$task_project->save();
}
return true;
}
/**
* 发送子任务
* @return bool
*/
public function sendSubTask()
{
$subTask = KeywordVideoTaskLog::where(['status' => KeywordVideoTaskLog::STATUS_INIT])->orderBy('id', 'asc')->limit($this->max_sub_task)->get();
if ($subTask->isEmpty())
return true;
foreach ($subTask as $val) {
$valData = json_decode($val->data);
$task_id = 'v6-' . uniqid();
$data = [
'project_data' => [
'tag_url' => $valData['url'],
'title' => $valData['title'],
'keywords' => $valData['keywords'],
'description' => $valData['description'],
'images' => $valData['images']
],
'task_id' => $task_id,
'callback_url' => env('APP_URL') . '/api/video_task_callback',
];
$result = Http::post('http://216.250.255.116:7866/create_task', $data);
$val->task_id = $task_id;
$val->status = KeywordVideoTaskLog::STATUS_RUNNING;
$val->request_result = $result;
$val->save();
}
return true;
}
/**
* 获取未生成页面的关键词
* @return mixed
*/
public function getProjectKeyword()
{
$keyword = Keyword::where('video', null)->whereNotNull('keyword_content')->inRandomOrder()->take(100)->get();
return $keyword;
}
/**
* 获取需要处理的任务
* @return int
*/
public function getTaskProject()
{
// $task_project = Model::where(['status' => Model::STATUS_OPEN])->orderBy('sort', 'desc')->first();
$project_id = 110;
return $project_id;
}
/**
* @remark :根据关键字获取产品主图
* @name :getKeywordList
* @author :lyh
* @method :post
* @time :2024/2/23 16:28
*/
public function getKeywordImage($keyword_id,$project_id){
$keywordModel = new Keyword();
$keywordInfo = $keywordModel->read(['id'=>$keyword_id]);
//TODO::所有产品
$thumb = $this->getRecommendAndHotProducts($keywordInfo['route'],$project_id);
$domainModel = new DomainInfo();
$domainInfo = $domainModel->read(['project_id'=>$project_id]);
if(!empty($domainInfo)){
$keywordInfo['route'] = $domainInfo['domain'].'/'.$keywordInfo['route'];
}
$keyword_arr = Keyword::where("project_id",$project_id)->where("status",1)->inRandomOrder()->take(10)->pluck('title')->toArray();;
$data = [
'url'=>$keywordInfo['route'],
'title'=>$keywordInfo['title'],
'keyword_title'=>$keywordInfo['keyword_title'],
'keyword_content'=>$keywordInfo['keyword_content'],
'product_list'=>$thumb ?? [],
'keyword_list'=>$keyword_arr ?? []
];
return $data;
}
/**
* 关键词聚合页-推荐&热门产品
*/
public function getRecommendAndHotProducts($route,$project_id): ?array
{
$productIds = [];
$productKeyword = Keyword::where("project_id",$project_id)->where("route",$route)->first();
if (!empty($productKeyword)){
$productsQuery = Product::where("project_id", $project_id)->where("status",1)->where("keyword_id","like","%,".$productKeyword->id.",%")->limit(7)->get();
if (!empty($productsQuery)){
foreach ($productsQuery as $item){
$productIds[] = $item->id;
}
if (count($productIds)<7){
$randomData = Product::where("project_id", $project_id)->where("status",1)->whereNotIn('id', $productIds)->inRandomOrder()->take(13-count($productIds))->get();
$products = $productsQuery->merge($randomData);
}else{
$products = $productsQuery;
}
}else{
$products = Product::where("project_id", $project_id)->where("status",1)->inRandomOrder()->take(13)->get();
}
}
$data = [];
if (!empty($products)){
foreach ($products as $item){
$data[] = !empty($item->thumb) && $item->thumb['url'] != "" ? getImageUrl($item->thumb['url']) : "";
}
}
return $data;
}
}