Merge branch 'hotfix/user' into develop

This commit is contained in:
2025-08-20 17:52:57 +08:00
+35 -2
View File
@@ -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'){