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]; } } }