作者 刘锟

AI分析行业脚本

<?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;
}
}
... ...
... ... @@ -35,7 +35,7 @@ class Temp extends Command
public function handle()
{
$this->domain_rewrite_https();
}
/**
... ...
<?php
namespace App\Models\Industry;
use App\Models\Base;
class ProjectIndustry extends Base
{
protected $table = 'gl_project_industry';
}
... ...
<?php
namespace App\Models\Industry;
use App\Helper\Arr;
use App\Models\Base;
class ProjectIndustryRelated extends Base
{
protected $table = 'gl_project_industry_related';
/**
* 项目行业数据关联
* @param $project_id
* @param $industry_ids
* @author Akun
* @date 2025/03/05 10:52
*/
public static function saveRelated($project_id, $industry_ids)
{
if (!is_array($industry_ids)) {
$industry_ids = array_filter(Arr::splitFilterToArray($industry_ids), 'intval');
}
//先删除
self::where('project_id', $project_id)->delete();
//批量保存
if (!empty($industry_ids)) {
$data = [];
foreach ($industry_ids as $industry_id) {
$data[] = [
'project_id' => $project_id,
'industry_id' => $industry_id,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
}
self::insert($data);
}
}
}
... ...