diff --git a/app/common/controller/UserController.php b/app/common/controller/UserController.php index fc8413d..c32f153 100644 --- a/app/common/controller/UserController.php +++ b/app/common/controller/UserController.php @@ -29,7 +29,8 @@ class UserController extends BaseController { try { $uid = $request->user->uid; - $result = (new UserService())->getUser($uid); + $corp_abbr = $request->basic_data['corp_abbr']; + $result = (new UserService())->getUser($uid,$corp_abbr); return $this->success($result, '获取用户信息'); } catch (\Throwable $e) { return $this->error($e->getMessage()); diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index f5daf14..c55c4cc 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -3,6 +3,7 @@ namespace app\common\service; use app\common\logic\UserLogic; +use app\common\model\Corp; use app\crm\model\Country; class UserService @@ -43,19 +44,24 @@ class UserService /** * 获取用户信息(包括菜单和权限) */ - public function getUser(int $uid) + public function getUser(int $uid, $corp_abbr) { $user = $this->logic->userInfo($uid); + $corp_name = Corp::where('abbr', $corp_abbr)->value('name'); + $ossservice = new OssService(); return [ 'id' => $user->id, 'name' => $user->name, 'position' => $user->position, 'socialize' => $user->socialize, 'socialize_pic' => $user->socialize_pic, + 'socialize_pic_full' => (!empty($user->socialize_pic))?$ossservice->generatePrivateUrl($user->socialize_pic):'', 'country_id' => $user->country_id, 'country_txt' => Country::where('id',$user->country_id)->value('cn_addr'), 'avatar' => $user->avatar, + 'avatar_full' => (!empty($user->avatar))?$ossservice->generatePrivateUrl($user->avatar):'', 'phone' => $user->phone, + 'corp_name' => $corp_name, 'email' => $user->email, 'status' => $user->status, 'roles' => $user->roles->pluck('name') diff --git a/app/crm/controller/CustomerController.php b/app/crm/controller/CustomerController.php index 50e85b9..775666f 100644 --- a/app/crm/controller/CustomerController.php +++ b/app/crm/controller/CustomerController.php @@ -166,7 +166,7 @@ class CustomerController extends BaseController return $this->success($this->page($result)); } catch (\Throwable $e) { - return $this->error($e->getMessage()); + return $this->error($e->getMessage().$e->getFile().$e->getLine()); } } @@ -217,7 +217,7 @@ class CustomerController extends BaseController return $this->error('记录不存在'); } - $res = Customer::getCustomer($res, $request, true); + $res = CustomerService::getCustomer($res, 'zh_CN', true); return $this->success($res); } diff --git a/app/crm/model/Customer.php b/app/crm/model/Customer.php index f28fb11..af5c571 100644 --- a/app/crm/model/Customer.php +++ b/app/crm/model/Customer.php @@ -58,61 +58,6 @@ class Customer extends Model return $date->format(Carbon::parse($date)->toDateTimeString()); } - /** - * @param $customer - * @param $request - * @param $tag 是否返回客户标签 - * @return mixed - */ - public static function getCustomer($customer, $request, $tag = false) - { - $language = 'zh_CN' ?: 'en'; - $customer['grade_txt'] = Util::language($language, Constants::GRADE_EN[$customer['grade']], Constants::GRADE_ZH[$customer['grade']], Constants::GRADE_RU[$customer['grade']]); - $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']); - - $customer = Util::formatDate($customer,['follow_last']); -// $nameLang = $language=='en'?'name_en':($language=='ru'?'name_ru':'name'); - $nameLang = '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; - - if ($tag) { - foreach ($customer['tags'] as &$tag) { - $tag['names'] = unserialize($tag['names']); - $tag['name'] = $tag['names'][$language]; - } - } - - return $customer; - } - /** * 客户的标签 */ diff --git a/app/crm/service/CustomerService.php b/app/crm/service/CustomerService.php index f085f44..648c0d8 100644 --- a/app/crm/service/CustomerService.php +++ b/app/crm/service/CustomerService.php @@ -3,7 +3,13 @@ namespace app\crm\service; use app\crm\logic\CustomerLogic; +use app\crm\model\CarType; +use app\crm\model\Channel; +use app\crm\model\Country; use app\crm\model\Customer; +use app\crm\model\Follow; +use app\crm\utils\Util; +use config\constants\Constants; class CustomerService { @@ -13,18 +19,108 @@ class CustomerService // $perPage = $request->input('per_page', 10); $perPage = $params['size'] ?? 10; $page = $params['current'] ?? 1; + $keyword = $params['keyword'] ?? ''; + $recycle = $params['recycle'] ?? ''; + $grade = $params['grade'] ?? ''; $where = []; $user_id =$params['user_id']; if(empty($user_id) || $user_id == 0){ $where[] = ['user_id', '=', $user_id]; } - $user = Customer::where($where)->paginate($perPage, ['*'], 'page', $page); -// $user = Customer::with('user')->where($where)->paginate($perPage, ['*'], 'page', $page); -// $user = $user->toArray(); -// foreach ($user['data'] as &$item) { -// unset($item['password']); -// } - return $user; + + $seas = $params['seas'] ?? ''; + if($seas == 'seas'){ + $where[] = ['user_id', 0]; + } + if($recycle == 'recycle'){ + $where[] = ['user_id', $user_id]; + } + + $query = Customer::where($where); + + if(!empty($tagId)){ + $query->whereHas('tags', function($query) use ($tagId){ + $query->where('tag.id', $tagId); + }); + } + if (!Util::isEmpty($keyword)) { + $query->where(function ($query) use ($keyword) { + $query->where('contacts', 'like', "%{$keyword}%") + ->orWhere('phone', 'like', "%{$keyword}%"); + }); + } + if (!Util::isEmpty($grade)) { + $query->whereIn('grade', $grade); + } + + if ($recycle == 'recycle' || $seas == 'seas') { + $query->onlyTrashed(); + } + + $customer = Customer::where($where)->paginate($perPage, ['*'], 'page', $page); + foreach ($customer as &$item) { + $item = $this->getCustomer($item); + if($seas == 'seas'){ + $item['seas_time'] = time()-strtotime($item['deleted_at']); + } + } + unset($item); + + return $customer; + } + + + /** + * @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']]); + $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']); + + $customer = Util::formatDate($customer,['follow_last']); + $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; + + if ($tag) { + foreach ($customer['tags'] as &$tag) { + $tag['names'] = unserialize($tag['names']); + $tag['name'] = $tag['names'][$language]; + } + } + + return $customer; }