php 数据遍历查询

  //查询出所有需要待更新的数据,分页处理
        $query = OrderExportJob::query();
        $page = 1;
        $limit = 1000;
        $count = $data = $query->from('order_export_job')
            ->where('job_status', 0)
            ->where('batch_id', $batchId)
            ->where('job_name','<>','OrderSkuPerformanceQueue')
            ->count();
        $totalPages = ceil($count / $limit);
        do {
            $offset = ($page - 1) * $limit;
            $data = $query->from('order_export_job')
                ->where('job_status', 0)
                ->where('batch_id', $batchId)
                ->where('job_name','<>','OrderSkuPerformanceQueue')
                ->offset($offset)
                ->limit($limit)
                ->get();
            if (!empty($data)) {
                $data = $data->toArray();
                if (!empty($data) && is_array($data)) {
                    foreach ($data as $row) {
                        try {
                            $this->exportTask($row);
                            OrderExportJob::where('id', $row['id'])->update([
                                'job_status' => 2, "updated_at" => date('Y-m-d H:i:s'), 'error_msg' => ''
                            ]);
                        } catch (\Exception $e) {
                            @file_put_contents("/tmp/3.txt","\n".$e->getTraceAsString()."\n",FILE_APPEND);
                            OrderExportJob::where('id', $row['id'])->update([
                                'job_status' => 3, "updated_at" => date('Y-m-d H:i:s'), 'error_msg' => $e->getMessage()
                            ]);
                            OrderDataProcess::where('id', $row['process_id'])->update(['data_status'=>2]);
                            continue;
                        }
                    }
                }
            }
            $page++;
        } while ($page <= $totalPages);

 

posted on 2022-10-31 13:34  kevin_yang123  阅读(86)  评论(0编辑  收藏  举报