作者 lyh

Merge branch 'zhl' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2024/02/26
* Time: 10:13
*/
namespace App\Console\Commands;
use App\Models\Product\Keyword;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
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 = 'VideoTask';
/**
* 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 = Model::where(['status' => Model::STATUS_OPEN])->orderBy('sort', 'desc')->first();
if (empty($task_project))
break;
$project = ProjectServer::useProject($task_project->project_id);
$keyword = $this->getProjectKeyword();
// 已经没有需要生成视频的关键词
if (FALSE == $keyword->isEmpty()) {
$task_project->status = Model::STATUS_CLOSE;
$task_project->save();
continue;
}
foreach ($keyword as $val) {
$log = TaskSub::where(['project_id' => $task_project->project_id, 'keyword_id' => $val->id])->first();
if ($log)
continue;
$array = [
'project_id' => $task_project->project_id,
'keyword_id' => $val->id,
'keyword' => $val->title,
'data' => json_encode(['url' => '', 'description' => '', 'images' => [], 'keywords' => []]),
'status' => TaskSub::STATUS_INIT,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s'),
];
TaskSub::insert($array);
$sub_task_num--;
}
$task_project->status = Model::STATUS_CLOSE;
$task_project->save();
}
return true;
}
/**
* 发送子任务
* @return bool
*/
public function sendSubTask()
{
$subTask = TaskSub::where(['status' => TaskSub::STATUS_INIT])->orderBy('id', 'asc')->limit($this->max_sub_task)->get();
if ($subTask->isEmpty())
return true;
foreach ($subTask as $val) {
$task_id = 'v6-' . uniqid();
$data = [
'project_data' => [
'tag_url' => '',
'title' => '',
'keywords' => [],
'description' => '',
'images' => ''
],
'task_id' => $task_id,
'callback_url' => '',
];
$result = Http::post('http://216.250.255.116:7866/create_task', $data);
$val->task_id = $task_id;
$val->status = TaskSub::STATUS_RUNING;
$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;
}
}
... ...