123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace App\Services\Sys;
- use App\Models\JuxingAdAccount;
- use App\Models\Sys\SysCustomerAdver;
- use App\Models\Sys\SysCustomer;
- use App\Models\Sys\SysIndustry;
- use App\Models\Sys\SysUserAdver;
- use App\Models\Sys\SysUserCusts;
- use App\Models\Sys\SysUserRole;
- use App\Support\Log;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- class CustomerService
- {
- public static function industryList($parentIndustryId = 0)
- {
- $industryList = SysIndustry::query()
- ->where('parent_industry_id', $parentIndustryId)
- ->where('enable', 1)
- ->get();
- if ($industryList->isEmpty()) return [];
- $res = [];
- foreach ($industryList as $value) {
- $temp = [
- 'industry_id' => $value->industry_id,
- 'industry_name' => $value->industry_name,
- 'industry_note' => $value->industry_note,
- 'industry_cert' => $value->industry_cert
- ];
- $temp['child'] = self::industryList($value->industry_id);
- $res[] = $temp;
- unset($temp);
- }
- return $res;
- }
- public static function list($isSelect, $page, $pageSize, $advertiserId=null, $nickName=null)
- {
- $userInfo = Auth::user();
- //筛选广告主ID
- $custIds = array();
- if($advertiserId || $nickName){
- if($advertiserId){
- $custIds = SysCustomerAdver::query()
- ->where('advertiser_id', $advertiserId)
- ->where('enable', 1)
- ->pluck('customer_id')
- ->all();
- } else {
- // 获取账号数据
- $advertiserIds = JuxingAdAccount::query()
- ->where('nick_name', 'like', '%'.$nickName.'%')
- ->where('enable', 1)
- ->pluck('advertiser_id')
- ->all();
- if(empty($advertiserIds)){
- return [[], 0];
- }
- $custIds = SysCustomerAdver::query()
- ->whereIn('advertiser_id', $advertiserIds)
- ->where('enable', 1)
- ->pluck('customer_id')
- ->all();
- }
- if(empty($custIds)){
- return [[], 0];
- }
- }
- // 获取客户数据
- $customerQuery = SysCustomer::query()
- ->select(['id', 'name', 'industry_id', 'remarks'])
- ->whereIn('id', $userInfo->custIds)
- ->where(function($query) use($custIds){
- if(!empty($custIds)) $query->whereIn('id', $custIds);
- })
- ->where('enable', 1);
- $total = $customerQuery->count();
- if ($total == 0) return [[], 0];
- $customerList = $customerQuery
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- // 获取行业数据
- $industryList = SysIndustry::query()
- ->whereIn('industry_id', $customerList->pluck('industry_id'))
- ->where('enable', 1)
- ->pluck('industry_name', 'industry_id');
- // 获取客户与账号的关联数据
- $cusAccountList = SysCustomerAdver::query( )
- ->select(['customer_id', 'advertiser_id'])
- ->whereIn('customer_id', $customerList->pluck('id'))
- ->where('enable', 1)
- ->get();
- // 获取账号数据
- $adAccountList = JuxingAdAccount::query()
- ->whereIn('advertiser_id', $cusAccountList->pluck('advertiser_id'))
- ->where('enable', 1)
- ->pluck('nick_name', 'advertiser_id');
- // 数据处理
- $cuAdList = [];
- foreach ($cusAccountList as $cusAccount) {
- $nickName = $adAccountList->get($cusAccount->advertiser_id) ?? null;
- if (empty($nickName)) continue;
- $cuAdList[$cusAccount->customer_id][] = [
- 'advertiser_id' => $cusAccount->advertiser_id,
- 'nick_name' => $nickName
- ];
- }
- foreach ($customerList as $key => $customer) {
- // 行业
- $customer->industry_name = $industryList->get($customer->industry_id) ?? null;
- // 账号
- $accountList = $cuAdList[$customer->id] ?? [];
- if ($isSelect && empty($accountList)) {
- unset($customerList[$key]);
- continue;
- }
- $customer->adAccountList = $accountList;
- }
- return [array_values($customerList->toArray()), $total];
- }
- public static function operate($customerId, $customerName, $industryId, $remarks)
- {
- // 检测客户名称是否重复
- $is_exists = SysCustomer::query()
- ->where(function ($query) use ($customerId) {
- if (isset($customerId)) $query->where('id', '!=', $customerId);
- })
- ->where('name', $customerName)
- ->where('enable', 1)
- ->exists();
- if ($is_exists) return [false, 1050];
- try {
- if (!isset($customerId)) {
- $customerInfo = SysCustomer::query()->create([
- 'name' => $customerName,
- 'industry_id' => $industryId,
- 'remarks' => $remarks
- ]);
- if (Auth::id() != 1) {
- // 检查角色是否是管理员
- $adminUserIds = SysUserRole::query()
- ->where('role_id', 2)
- ->where('enable', 1)
- ->pluck('sys_user_id')
- ->all();
- if (!in_array(Auth::id(), $adminUserIds)) {
- // 创建管理员与客户的关联
- $inData = [
- [
- 'sys_user_id' => Auth::id(),
- 'customer_id' => $customerInfo->id
- ]
- ];
- foreach ($adminUserIds as $adminUserId) {
- $inData[] = [
- 'sys_user_id' => $adminUserId,
- 'customer_id' => $customerInfo->id
- ];
- }
- } else {
- $inData = [
- 'sys_user_id' => Auth::id(),
- 'customer_id' => $customerInfo->id
- ];
- }
- // 创建用户与客户的关联
- SysUserCusts::query()->insert($inData);
- }
- } else {
- SysCustomer::query()
- ->where('id', $customerId)
- ->update([
- 'name' => $customerName,
- 'industry_id' => $industryId,
- 'remarks' => $remarks
- ]);
- }
- return [true, 0];
- } catch (\Throwable $e) {
- Log::error($e->getMessage(), [
- 'param' => [
- 'customerId' => $customerId,
- 'customerName' => $customerName,
- 'industryId' => $industryId
- ],
- 'trace' => $e->getTraceAsString()
- ], 'CustomerService');
- return [false, 10];
- }
- }
- public static function delete($customerId)
- {
- $customerInfo = SysCustomer::query()
- ->where('id', $customerId)
- ->where('enable', 1)
- ->first();
- if (empty($customerInfo)) return [false, 1051];
- $accountIsExist = SysCustomerAdver::query()
- ->where('customer_id', $customerId)
- ->where('enable', 1)
- ->exists();
- if ($accountIsExist) return [false, 1052];
- $customerInfo->update(['enable' => 0]);
- return [true, 0];
- }
- public static function accountAuth($customerId, $nickName)
- {
- // 登录用户信息
- $adminId = Auth::id();
- // 授权地址
- $baseUrl = 'https://developers.e.kuaishou.com/tools/authorize?';
- $query = http_build_query([
- 'app_id' => config('kwai')['juxing']['app_id'],
- 'scope' => '["select_service"]',
- //'oauth_type' => 'ad_social',
- 'oauth_type' => 'advertiser',
- 'state' => base64_encode(json_encode([
- 'customer_id' => $customerId,
- 'nick_name' => $nickName,
- 'admin_id' => $adminId
- ])),
- // 'redirect_uri' => route('juxingAccountAuthCallback')
- 'redirect_uri' => 'http://zhitou.wenxingshuju.com/juxing/callback'
- ]);
- return ['url' => $baseUrl.$query];
- }
- public static function accountDel($advertiserId)
- {
- $accountInfo = JuxingAdAccount::query()
- ->where('advertiser_id', $advertiserId)
- ->where('enable', 1)
- ->first();
- if (empty($accountInfo)) return [false, 1000];
- DB::beginTransaction();
- try {
- // 清除客户账号关联数据
- SysCustomerAdver::query()
- ->where('advertiser_id', $advertiserId)
- ->where('enable', 1)
- ->update(['enable' => 0]);
- // 清除用户账号关联数据
- SysUserAdver::query()
- ->where('advertiser_id', $advertiserId)
- ->where('enable', 1)
- ->update(['enable' => 0]);
- $accountInfo->update(['enable' => 0]);
- DB::commit();
- return [true, 0];
- } catch (\Throwable $e) {
- Log::error('账号删除失败', [
- 'param' => [
- 'advertiserId' => $advertiserId,
- ],
- 'msg' => $e->getMessage(),
- 'trace' => $e->getTrace()
- ], 'CustomerService');
- DB::rollBack();
- return [false, 10];
- }
- }
- }
|