VideoTask.php 6.9 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\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' => []]),
                    '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' => [],
                    'description' => $valData['description'],
                    'images' => $valData['images']
                ],
                'task_id' => $task_id,
                'callback_url' => env('APP_URL') . '/api/video_task_callback',
//                'callback_url' => url('a/getKeywordVideo?project_id='.$val->project_id.'&keyword_id='.$val->keyword_id.'&video='),
            ];
            $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]);
        $productModel = new Product();
        $productList = $productModel->list(['keyword_id'=>['like','%,'.$keywordInfo['id'].',%']],['thumb','title']);
        if(count($productList) < 5){
            $productList = $productModel->inRandomOrder()->take(100)->get()->toArray();
            //获取7个产品主图
        }
        $product_image = [];
        foreach ($productList as  $v){
            $v = (array)$v;
            $image = [];
            if(!empty($v['thumb']) && !empty($v['thumb']['url'])){
                $image['image'] = getImageUrl($v['thumb']['url']);
                $image['title'] = $v['title'];
                $product_image[] = $image;
            }
            if(count($product_image) > 6){
                break;
            }
        }
        $domainModel = new DomainInfo();
        $domainInfo = $domainModel->read(['project_id'=>$project_id]);
        if(!empty($domainInfo)){
            $keywordInfo['route'] = $domainInfo['domain'].'/'.$keywordInfo['route'];
        }
        $data = [
            'url'=>$keywordInfo['route'],
            'title'=>$keywordInfo['title'],
            'keyword_title'=>$keywordInfo['keyword_title'],
            'keyword_content'=>$keywordInfo['keyword_content'],
            'product_list'=>$product_image
        ];
        return $data;
    }
}