123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/9/27
- * Time: 11:50
- */
- namespace App\Service;
- use App\Models\IntelligentMassSending\IntelligentMassSendingAccount;
- use App\Models\IntelligentMassSending\IntelligentMassSendingSmallApp;
- use App\Models\OfficialAccount;
- use App\Models\PlatformPlaylet;
- use App\Models\TencentAdConf;
- class IntelligentMassSendingService
- {
- public static function platformIndex()
- {
- $data = config('platform.platform');
- return array_values($data);
- }
- public static function addAccount($params)
- {
- # 判断账号是否已经使用
- $row = IntelligentMassSendingAccount::query()
- ->where('platform_id', $params['platform_id'])
- ->where('enable', 1)
- ->where('account', $params['account'])
- ->first();
- if(!empty($row)) {
- return ['账号名称已存在', 5101];
- }
- $res = IntelligentMassSendingAccount::query()
- ->insertGetId($params);
- $code = $res>0 ? 0 : 400;
- return [$res, $code];
- }
- public static function accountIndex($page, $pageSize, $sysGroupId)
- {
- $offset = ($page - 1) * $pageSize;
- $query = IntelligentMassSendingAccount::query()
- ->where('sys_group_id', $sysGroupId)
- ->where('enable', 1);
- $countQuery = clone $query;
- $count = $countQuery->count();
- $list = $query->selectRaw('id as account_id, account, description, status, active_status,'
- .' service_provider_id, platform_id')
- ->orderBy('id', 'desc')
- ->offset($offset)
- ->limit($pageSize)
- ->get();
- # 提取服务商ID
- $serviceProviderIdList = $list->pluck('service_provider_id')->toArray();
- # 根据服务商ID统一计算关联的剧集数
- $playletStat = PlatformPlaylet::query()
- ->whereIn('partner_id', $serviceProviderIdList)
- ->where('enable', 1)
- ->where('state', 1)
- ->selectRaw('partner_id, count(1) as count')
- ->groupBy('partner_id')
- ->get();
- $platformArr = array_values(config('platform.platform'));
- $platformArr = array_column($platformArr, 'platform_name', 'platform_id');
- foreach($list as $value) {
- $playletInfo = $playletStat->where('partner_id', $value->service_provider_id)->first();
- $value->playlet_num = $playletInfo->count ?? 0;
- unset($value->service_provider_id);
- $value->platform_name = $platformArr[$value->platform_id] ?? null;
- }
- return [$list, $count];
- }
- public static function editAccount($accountId, $params)
- {
- # 判断账号是否已经使用
- $row = IntelligentMassSendingAccount::query()
- ->where('platform_id', $params['platform_id'])
- ->where('enable', 1)
- ->where('account', $params['account'])
- ->where('id', '!=', $accountId)
- ->first();
- if(!empty($row)) {
- return ['账号名称已存在', 5101];
- }
- $res = IntelligentMassSendingAccount::query()
- ->where('id', $accountId)
- ->update($params);
- return [$res, 0];
- }
- public static function accountList($platformId, $sysGroupId)
- {
- $list = IntelligentMassSendingAccount::query()
- ->selectRaw('id as account_id, account')
- ->where('enable', 1)
- ->where('status', 1)
- ->where('active_status', 1)
- ->where('sys_group_id', $sysGroupId)
- ->where(function($query) use ($platformId){
- if(!empty($platformId)) $query->where('platform_id', $platformId);
- })->get();
- return $list;
- }
- public static function addSmallApp($params)
- {
- # 判断账号是否已经使用
- $row = IntelligentMassSendingSmallApp::query()
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('app_id', $params['app_id'])
- ->first();
- if(!empty($row)) {
- return ['小程序已存在', 5102];
- }
- $res = IntelligentMassSendingSmallApp::query()
- ->insertGetId($params);
- $code = $res>0 ? 0 : 400;
- return [$res, $code];
- }
- public static function editSmallApp($dataId, $params)
- {
- # 判断账号是否已经使用
- $row = IntelligentMassSendingSmallApp::query()
- ->where('sys_group_id', $params['sys_group_id'])
- ->where('enable', 1)
- ->where('app_id', $params['app_id'])
- ->where('id', '!=', $dataId)
- ->first();
- if(!empty($row)) {
- return ['小程序已存在', 5102];
- }
- $res = IntelligentMassSendingSmallApp::query()
- ->where('id', $dataId)
- ->update($params);
- return [$res, 0];
- }
- public static function smallAppIndex($appName, $page, $pageSize, $sysGroupId)
- {
- $offset = ($page -1) * $pageSize;
- $query = IntelligentMassSendingSmallApp::query()
- ->where('sys_group_id', $sysGroupId)
- ->where('enable', 1);
- if($appName) {
- $query->where('app_name', 'like', '%'.$appName.'%');
- }
- $countQuery = clone $query;
- $count = $countQuery->count();
- $list = $query->selectRaw('id as data_id, platform_id, app_id, app_name, status, create_time')
- ->orderByDesc('id')
- ->offset($offset)
- ->limit($pageSize)
- ->get();
- return [$list, $count];
- }
- public static function appList($corpid)
- {
- $list = TencentAdConf::query()
- ->where('corp_id', $corpid)
- ->where('enable', 1)
- ->select(['app_id'])
- ->get();
- $appIdList = $list->isNotEmpty() ? $list->pluck('app_id')->toArray() : [];
- if(empty($appIdList)) {
- return [];
- } else {
- $list = OfficialAccount::query()
- ->whereIn('mp_app_id', $appIdList)
- ->where('enable', 1)
- ->select(['mp_app_id', 'mp_name'])
- ->get();
- return $list;
- }
- }
- public static function smallAppList($sysGroupId, $platformId)
- {
- $list = IntelligentMassSendingSmallApp::query()
- ->where('enable', 1)
- ->where('sys_group_id', $sysGroupId)
- ->where(function($query) use ($platformId){
- if(!empty($platformId)) $query->where('platform_id', $platformId);
- })
- ->where('status', 1)
- ->selectRaw('app_id, app_name, platform_id')
- ->get();
- # 添加平台信息
- $platformInfoList = config('platform.platform');
- foreach($list as $info) {
- $platformInfo = $platformInfoList[$info->platform_id] ?? 0;
- $info->platform_name = $platformInfo['platform_name'] ?? null;
- }
- return $list;
- }
- }
|