AiBlogTask.php 9.7 KB
<?php
/**
 * @remark :
 * @name   :AiBlogTask.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/14 11:14
 */

namespace App\Console\Commands\Ai;

use App\Helper\Arr;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Ai\AiBlogList;
use App\Models\Com\Notify;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\s;

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

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

    public function handle(){
        $aiBlogTaskModel = new AiBlogTaskModel();
        while (true){
            $list = $aiBlogTaskModel->formatQuery(['status'=>1,'type'=>2])->inRandomOrder()->limit(1000)->get();
            if(empty($list)){
                sleep(300);
                continue;
            }
            $list = $list->toArray();
            $updateProject = [];
            foreach ($list as $item){
                echo '开始->任务id:' . $item['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
                //获取配置
                $aiSettingInfo = $this->getSetting($item['project_id']);
                $aiBlogService = new AiBlogService();
                $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
                $aiBlogService->key = $aiSettingInfo['key'];
                $aiBlogService->task_id = $item['task_id'];
                $result = $aiBlogService->getDetail();
                if(!isset($result['status'])){
                    echo json_encode($result,true).PHP_EOL;
                    continue;
                }
                if($result['status'] != 200){
                    echo '错误状态码:'.$result['status'].PHP_EOL;
                    continue;
                }
                //保存当前项目ai_blog数据
                ProjectServer::useProject($item['project_id']);
                $aiBlogModel = new AiBlog();
                $aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id']);
                if($aiBlogInfo === false){
                    echo '任务id不存在:'.$item['task_id'].PHP_EOL;
                    $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]);
                    continue;
                }
                if (!in_array($result['data']['author_id'], $updateProject[$item['project_id']] ?? [])) {
                    $updateProject[$item['project_id']][] = $result['data']['author_id'];
                }
                //拿到返回的路由查看是否重复
                $route = RouteMap::setRoute($result['data']['url'], RouteMap::SOURCE_AI_BLOG, $aiBlogInfo['id'], $item['project_id']);
                if($route != $result['data']['url']){
                    $aiBlogService->updateDetail(['route'=>$route,'task_id'=>$item['task_id']]);
                }
                $aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>2], ['task_id'=>$item['task_id']]);
                DB::disconnect('custom_mysql');
                $aiBlogTaskModel->edit(['status'=>2],['id'=>$item['id']]);
                echo '结束->任务id:' . $item['task_id'] . PHP_EOL . date('Y-m-d H:i:s');
            }
            //TODO::更新列表页及作者
            $this->updateProject($updateProject);
        }
        return true;
    }

    /**
     * @remark :更新项目作者页面及列表页
     * @name   :updateProject
     * @author :lyh
     * @method :post
     * @time   :2025/3/4 10:25
     */
    public function updateProject($updateProject){
        if(empty($updateProject)){
            return true;
        }
        foreach ($updateProject as $project_id => $author){
            ProjectServer::useProject($project_id);
            $aiSettingInfo = $this->getSetting($project_id);
            $this->updateBlogList($aiSettingInfo);
            //更新作者
            foreach ($author as $val){
                $this->updateAiBlogAuthor($aiSettingInfo,$val);
            }
            DB::disconnect('custom_mysql');
            $this->curlDelRoute($project_id);
        }
        return true;
    }

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

    /**
     * @remark :更新作者的页面
     * @name   :updateAiBlogAuthor
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 11:53
     */
    public function updateAiBlogAuthor($aiSettingInfo,$author_id){
        if(empty($author_id)){
            return true;
        }
        $aiBlogService = new AiBlogService();
        $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
        $aiBlogService->key = $aiSettingInfo['key'];
        $aiBlogService->author_id = $author_id;
        $result = $aiBlogService->getAuthorDetail();
        if(isset($result['status']) && $result['status'] == 200){
            //当前作者的页面
            $aiBlogAuthorModel = new AiBlogAuthor();
            if(!empty($result['data']['section'])){
                $aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$author_id]);
            }
        }
        return true;
    }

    /**
     * @remark :更新列表页
     * @name   :updateBlogList
     * @author :lyh
     * @method :post
     * @time   :2025/2/26 15:42
     */
    public function updateBlogList($aiSettingInfo){
        $aiBlogService = new AiBlogService();
        $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
        $aiBlogService->key = $aiSettingInfo['key'];
        $page = 1;
        $saveData = [];
        $result = $aiBlogService->getAiBlogList($page,15);
        if(!isset($result['status']) && $result['status'] != 200){
            return true;
        }
        $total_page = $result['data']['total_page'];
        //组装数据保存
        $saveData[] = [
            'route'=>$page,
            'text'=>$result['data']['section'],
        ];
        while ($total_page > $page){
            $page++;
            $result = $aiBlogService->getAiBlogList($page,15);
            if(isset($result['status']) && $result['status'] == 200){
                $saveData[] = [
                    'route'=>$page,
                    'text'=>$result['data']['section'],
                ];
            }
        }
        $aiBlogListModel = new AiBlogList();
        if(!empty($saveData)){
            //写一条路由信息
            $aiBlogListModel->truncate();
            $aiBlogListModel->insertAll($saveData);
        }
        return true;
    }

    /**
     * @remark :通知C端生成界面
     * @name   :sendNotice
     * @author :lyh
     * @method :post
     * @time   :2025/3/6 11:51
     */
    public function curlDelRoute($project_id){
        $domainModel = new DomainInfo();
        //获取项目域名
        $domain = $domainModel->getProjectIdDomain($project_id);
        if(!empty($domain)){
            //判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
            $project_model = new Project();
            $project_info = $project_model->read(['id'=>$project_id],['serve_id']);
            if(!$project_info){
                echo '项目不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s');
                return true;
            }
            $serve_ip_model = new ServersIp();
            $serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
            $servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
            if($servers_id == ServerConfig::SELF_SITE_ID){
                //判断是否已有更新进行中
                $notify_model = new Notify();
                $data = [
                    'project_id' => $project_id,
                    'type' => Notify::TYPE_MASTER,
                    'route' => Notify::ROUTE_AI_BLOG,
                    'server_id' => ServerConfig::SELF_SITE_ID,
                    'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
                ];
                $notify = $notify_model->read($data,['id']);

                if(!$notify){
                    $domain_array = parse_url($domain);
                    $data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);
                    $data['status'] = Notify::STATUS_INIT;
                    $data['sort'] = 2;
                    $notify_model->add($data);
                }
                echo '自建站项目:'.$project_id.'更新';
            }else{
                $url = $domain.'api/update_page/?project_id='.$project_id.'&route=7';
                $res = http_get($url);
                echo '返回数据:'.json_encode($res,true).PHP_EOL;
            }
        }else{
            echo '域名不存在:' . $project_id . PHP_EOL . date('Y-m-d H:i:s');
        }
        return true;
    }
}