CheckProjectIndustry.php 3.0 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 Illuminate\Console\Command;

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('AI指令未配置');
            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;
            }

            $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();

                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分析行业失败');
            }
        }
    }

    /**
     * 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);
    }

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