From 691cb5421abac61b1ee082cef31317b428691fea Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 20 Aug 2025 17:52:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/crm/service/CustomerService.php | 37 +++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/app/crm/service/CustomerService.php b/app/crm/service/CustomerService.php index ef5aa71..64e0ce8 100644 --- a/app/crm/service/CustomerService.php +++ b/app/crm/service/CustomerService.php @@ -10,12 +10,16 @@ use app\crm\logic\CustomerLogic; use app\crm\model\Channel; use app\crm\model\Country; use app\crm\model\Customer; +use app\crm\model\Tag; use app\crm\model\CustomerHis; use app\crm\model\Follow; use app\crm\utils\Util; use config\constants\Constants; use support\Request; +/** + * @method mixed complexQuery(array $params) + */ class CustomerService { @@ -37,6 +41,8 @@ class CustomerService $follow = $params['follow'] ?? ''; $follow_last = $params['follow_last'] ?? ''; $tagId = $params['tag_id'] ?? []; + $sort = $params['sort'] ?? null; + $sortId = $params['sort_id'] ?? null; $countryId = $params['country_id'] ?? ''; $where = []; $user_id =$params['user_id']; @@ -72,7 +78,7 @@ class CustomerService $where[] = ['follow_last', '>', strtotime('-7 day')]; } - $query = Customer::where($where); + $query = Customer::select('crm_customer.*')->where($where); if(!empty($tagId)){ //查询有$tagId 里任意一个标签的客户 @@ -115,7 +121,34 @@ class CustomerService $query->withTrashed(); } - $customer = $query->orderBy('id','desc')->paginate($perPage, ['*'], 'page', $page); + if ($sort == 'tag_group') { + // 排序:按关联的 crm_tag.sort 最小值升序,其次按客户ID倒序 + $minSortSub = Tag::selectRaw('MIN(crm_tag.sort) AS min_sort, crm_customer_tag.customer_id') + ->join('crm_customer_tag', 'crm_customer_tag.tag_id', '=', 'crm_tag.id') + ->whereNull('crm_tag.deleted_at') + ->when(!empty($sortId), function ($q) use ($sortId) { + $q->where('crm_tag.group_id', $sortId); + }) + ->groupBy('crm_customer_tag.customer_id'); + + $query->leftJoinSub($minSortSub, 'tag_min', function ($join) { + $join->on('crm_customer.id', '=', 'tag_min.customer_id'); + }); + + $query = $query->orderByRaw('tag_min.min_sort IS NULL') + ->orderBy('tag_min.min_sort', 'asc') + ->orderBy('crm_customer.id', 'desc'); + }elseif ($sort == 'follow_last') { + $query = $query->orderBy('crm_customer.follow_last', 'desc'); + }elseif ($sort == 'follow_next') { + $query = $query->orderBy('crm_customer.follow_next', 'asc'); + $query = $query->orderByRaw('crm_customer.follow_next IS NULL, crm_customer.follow_next ASC'); + }elseif ($sort == 'id' || empty($sort)) { + $query = $query->orderBy('crm_customer.id', 'desc'); + } + + $customer = $query->paginate($perPage, ['*'], 'page', $page); + foreach ($customer as &$item) { $item = $this->getCustomer($item); if($seas == 'seas'){