VideoTask.php 4.7 KB
<?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\Models\Com\KeywordVideoTask;
use App\Models\Com\KeywordVideoTaskLog;
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 = '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;
                }
                $array = [
                    'project_id' => $task_project->project_id,
                    'keyword_id' => $val->id,
                    'keyword' => $val->title,
                    'data' => json_encode(['url' => '', 'description' => '', 'images' => [], 'keywords' => []]),
                    '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' => 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 = STATUS_RUNING::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;
    }
}