123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AccountConfigNoUserRelation;
- use App\Models\DramaUserRela;
- use App\Models\OfficialAccount;
- use App\Models\OfficialWebUserActionSetId;
- use App\Models\ReportRules;
- use App\Models\TencentAdAuth;
- use Illuminate\Support\Facades\DB;
- class ProvisionalStatService
- {
- public static function wxAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId)
- {
- $mpAppIdList = OfficialAccount::getSysGroupMpAppIdList($sysGroupId, 'ProvisionalStatService.wxAccountList');
- $wxAccountQuery = TencentAdAuth::select(['wechat_account_id', 'account_name', 'created_at', 'is_provisional_stat'])
- ->where('enable', 1)->where('is_provisional_stat', 1)
- ->whereIn('wechat_account_id', $mpAppIdList);
- if (!$isSelect && !empty($keyword)) $wxAccountQuery->where('account_name', 'like', '%'.$keyword.'%');
- $total = (clone $wxAccountQuery)->distinct('wechat_account_id')->count();
- $wxAccountQuery->groupBy(['wechat_account_id'])->orderByDesc('created_at');
- if (!$isSelect) {
- if ($total == 0) return [[], 0];
- $wxAccountList = $wxAccountQuery
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- $appIds = $wxAccountList->pluck('wechat_account_id');
- # 获取绑定的adq账号
- $adqAccountIds = AccountConfigNoUserRelation::whereIn('app_id', $appIds)->where('sys_group_id', $sysGroupId)
- ->where('enable', 1)->get();
- foreach ($wxAccountList as $value) {
- $accountIdInfo = $adqAccountIds->where('app_id', $value->wechat_account_id)->pluck('account_id')->toArray();
- $value->account_id = $accountIdInfo ? implode(',', $accountIdInfo): '';
- }
- return [$wxAccountList, $total];
- } else {
- if ($total == 0) return [];
- $wxAccountList = $wxAccountQuery->get();
- return $wxAccountList;
- }
- }
- public static function adqAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId)
- {
- $adqAccountQuery = OfficialWebUserActionSetId::where('sys_group_id', $sysGroupId)
- ->where('enable', 1)->where('is_provisional_stat', 1);
- if (!$isSelect && !empty($keyword)) $adqAccountQuery->where('account_id', 'like', '%'.$keyword.'%');
- $total = (clone $adqAccountQuery)->distinct('account_id')->count();
- $adqAccountQuery->orderByDesc('create_time');
- if (!$isSelect) {
- if ($total == 0) return [[], 0];
- $adqAccountList = $adqAccountQuery
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- # 获取绑定的公众账号信息
- $adqAccountIds = $adqAccountList->pluck('account_id')->unique();
- $bindAccountList = AccountConfigNoUserRelation::where('sys_group_id', $sysGroupId)->where('enable', 1)
- ->whereIn('account_id', $adqAccountIds)
- ->get();
- $mpAccountIds = $bindAccountList->pluck('app_id')->unique();
- $mpAccountData = TencentAdAuth::select(['wechat_account_id', 'account_name'])->where('enable', 1)
- ->whereIn('wechat_account_id', $mpAccountIds)->get();
- foreach ($adqAccountList as $value) {
- # 关联账号名称
- $mpAccountIdList = $bindAccountList->where('account_id', $value->account_id)->pluck('app_id');
- $mpAccountName = $mpAccountData->whereIn('wechat_account_id', $mpAccountIdList)->pluck('account_name')->toArray();
- $value->bind_account_list = empty($mpAccountName) ? '' : implode(',', $mpAccountName);
- $value->user_action_set_id = $value->web_user_action_set_id;
- unset($value->web_user_action_set_id);
- }
- return [$adqAccountList, $total];
- } else {
- if ($total == 0) return [];
- $adqAccountList = $adqAccountQuery->select('account_id')->get();
- return $adqAccountList;
- }
- }
- /**
- * 公众号账号绑定adq
- * */
- public static function bindAccount($sysGroupId, $appId, $accountIds, $adType)
- {
- try {
- $accountIds = explode(',', $accountIds);
- DB::beginTransaction();
- AccountConfigNoUserRelation::where('app_id', $appId)->where('sys_group_id', $sysGroupId)->update(['enable'=> 0]);
- if(empty($accountIds)) {
- DB::commit();
- return 0;
- }
- foreach ($accountIds as $accountId) {
- AccountConfigNoUserRelation::updateOrCreate(['app_id' => $appId, 'sys_group_id' => $sysGroupId, 'account_id' => $accountId], [
- 'enable' => 1, 'ad_type' => $adType
- ]);
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::logError('账号绑定过程发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'sys_group_id' => $sysGroupId,
- 'appid' => $appId,
- 'account_id' => $accountIds
- ], 'ProvisionalBindAccount-Exception');
- return 5104;
- }
- return 0;
- }
- }
|