123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AccountConfigNoUserRelation;
- use App\Models\AdqUser;
- use App\Models\DjUser;
- use App\Models\DramaSeries;
- use App\Models\DramaUserRela;
- use App\Models\OfficialAccount;
- use App\Models\OfficialAccountRelation;
- use App\Models\OfficialWebUserActionReportConf;
- use App\Models\OfficialWebUserActionSetId;
- use App\Models\OperateLog;
- use App\Models\ReportRules;
- use App\Models\OfficialReportRules;
- use App\Models\System\Users;
- use App\Models\TencentAdAuth;
- use App\Models\TencentAdConf;
- use App\Models\AuthorizeCorp;
- use App\RedisModel;
- use App\Support\EmailQueue;
- use Illuminate\Support\Facades\Auth;
- class PitcherService
- {
- public static function wxAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId, $adminId, $isSystemAdmin)
- {
- // $mpAppIdList = OfficialAccount::getSysGroupMpAppIdList($sysGroupId, 'PitcherService.wxAccountList');
- $mpAppIdList = OfficialAccount::getAccountMpAppIdList($adminId, $sysGroupId, $isSystemAdmin);
- return PitcherService::officialAccountsMap($keyword, $isSelect, $page, $pageSize, $mpAppIdList, $sysGroupId, $adminId, $isSystemAdmin);
- }
- public static function officialAccountsList($keyword, $isSelect, $page, $pageSize, $sysGroupId, $adminId, $isSystemAdmin)
- {
- $mpAppIdList = OfficialAccountRelation::getSysGroupMpAppIdList($sysGroupId, true, []);
- return PitcherService::officialAccountsMap($keyword, $isSelect, $page, $pageSize, $mpAppIdList, $sysGroupId, $adminId, $isSystemAdmin);
- }
- public static function officialAccountsMap($keyword, $isSelect, $page, $pageSize, $mpAppIdList, $sysGroupId, $adminId, $isSystemAdmin)
- {
- $wxAccountQuery = TencentAdAuth::query()
- ->select(['wechat_account_id', 'account_name', 'created_at', 'is_provisional_stat'])
- ->where('enable', 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();
- $bindInfoList = DramaUserRela::getAccountDramaCount($wxAccountList->pluck('wechat_account_id'), $sysGroupId, 1);
- $accountRules = OfficialReportRules::query()
- ->whereIn('app_id', $wxAccountList->pluck('wechat_account_id'))
- ->select('app_id', 'report_enable', 'report_type', 'purchase_enable', 'miss_enable', 'miss_setvale')
- ->get()
- ->keyBy('app_id')
- ->toArray();
- $accountAdConf = TencentAdConf::query()
- ->whereIn('app_id', $wxAccountList->pluck('wechat_account_id'))
- ->select('app_id', 'user_action_set_id', 'corp_id', 'corp_name', 'channel')
- ->get()
- ->keyBy('app_id')
- ->toArray();
- $rules = ReportRules::query()->where('enable', 1)->pluck('name', 'id')->all();
- foreach ($wxAccountList as $value) {
- $value->drama_count = $bindInfoList->get($value->wechat_account_id) ?? 0;
- $value->report_enable = $accountRules[$value->wechat_account_id]['report_enable'] ?? 0;
- $value->report_type = $accountRules[$value->wechat_account_id]['report_type'] ?? 0;
- $value->purchase_enable = $accountRules[$value->wechat_account_id]['purchase_enable'] ?? 0;
- $value->miss_enable = $accountRules[$value->wechat_account_id]['miss_enable'] ?? 0;
- $value->miss_setvale = $accountRules[$value->wechat_account_id]['miss_setvale'] ?? 0;
- $value->user_action_set_id = $accountAdConf[$value->wechat_account_id]['user_action_set_id'] ?? 0;
- $value->corp_id = $accountAdConf[$value->wechat_account_id]['corp_id'] ?? 0;
- $value->corp_name = $accountAdConf[$value->wechat_account_id]['corp_name'] ?? 0;
- $value->rule_name = $rules[$value->report_type] ?? '';
- $value->channel = $accountAdConf[$value->wechat_account_id]['channel'] ?? 0;
- }
- return [$wxAccountList, $total];
- } else {
- if ($total == 0) return [];
- $wxAccountList = $wxAccountQuery->get();
- return $wxAccountList;
- }
- }
- /**
- * 获取账号所有投放账号信息
- * */
- public static function adAccountList($keyword, $sysGroupId, $adminId, $isSystemAdmin)
- {
- $isAllMp = $isAllAdq = true;
- $mpAccountIdList = $adqAccountIdList = [];
- if($sysGroupId != $adminId && !$isSystemAdmin) {
- # 获取该账号已分配的adq账号
- $accountAuthInfo = Users::select('is_all_adq', 'adq_account', 'is_all_mp', 'mp_account')
- ->where('enable', 1)->where('id', $adminId)
- ->first();
- if(empty($accountAuthInfo)) return [];
- if(!$accountAuthInfo->is_all_adq) {
- $isAllAdq = false;
- $adqAccountIdList = $accountAuthInfo->adq_account ? explode(',', $accountAuthInfo->adq_account) : [];
- }
- if(!$accountAuthInfo->is_all_mp) {
- $isAllMp = false;
- $mpAccountIdList = $accountAuthInfo->mp_account ? explode(',', $accountAuthInfo->mp_account) : [];
- }
- }
- # 获取企业下的adq账号
- $adqAccountList = OfficialWebUserActionSetId::selectRaw('account_id, account_id as account_name')
- ->where(function($query) use($keyword) {
- if($keyword) $query->where('account_id', 'like', '%'.$keyword.'%');
- })
- ->where(function($query) use($isAllAdq, $adqAccountIdList) {
- if($isAllAdq === false) {
- $query->whereIn('account_id', $adqAccountIdList);
- }
- })
- ->where('sys_group_id', $sysGroupId)
- ->where('enable', 1)
- ->groupBy(['account_id'])
- ->get()->toArray();
- # 获取企业下的mp账号
- $mpAccountIds = OfficialAccountRelation::selectRaw('app_id')->where('enable', 1)
- ->where('sys_group_id', $sysGroupId)
- ->where(function($query) use($isAllMp, $mpAccountIdList) {
- if($isAllMp === false) {
- $query->whereIn('app_id', $mpAccountIdList);
- }
- })
- ->distinct()->pluck('app_id');
- $mpAccountList = TencentAdAuth::selectRaw('wechat_account_id as account_id, account_name')
- ->where(function($query) use($keyword) {
- if($keyword) $query->where('account_name', 'like', '%'.$keyword.'%');
- })
- ->where('enable', 1)
- ->whereIn('wechat_account_id', $mpAccountIds)
- ->get()->toArray();
- return array_merge($adqAccountList, $mpAccountList);
- }
- /**
- * 获取当前登录账号可见的adq账号
- * */
- public static function adqAccountListForUser($adminId, $sysGroupId, $isSystemAdmin)
- {
- $isAll = true;
- $accountList = [];
- if($sysGroupId != $adminId && !$isSystemAdmin) {
- # 获取该账号已分配的adq账号
- $accountAuthInfo = Users::select('is_all_adq', 'adq_account')->where('enable', 1)->where('id', $adminId)->first();
- if(empty($accountAuthInfo)) return [];
- if(!$accountAuthInfo->is_all_adq) {
- $isAll = false;
- $accountList = $accountAuthInfo->adq_account ? explode(',', $accountAuthInfo->adq_account) : [];
- }
- }
- $adqAccountList = OfficialWebUserActionSetId::where('sys_group_id', $sysGroupId)
- ->where(function ($query) use($isAll, $accountList) {
- if($isAll === false) {
- $query->whereIn('account_id', $accountList);
- }
- })
- ->where('enable', 1)->pluck('account_id')->toArray();
- return $adqAccountList;
- }
- public static function dramaList($keyword, $isSelect, $page, $pageSize, $sysGroupId)
- {
- $dramaQuery = DramaSeries::query()
- ->select(['id', 'name', 'created_at', 'enable'])
- ->where(function ($query) use ($isSelect, $keyword) {
- if (!$isSelect && !empty($keyword)) $query->where('name', 'like', '%'.$keyword.'%');
- })
- ->where('sys_group_id', $sysGroupId)
- ->orderByDesc('created_at');
- $total = $dramaQuery->count();
- if (!$isSelect) {
- if ($total == 0) return [[], 0];
- $dramaList = $dramaQuery->offset(($page - 1) * $pageSize)->limit($pageSize)->get();
- return [$dramaList, $total];
- } else {
- if ($total == 0) return [];
- $dramaList = $dramaQuery->get();
- return $dramaList;
- }
- }
- public static function pitcherList($sysGroupId)
- {
- $res = Users::query()
- ->where('group_admin_id', $sysGroupId)
- ->where('is_promoter', 1)
- ->where('enable', 1)
- ->select("id","name")
- ->get();
- if($res->isEmpty()) {
- return [];
- }
- Log::logInfo('pitcherList', [
- 'sys_group_id' => $sysGroupId,
- 'user_list' => $res->toArray()], 'app_auth');
- return $res->toArray();
- }
- public static function bind($appId, $dramaId, $userId, $startDate, $endDate, $sysGroupId)
- {
- $wxAccountInfo = OfficialAccount::query()
- ->where('mp_app_id', $appId)
- ->where('enable', 1)
- ->first();
- if (empty($wxAccountInfo)) return [false, 3101];
- $dramaInfo = DramaSeries::getDramaInfoById($dramaId);
- if (empty($dramaInfo)) return [false, 3102];
- $userInfo = Users::query()
- ->where('id', $userId)
- ->where('enable', 1)
- ->first();
- if (empty($userInfo)) return [false, 3103];
- // 时间校验
- $checkRes = self::dateCheck($appId, $startDate, $endDate);
- if (!$checkRes) return [false, 3105];
- $result = [
- 'app_id' => $appId,
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'sys_group_id' => $sysGroupId,
- 'order_type' => 1,
- ];
- DataStatisticsService::mpRelaAdd($appId, $dramaId, $userId, $startDate, $endDate);
- try {
- DramaUserRela::query()->firstOrCreate($result, $result);
- } catch (\Throwable $e) {
- Log::logError('数据关系绑定失败', [
- 'appId' => $appId,
- 'dramaId' => $dramaId,
- 'userId' => $userId,
- 'startDate' => $startDate,
- 'endDate' => $endDate,
- 'order_type'=> 1,
- ], 'PitcherService');
- return [false, 3106];
- }
- // 日志记录
- Log::logInfo('编辑账号绑定短剧投手', [
- 'old' => [],
- 'new' => [
- 'app_id' => $appId,
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'sys_group_id' => $sysGroupId,
- ],
- 'operate' => 'add',
- 'order_type' => 1
- ], 'pitcher_change');
- return [true, 0];
- }
- public static function index($appId, $page, $pageSize, $sysGroupId)
- {
- $tDate = date('Y-m-d');
- $fieldList = 'id, app_id, drama_id, user_id, start_date, end_date, enable';
- $relaList = DramaUserRela::getBindList($appId, $sysGroupId, $page, $pageSize, $fieldList, 1);
- if ($relaList->isEmpty()) return [[], 0];
- $total = $relaList->count();
- $dramaList = DramaSeries::query()->whereIn('id', $relaList->pluck('drama_id'))->pluck('name', 'id');
- $userList = Users::query()->whereIn('id', $relaList->pluck('user_id'))->pluck('name', 'id');
- foreach ($relaList as $value) {
- $value->is_contain = 0;
- if (($tDate >= $value->start_date) && ($tDate <= $value->end_date)) {
- $value->is_contain = 1;
- }
- $value->drama_name = $dramaList->get($value->drama_id) ?? null;
- $value->user_name = $userList->get($value->user_id) ?? null;
- }
- return [$relaList, $total];
- }
- public static function relaEdit($relaId, $dramaId, $userId, $startDate, $endDate, $enable)
- {
- $relaInfo = DramaUserRela::query()->find($relaId);
- if (empty($relaInfo)) return [false, 3104];
- $old = $relaInfo->toArray();
- // 时间校验
- $checkRes = self::dateCheck($relaInfo->app_id, $startDate, $endDate, $relaId);
- if (!$checkRes) return [false, 3105];
- try {
- $relaInfo->update([
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'enable' => $enable
- ]);
- } catch (\Throwable $e) {
- Log::logError('绑定数据编辑失败', [
- 'relaId' => $relaId,
- 'dramaId' => $dramaId,
- 'userId' => $userId,
- 'startDate' => $startDate,
- 'endDate' => $endDate,
- 'enable' => $enable
- ], 'PitcherService');
- return [false, 3106];
- }
- DataStatisticsService::mpRelaEdit($dramaId, $userId, $startDate, $endDate, $enable, $old);
- // 日志记录
- Log::logInfo('编辑账号绑定短剧投手', [
- 'old' => $old,
- 'new' => [
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'enable' => $enable
- ],
- 'operate' => 'edit',
- 'order_type' => 1,
- ], 'pitcher_change');
- return [true, 0];
- }
- public static function dateCheck($appId, $stDate, $enDate, $relaId = null)
- {
- $dateList = DramaUserRela::query()
- ->select(['start_date', 'end_date'])
- ->where('app_id', $appId)
- ->where(function ($query) use ($relaId) {
- if (!empty($relaId)) $query->where('id', '!=', $relaId);
- })
- ->where('end_date', '>=', $stDate)
- ->where('enable', 1)
- ->get();
- if ($dateList->isEmpty()) return true;
- $dateArr = $dateList->toArray();
- $dateArr[] = [
- 'start_date' => $stDate,
- 'end_date' => $enDate
- ];
- $stDataList = array_column($dateArr, 'start_date');
- array_multisort($stDataList, SORT_ASC, $dateArr);
- foreach ($dateArr as $k => $v){
- if ($v['start_date'] > $v['end_date'])
- return false;
- if ($k > 0 && ($dateArr[$k]['start_date'] <= $dateArr[$k-1]['end_date']))
- return false;
- }
- return true;
- }
- public static function adqDateCheck($accountId, $stDate, $enDate, $sysGroupId, $relaId = null)
- {
- $dateList = DramaUserRela::query()
- ->select(['start_date', 'end_date'])
- ->where('account_id', $accountId)
- ->where('sys_group_id', $sysGroupId)
- ->where(function ($query) use ($relaId) {
- if (!empty($relaId)) $query->where('id', '!=', $relaId);
- })
- ->where('end_date', '>=', $stDate)
- ->where('enable', 1)
- ->get();
- if ($dateList->isEmpty()) return true;
- $dateArr = $dateList->toArray();
- $dateArr[] = [
- 'start_date' => $stDate,
- 'end_date' => $enDate
- ];
- $stDataList = array_column($dateArr, 'start_date');
- array_multisort($stDataList, SORT_ASC, $dateArr);
- foreach ($dateArr as $k => $v){
- if ($v['start_date'] > $v['end_date'])
- return false;
- if ($k > 0 && ($dateArr[$k]['start_date'] <= $dateArr[$k-1]['end_date']))
- return false;
- }
- return true;
- }
- public static function dramaAdd($dramaName, $sysGroupId, &$errno)
- {
- # 校验剧是否已存在
- $isExist = DramaSeries::where('sys_group_id', $sysGroupId)->where('name', $dramaName)->exists();
- if($isExist) {
- $errno = 3108;
- return [];
- }
- $res = DramaSeries::updateOrInsert([
- 'name' => $dramaName,
- 'sys_group_id' => $sysGroupId
- ], [
- 'name' => $dramaName,
- 'sys_group_id' => $sysGroupId
- ]);
- $errno = $res ? 0 : 3109;
- return [];
- }
- public static function dramaEdit($dramaId, $dramaName, $enable)
- {
- $dramaInfo = DramaSeries::query()->find($dramaId);
- if (empty($dramaInfo)) return [false, 3102];
- try {
- $dramaInfo->update([
- 'name' => $dramaName,
- 'enable' => $enable
- ]);
- } catch (\Throwable $e) {
- Log::logError('剧集编辑失败', [
- 'dramaId' => $dramaId,
- 'dramaName' => $dramaName,
- 'enable' => $enable
- ], 'PitcherService');
- return [false, 3106];
- }
- return [true, 0];
- }
- public static function reportRuleList()
- {
- $res = ReportRules::select('id', 'name')->where('enable', 1)->orderBy('id')->get();
- return $res;
- }
- public static function setReportRule($app_ids, $report_type, $report_enable, $purchase_enable, $miss_enable, $miss_setvale, $if_all=0)
- {
- if ($miss_enable == 1 && $miss_setvale == 0) return [false, 4408];
- $data = [
- 'report_type' => $report_type,
- 'report_enable' => $report_enable,
- 'purchase_enable' => $purchase_enable,
- 'miss_enable' => $miss_enable,
- 'miss_setvale' => $miss_setvale,
- 'enable' => 1
- ];
- //配置所有为一种规则
- if($if_all == 1){
- $app_ids = TencentAdAuth::query()
- ->where('enable', 1)
- ->select('wechat_account_id')
- ->distinct()
- ->pluck('wechat_account_id')
- ->all();
- }
- foreach($app_ids as $app_id){
- $rulesRes = OfficialReportRules::updateOrCreate(['app_id' => $app_id], $data);
- // 写入缓存
- RedisModel::hSet(OfficialReportRules::OFFICIAL_REPORT_RULES_CONF, $app_id, json_encode([
- 'report_enable' => $rulesRes->report_enable,
- 'report_type' => $rulesRes->report_type,
- 'purchase_enable' => $rulesRes->purchase_enable,
- 'miss_enable' => $rulesRes->miss_enable,
- 'miss_setvale' => $rulesRes->miss_setvale,
- 'enable' => $rulesRes->enable
- ]));
- // 判断漏单是否关闭,若关闭,则将漏单计数清零
- if(0 == $miss_enable) {
- RedisModel::hDel(OfficialReportRules::OFFICIAL_REPORT_MISS_INCR_NUM, $app_id);
- }
- }
- return [true, 0];
- }
- public static function setAccountReportConf($app_id, $user_action_set_id, $corp_id, $channel, $isProvisionalStat, $sysGroupId)
- {
- $corp_name = AuthorizeCorp::where('corpid', $corp_id)->value('corp_name');
- $conf = TencentAdConf::where('app_id', $app_id)->first();
- $account = TencentAdAuth::where('wechat_account_id', $app_id)->select(['account_id', 'is_provisional_stat'])->first();
- if($account->is_provisional_stat != $isProvisionalStat) {
- $result = TencentAdAuth::where('wechat_account_id', $app_id)->update(['is_provisional_stat' => $isProvisionalStat]);
- if($isProvisionalStat == 0) {
- AccountConfigNoUserRelation::where('app_id', $app_id)->where('enable', 1)->where('sys_group_id', $sysGroupId)
- ->update(['enable' => 0]);
- }
- }
- if( isset($conf->id) ){
- $conf->user_action_set_id = $user_action_set_id;
- $conf->corp_id = $corp_id;
- $conf->corp_name = $corp_name;
- $conf->channel = $channel;
- } else {
- $conf = new TencentAdConf();
- $conf->account_id = $account->account_id ?? null;
- $conf->app_id = $app_id;
- $conf->user_action_set_id = $user_action_set_id;
- $conf->corp_id = $corp_id;
- $conf->corp_name = $corp_name;
- $conf->channel = $channel;
- }
- return $conf->save();
- }
- public static function getCorpIdList()
- {
- $list = AuthorizeCorp::select('corpid', 'corp_name')->where('enable', 1)->where('is_customized_app', 1)->get();
- return $list;
- }
- public static function adqAccountList($keyword, $isSelect, $page, $pageSize, $sysGroupId, $adminId, $isSystemAdmin)
- {
- $isAll = true;
- $accountList = [];
- if($sysGroupId != $adminId && !$isSystemAdmin) {
- # 获取该账号已分配的adq账号
- $accountAuthInfo = Users::query()->select('is_all_adq', 'adq_account')
- ->where('enable', 1)->where('id', $adminId)->first();
- if(empty($accountAuthInfo)) return [];
- if(!$accountAuthInfo->is_all_adq) {
- $isAll = false;
- $accountList = $accountAuthInfo->adq_account ? explode(',', $accountAuthInfo->adq_account) : [];
- }
- }
- $adqAccountQuery = OfficialWebUserActionSetId::query()->where('sys_group_id', $sysGroupId)
- ->where(function ($query) use($isAll, $accountList) {
- if($isAll === false) {
- $query->whereIn('account_id', $accountList);
- }
- })
- ->where('enable', 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();
- $bindInfoList = DramaUserRela::getAccountDramaCount($adqAccountList->pluck('account_id'), $sysGroupId, 2);
- $rules = ReportRules::query()->where('enable', 1)->pluck('name', 'id')->all();
- foreach ($adqAccountList as $value) {
- $value->drama_count = $bindInfoList->get($value->account_id) ?? 0;
- $value->rule_name = $rules[$value->report_type] ?? '';
- $value->user_action_set_id = $value->web_user_action_set_id;
- $value->amount_setvale = !empty($value->amount_setvale) ? json_decode($value->amount_setvale, 1) : [];
- unset($value->web_user_action_set_id);
- }
- return [$adqAccountList, $total];
- } else {
- if ($total == 0) return [];
- $adqAccountList = $adqAccountQuery->select('account_id')->get();
- return $adqAccountList;
- }
- }
- public static function adqPitcherIndex($accountId, $page, $pageSize, $sysGroupId)
- {
- $tDate = date('Y-m-d');
- $fieldList = 'id as rela_id, account_id, drama_id, user_id, start_date, end_date, enable';
- $relaList = DramaUserRela::getBindList($accountId, $sysGroupId, $page, $pageSize, $fieldList, 2);
- $total = $relaList->count();
- if ($relaList->isEmpty()) return [[], 0];
- $dramaList = DramaSeries::query()->whereIn('id', $relaList->pluck('drama_id'))->pluck('name', 'id');
- $userList = Users::query()->whereIn('id', $relaList->pluck('user_id'))->pluck('name', 'id');
- foreach ($relaList as $value) {
- $value->is_contain = 0;
- if (($tDate >= $value->start_date) && ($tDate <= $value->end_date)) {
- $value->is_contain = 1;
- }
- $value->drama_name = $dramaList->get($value->drama_id) ?? null;
- $value->user_name = $userList->get($value->user_id) ?? null;
- }
- return [$relaList, $total];
- }
- public static function adqBindPitcher($accountId, $dramaId, $userId, $startDate, $endDate, $sysGroupId)
- {
- $adqAccountInfo = OfficialWebUserActionSetId::query()
- ->where('account_id', $accountId)
- ->where('enable', 1)
- ->where('sys_group_id', $sysGroupId)
- ->first();
- if (empty($adqAccountInfo)) return [false, 3107];
- $dramaInfo = DramaSeries::query()
- ->where('id', $dramaId)
- ->where('enable', 1)
- ->first();
- if (empty($dramaInfo)) return [false, 3102];
- $userInfo = Users::query()
- ->where('id', $userId)
- ->where('enable', 1)
- ->first();
- if (empty($userInfo)) return [false, 3103];
- // 时间校验
- $checkRes = self::adqDateCheck($accountId, $startDate, $endDate, $sysGroupId);
- if (!$checkRes) return [false, 3105];
- $result = [
- 'account_id' => $accountId,
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'sys_group_id' => $sysGroupId,
- 'order_type' => 2,
- ];
- DataStatisticsService::adqRelaAdd($accountId, $dramaId, $userId, $startDate, $endDate);
- try {
- DramaUserRela::query()->firstOrCreate($result, $result);
- } catch (\Throwable $e) {
- Log::logError('数据关系绑定失败', [
- 'accountId' => $accountId,
- 'dramaId' => $dramaId,
- 'userId' => $userId,
- 'startDate' => $startDate,
- 'endDate' => $endDate
- ], 'PitcherService');
- return [false, 3106];
- }
- // 日志记录
- Log::logInfo('编辑账号绑定短剧投手', [
- 'old' => [],
- 'new' => [
- 'account_id' => $accountId,
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'sys_group_id' => $sysGroupId,
- 'order_type' => 2,
- ],
- 'operate' => 'add',
- 'order_type' => 2,
- ], 'pitcher_change');
- return [true, 0];
- }
- public static function adqEditPitcher($relaId, $dramaId, $userId, $startDate, $endDate, $enable, $sysGroupId)
- {
- $relaInfo = DramaUserRela::query()->find($relaId);
- if (empty($relaInfo)) return [false, 3104];
- $old = $relaInfo->toArray();
- // 时间校验
- $checkRes = self::adqDateCheck($relaInfo->account_id, $startDate, $endDate, $sysGroupId, $relaId);
- if (!$checkRes) return [false, 3105];
- try {
- $relaInfo->update([
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'enable' => $enable
- ]);
- } catch (\Throwable $e) {
- Log::logError('绑定数据编辑失败', [
- 'relaId' => $relaId,
- 'dramaId' => $dramaId,
- 'userId' => $userId,
- 'startDate' => $startDate,
- 'endDate' => $endDate,
- 'enable' => $enable
- ], 'PitcherService');
- return [false, 3106];
- }
- // 日志记录
- Log::logInfo('编辑账号绑定短剧投手', [
- 'old' => $old,
- 'new' => [
- 'drama_id' => $dramaId,
- 'user_id' => $userId,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- 'enable' => $enable
- ],
- 'operate' => 'edit',
- 'order_type' => 2,
- ], 'pitcher_change');
- DataStatisticsService::adqRelaEdit($dramaId, $userId, $startDate, $endDate, $enable, $old);
- return [true, 0];
- }
- public static function adqBindUserActionSetId($accountId, $userActionSetId, $corpId, $sysGroupId, $isProvisionalStat)
- {
- $corpName = null;
- if(!empty($corpId)) {
- $corpName = AuthorizeCorp::query()
- ->where('corpid', $corpId)
- ->value('corp_name');
- }
- $conf = OfficialWebUserActionSetId::query()
- ->where('account_id', $accountId)
- ->where('sys_group_id', $sysGroupId)
- ->first();
- \DB::beginTransaction();//开启事务
- if( isset($conf->id) ){
- $originalWebUserActionSetId = $conf->web_user_action_set_id;
- $conf->web_user_action_set_id = $userActionSetId;
- $conf->corp_id = $corpId;
- $conf->corp_name = $corpName;
- $conf->is_provisional_stat = $isProvisionalStat;
- if($isProvisionalStat == 0 && $conf->is_provisional_stat != $isProvisionalStat) {
- AccountConfigNoUserRelation::where('sys_group_id', $sysGroupId)->where('account_id', $accountId)
- ->where('enable', 1)->update(['enable' => 0]);
- }
- } else {
- $originalWebUserActionSetId = null;
- $conf = new OfficialWebUserActionSetId();
- $conf->account_id = $accountId;
- $conf->web_user_action_set_id = $userActionSetId;
- $conf->corp_id = $corpId;
- $conf->corp_name = $corpName;
- $conf->sys_group_id = $sysGroupId;
- $conf->is_provisional_stat = $isProvisionalStat;
- }
- $res1 = $conf->save();
- $count = 0;
- if(!empty($corpId)) {
- $count = AdqUser::query()
- ->where('corpid', $corpId)
- ->where('account_id', $accountId)
- ->count();
- }
- if($count > 0){
- $res2 = AdqUser::query()
- ->where('corpid', $corpId)
- ->where('account_id', $accountId)
- ->update([
- 'user_action_set_id' => $userActionSetId,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- } else {
- $res2 = true;
- }
- if($res1 && $res2) {
- \DB::commit();
- return true;
- } else {
- \DB::rollback();//数据库回滚
- return false;
- }
- }
- public static function setAdqAccountReportRule($accountIds, $reportType, $reportEnable, $purchaseEnable, $missEnable
- , $missSetvale, $sysGroupId, $missValue, $ifAll, $amountEnable, $amountSetvale, $resetEnable, $resetSetvale, $adminId)
- {
- if ($missEnable == 1 && $missSetvale == 0) return [false, 4408];
- // 参数验证
- $errcode = 0;
- PitcherService::decideAmount($amountEnable, $amountSetvale, $errcode);
- if($errcode) return [false, $errcode];
- $newConfList = [];
- // 按照金额排序
- if(1 == $amountEnable){
- $stAmountDataList = array_column($amountSetvale, 'start_amount');
- array_multisort($stAmountDataList, SORT_ASC, $amountSetvale);
- foreach($amountSetvale as $val) {
- if(3 == $val['type']) {
- $hourSetVale = $val['hour_setvale'];
- $stTimeDataList = array_column($hourSetVale, 'start_time');
- array_multisort($stTimeDataList, SORT_ASC, $hourSetVale);
- foreach($hourSetVale as $v) {
- $newConfList[] = $val['start_amount'].'-'.$val['end_amount'].'-'.$v['start_time'].'-'.$v['end_time'];
- }
- } else {
- $newConfList[] = $val['start_amount'].'-'.$val['end_amount'];
- }
- }
- }
- $data = [
- 'report_type' => $reportType,
- 'report_enable' => $reportEnable,
- 'purchase_enable' => $purchaseEnable,
- 'miss_enable' => $missEnable,
- 'miss_setvale' => $missSetvale,
- 'miss_value' => $missValue,
- 'enable' => 1,
- 'amount_enable' => $amountEnable,
- 'amount_setvale' => json_encode($amountSetvale, 256),
- 'reset_enable' => $resetEnable,
- 'reset_setvale' => $resetSetvale
- ];
- //配置所有为一种规则
- if($ifAll == 1){
- $accountIds = OfficialWebUserActionSetId::query()
- ->select('account_id')
- ->where('sys_group_id', $sysGroupId)
- ->distinct()
- ->pluck('account_id')
- ->all();
- }
- foreach($accountIds as $accountId){
- $rulesRes = OfficialWebUserActionSetId::query()
- ->updateOrCreate([
- 'account_id' => $accountId
- ], $data);
- // 写入缓存
- RedisModel::hSet(OfficialWebUserActionSetId::OFFICIAL_ADQ_ACCOUNT_REPORT_RULES_CONF,
- $accountId.'-'.$sysGroupId, json_encode([
- 'report_enable' => $rulesRes->report_enable,
- 'report_type' => $rulesRes->report_type,
- 'purchase_enable' => $rulesRes->purchase_enable,
- 'miss_enable' => $rulesRes->miss_enable,
- 'miss_setvale' => $rulesRes->miss_setvale,
- 'enable' => $rulesRes->enable,
- 'account_id' => $accountId,
- 'miss_value' => $missValue,
- 'amount_enable' => $amountEnable,
- 'amount_setvale' => json_encode($amountSetvale, 256)
- ]));
- // 操作记录
- OperateLog::saveData(3, json_encode(array_merge($data, ['account_id' => $accountId]), 256), $adminId);
- // 判断漏单是否关闭,若关闭,则将漏单计数清零
- if(0 == $missEnable) {
- RedisModel::hDel(OfficialWebUserActionSetId::OFFICIAL_ADQ_ACCOUNT_REPORT_MISS_INCR_NUM,
- $accountId.'-'.$sysGroupId);
- }
- // 获取账号下所有键
- $configList = RedisModel::hGetAll(OfficialWebUserActionSetId::ADQ_ACCOUNT_REPORT_MISS_INCR_NUM.'-'.$accountId
- .'-'.$sysGroupId);
- if(!empty($configList)) {
- // 将本次修改后失效的配置删除
- foreach($configList as $key=>$val) {
- if(!in_array($key, $newConfList)){
- RedisModel::hDel(OfficialWebUserActionSetId::ADQ_ACCOUNT_REPORT_MISS_INCR_NUM.'-'.$accountId
- .'-'.$sysGroupId, $key);
- }
- }
- }
- }
- return [true, 0];
- }
- public static function decideAmount($amountEnable, $amountSetvale, &$errcode)
- {
- # 已经开启回传金额设置,但是实际设置内容为空
- if(1 == $amountEnable && empty($amountSetvale)) {
- $errcode = 4409;
- return ;
- }
- # 验证字段是否缺失以及金额范围存在交叉部分
- $stAmountDataList = array_column($amountSetvale, 'start_amount');
- array_multisort($stAmountDataList, SORT_ASC, $amountSetvale);
- foreach ($amountSetvale as $k => $v){
- if(!isset($v['start_amount']) || !isset($v['end_amount']) || !isset($v['type']) || !isset($v['hour_setvale'])
- || $v['start_amount'] < 0 || $v['end_amount'] <= 0 || !in_array($v['type'], [1, 2,3]) || (3 == $v['type'] && empty($v['hour_setvale']))){
- $errcode = 4410; // 填写参数有误
- return ;
- }
- if ($v['start_amount'] >= $v['end_amount']) {
- $errcode = 4411; // 最小金额要需小于最高金额
- return ;
- }
- if ($k > 0 && ($amountSetvale[$k]['start_amount'] < $amountSetvale[$k-1]['end_amount'])) {
- $errcode = 4412; // 多项配置之间金额范围存在交叉
- return ;
- }
- if(3 == $v['type']) {
- foreach($v['hour_setvale'] as $kk => $vv) {
- if(!isset($vv['start_time']) || !isset($vv['end_time']) || !isset($vv['miss_setvale']) || !isset($vv['miss_value'])
- || !isset($vv['return_type']) ||
- ($vv['return_type'] == 3 && ($vv['miss_setvale'] <= 0 || $vv['miss_value'] <= 0))) {
- $errcode = 4410;// 填写参数有误
- return ;
- }
- if($vv['start_time'] >= $vv['end_time']) {
- $errcode = 4413; // 起始时间需小于截止时间
- }
- if ($kk > 0 && ($v['hour_setvale'][$kk]['start_time'] < $v['hour_setvale'][$kk - 1]['end_time'])) {
- $errcode = 4414;// 多项配置之间时间范围存在交叉
- return ;
- }
- if ($kk > 0 && (date('H:i', strtotime($v['hour_setvale'][$kk]['start_time'] . ' -1 minutes'))
- != $v['hour_setvale'][$kk - 1]['end_time']) ) {
- $errcode = 4415;
- return ;
- }
- }
- }
- }
- }
- # adq账号绑定客服
- public static function adqAccountBindUserAction($accountId, $userList, $confirm, $sysGroupId) {
- try {
- Log::logInfo('投放账号绑定客服配置记录',[
- 'account_id' => $accountId, 'user_list' => $userList, 'confirm' => $confirm, 'sys_group_id' => $sysGroupId
- ], 'adqAccountBindUserConfig');
- # 直接清空所有绑定关系
- if(empty($userList)) {
- AdqUser::query()->where('account_id', $accountId)->update(['account_id' => null, 'user_action_set_id' => null]);
- return ['', 0];
- }
- # 查询所选择客服是否已经绑定过其他投放账号
- $userSqlArr = [];
- foreach ($userList as $userInfo) {
- $corpid = $userInfo['corpid'];
- $users = $userInfo['user_list'];
- foreach ($users as $userId) {
- $userSqlArr[] = '("' . $corpid . '","' . $userId . '")';
- }
- }
- if (0 == $confirm) {
- $adqUserConf = AdqUser::search([
- 'user_sql' => $userSqlArr,
- 'not_exist_account_list' => [$accountId],
- 'account_exist' => 1
- ]);
- if ($adqUserConf->isNotEmpty()) {
- # 查询已经绑定过其他投放账号的企微以及客服信息
- $corpidList = array_column($userList, 'corpid');
- $corpDataList = AuthorizeCorp::getAllCorpList($corpidList);
- $userList = DjUser::getUserBySearch([
- 'user_sql' => $userSqlArr
- ]);
- $existUser = [];
- foreach ($adqUserConf as $conf) {
- $corpData = $corpDataList->where('corpid', $conf->corpid)->first();
- $corpName = $corpData->corp_name ?? '';
- $userData = $userList->where('corpid', $conf->corpid)->where('user_id', $conf->user_id)->first();
- $userName = $userData->name ?? '';
- $existUser[] = [
- 'corp_id' => $conf->corpid,
- 'corp_name' => $corpName,
- 'user_id' => $conf->user_id,
- 'user_name' => $userName
- ];
- }
- return [$existUser, 4905];
- }
- }
- # 写入数据
- $alreadyExistAdqUserConf = AdqUser::search([
- 'user_sql' => $userSqlArr,
- ]);
- $existIdList = array_column($alreadyExistAdqUserConf->toArray(), 'id');
- # 查询投放账号对应的数据源ID
- $userActionSetId = OfficialWebUserActionSetId::getAccountBindUserActionSetId($sysGroupId, $accountId);
- $insertData = [];
- foreach ($userList as $userInfo) {
- $corpid = $userInfo['corpid'];
- $users = $userInfo['user_list'];
- foreach ($users as $userId) {
- $existRow = $alreadyExistAdqUserConf->where('corpid', $corpid)->where('user_id', $userId)->first();
- if (empty($existRow)) {
- $insertData[] = [
- 'corpid' => $corpid,
- 'user_id' => $userId,
- 'account_id' => $accountId,
- 'user_action_set_id' => $userActionSetId
- ];
- }
- }
- }
- \DB::begintransaction();
- AdqUser::query()->where('account_id', $accountId)->update(['account_id' => null, 'user_action_set_id' => null]);
- if (!empty($existIdList)) {
- # 全部都需要新增
- AdqUser::query()->whereIn('id', $existIdList)->update(['account_id' => $accountId, 'user_action_set_id' => $userActionSetId]);
- }
- if (!empty($insertData)) {
- AdqUser::query()->insert($insertData);
- }
- \DB::commit();
- return ['', 0];
- } catch (\Exception $e) {
- Log::logError('投放账号绑定客服异常', [
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'message'=> $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ], 'adqAccountBindUserError');
- EmailQueue::rPush('投放账号绑定客服异常', 'file:' . $e->getFile() . '; line:' . $e->getLine()
- . '; message:' . $e->getMessage(), ['song.shen@kuxuan-inc.com'], '猎羽');
- \DB::rollBack();
- return ['系统异常', 500];
- }
- }
- public static function adqAccountBindUserList($accountId) {
- $adqUserConf = AdqUser::search([
- 'account_id' => $accountId
- ]);
- if($adqUserConf->isNotEmpty()) {
- $userSqlArr = [];
- $corpidArr = [];
- $appIdArr = [];
- foreach($adqUserConf as $conf) {
- $corpid = $conf->corpid;
- $userId = $conf->user_id;
- $userSqlArr[] = '("' . $corpid . '","' . $userId . '")';
- $corpidArr[] = $corpid;
- $appIdArr[] = $conf->app_id;
- }
- $corpDataList = AuthorizeCorp::getAllCorpList($corpidArr);
- $userList = DjUser::getUserBySearch([
- 'user_sql' => $userSqlArr
- ]);
- $appIdArr = array_unique(array_filter($appIdArr));
- if(!empty($appIdArr)) {
- $appData = OfficialAccount::getAppInfoList($appIdArr);
- } else {
- $appData = [];
- }
- $existUser = [];
- foreach($adqUserConf as $conf) {
- $corpData = $corpDataList->where('corpid', $conf->corpid)->first();
- $corpName = $corpData->corp_name ?? '';
- $userData = $userList->where('corpid', $conf->corpid)->where('user_id', $conf->user_id)->first();
- $userName = $userData->name ?? '';
- if(empty($conf->app_id)) {
- $appName = null;
- } else {
- $appName = $appData[$conf->app_id] ?? null;
- }
- $existUser[] = [
- 'data_id' => $conf->id,
- 'corp_id' => $conf->corpid,
- 'corp_name' => $corpName,
- 'user_id' => $conf->user_id,
- 'user_name' => $userName,
- 'app_id' => $conf->app_id,
- 'app_name' => $appName,
- ];
- }
- return $existUser;
- } else {
- return [];
- }
- }
- public static function adqAccountBindUserDelete($dataId) {
- return AdqUser::query()->where('id', $dataId)->update(['account_id' => null, 'user_action_set_id' => null]);
- }
- public static function adqAccountBindAppId($dataIdList, $appId, &$errcode)
- {
- if(empty($appId)) {
- $appId = null;
- }
- # 验证APP_id
- if ( !empty($appId) ) {
- $exist = OfficialAccount::query()->where('mp_app_id', $appId)->where('enable', 1)->first();
- if(empty($exist)) {
- $errcode = 3101;
- return ;
- }
- }
- $res = AdqUser::query()->whereIn('id', $dataIdList)->update([
- 'app_id' => $appId,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if(!$res) {
- $errcode = 500;
- }
- return ;
- }
- }
|