作者 刘锟

AI分析行业脚本

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Industry;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Helper\Common;
  7 +use App\Helper\Gpt;
  8 +use App\Models\Ai\AiCommand;
  9 +use App\Models\Domain\DomainInfo;
  10 +use App\Models\Industry\ProjectIndustry;
  11 +use App\Models\Industry\ProjectIndustryRelated;
  12 +use Illuminate\Console\Command;
  13 +
  14 +class CheckProjectIndustry extends Command
  15 +{
  16 + /**
  17 + * The name and signature of the console command.
  18 + *
  19 + * @var string
  20 + */
  21 + protected $signature = 'check_project_industry';
  22 +
  23 + /**
  24 + * The console command description.
  25 + *
  26 + * @var string
  27 + */
  28 + protected $description = '匹配上线项目所属行业';
  29 +
  30 + public function handle()
  31 + {
  32 + $ai_command = AiCommand::where('key', 'project_industry')->value('ai');
  33 + if (!$ai_command) {
  34 + $this->output('AI指令未配置');
  35 + return;
  36 + }
  37 +
  38 + $industry_list = ProjectIndustry::where('status', 1)->pluck('industry_name')->toArray();
  39 + if (!$industry_list) {
  40 + $this->output('行业匹配数据为空');
  41 + return;
  42 + }
  43 + $industry_names = implode(',', $industry_list);
  44 +
  45 + $domainModel = new DomainInfo();
  46 + $list = $domainModel->select(['id', 'domain', 'project_id'])->where('status', 1)->where('project_id', '>', 0)->get()->toArray();
  47 +
  48 + foreach ($list as $value) {
  49 + $project_id = $value['project_id'];
  50 + $domain = $value['domain'];
  51 +
  52 + $has_related_count = ProjectIndustryRelated::select(['id'])->where('project_id', $project_id)->count();
  53 + if ($has_related_count > 0) {
  54 + $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | 已分析过滤');
  55 + continue;
  56 + }
  57 +
  58 + $project_industry = $this->getIndustryByAI($ai_command, $industry_names, $domain);
  59 +
  60 + if ($project_industry) {
  61 + $project_industry_name = Arr::splitFilterToArray($project_industry);
  62 +
  63 + $project_industry_id = ProjectIndustry::where('status', 1)->whereIn('industry_name', $project_industry_name)->pluck('id')->toArray();
  64 +
  65 + ProjectIndustryRelated::saveRelated($project_id, $project_industry_id);
  66 +
  67 + $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | success');
  68 + } else {
  69 + $this->output('project_id:' . $project_id . ' , domain:' . $domain . ' | AI分析行业失败');
  70 + }
  71 + }
  72 + }
  73 +
  74 + /**
  75 + * AI分析行业
  76 + * @param $ai_command
  77 + * @param $industry_names
  78 + * @param $domain
  79 + * @return string|string[]
  80 + * @author Akun
  81 + * @date 2025/03/05 10:42
  82 + */
  83 + public function getIndustryByAI($ai_command, $industry_names, $domain)
  84 + {
  85 + $ai_command = str_replace('{domain}', 'https://' . $domain, $ai_command);
  86 + $ai_command = str_replace('{industry}', $industry_names, $ai_command);
  87 +
  88 + $text = Gpt::instance()->openai_chat_qqs($ai_command);
  89 +
  90 + return Common::deal_str($text);
  91 + }
  92 +
  93 + public function output($msg)
  94 + {
  95 + echo date('Y-m-d H:i:s') . ' | ' . $msg . PHP_EOL;
  96 + }
  97 +}
@@ -35,7 +35,7 @@ class Temp extends Command @@ -35,7 +35,7 @@ class Temp extends Command
35 35
36 public function handle() 36 public function handle()
37 { 37 {
38 - $this->domain_rewrite_https(); 38 +
39 } 39 }
40 40
41 /** 41 /**
  1 +<?php
  2 +
  3 +namespace App\Models\Industry;
  4 +
  5 +use App\Models\Base;
  6 +
  7 +class ProjectIndustry extends Base
  8 +{
  9 + protected $table = 'gl_project_industry';
  10 +}
  1 +<?php
  2 +
  3 +namespace App\Models\Industry;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Models\Base;
  7 +
  8 +class ProjectIndustryRelated extends Base
  9 +{
  10 + protected $table = 'gl_project_industry_related';
  11 +
  12 +
  13 + /**
  14 + * 项目行业数据关联
  15 + * @param $project_id
  16 + * @param $industry_ids
  17 + * @author Akun
  18 + * @date 2025/03/05 10:52
  19 + */
  20 + public static function saveRelated($project_id, $industry_ids)
  21 + {
  22 + if (!is_array($industry_ids)) {
  23 + $industry_ids = array_filter(Arr::splitFilterToArray($industry_ids), 'intval');
  24 + }
  25 + //先删除
  26 + self::where('project_id', $project_id)->delete();
  27 +
  28 + //批量保存
  29 + if (!empty($industry_ids)) {
  30 + $data = [];
  31 + foreach ($industry_ids as $industry_id) {
  32 + $data[] = [
  33 + 'project_id' => $project_id,
  34 + 'industry_id' => $industry_id,
  35 + 'created_at' => date('Y-m-d H:i:s'),
  36 + 'updated_at' => date('Y-m-d H:i:s')
  37 + ];
  38 + }
  39 + self::insert($data);
  40 + }
  41 + }
  42 +}