From bbfd80e5b355953eef960ad28ab50bd03367c940 Mon Sep 17 00:00:00 2001 From: yanzai Date: Tue, 25 Nov 2025 17:18:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=B7=E6=BA=90=E6=98=8E=E7=BB=86=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=85=B3=E8=81=94=E7=BA=BF=E4=B8=8B=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../API/Admin/YeWu/PlanController.php | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Laravel/app/Http/Controllers/API/Admin/YeWu/PlanController.php b/Laravel/app/Http/Controllers/API/Admin/YeWu/PlanController.php index 8ae6aaf..a9874ba 100644 --- a/Laravel/app/Http/Controllers/API/Admin/YeWu/PlanController.php +++ b/Laravel/app/Http/Controllers/API/Admin/YeWu/PlanController.php @@ -156,23 +156,30 @@ class PlanController extends Controller $page = request('page'); $pageSize = request('pageSize'); $searchInfo = request('searchInfo'); + +// 构建 orders 子查询:每个 plan_id 只保留一条记录(例如 id 最大的) + $orderSubQuery = DB::table('orders as b') + ->select(DB::raw('MAX(id) as max_order_id'), 'plan_id') + ->whereIn('status', [1, 2, 4]) + ->groupBy('plan_id'); + $list = DB::table('plans as a') - ->select('a.*','b.name as username','b.phone') - ->leftJoin('orders as b', function($join) { - $join->on('a.id', '=', 'b.plan_id') - ->whereIn('b.status', [2,4]); + ->select('a.*', 'b.name as username', 'b.phone') + ->leftJoinSub($orderSubQuery, 'filtered_orders', function ($join) { + $join->on('a.id', '=', 'filtered_orders.plan_id'); }) + ->leftJoin('orders as b', 'filtered_orders.max_order_id', '=', 'b.id') ->where('a.is_del', 0); - if (!isset($searchInfo['date'])) { $searchInfo['date'] = date('Y-m-d'); } - if (isset($searchInfo['plan_type']) and $searchInfo['plan_type']===0) { - $list = $list->where(['a.type' => 0]); + if (isset($searchInfo['plan_type']) && $searchInfo['plan_type'] === 0) { + $list = $list->where('a.type', 0); } - $list = $list->where(['a.date' => $searchInfo['date']]); - $list = $list->orderBy('a.time', 'asc')->get(); + $list = $list->where('a.date', $searchInfo['date']) + ->orderBy('a.time', 'asc') + ->get(); $plan_model=DB::table('plan_model_time') ->select('plan_model_time.*', 'plan_type.name as plan_type_name') ->leftJoin('plan_type', 'plan_type.id', '=', 'plan_model_time.plan_type_id') @@ -188,7 +195,7 @@ class PlanController extends Controller break; } } - if(!empty($searchInfo['plan_type']) and$searchInfo['plan_type']==$v->plan_type_id and $v->type<>0 ){ + if(!empty($searchInfo['plan_type']) and $searchInfo['plan_type']==$v->plan_type_id and $v->type<>0 ){ $list_l[]=$list[$k]; } continue;