ProjectComment.php 3.0 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 ProjectComment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project_comment';

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

    public $number = 100;

    public function handle(){
        $projectModel = new Project();
        $ids = $projectModel->selectField(['delete_status' => 0,'project_type'=>0,'extend_type'=>0,'type'=>['in',[1,2,3,4,6]]], 'id');
        foreach ($ids as $id){
            echo date('Y-m-d H:i:s').'start:' . $id . PHP_EOL;
            $project_id = $id;
            echo date('Y-m-d H:i:s').'执行的项目id:' . $project_id . PHP_EOL;
            try {
                $this->_action($project_id);
            }catch (\Exception $e){
                echo date('Y-m-d H:i:s').'错误信息:'.$e->getMessage().PHP_EOL;
                continue;
            }
            echo date('Y-m-d H:i:s').'end:' . $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']);
        $info['ai'] = str_replace('50', '100', $info['ai']);
        $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){
                $item = str_replace("'", '"', $item);
                // 解码成 PHP 关联数组
                $item = json_decode($item, true);
                if(!isset($item['name']) || !isset($item['comment'])){
                    continue;
                }
                $twoMonthsAgo = strtotime('-2 months');
                $randomTimestamp = rand($twoMonthsAgo,  time());
                $randomDateTime = date('Y-m-d H:i:s', $randomTimestamp);
                $data[] = [
                    'nickname'=>$item['name'],
                    'text'=>$item['comment'],
                    'project_id'=>$project_id,
                    'type'=>1,
                    'uid'=>0,
                    'start_time'=>$randomDateTime,
                    'created_at'=>date('Y-m-d H:i:s'),
                    'updated_at'=>date('Y-m-d H:i:s')
                ];
            }
            $keywordCommentModel = new AggregateKeywordComment();
            $keywordCommentModel->insertAll($data);
        }
        return true;
    }
}