作者 lyh

变更数据

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :GeoWritingsTask.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/10/27 14:12
  8 + */
  9 +
  10 +namespace App\Console\Commands\Geo;
  11 +
  12 +use App\Helper\Gpt;
  13 +use App\Models\Geo\GeoWritings;
  14 +use Illuminate\Console\Command;
  15 +use Illuminate\Support\Facades\Redis;
  16 +use App\Models\Geo\GeoWritingsTask as GeoWritingsTaskModel;
  17 +
  18 +class GeoWritingsTask extends Command
  19 +{
  20 + /**
  21 + * The name and signature of the console command.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $signature = 'geo_writings_task';
  26 +
  27 + public $porject_id;//记录当时执行的project_id
  28 +
  29 + /**
  30 + * The console command description.
  31 + *
  32 + * @var string
  33 + */
  34 + protected $description = 'geoAi生成文章';
  35 +
  36 + public function handle(){
  37 + while (true){
  38 + $geoWritingsTaskModel = new GeoWritingsTaskModel();
  39 + $task_id = $this->getTaskId();
  40 + if(empty($task_id)){
  41 + sleep(60);
  42 + continue;
  43 + }
  44 + echo date("Y-m-d H:i:s").',执行的任务id'.$task_id.PHP_EOL;
  45 + $info = $geoWritingsTaskModel->read(['id'=>$task_id]);
  46 + if($info === false){
  47 + echo date("Y-m-d H:i:s").',任务id数据不存在:'.$task_id.PHP_EOL;
  48 + continue;
  49 + }
  50 + //生成引言
  51 + $aiCommand1 = "请根据这个文章标题:{$info['title']},并同时参考公司的介绍’{$info['description']}‘以及公司参与的事件内容’{$info['event_content']}‘,给我写一个英文Press Release前言内容,前言内容请参考并引用{$info['keyword']}行业的一些专业数据报告,只需要1个段落,大约150-200字,请一定要出现这个关键词“{$info['prefix']}{$info['keyword']}{$info['suffix']}”,所有内容一定要用英文, 只需要回复我引言内容,不需要别的内容(比如序号、你的提示、寒暄、解释、注释之类的)";
  52 + $gptHelper = new Gpt();
  53 + $introduction = $gptHelper->openai_chat_qqs($aiCommand1);
  54 + //生成内容
  55 + $aiCommand2 = "请根据这个文章标题:{$info['title']},并同时参考公司的介绍{$info['description']},以及公司参与的事件内容{$info['event_content']},给我写一篇英文Press Release内容正文(已经有前言内容了),内容请参考并引用“{$info['prefix']}{$info['keyword']}{$info['suffix']}”行业的一些专业数据报告,新闻内容需要 5-6 个大纲,每个大纲需要标题和 1-2 段内容,最后1-2个大纲主要介绍企业的核心优势、主营产品应用场景、主要客户案例,并最后附带内容{$info['footer']},最后只需要回复我新闻稿内容,整个新闻稿内容字数1000字左右,不需要别的内容(比如序号、你的提示、寒暄、解释、注释之类的)";
  56 + $main = $gptHelper->openai_chat_qqs($aiCommand2);
  57 + $data = [
  58 + 'introduction'=>$introduction,
  59 + 'main'=>$main,
  60 + 'status'=>2
  61 + ];
  62 + $geoWritingsTaskModel->edit($data,['task_id'=>$task_id]);
  63 + //组装一条数据
  64 + $geoWritingsModel = new GeoWritings();
  65 + $saveData = [
  66 + 'project_id'=>$info['project_id'],
  67 + 'type'=>$geoWritingsModel::TYPE_AI_CREATE,
  68 + 'title'=>$info['title'],
  69 + 'content'=>$introduction.PHP_EOL.$main,
  70 + 'content_length'=>strlen($introduction.PHP_EOL.$main),
  71 + 'uniqid'=>md5(uniqid().$task_id.$info['project_id']),
  72 + ];
  73 + }
  74 + }
  75 +
  76 + /**
  77 + * @remark :获取任务id
  78 + * @name :getTaskId
  79 + * @author :lyh
  80 + * @method :post
  81 + * @time :2025/10/27 14:22
  82 + */
  83 + public function getTaskId(){
  84 + $task_id = Redis::rpop('geo_writings_task');
  85 + $geoWritingsTaskModel = new GeoWritingsTaskModel();
  86 + if (empty($task_id)) {
  87 + $ids = $geoWritingsTaskModel->formatQuery(['status'=>0])->limit(100)->pluck('id');
  88 + if(!empty($ids)){
  89 + foreach ($ids as $id) {
  90 + Redis::lpush('geo_writings_task', $id);
  91 + }
  92 + $task_id = Redis::rpop('geo_writings_task');
  93 + }
  94 + }else{
  95 + $geoWritingsTaskModel->edit(['status'=>1],['id'=>$task_id]);
  96 + }
  97 + return $task_id;
  98 + }
  99 +}