CheckProjectIndustry.php 5.6 KB
<?php

namespace App\Console\Commands\Industry;

use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Models\Ai\AiCommand;
use App\Models\Domain\DomainInfo;
use App\Models\Industry\ProjectIndustry;
use App\Models\Industry\ProjectIndustryRelated;
use App\Models\WebSetting\WebSetting;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '匹配上线项目所属行业';

    public function handle()
    {
        $ai_command = AiCommand::where('key', 'project_industry')->value('ai');
        if (!$ai_command) {
            $this->output('project_industry指令未配置');
            return;
        }

        $ai_command_new = AiCommand::where('key', 'project_industry2')->value('ai');
        if (!$ai_command_new) {
            $this->output('project_industry2指令未配置');
            return;
        }

        $industry_list = ProjectIndustry::where('status', 1)->pluck('industry_name')->toArray();
        if (!$industry_list) {
            $this->output('行业匹配数据为空');
            return;
        }
        $industry_names = implode(',', $industry_list);

        $domainModel = new DomainInfo();
        $list = $domainModel->select(['id', 'domain', 'project_id'])->where('status', 1)->where('project_id', '>', 0)->get()->toArray();

        foreach ($list as $value) {
            $project_id = $value['project_id'];
            $domain = $value['domain'];

            $has_related_count = ProjectIndustryRelated::select(['id'])->where('project_id', $project_id)->count();
            if ($has_related_count > 0) {
                $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | 已分析过滤');
                continue;
            }

            $is_industry = false;
            $project = ProjectServer::useProject($project_id);
            if ($project) {
                $webSettingModel = new WebSetting();
                $web_setting = $webSettingModel->read(['project_id' => $project_id], ['title', 'keyword']);
                if (isset($web_setting['title']) && $web_setting['title'] && isset($web_setting['keyword']) && $web_setting['keyword']) {
                    $project_industry = $this->getIndustryByAINew($ai_command_new, $industry_names, $web_setting['title'], $web_setting['keyword']);
                    if ($project_industry) {
                        $project_industry_name = Arr::splitFilterToArray($project_industry);

                        $project_industry_id = ProjectIndustry::where('status', 1)->whereIn('industry_name', $project_industry_name)->pluck('id')->toArray();

                        if ($project_industry_id) {
                            ProjectIndustryRelated::saveRelated($project_id, $project_industry_id);

                            $is_industry = true;

                            $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | success');
                        }
                    }
                }

                DB::disconnect('custom_mysql');
            }

            if (!$is_industry) {
                $project_industry = $this->getIndustryByAI($ai_command, $industry_names, $domain);

                if ($project_industry) {
                    $project_industry_name = Arr::splitFilterToArray($project_industry);

                    $project_industry_id = ProjectIndustry::where('status', 1)->whereIn('industry_name', $project_industry_name)->pluck('id')->toArray();

                    if ($project_industry_id) {
                        ProjectIndustryRelated::saveRelated($project_id, $project_industry_id);

                        $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | success');
                    } else {
                        $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | AI分析行业失败');
                    }
                } else {
                    $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | AI分析行业失败');
                }
            }
        }
    }

    /**
     * AI分析行业
     * @param $ai_command
     * @param $industry_names
     * @param $domain
     * @return string|string[]
     * @author Akun
     * @date 2025/03/05 10:42
     */
    public function getIndustryByAI($ai_command, $industry_names, $domain)
    {
        $ai_command = str_replace('{domain}', 'https://' . $domain, $ai_command);
        $ai_command = str_replace('{industry}', $industry_names, $ai_command);

        $text = Gpt::instance()->openai_chat_qqs($ai_command);

        return Common::deal_str($text);
    }

    /**
     * AI分析行业2.0
     * @param $ai_command
     * @param $industry_names
     * @param $title
     * @param $keyword
     * @return string|string[]
     * @author Akun
     * @date 2025/08/01 10:10
     */
    public function getIndustryByAINew($ai_command, $industry_names, $title, $keyword)
    {
        $ai_command = str_replace('{industry}', $industry_names, $ai_command);
        $ai_command = str_replace('{title}', $title, $ai_command);
        $ai_command = str_replace('{keyword}', $keyword, $ai_command);

        $text = Gpt::instance()->openai_chat_qqs($ai_command);

        return Common::deal_str($text);
    }

    public function output($msg)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $msg . PHP_EOL;
    }
}