From 73b874c72ad55eb9eef21c36160ac0de58f0189e Mon Sep 17 00:00:00 2001
From: quanwei <419654421@qq.com>
Date: Thu, 05 Feb 2026 10:30:43 +0800
Subject: [PATCH] 优化名片

---
 admin/app/api/model/supplier/Supplier.php |   37 ++++++++++++++++++++++++++++++++++---
 1 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/admin/app/api/model/supplier/Supplier.php b/admin/app/api/model/supplier/Supplier.php
index df7fbfb..c96f887 100644
--- a/admin/app/api/model/supplier/Supplier.php
+++ b/admin/app/api/model/supplier/Supplier.php
@@ -402,11 +402,21 @@
                 ->where('is_delete', '=', 0)
                 ->group('shop_supplier_id')
                 ->buildSql();
+            
+            // 添加评论数量子查询
+            $commentSubQuery = \app\common\model\product\Comment::alias('c')
+                ->field('shop_supplier_id, COUNT(*) as comment_count')
+                ->where('status', '=', 1) // 已审核通过的评论
+                ->where('is_delete', '=', 0)
+                ->group('shop_supplier_id')
+                ->buildSql();
                 
             $listRaw = $baseQuery
                 ->join([$subQuery => 'avg_sub'], 's.shop_supplier_id = avg_sub.shop_supplier_id', 'LEFT')
+                ->join([$commentSubQuery => 'comment_count_sub'], 's.shop_supplier_id = comment_count_sub.shop_supplier_id', 'LEFT')
                 ->field("s.shop_supplier_id,s.supplier_type,s.name as supplier_name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude,supplier_type,
-                    COALESCE(avg_sub.avg_price, 0) as avg_price")
+                    COALESCE(avg_sub.avg_price, 0) as avg_price,
+                    COALESCE(comment_count_sub.comment_count, 0) as comment_count")
                 ->with(['logo', 'category']);
                 
             // 添加价格范围筛选条件
@@ -422,17 +432,37 @@
             $listRaw = $listRaw->order('avg_sub.avg_price ' . ($priceSortOrder ?? 'desc') . ', s.create_time desc')
                 ->paginate($param);
         } else if ($needDistanceSort) {
+            // 添加评论数量子查询
+            $commentSubQuery = \app\common\model\product\Comment::alias('c')
+                ->field('shop_supplier_id, COUNT(*) as comment_count')
+                ->where('status', '=', 1) // 已审核通过的评论
+                ->where('is_delete', '=', 0)
+                ->group('shop_supplier_id')
+                ->buildSql();
+            
             // 使用子查询计算距离并排序(需要数据库支持地理函数)
             $listRaw = $baseQuery
+                ->join([$commentSubQuery => 'comment_count_sub'], 's.shop_supplier_id = comment_count_sub.shop_supplier_id', 'LEFT')
                 ->field("s.shop_supplier_id,s.supplier_type,s.name as supplier_name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude,supplier_type,
-                    IF(s.latitude > 0 AND s.longitude > 0, " . $user_latitude . ", 99999) as dist_sort")
+                    IF(s.latitude > 0 AND s.longitude > 0, " . $user_latitude . ", 99999) as dist_sort,
+                    COALESCE(comment_count_sub.comment_count, 0) as comment_count")
                 ->with(['logo', 'category'])
                 ->order('dist_sort asc, s.create_time desc')
                 ->paginate($param);
         } else {
+            // 添加评论数量子查询
+            $commentSubQuery = \app\common\model\product\Comment::alias('c')
+                ->field('shop_supplier_id, COUNT(*) as comment_count')
+                ->where('status', '=', 1) // 已审核通过的评论
+                ->where('is_delete', '=', 0)
+                ->group('shop_supplier_id')
+                ->buildSql();
+            
             // 普通查询
             $listRaw = $baseQuery
-                ->field("s.shop_supplier_id,s.supplier_type,s.name as supplier_name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude,supplier_type")
+                ->join([$commentSubQuery => 'comment_count_sub'], 's.shop_supplier_id = comment_count_sub.shop_supplier_id', 'LEFT')
+                ->field("s.shop_supplier_id,s.supplier_type,s.name as supplier_name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude,supplier_type,
+                    COALESCE(comment_count_sub.comment_count, 0) as comment_count")
                 ->order($sort)
                 ->paginate($param);
         }
@@ -535,6 +565,7 @@
                 $v['server_score_text'] = ' 超赞';
             }
             $v['comment'] = $v['fav_count'] ?: 0;
+            $v['comment_count'] = isset($v['comment_count']) ? $v['comment_count'] : 0; // 评论数量
             $v['average_price'] = $average_price; // 人均消费,根据历史订单计算
             $v['distance'] = $distance; // 距离,根据经纬度计算
             $v['max_reduce_price'] = $max_reduce_price; // 最高减价,根据优惠券计算

--
Gitblit v1.9.2