lyhDemo.php 6.4 KB
<?php
/**
 * @remark :
 * @name   :lyhDemo.php
 * @author :lyh
 * @method :post
 * @time   :2025/3/24 9:38
 */

namespace App\Console\Commands\LyhTest;

use App\Console\Commands\Domain\DomainInfo;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Com\NoticeLog;
use App\Models\Project\AiBlogTask;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\Project\OnlineCheck;
use App\Models\Project\Project;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Models\Visit\Visit;
use App\Models\WebSetting\WebLanguage;
use App\Models\WebSetting\WebSetting;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新路由';

    public function handle(){
        $projectIds = DB::table('gl_project_ai_setting')
            ->whereIn('mch_id', function ($query) {
                $query->select('mch_id')
                    ->from('gl_project_ai_setting')
                    ->groupBy('mch_id')
                    ->havingRaw('COUNT(*) > 1');
            })
            ->pluck('project_id');
        $projectIds[] = 811;
        $projectIds[] = 1367;
        $projectIds[] = 1370;
        $projectIds[] = 2201;
        $projectIds[] = 2260;
        $projectIds[] = 2259;
//        $aiSettingModel = new ProjectAiSetting();
//        foreach ($projectIds as $item){
//            $info = $aiSettingModel->read(['project_id'=>$item]);
//            if($info === false){
//                echo '项目id:'.$item.PHP_EOL;
//            }else{
//                $this->createAuthor($item,$info['mch_id'],$info['key']);
//            }
//        }
//        return true;
        $aiSettingModel = new ProjectAiSetting();
        $aiSettingModel->del(['project_id'=>['in',$projectIds]]);
        $projectModel = new Project();
        $logic = new ProjectLogic();
        $lists = $projectModel->list(['delete_status' => 0,'extend_type'=>0,'id'=>['in',$projectIds]], 'id', ['id']);
        $title = [];
        foreach ($lists as $val) {
            //清空作者
            ProjectServer::useProject($val['id']);
            AiBlogAuthor::truncate();
            $routeMapModel = new RouteMap();
            $routeMapModel->del(['source'=>$routeMapModel::SOURCE_AI_BLOG_AUTHOR]);
            DB::disconnect('custom_mysql');
            //重新创建项目拉取作者
            $info = $logic->getProjectInfo($val['id']);
            $title[] = $info['company']?:$info['title'];
            if(empty($info['main_lang_id'])){
                $info['main_lang_id'] = 1;
            }
            if(empty($info['is_ai_blog'])){
                $info['is_ai_blog'] = 1;
            }
            try {
                $this->setAiBlog($info['id'],$info['main_lang_id'],$info['is_ai_blog'] ?? 0,
                    $info['company']??"", $info['deploy_optimize']['company_en_name'] ?? '',
                    $info['deploy_optimize']['company_en_description'] ?? '',$info['is_ai_video'] ?? 0,$info['is_related_video'] ?? 0);
            }catch (\Exception $e){
                continue;
            }

        }
        return true;
    }
    public function setAiBlog($project_id,$main_lang_id,$is_ai_blog,$company,$company_en_name,$company_en_description,$is_ai_video = 0,$is_related_video = 0){
        if(empty($main_lang_id) || (empty($is_ai_blog) && empty($is_ai_video))){
            echo '创建失败:'.$project_id.PHP_EOL;
        }
        $projectInfo = $this->model->read(['id'=>$project_id],['title','main_lang_id','company']);
        $projectOptimize = DeployOptimize::where('project_id', $project_id)->first();
        //获取项目主语种
        $languageModel = new WebLanguage();
        $languageInfo = $languageModel->read(['id'=>$main_lang_id],['short']);
        if($languageInfo == false){
            echo '创建失败:'.$project_id.PHP_EOL;
        }
        $aiSettingModel = new ProjectAiSetting();
        $aiSettingInfo = $aiSettingModel->read(['project_id'=>$project_id]);
        if($aiSettingInfo === false){
            $aiBlogService = new AiBlogService();
            $result = $aiBlogService->createProject($projectInfo['company']?:$projectInfo['title'],$languageInfo['short'],$company_en_description,$company_en_name,$is_related_video);
            if(isset($result['status']) && $result['status'] == 200){
                //查看当前项目是否已有记录
                $resData = [
                    'project_id'=>$project_id,
                    'mch_id'=>$result['data']['mch_id'],
                    'key'=>$result['data']['key'],
                ];
                $aiSettingModel->add($resData);
                $this->createAuthor($project_id,$result['data']['mch_id'],$result['data']['key']);
            }else{
                echo '创建失败:'.$project_id.PHP_EOL;
            }
        }else{
            //有信息更新
            if(($projectInfo['company'] != $company) || ($projectInfo['main_lang_id'] != $main_lang_id)
                || ($projectOptimize['company_en_name'] != $company_en_name) || ($projectOptimize['company_en_description'] != $company_en_description)){
                $aiBlogService = new AiBlogService();
                $aiBlogService->mch_id = $aiSettingInfo['mch_id'];
                $aiBlogService->key = $aiSettingInfo['key'];
                $aiBlogService->updatedProject($projectInfo['company']?:$projectInfo['title'],$languageInfo['short'],$company_en_description,$company_en_name,$is_related_video);
            }
        }
        return true;
    }

    public function createAuthor($project_id,$mch_id,$key){
        //查看当前项目是否已经创建了作者
        $aiBlogTaskModel = new AiBlogTask();
        $aiBlogService = new AiBlogService();
        $aiBlogService->mch_id = $mch_id;
        $aiBlogService->key = $key;
        $result = $aiBlogService->createAuthor();
        if($result['status'] == 200){
            //查看当前是否已有未执行的
            $aiBlogTaskModel->add(['project_id'=>$project_id,'status'=>1,'type'=>1]);
        }
        return true;
    }
}