Browse Source

超节损队列优化

qiuenguang 11 months ago
parent
commit
b2e78681eb
1 changed files with 38 additions and 27 deletions
  1. 38 27
      application/api/controller/OrderSuperLoss.php

+ 38 - 27
application/api/controller/OrderSuperLoss.php

@@ -2069,13 +2069,15 @@ class OrderSuperLoss extends Api
     //循环插入超节损数据
     //循环插入超节损数据
     public function orderLossData()
     public function orderLossData()
     {
     {
-        if ($this->request->isGet() === false){
+        if ($this->request->isGet() === false) {
             $this->error('请求错误');
             $this->error('请求错误');
         }
         }
+
         $param = $this->request->param();
         $param = $this->request->param();
         $where = [];
         $where = [];
-        // 判断接收数据,创建查询条件
-        if (isset($param['gdbh']) && isset($param['yjno']) && !empty($param['gdbh']) && !empty($param['yjno'])) {
+
+        // 创建查询条件
+        if (!empty($param['gdbh']) && !empty($param['yjno'])) {
             $where['a.jjcp_gdbh'] = $param['gdbh'];
             $where['a.jjcp_gdbh'] = $param['gdbh'];
             $where['a.jjcp_yjno'] = $param['yjno'];
             $where['a.jjcp_yjno'] = $param['yjno'];
         }
         }
@@ -2084,7 +2086,7 @@ class OrderSuperLoss extends Api
         }
         }
         $where['a.jjcp_smb'] = ['like', '末%'];
         $where['a.jjcp_smb'] = ['like', '末%'];
 
 
-        // 根据查询条件查询出需要计算超节损的工单和印件
+        // 查询数据
         $list = Db::name('成品入仓')
         $list = Db::name('成品入仓')
             ->alias('a')
             ->alias('a')
             ->join('设备_产量计酬 b', 'a.jjcp_gdbh = b.sczl_gdbh AND a.jjcp_yjno = b.sczl_yjno')
             ->join('设备_产量计酬 b', 'a.jjcp_gdbh = b.sczl_gdbh AND a.jjcp_yjno = b.sczl_yjno')
@@ -2098,31 +2100,37 @@ class OrderSuperLoss extends Api
             ->group('a.jjcp_gdbh,a.jjcp_yjno')
             ->group('a.jjcp_gdbh,a.jjcp_yjno')
             ->select();
             ->select();
 
 
-        // 创建redis
+        // Redis配置
         $options = [
         $options = [
-            'host'       => '127.0.0.1',
-            'port'       => 6379,
-            'password'   => '',
-            'select'     => 14,
-            'timeout'    => 0,
-            'expire'     => 0,
-            'persistent' => false,
-            'prefix'     => '',
+            'host'     => '127.0.0.1',
+            'port'     => 6379,
+            'password' => '',
+            'select'   => 14,
+            'timeout'  => 2, // 增加超时设置
         ];
         ];
-        $redis = new Redis($options);
 
 
-        // 生成批次ID
-        $batchId = 'batch_' . uniqid() . '_' . time();
-        $totalTasks = count($list);
-
-        //设置队列信息
-        $pipe = $redis->pipeline();
         try {
         try {
+            // 创建原生Redis连接
+            $redis = new \Redis();
+            $redis->connect($options['host'], $options['port'], $options['timeout']);
+
+            if (!empty($options['password'])) {
+                $redis->auth($options['password']);
+            }
+            $redis->select($options['select']);
+
+            // 生成批次ID
+            $batchId = 'batch_' . uniqid() . '_' . time();
+            $totalTasks = count($list);
+
+            // 使用管道操作
+            $pipe = $redis->multi(\Redis::PIPELINE);
+
             // 设置批次信息
             // 设置批次信息
-            $pipe->hmset("batch:{$batchId}", [
-                'total'    => $totalTasks,
-                'processed' => 0,
-                'status'    => 'processing',
+            $pipe->hMSet("batch:{$batchId}", [
+                'total'      => $totalTasks,
+                'processed'  => 0,
+                'status'     => 'processing',
                 'created_at' => date('Y-m-d H:i:s')
                 'created_at' => date('Y-m-d H:i:s')
             ]);
             ]);
             $pipe->expire("batch:{$batchId}", 3600);
             $pipe->expire("batch:{$batchId}", 3600);
@@ -2135,19 +2143,22 @@ class OrderSuperLoss extends Api
                     'yjno'     => $item['jjcp_yjno'],
                     'yjno'     => $item['jjcp_yjno'],
                     'retries'  => 0
                     'retries'  => 0
                 ];
                 ];
-                $pipe->rpush('order_loss_queue', json_encode($task));
+                $pipe->rPush('order_loss_queue', json_encode($task));
             }
             }
 
 
             // 执行管道
             // 执行管道
             $pipe->exec();
             $pipe->exec();
+
+        } catch (\RedisException $e) {
+            $this->error('Redis连接失败: ' . $e->getMessage());
         } catch (\Exception $e) {
         } catch (\Exception $e) {
-            $pipe->discard();
             $this->error('队列操作失败: ' . $e->getMessage());
             $this->error('队列操作失败: ' . $e->getMessage());
         }
         }
 
 
         $this->success('任务已加入队列处理', [
         $this->success('任务已加入队列处理', [
             'batch_id'    => $batchId,
             'batch_id'    => $batchId,
-            'queue_count' => $redis->llen('order_loss_queue')]);
+            'queue_count' => $redis->lLen('order_loss_queue')
+        ]);
     }
     }
 }
 }