2025-07-09 15:10:14 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\crm\service;
|
|
|
|
|
|
2025-07-30 15:15:57 +08:00
|
|
|
use app\common\model\Corp;
|
2025-08-19 13:54:19 +08:00
|
|
|
use app\common\model\CustomerAudit;
|
2025-07-30 10:13:26 +08:00
|
|
|
use app\common\model\User;
|
2025-08-08 15:52:13 +08:00
|
|
|
use app\common\traits\crud\CrudServiceTrait;
|
2025-07-17 09:36:46 +08:00
|
|
|
use app\crm\logic\CustomerLogic;
|
2025-07-18 15:15:37 +08:00
|
|
|
use app\crm\model\Channel;
|
|
|
|
|
use app\crm\model\Country;
|
2025-07-09 15:10:14 +08:00
|
|
|
use app\crm\model\Customer;
|
2025-08-20 17:52:44 +08:00
|
|
|
use app\crm\model\Tag;
|
2025-08-01 14:20:12 +08:00
|
|
|
use app\crm\model\CustomerHis;
|
2025-07-18 15:15:37 +08:00
|
|
|
use app\crm\model\Follow;
|
|
|
|
|
use app\crm\utils\Util;
|
|
|
|
|
use config\constants\Constants;
|
2025-08-08 15:52:13 +08:00
|
|
|
use support\Request;
|
2025-07-09 15:10:14 +08:00
|
|
|
|
2025-08-20 17:52:44 +08:00
|
|
|
/**
|
|
|
|
|
* @method mixed complexQuery(array $params)
|
|
|
|
|
*/
|
2025-07-09 15:10:14 +08:00
|
|
|
class CustomerService
|
|
|
|
|
{
|
2025-08-08 15:52:13 +08:00
|
|
|
|
|
|
|
|
use CrudServiceTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 指定当前要操作的模型
|
|
|
|
|
*/
|
|
|
|
|
protected string $modelClass = Customer::class;
|
|
|
|
|
|
2025-07-09 15:10:14 +08:00
|
|
|
public function list($params)
|
|
|
|
|
{
|
|
|
|
|
// $page = $request->input('page', 1);
|
|
|
|
|
// $perPage = $request->input('per_page', 10);
|
|
|
|
|
$perPage = $params['size'] ?? 10;
|
|
|
|
|
$page = $params['current'] ?? 1;
|
2025-07-18 15:15:37 +08:00
|
|
|
$keyword = $params['keyword'] ?? '';
|
|
|
|
|
$recycle = $params['recycle'] ?? '';
|
2025-07-18 16:45:16 +08:00
|
|
|
$follow = $params['follow'] ?? '';
|
|
|
|
|
$follow_last = $params['follow_last'] ?? '';
|
2025-07-21 14:29:58 +08:00
|
|
|
$tagId = $params['tag_id'] ?? [];
|
2025-08-20 17:52:44 +08:00
|
|
|
$sort = $params['sort'] ?? null;
|
|
|
|
|
$sortId = $params['sort_id'] ?? null;
|
2025-08-14 20:08:57 +08:00
|
|
|
$countryId = $params['country_id'] ?? '';
|
2025-07-09 15:10:14 +08:00
|
|
|
$where = [];
|
|
|
|
|
$user_id =$params['user_id'];
|
2025-07-18 15:15:37 +08:00
|
|
|
|
|
|
|
|
$seas = $params['seas'] ?? '';
|
|
|
|
|
if($seas == 'seas'){
|
|
|
|
|
$where[] = ['user_id', 0];
|
2025-07-25 17:22:22 +08:00
|
|
|
// }elseif($recycle == 'recycle'){
|
|
|
|
|
// $where[] = ['user_id', $user_id];
|
|
|
|
|
}else{
|
2025-07-18 15:15:37 +08:00
|
|
|
$where[] = ['user_id', $user_id];
|
|
|
|
|
}
|
2025-08-01 14:20:12 +08:00
|
|
|
$where[] = ['status', 1];
|
2025-07-18 15:15:37 +08:00
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
if (!empty($countryId)){
|
|
|
|
|
$where[] = ['country_id', $countryId];
|
|
|
|
|
}
|
2025-07-18 16:45:16 +08:00
|
|
|
|
|
|
|
|
$grade = [];
|
|
|
|
|
if (!Util::isEmpty($follow) && $follow == 0) {
|
|
|
|
|
$where[] = ['grade', '<=', 2];
|
|
|
|
|
} elseif (!Util::isEmpty($follow) && $follow == 1) {
|
|
|
|
|
$grade = [3, 4];
|
|
|
|
|
} elseif (!Util::isEmpty($follow) && $follow == 2) {
|
|
|
|
|
$where[] = ['grade', '=', 5];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Util::isEmpty($follow_last) && $follow_last == 0) {
|
|
|
|
|
$where[] = ['follow_last', '>', strtotime('-1 day')];
|
|
|
|
|
} elseif (!Util::isEmpty($follow_last) && $follow_last == 1) {
|
|
|
|
|
$where[] = ['follow_last', '>', strtotime('-3 day')];
|
|
|
|
|
} elseif (!Util::isEmpty($follow_last) && $follow_last == 2) {
|
|
|
|
|
$where[] = ['follow_last', '>', strtotime('-7 day')];
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 17:52:44 +08:00
|
|
|
$query = Customer::select('crm_customer.*')->where($where);
|
2025-07-18 15:15:37 +08:00
|
|
|
|
|
|
|
|
if(!empty($tagId)){
|
2025-07-21 14:29:58 +08:00
|
|
|
//查询有$tagId 里任意一个标签的客户
|
|
|
|
|
//$query->whereHas('tags', function($query) use ($tagId){
|
|
|
|
|
// $query->whereIn ('tag.id', $tagId);
|
|
|
|
|
//});
|
|
|
|
|
//查询有$tagId 里所有标签的客户
|
|
|
|
|
foreach ($tagId as $id) {
|
|
|
|
|
$query->whereHas('tags', function($query) use ($id){
|
2025-07-25 16:14:09 +08:00
|
|
|
$query->where('tag_id', $id);
|
2025-07-21 14:29:58 +08:00
|
|
|
});
|
|
|
|
|
}
|
2025-07-18 15:15:37 +08:00
|
|
|
}
|
|
|
|
|
if (!Util::isEmpty($keyword)) {
|
|
|
|
|
$query->where(function ($query) use ($keyword) {
|
|
|
|
|
$query->where('contacts', 'like', "%{$keyword}%")
|
2025-08-14 20:08:57 +08:00
|
|
|
->orWhere('phone', 'like', "%{$keyword}%")
|
|
|
|
|
->orWhere('email', 'like', "%{$keyword}%")
|
|
|
|
|
->orWhere('WhatsApp', 'like', "%{$keyword}%")
|
|
|
|
|
->orWhere('code', 'like', "%{$keyword}%");
|
2025-07-18 15:15:37 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!Util::isEmpty($grade)) {
|
|
|
|
|
$query->whereIn('grade', $grade);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 14:39:40 +08:00
|
|
|
if ($recycle == 'recycle') {
|
2025-07-18 15:15:37 +08:00
|
|
|
$query->onlyTrashed();
|
|
|
|
|
}
|
2025-07-25 14:39:40 +08:00
|
|
|
if ($seas == 'seas') {
|
2025-07-30 15:39:41 +08:00
|
|
|
$corp = Corp::where('abbr', $params['corp_abbr'])->first();
|
|
|
|
|
$corpId = $corp?$corp->id:0;
|
2025-07-30 15:15:57 +08:00
|
|
|
if($corpId == 1){
|
|
|
|
|
$query->whereIn('channel_id', [1,2,3,4,5,6,7,8]);
|
|
|
|
|
} elseif($corpId == 3) {
|
|
|
|
|
$query->whereIn('channel_id', [9,10,11]);
|
|
|
|
|
} else{
|
|
|
|
|
$query->whereIn('channel_id', [0]);
|
|
|
|
|
}
|
2025-07-25 14:41:26 +08:00
|
|
|
$query->withTrashed();
|
2025-07-25 14:39:40 +08:00
|
|
|
}
|
2025-07-18 15:15:37 +08:00
|
|
|
|
2025-08-20 17:52:44 +08:00
|
|
|
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->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);
|
|
|
|
|
|
2025-07-18 15:15:37 +08:00
|
|
|
foreach ($customer as &$item) {
|
|
|
|
|
$item = $this->getCustomer($item);
|
|
|
|
|
if($seas == 'seas'){
|
2025-07-25 15:19:33 +08:00
|
|
|
$item['phone'] = Util::mask($item['phone']);
|
|
|
|
|
$item['email'] = Util::mask($item['email']);
|
2025-07-25 17:00:03 +08:00
|
|
|
$item['WhatsApp'] = Util::mask($item['WhatsApp']);
|
2025-07-25 14:43:11 +08:00
|
|
|
$item['seas_time'] = time()-(empty($item['deleted_at'])?strtotime($item['updated_at']):strtotime($item['deleted_at']));
|
2025-07-18 15:15:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset($item);
|
|
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 15:52:13 +08:00
|
|
|
|
|
|
|
|
/** 分页列表 */
|
|
|
|
|
public function serviceList(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if (method_exists($this, 'complexQuery')) {
|
|
|
|
|
$result = $this->complexQuery($request->get());
|
|
|
|
|
} else {
|
|
|
|
|
$config = $this->buildQueryConfig($request, 'index');
|
|
|
|
|
$limit = (int)$request->get('size', 15);
|
|
|
|
|
$page = (int)$request->get('current', 1);
|
|
|
|
|
$result = $this->logicList($config, $limit, $page);
|
|
|
|
|
}
|
|
|
|
|
return $this->postProcess('List', $result);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 构造 CrudUtil 查询配置
|
|
|
|
|
*/
|
|
|
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
|
|
|
{
|
|
|
|
|
$p = $request->all();
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$config['model'] = $this->modelClass;
|
|
|
|
|
switch ($scene) {
|
|
|
|
|
case 'index':
|
|
|
|
|
$config['where']['status'] = 1; // 关键字
|
2025-08-14 20:08:57 +08:00
|
|
|
if (isset($p['user_id']) && $p['user_id']) {
|
|
|
|
|
$config['where']['user_id'] = $p['user_id'];
|
|
|
|
|
} // 关键字
|
2025-08-08 15:52:13 +08:00
|
|
|
if (isset($p['keyword']) && $p['keyword']) {
|
|
|
|
|
$k = '%' . $p['keyword'] . '%';
|
2025-08-14 20:08:57 +08:00
|
|
|
$config['orWhere'][] = ['contacts','like', $k];
|
|
|
|
|
$config['orWhere'][] = ['phone','like', $k];
|
|
|
|
|
$config['orWhere'][] = ['email','like', $k];
|
|
|
|
|
$config['orWhere'][] = ['WhatsApp','like', $k];
|
|
|
|
|
$config['orWhere'][] = ['code','like', $k];
|
2025-08-08 15:52:13 +08:00
|
|
|
}
|
|
|
|
|
$config['order']['id'] = 'desc';
|
|
|
|
|
$config['dataLimit']['type'] = 'relation';
|
|
|
|
|
$config['dataLimit']['userId'] = $request->user->uid;
|
|
|
|
|
$config['dataLimit']['userField'] = 'user_id';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 20:08:57 +08:00
|
|
|
/**
|
|
|
|
|
* 无用线索举报
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return false
|
|
|
|
|
*/
|
2025-08-01 14:20:12 +08:00
|
|
|
public function report($params)
|
|
|
|
|
{
|
|
|
|
|
$id = $params['id'];
|
|
|
|
|
$userId = $params['user_id'];
|
|
|
|
|
|
|
|
|
|
$where = [
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
];
|
|
|
|
|
$customer = Customer::where($where)->withTrashed()->first();
|
|
|
|
|
if(empty($customer)){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-19 13:54:19 +08:00
|
|
|
CustomerAudit::create([
|
|
|
|
|
'customer_id' => $id,
|
|
|
|
|
'report_user_id' => $userId,
|
|
|
|
|
'remark' => $params['remark'],
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'img' => implode(',', $params['img']),
|
|
|
|
|
]);
|
|
|
|
|
|
2025-08-01 14:20:12 +08:00
|
|
|
CustomerHis::create([
|
|
|
|
|
'customer_id' => $id,
|
|
|
|
|
'user_his' => $userId,
|
|
|
|
|
'user_id' => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-08-06 14:59:38 +08:00
|
|
|
$customer->user_id = 0;
|
2025-08-01 14:20:12 +08:00
|
|
|
$customer->status = 2;
|
|
|
|
|
$res = $customer->save();
|
|
|
|
|
|
|
|
|
|
//软删除
|
|
|
|
|
$customer->delete();
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 15:15:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $customer
|
|
|
|
|
* @param $request
|
|
|
|
|
* @param $tag 是否返回客户标签
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function getCustomer($customer, $language = 'zh_CN', $tag = false)
|
|
|
|
|
{
|
|
|
|
|
$customer['grade_txt'] = Util::language($language, Constants::GRADE_EN[$customer['grade']], Constants::GRADE_ZH[$customer['grade']], Constants::GRADE_RU[$customer['grade']]);
|
2025-07-29 09:32:23 +08:00
|
|
|
$customer['magnitude'] = ($customer['magnitude']==0)?1:$customer['magnitude'];
|
2025-07-18 15:15:37 +08:00
|
|
|
$customer['magnitude_txt'] = Util::language($language, Constants::MAGNITUDE_EN[$customer['magnitude']], Constants::MAGNITUDE[$customer['magnitude']], Constants::MAGNITUDE_RU[$customer['magnitude']]);
|
|
|
|
|
$customer['sex_txt'] = Util::language($language, Constants::SEX_EN[$customer['sex']], Constants::SEX[$customer['sex']], Constants::SEX_RU[$customer['sex']]);
|
|
|
|
|
|
|
|
|
|
if ($customer['grade']<=2) {
|
|
|
|
|
$customer['follow'] = 0;
|
|
|
|
|
$customer['follow_txt'] = Util::language($language,'Following','跟进中','Продолжение');
|
|
|
|
|
}elseif ($customer['grade']<5) {
|
|
|
|
|
$customer['follow'] = 1;
|
|
|
|
|
$customer['follow_txt'] = Util::language($language,'transacted','已成交','Транзакция завершена');
|
|
|
|
|
}else{
|
|
|
|
|
$customer['follow'] = 2;
|
|
|
|
|
$customer['follow_txt'] = Util::language($language,'lost','已流失','Потерянный');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$where = [
|
|
|
|
|
'customer_id' => $customer['id']
|
|
|
|
|
];
|
|
|
|
|
$begin_follow = Follow::select('id','created_at','updated_at')
|
|
|
|
|
->where($where)
|
|
|
|
|
->min('created_at');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$customer['begin_follow'] = Util::timeTotDate($begin_follow);
|
|
|
|
|
// $customer['created_at'] = Util::timeTotDate($customer['created_at']);
|
|
|
|
|
// $customer['updated_at'] = Util::timeTotDate($customer['updated_at']);
|
|
|
|
|
|
2025-08-20 11:29:05 +08:00
|
|
|
$customer = Util::formatDate($customer,['follow_next','follow_last']);
|
2025-07-18 15:15:37 +08:00
|
|
|
$nameLang = $language=='en'?'name_en':($language=='ru'?'name_ru':'name');
|
|
|
|
|
$countryLang = $language=='en'?'en_addr':'cn_addr';
|
|
|
|
|
// $customer['car_type_name'] = empty($customer['car_type_id'])?'':CarType::where(['id'=>$customer['car_type_id']])->value($nameLang);
|
|
|
|
|
$customer['country_name'] = empty($customer['country_id'])?'':Country::where(['id'=>$customer['country_id']])->value($countryLang);
|
|
|
|
|
$customer['channel_name'] = empty($customer['channel_id'])?'':Channel::where(['id'=>$customer['channel_id']])->value($nameLang);
|
|
|
|
|
// $customer['last_follow'] = $last_follow;
|
|
|
|
|
|
2025-07-30 10:22:25 +08:00
|
|
|
$customer['enter_user_name'] = empty($customer['enter_user_id']) ? '' : User::where('id',$customer['enter_user_id'])->value('name');
|
2025-08-08 15:52:13 +08:00
|
|
|
$customer['user_name'] = empty($customer['user_id']) ? '公海' : User::where('id',$customer['user_id'])->value('name');
|
2025-07-30 10:13:26 +08:00
|
|
|
|
2025-07-18 15:15:37 +08:00
|
|
|
if ($tag) {
|
|
|
|
|
foreach ($customer['tags'] as &$tag) {
|
|
|
|
|
$tag['names'] = unserialize($tag['names']);
|
|
|
|
|
$tag['name'] = $tag['names'][$language];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $customer;
|
2025-07-09 15:10:14 +08:00
|
|
|
}
|
2025-07-17 09:36:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public function detail($params)
|
|
|
|
|
{
|
|
|
|
|
$id =$params['id'];
|
|
|
|
|
$customer = Customer::with('tags')->select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex')
|
|
|
|
|
->where('id', $id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
return $customer;
|
|
|
|
|
}
|
2025-07-09 15:10:14 +08:00
|
|
|
}
|