InitKeywordComment.php 2.4 KB
<?php
/**
 * @remark :
 * @name   :InitKeywordComment.php
 * @author :lyh
 * @method :post
 * @time   :2025/6/3 15:38
 */

namespace App\Console\Commands\Project;

use App\Helper\Common;
use App\Helper\Gpt;
use App\Models\Ai\AiCommand;
use App\Models\Com\NoticeLog;
use App\Models\Project\AggregateKeywordComment;
use App\Models\Project\Project;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化关键字评论';



    public function handle(){
        return $this->_action(467);
        while (true){
            $list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD_COMMON)->where('status', NoticeLog::STATUS_PENDING)->get();
            if(empty($list)){
                sleep(200);
                continue;
            }
            foreach ($list as $item){
                echo date('Y-m-d H:i:s').'start:' . $item['id'] . PHP_EOL;
                $project_id = $item['data']['project_id'];
                echo date('Y-m-d H:i:s').'执行的项目id:' . $project_id . PHP_EOL;
                $this->_action($project_id);
                echo date('Y-m-d H:i:s').'end:' . $item['id'] . PHP_EOL;
            }
        }
        return true;
    }

    /**
     * @remark :执行的方法
     * @name   :_action
     * @author :lyh
     * @method :post
     * @time   :2025/6/3 15:42
     */
    public function _action($project_id){
        $aiCommonModel = new AiCommand();
        $info = $aiCommonModel->read(['key'=>'tag_comment']);
        $text = Gpt::instance()->openai_chat_qqs($info['ai']);
        $text = Common::deal_keywords($text);
        preg_match_all('/\{[^{}]*\}/', $text, $matches);
        if(!empty($text)){
            $data = [];
            foreach ($matches[0] as $item){
                $data[] = [
                    'nickname'=>$item['name'],
                    'text'=>$item['comment'],
                    'project_id'=>$project_id,
                    'type'=>1,
                    'uid'=>0,
                ];
            }
            $keywordCommonModel = new AggregateKeywordComment();
            $keywordCommonModel->insertAll($data);
        }
        return true;
    }
}