liuhairui 6 сар өмнө
parent
commit
9b983edd03

+ 138 - 140
application/api/controller/WorkOrder.php

@@ -19,7 +19,6 @@ class WorkOrder extends Api
 
     /**
      * 出图接口
-     *
      * 此方法处理图像转换为文本的请求,将图像信息存入队列以供后续处理。
      */
     public function imageToText()
@@ -30,35 +29,28 @@ class WorkOrder extends Api
         $this->success('任务成功提交至队列');
     }
 
-
-
-    //查询队列列表
+    /**
+     * 查询队列列表
+     */
     public function get_queue_logs()
     {
         $params = $this->request->param('old_image_file', '');
-
-        // 查询 queue_logs 表
         $queue_logs = Db::name('queue_logs')
             ->where('old_image_file', $params)
             ->order('id desc')
             ->select();
-
         foreach ($queue_logs as &$log) {
             $taskId = $log['id'];
-
             // 从 image_task_log 表统计状态
             $statusCount = Db::name('image_task_log')
                 ->field('status, COUNT(*) as count')
                 ->where('task_id', $taskId)
                 ->group('status')
                 ->select();
-
-            // 初始化统计数据
             $log['已完成数量'] = 0;
             $log['处理中数量'] = 0;
             $log['排队中的数量'] = 0;
-
-            // 遍历状态聚合数据
+            $log['失败数量'] = 0;
             foreach ($statusCount as $item) {
                 switch ($item['status']) {
                     case 0:
@@ -70,10 +62,12 @@ class WorkOrder extends Api
                     case 2:
                         $log['已完成数量'] = $item['count'];
                         break;
+                    case -1:
+                        $log['失败数量'] = $item['count'];
+                        break;
                 }
             }
         }
-
         return json([
             'code' => 0,
             'msg' => '查询成功',
@@ -82,105 +76,59 @@ class WorkOrder extends Api
         ]);
     }
 
-    //任务状态统计接口
-    public function getTaskProgress()
-    {
-        $params = $this->request->param('id', '1');
-
-        $task_id = Db::name('image_task_log')->order('id desc')->where('task_id',$params)->value('task_id');
-        $lsit = Db::name('image_task_log')->order('id desc')->where('task_id',$params)->select();
-
-
-        $total = Db::name('image_task_log')->where('task_id', $task_id)->count();
-        $processing = Db::name('image_task_log')->where('task_id', $task_id)->where('status', 1)->count();
-        $success = Db::name('image_task_log')->where('task_id', $task_id)->where('status', 2)->count();
-        $fail = Db::name('image_task_log')->where('task_id', $task_id)->where('status', -1)->count();
-
-        $status = Db::name('queue_logs')->where('id', $task_id)->value('status');
-
-        return json([
-            'data' => $lsit,
-            'task_id' => $task_id,
-            '总数' => $total,
-            '处理中' => $processing,
-            '已完成' => $success,
-            '失败' => $fail,
-            '当前状态' => $status
-        ]);
-    }
-
     /**
-     * 开启队列任务
-     */
-    public function kaiStats()
-    {
-        // 判断是否已有监听进程在运行
-        $check = shell_exec("ps aux | grep 'queue:listen' | grep -v grep");
-        if ($check) {
-            return json([
-                'code' => 1,
-                'msg'  => '监听进程已存在,请勿重复启动'
-            ]);
-        }
-        // 启动监听
-        $command = 'nohup php think queue:listen --queue --timeout=300 --sleep=3 --memory=256 > /var/log/job_queue.log 2>&1 &';
-        exec($command, $output, $status);
-        if ($status === 0) {
-            return json([
-                'code' => 0,
-                'msg'  => '队列监听已启动'
-            ]);
-        } else {
-            return json([
-                'code' => 1,
-                'msg'  => '队列启动失败',
-                'output' => $output
-            ]);
-        }
-    }
-
-    /**
-     * 查看队列任务
+     * 查询总队列状态
      */
     public function queueStats()
     {
-        $statusCounts = Db::name('queue_log')
+        $statusList = Db::name('image_task_log')
             ->field('status, COUNT(*) as total')
-            ->whereTime('created_at', 'today')
-            ->where('status','<>', 4)
+            ->whereTime('create_time', 'today')
             ->group('status')
             ->select();
-        $result = [
-            '待处理' => 0,
-            '处理中' => 0,
-            '成功' => 0,
-            '失败' => 0
-        ];
-        $total = 0;
-
-        foreach ($statusCounts as $row) {
-            $count = $row['total'];
-            $total += $count;
-            switch ($row['status']) {
-                case 0:
-                    $result['待处理'] = $count;
-                    break;
-                case 1:
-                    $result['处理中'] = $count;
-                    break;
-                case 2:
-                    $result['成功'] = $count;
-                    break;
-                case 3:
-                    $result['失败'] = $count;
-                    break;
-            }
+        $statusCount = [];
+        foreach ($statusList as $item) {
+            $statusCount[$item['status']] = $item['total'];
         }
-
+        // 总数为所有状态和
+        $total = array_sum($statusCount);
+        //获取队列当前状态
+        $statusText = Db::name('queue_logs')->order('id desc')->value('status');
         return json([
             'code' => 0,
             'msg' => '获取成功',
-            'data' => ['总任务数' => $total] + $result
+            'data' => [
+                '总任务数'     => $total,
+                '待处理'   => $statusCount[0]  ?? 0,
+                '处理中'   => $statusCount[1]  ?? 0,
+                '成功'   => $statusCount[2]  ?? 0,
+                '失败'     => $statusCount[-1] ?? 0,
+                '当前状态' => $statusText
+            ]
+
+        ]);
+    }
+
+
+    /**
+     * 显示当前运行中的队列监听进程
+     */
+    public function viewQueueStatus()
+    {
+        $redis = new \Redis();
+        $redis->connect('127.0.0.1', 6379);
+        $redis->select(15);
+        $key = 'queues:imgtotxt';
+        $count = $redis->lLen($key);
+        $list = $redis->lRange($key, 0, 9);
+        $parsed = array_map(function ($item) {
+            return json_decode($item, true);
+        }, $list);
+        return json([
+            'code' => 0,
+            'msg'  => '查询成功',
+            'count' => $count,
+            'tasks_preview' => $parsed
         ]);
     }
 
@@ -193,19 +141,14 @@ class WorkOrder extends Api
         $redis->connect('127.0.0.1', 6379);
         $redis->select(15);
 
-        $key = 'queues:txttoimg';
-        $key1 = 'queues:imgtotxt';
-        $count = $redis->lLen($key);
-        $count1 = $redis->lLen($key1);
-        $count = $count+$count1;
-        // 计算时间:当前时间前30分钟
-//        $time = date('Y-m-d H:i:s', strtotime('-30 minutes'));
-
+        $key_txttoimg = 'queues:txttoimg';
+        $key_txttotxt = 'queues:txttotxt';
+        $key_imgtotxt = 'queues:imgtotxt';
+        $count = $redis->lLen($key_txttoimg);
+        $count1 = $redis->lLen($key_txttotxt);
+        $count2 = $redis->lLen($key_imgtotxt);
+        $count = $count+$count1+$count2;
         if ($count === 0) {
-            // 删除数据库中状态为0且 created_at 在最近30分钟的数据
-            $deleteCount = Db::name('queue_log')
-//                ->where('created_at', '>=', $time)
-                ->delete();
             return json([
                 'code' => 1,
                 'msg'  => '暂无队列需要停止'
@@ -213,41 +156,96 @@ class WorkOrder extends Api
         }
 
         // 清空 Redis 队列
-        $redis->del($key);
-        $redis->del($key1);
-
-
-        // 删除数据库中状态为0且 created_at 在最近30分钟的数据
-        $deleteCount = Db::name('queue_log')
-//            ->where('created_at', '>=', $time)
-            ->delete();
+        $redis->del($key_txttoimg);
+        $redis->del($key_txttotxt);
+        $redis->del($key_imgtotxt);
 
         return json([
             'code' => 0,
             'msg'  => '已成功停止队列任务'
         ]);
     }
+
+
     /**
-     * 显示当前运行中的队列监听进程
+     * 开启队列任务
+     * 暂时用不到、服务器已开启自动开启队列模式
      */
-    public function viewQueueStatus()
-    {
-        $redis = new \Redis();
-        $redis->connect('127.0.0.1', 6379);
-        $redis->select(15);
-        $key = 'queues:imgtotxt';
-        $count = $redis->lLen($key);
-        $list = $redis->lRange($key, 0, 9);
-        $parsed = array_map(function ($item) {
-            return json_decode($item, true);
-        }, $list);
-        return json([
-            'code' => 0,
-            'msg'  => '查询成功',
-            'count' => $count,
-            'tasks_preview' => $parsed
-        ]);
-    }
+//    public function kaiStats()
+//    {
+//        // 判断是否已有监听进程在运行
+//        $check = shell_exec("ps aux | grep 'queue:listen' | grep -v grep");
+//        if ($check) {
+//            return json([
+//                'code' => 1,
+//                'msg'  => '监听进程已存在,请勿重复启动'
+//            ]);
+//        }
+//        // 启动监听
+//        $command = 'nohup php think queue:listen --queue --timeout=300 --sleep=3 --memory=256 > /var/log/job_queue.log 2>&1 &';
+//        exec($command, $output, $status);
+//        if ($status === 0) {
+//            return json([
+//                'code' => 0,
+//                'msg'  => '队列监听已启动'
+//            ]);
+//        } else {
+//            return json([
+//                'code' => 1,
+//                'msg'  => '队列启动失败',
+//                'output' => $output
+//            ]);
+//        }
+//    }
+
+    /**
+     * 查看总队列任务
+     */
+//    public function queueStats()
+//    {
+//        $statusCounts = Db::name('image_task_log')
+//            ->field('status, COUNT(*) as total')
+//            ->whereTime('create_time', 'today')
+//            ->order('id desc')
+//            ->select();
+//
+//        $result = [
+//            '待处理' => 0,
+//            '处理中' => 0,
+//            '成功' => 0,
+//            '失败' => 0
+//        ];
+//        $total = 0;
+//
+//        foreach ($statusCounts as $row) {
+//            $count = $row['total'];
+//            $total += $count;
+//            switch ($row['status']) {
+//                case 0:
+//                    $result['待处理'] = $count;
+//                    break;
+//                case 1:
+//                    $result['处理中'] = $count;
+//                    break;
+//                case 2:
+//                    $result['成功'] = $count;
+//                    break;
+//                case 3:
+//                    $result['失败'] = $count;
+//                    break;
+//            }
+//        }
+//
+//        return json([
+//            'code' => 0,
+//            'msg' => '获取成功',
+//            'data' => ['总任务数' => $total] + $result
+//        ]);
+//    }
+
+
+
+
 
 
     //单个调用[可以用来做测试]

+ 1 - 1
application/job/ImageJob.php

@@ -58,7 +58,7 @@ class ImageJob{
 
             if ($logId) {
                 Db::name('image_task_log')->where('id', $logId)->update([
-                    'status' => 99,
+                    'status' => -1,
                     'log' => '图生文失败:' . $e->getMessage(),
                     'update_time' => date('Y-m-d H:i:s')
                 ]);

+ 8 - 3
application/job/TextToTextJob.php

@@ -49,7 +49,7 @@ class TextToTextJob
                     echo "处理时间:{$currentTime}\n";
                     echo "👉 正在处理第 " . ($index + 1) . " 条,ID: {$row['id']}\n";
 
-                    $result = $this->textToTxt($data["prompt"], $row['id']);
+                    $result = $this->textToTxt($row['id']);
                     echo $result;
                     echo "✅ 处理结果:完成\n";
                     echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
@@ -98,8 +98,13 @@ class TextToTextJob
     /**
      * 文生文接口
      */
-    public function textToTxt($prompt,$id)
+    public function textToTxt($id)
     {
+        $template = Db::name('template')
+            ->field('id,english_content')
+            ->where('ids',1)
+            ->find();
+
         $record = Db::name('text_to_image')
             ->field('id,english_description')
             ->where('id',$id)
@@ -108,7 +113,7 @@ class TextToTextJob
         if (!$record) {return '没有找到匹配的图像记录';}
 
         // 调用文生文
-        $gptRes = $this->TxtGptApi($prompt.$record['english_description']);
+        $gptRes = $this->TxtGptApi($template['english_content'].$record['english_description']);
         $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
 
         // 更新数据库记录

+ 5 - 1
application/service/ImageService.php

@@ -20,13 +20,17 @@ class ImageService{
 
         // 遍历每个图像,进行处理
         foreach ($batch as $k => $v) {
+            $template = Db::name('template')
+                ->field('id,english_content')
+                ->where('ids',1)
+                ->find();
             $baseItem = [
                 "sourceDir" => $this->sourceDir($v, 1), // 获取源目录
                 "outputDir" => $this->sourceDir($v, 2), // 获取输出目录
                 "file_name" => $this->sourceDir($v, 3), // 获取文件名
                 "type" => $params['type'] ?? '', // 获取处理类型
                 "selectedOption" => $params['selectedOption'], //生图模型参数
-                "prompt" => $params['prompt'], // 获取处理提示
+                "prompt" => $template['content'], // 获取处理提示
                 "width" => $params['width'], // 获取图像宽度
                 "height" => $params['height'] // 获取图像高度
             ];