企微短剧业务系统

ProvisionalStatService.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AccountConfigNoUserRelation;
  5. use App\Models\DramaUserRela;
  6. use App\Models\OfficialAccount;
  7. use App\Models\OfficialWebUserActionSetId;
  8. use App\Models\ReportRules;
  9. use App\Models\TencentAdAuth;
  10. use Illuminate\Support\Facades\DB;
  11. class ProvisionalStatService
  12. {
  13. public static function wxAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId)
  14. {
  15. $mpAppIdList = OfficialAccount::getSysGroupMpAppIdList($sysGroupId, 'ProvisionalStatService.wxAccountList');
  16. $wxAccountQuery = TencentAdAuth::select(['wechat_account_id', 'account_name', 'created_at', 'is_provisional_stat'])
  17. ->where('enable', 1)->where('is_provisional_stat', 1)
  18. ->whereIn('wechat_account_id', $mpAppIdList);
  19. if (!$isSelect && !empty($keyword)) $wxAccountQuery->where('account_name', 'like', '%'.$keyword.'%');
  20. $total = (clone $wxAccountQuery)->distinct('wechat_account_id')->count();
  21. $wxAccountQuery->groupBy(['wechat_account_id'])->orderByDesc('created_at');
  22. if (!$isSelect) {
  23. if ($total == 0) return [[], 0];
  24. $wxAccountList = $wxAccountQuery
  25. ->offset(($page - 1) * $pageSize)
  26. ->limit($pageSize)
  27. ->get();
  28. $appIds = $wxAccountList->pluck('wechat_account_id');
  29. # 获取绑定的adq账号
  30. $adqAccountIds = AccountConfigNoUserRelation::whereIn('app_id', $appIds)->where('sys_group_id', $sysGroupId)
  31. ->where('enable', 1)->get();
  32. foreach ($wxAccountList as $value) {
  33. $accountIdInfo = $adqAccountIds->where('app_id', $value->wechat_account_id)->pluck('account_id')->toArray();
  34. $value->account_id = $accountIdInfo ? implode(',', $accountIdInfo): '';
  35. }
  36. return [$wxAccountList, $total];
  37. } else {
  38. if ($total == 0) return [];
  39. $wxAccountList = $wxAccountQuery->get();
  40. return $wxAccountList;
  41. }
  42. }
  43. public static function adqAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId)
  44. {
  45. $adqAccountQuery = OfficialWebUserActionSetId::where('sys_group_id', $sysGroupId)
  46. ->where('enable', 1)->where('is_provisional_stat', 1);
  47. if (!$isSelect && !empty($keyword)) $adqAccountQuery->where('account_id', 'like', '%'.$keyword.'%');
  48. $total = (clone $adqAccountQuery)->distinct('account_id')->count();
  49. $adqAccountQuery->orderByDesc('create_time');
  50. if (!$isSelect) {
  51. if ($total == 0) return [[], 0];
  52. $adqAccountList = $adqAccountQuery
  53. ->offset(($page - 1) * $pageSize)
  54. ->limit($pageSize)
  55. ->get();
  56. # 获取绑定的公众账号信息
  57. $adqAccountIds = $adqAccountList->pluck('account_id')->unique();
  58. $bindAccountList = AccountConfigNoUserRelation::where('sys_group_id', $sysGroupId)->where('enable', 1)
  59. ->whereIn('account_id', $adqAccountIds)
  60. ->get();
  61. $mpAccountIds = $bindAccountList->pluck('app_id')->unique();
  62. $mpAccountData = TencentAdAuth::select(['wechat_account_id', 'account_name'])->where('enable', 1)
  63. ->whereIn('wechat_account_id', $mpAccountIds)->get();
  64. foreach ($adqAccountList as $value) {
  65. # 关联账号名称
  66. $mpAccountIdList = $bindAccountList->where('account_id', $value->account_id)->pluck('app_id');
  67. $mpAccountName = $mpAccountData->whereIn('wechat_account_id', $mpAccountIdList)->pluck('account_name')->toArray();
  68. $value->bind_account_list = empty($mpAccountName) ? '' : implode(',', $mpAccountName);
  69. $value->user_action_set_id = $value->web_user_action_set_id;
  70. unset($value->web_user_action_set_id);
  71. }
  72. return [$adqAccountList, $total];
  73. } else {
  74. if ($total == 0) return [];
  75. $adqAccountList = $adqAccountQuery->select('account_id')->get();
  76. return $adqAccountList;
  77. }
  78. }
  79. /**
  80. * 公众号账号绑定adq
  81. * */
  82. public static function bindAccount($sysGroupId, $appId, $accountIds, $adType)
  83. {
  84. try {
  85. $accountIds = explode(',', $accountIds);
  86. DB::beginTransaction();
  87. AccountConfigNoUserRelation::where('app_id', $appId)->where('sys_group_id', $sysGroupId)->update(['enable'=> 0]);
  88. if(empty($accountIds)) {
  89. DB::commit();
  90. return 0;
  91. }
  92. foreach ($accountIds as $accountId) {
  93. AccountConfigNoUserRelation::updateOrCreate(['app_id' => $appId, 'sys_group_id' => $sysGroupId, 'account_id' => $accountId], [
  94. 'enable' => 1, 'ad_type' => $adType
  95. ]);
  96. }
  97. DB::commit();
  98. } catch (\Exception $e) {
  99. DB::rollBack();
  100. Log::logError('账号绑定过程发生异常', [
  101. 'line' => $e->getLine(),
  102. 'msg' => $e->getMessage(),
  103. 'sys_group_id' => $sysGroupId,
  104. 'appid' => $appId,
  105. 'account_id' => $accountIds
  106. ], 'ProvisionalBindAccount-Exception');
  107. return 5104;
  108. }
  109. return 0;
  110. }
  111. }