Geen omschrijving

CustomerService.php 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Services\Sys;
  3. use App\Models\JuxingAdAccount;
  4. use App\Models\Sys\SysCustomerAdver;
  5. use App\Models\Sys\SysCustomer;
  6. use App\Models\Sys\SysIndustry;
  7. use App\Models\Sys\SysUserAdver;
  8. use App\Models\Sys\SysUserCusts;
  9. use App\Models\Sys\SysUserRole;
  10. use App\Support\Log;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\DB;
  13. class CustomerService
  14. {
  15. public static function industryList($parentIndustryId = 0)
  16. {
  17. $industryList = SysIndustry::query()
  18. ->where('parent_industry_id', $parentIndustryId)
  19. ->where('enable', 1)
  20. ->get();
  21. if ($industryList->isEmpty()) return [];
  22. $res = [];
  23. foreach ($industryList as $value) {
  24. $temp = [
  25. 'industry_id' => $value->industry_id,
  26. 'industry_name' => $value->industry_name,
  27. 'industry_note' => $value->industry_note,
  28. 'industry_cert' => $value->industry_cert
  29. ];
  30. $temp['child'] = self::industryList($value->industry_id);
  31. $res[] = $temp;
  32. unset($temp);
  33. }
  34. return $res;
  35. }
  36. public static function list($isSelect, $page, $pageSize, $advertiserId=null, $nickName=null)
  37. {
  38. $userInfo = Auth::user();
  39. //筛选广告主ID
  40. $custIds = array();
  41. if($advertiserId || $nickName){
  42. if($advertiserId){
  43. $custIds = SysCustomerAdver::query()
  44. ->where('advertiser_id', $advertiserId)
  45. ->where('enable', 1)
  46. ->pluck('customer_id')
  47. ->all();
  48. } else {
  49. // 获取账号数据
  50. $advertiserIds = JuxingAdAccount::query()
  51. ->where('nick_name', 'like', '%'.$nickName.'%')
  52. ->where('enable', 1)
  53. ->pluck('advertiser_id')
  54. ->all();
  55. if(empty($advertiserIds)){
  56. return [[], 0];
  57. }
  58. $custIds = SysCustomerAdver::query()
  59. ->whereIn('advertiser_id', $advertiserIds)
  60. ->where('enable', 1)
  61. ->pluck('customer_id')
  62. ->all();
  63. }
  64. if(empty($custIds)){
  65. return [[], 0];
  66. }
  67. }
  68. // 获取客户数据
  69. $customerQuery = SysCustomer::query()
  70. ->select(['id', 'name', 'industry_id', 'remarks'])
  71. ->whereIn('id', $userInfo->custIds)
  72. ->where(function($query) use($custIds){
  73. if(!empty($custIds)) $query->whereIn('id', $custIds);
  74. })
  75. ->where('enable', 1);
  76. $total = $customerQuery->count();
  77. if ($total == 0) return [[], 0];
  78. $customerList = $customerQuery
  79. ->offset(($page - 1) * $pageSize)
  80. ->limit($pageSize)
  81. ->get();
  82. // 获取行业数据
  83. $industryList = SysIndustry::query()
  84. ->whereIn('industry_id', $customerList->pluck('industry_id'))
  85. ->where('enable', 1)
  86. ->pluck('industry_name', 'industry_id');
  87. // 获取客户与账号的关联数据
  88. $cusAccountList = SysCustomerAdver::query( )
  89. ->select(['customer_id', 'advertiser_id'])
  90. ->whereIn('customer_id', $customerList->pluck('id'))
  91. ->where('enable', 1)
  92. ->get();
  93. // 获取账号数据
  94. $adAccountList = JuxingAdAccount::query()
  95. ->whereIn('advertiser_id', $cusAccountList->pluck('advertiser_id'))
  96. ->where('enable', 1)
  97. ->pluck('nick_name', 'advertiser_id');
  98. // 数据处理
  99. $cuAdList = [];
  100. foreach ($cusAccountList as $cusAccount) {
  101. $nickName = $adAccountList->get($cusAccount->advertiser_id) ?? null;
  102. if (empty($nickName)) continue;
  103. $cuAdList[$cusAccount->customer_id][] = [
  104. 'advertiser_id' => $cusAccount->advertiser_id,
  105. 'nick_name' => $nickName
  106. ];
  107. }
  108. foreach ($customerList as $key => $customer) {
  109. // 行业
  110. $customer->industry_name = $industryList->get($customer->industry_id) ?? null;
  111. // 账号
  112. $accountList = $cuAdList[$customer->id] ?? [];
  113. if ($isSelect && empty($accountList)) {
  114. unset($customerList[$key]);
  115. continue;
  116. }
  117. $customer->adAccountList = $accountList;
  118. }
  119. return [array_values($customerList->toArray()), $total];
  120. }
  121. public static function operate($customerId, $customerName, $industryId, $remarks)
  122. {
  123. // 检测客户名称是否重复
  124. $is_exists = SysCustomer::query()
  125. ->where(function ($query) use ($customerId) {
  126. if (isset($customerId)) $query->where('id', '!=', $customerId);
  127. })
  128. ->where('name', $customerName)
  129. ->where('enable', 1)
  130. ->exists();
  131. if ($is_exists) return [false, 1050];
  132. try {
  133. if (!isset($customerId)) {
  134. $customerInfo = SysCustomer::query()->create([
  135. 'name' => $customerName,
  136. 'industry_id' => $industryId,
  137. 'remarks' => $remarks
  138. ]);
  139. if (Auth::id() != 1) {
  140. // 检查角色是否是管理员
  141. $adminUserIds = SysUserRole::query()
  142. ->where('role_id', 2)
  143. ->where('enable', 1)
  144. ->pluck('sys_user_id')
  145. ->all();
  146. if (!in_array(Auth::id(), $adminUserIds)) {
  147. // 创建管理员与客户的关联
  148. $inData = [
  149. [
  150. 'sys_user_id' => Auth::id(),
  151. 'customer_id' => $customerInfo->id
  152. ]
  153. ];
  154. foreach ($adminUserIds as $adminUserId) {
  155. $inData[] = [
  156. 'sys_user_id' => $adminUserId,
  157. 'customer_id' => $customerInfo->id
  158. ];
  159. }
  160. } else {
  161. $inData = [
  162. 'sys_user_id' => Auth::id(),
  163. 'customer_id' => $customerInfo->id
  164. ];
  165. }
  166. // 创建用户与客户的关联
  167. SysUserCusts::query()->insert($inData);
  168. }
  169. } else {
  170. SysCustomer::query()
  171. ->where('id', $customerId)
  172. ->update([
  173. 'name' => $customerName,
  174. 'industry_id' => $industryId,
  175. 'remarks' => $remarks
  176. ]);
  177. }
  178. return [true, 0];
  179. } catch (\Throwable $e) {
  180. Log::error($e->getMessage(), [
  181. 'param' => [
  182. 'customerId' => $customerId,
  183. 'customerName' => $customerName,
  184. 'industryId' => $industryId
  185. ],
  186. 'trace' => $e->getTraceAsString()
  187. ], 'CustomerService');
  188. return [false, 10];
  189. }
  190. }
  191. public static function delete($customerId)
  192. {
  193. $customerInfo = SysCustomer::query()
  194. ->where('id', $customerId)
  195. ->where('enable', 1)
  196. ->first();
  197. if (empty($customerInfo)) return [false, 1051];
  198. $accountIsExist = SysCustomerAdver::query()
  199. ->where('customer_id', $customerId)
  200. ->where('enable', 1)
  201. ->exists();
  202. if ($accountIsExist) return [false, 1052];
  203. $customerInfo->update(['enable' => 0]);
  204. return [true, 0];
  205. }
  206. public static function accountAuth($customerId, $nickName)
  207. {
  208. // 登录用户信息
  209. $adminId = Auth::id();
  210. // 授权地址
  211. $baseUrl = 'https://developers.e.kuaishou.com/tools/authorize?';
  212. $query = http_build_query([
  213. 'app_id' => config('kwai')['juxing']['app_id'],
  214. 'scope' => '["select_service"]',
  215. //'oauth_type' => 'ad_social',
  216. 'oauth_type' => 'advertiser',
  217. 'state' => base64_encode(json_encode([
  218. 'customer_id' => $customerId,
  219. 'nick_name' => $nickName,
  220. 'admin_id' => $adminId
  221. ])),
  222. // 'redirect_uri' => route('juxingAccountAuthCallback')
  223. 'redirect_uri' => 'http://zhitou.wenxingshuju.com/juxing/callback'
  224. ]);
  225. return ['url' => $baseUrl.$query];
  226. }
  227. public static function accountDel($advertiserId)
  228. {
  229. $accountInfo = JuxingAdAccount::query()
  230. ->where('advertiser_id', $advertiserId)
  231. ->where('enable', 1)
  232. ->first();
  233. if (empty($accountInfo)) return [false, 1000];
  234. DB::beginTransaction();
  235. try {
  236. // 清除客户账号关联数据
  237. SysCustomerAdver::query()
  238. ->where('advertiser_id', $advertiserId)
  239. ->where('enable', 1)
  240. ->update(['enable' => 0]);
  241. // 清除用户账号关联数据
  242. SysUserAdver::query()
  243. ->where('advertiser_id', $advertiserId)
  244. ->where('enable', 1)
  245. ->update(['enable' => 0]);
  246. $accountInfo->update(['enable' => 0]);
  247. DB::commit();
  248. return [true, 0];
  249. } catch (\Throwable $e) {
  250. Log::error('账号删除失败', [
  251. 'param' => [
  252. 'advertiserId' => $advertiserId,
  253. ],
  254. 'msg' => $e->getMessage(),
  255. 'trace' => $e->getTrace()
  256. ], 'CustomerService');
  257. DB::rollBack();
  258. return [false, 10];
  259. }
  260. }
  261. }