AiBlogAuthorTask.php 5.3 KB
<?php
/**
 * @remark :
 * @name   :AiBlogAuthorTask.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/21 11:12
 */

namespace App\Console\Commands\Ai;

use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class AiBlogAuthorTask extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'save_ai_blog_author';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '查询ai_blog_author是否已经生成';


    /**
     * @remark :获取作者
     * @name   :handle
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 11:30
     */
    public function handle(){
        $aiBlogTaskModel = new AiBlogTaskModel();
        while (true){
            $info = $aiBlogTaskModel->where('status',1)->where('type',1)->inRandomOrder()->first();
            if(empty($info)){
                sleep(300);
                continue;
            }
            $info = $info->toArray();
            echo date('Y-m-d H:i:s').'开始->project_id:' . $info['project_id'] . PHP_EOL;
            //获取配置
            $aiSettingInfo = $this->getSetting($info['project_id']);
            if(empty($aiSettingInfo)){
                continue;
            }
            $aiBlogService = new AiBlogService();
            $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
            $aiBlogService->key = $aiSettingInfo['key'];
            $result = $aiBlogService->getAuthor();
            if(!isset($result['status'])){
                echo '错误:'.json_encode($result,true);
                continue;
            }
            if($result['status'] != 200){
                sleep(10);
                continue;
            }
            if(empty($result['data'])){
                echo '没有作者任务-'.PHP_EOL;
                continue;
            }
            //保存当前项目ai_blog数据
            ProjectServer::useProject($info['project_id']);
            $this->saveAiBlogAuthor($result['data'] ?? [],$info['project_id']);
            RouteMap::setRoute('top-blog',RouteMap::SOURCE_AI_BLOG_LIST,0,$info['project_id']);//写一条列表页路由
            RouteMap::setRoute('top-video',RouteMap::SOURCE_AI_VIDEO_LIST,0,$info['project_id']);//写一条列表页路由
            DB::disconnect('custom_mysql');
            //修改任务状态
            $aiBlogTaskModel->edit(['status'=>2],['id'=>$info['id']]);
            echo date('Y-m-d H:i:s').'结束->任务id:' . $info['id'] . PHP_EOL;
        }
        return true;
    }


    /**
     * @remark :获取项目配置
     * @name   :getSetting
     * @author :lyh
     * @method :post
     * @time   :2025/2/14 11:27
     */
    public function getSetting($project_id){
        $ai_cache = Cache::get('ai_blog_'.$project_id);
        if($ai_cache){
            return $ai_cache;
        }
        $projectAiSettingModel = new ProjectAiSetting();
        $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
        Cache::put('ai_blog_'.$project_id,$aiSettingInfo,3600);
        return $aiSettingInfo;
    }

    /**
     * @remark :保存数据
     * @name   :saveAiBlogAuthor
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 11:36
     */
    public function saveAiBlogAuthor($data,$project_id){
        if(empty($data)){
            return true;
        }
        $aiBlogAuthorModel = new AiBlogAuthor();
        foreach ($data as $v){
            $param = [
                'author_id'=>$v['id'],
                'title'=>$v['title'],
                'image'=>str_replace_url($v['picture']),
                'description'=>$v['description'],
            ];
            //查询当前数据是否存在
            $info = $aiBlogAuthorModel->read(['author_id'=>$v['id']]);
            try {
                if($info === false){
                    echo '执行新增'.PHP_EOL;
                    $id = $aiBlogAuthorModel->addReturnId($param);
                    $param['route'] = RouteMap::setRoute($v['route'] ?? $v['title'], RouteMap::SOURCE_AI_BLOG_AUTHOR, $id, $project_id);
                    $aiBlogAuthorModel->edit(['route'=>$param['route']],['id'=>$id]);
                }else{
                    $param['route'] = RouteMap::setRoute($v['route'] ?? $v['title'], RouteMap::SOURCE_AI_BLOG_AUTHOR, $info['id'], $project_id);
                    $aiBlogAuthorModel->edit($param,['id'=>$info['id']]);
                    echo '执行更新'.PHP_EOL;
                }
                $aiSettingInfo = $this->getSetting($project_id);
                $aiBlogService = new AiBlogService();
                $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
                $aiBlogService->key = $aiSettingInfo['key'];
                $aiBlogService->updateAuthorInfo(['author_id'=>$param['author_id'],'route'=>$param['route'],'title'=>$param['title'],'picture'=>$param['image'],'description'=>$param['description']]);
            }catch (\Exception $e){
                echo 'error:'.$e->getMessage().PHP_EOL;
                continue;
            }
        }
        return true;
    }
}