企微短剧业务系统

IntelligentMassSendingService.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/9/27
  6. * Time: 11:50
  7. */
  8. namespace App\Service;
  9. use App\Models\IntelligentMassSending\IntelligentMassSendingAccount;
  10. use App\Models\IntelligentMassSending\IntelligentMassSendingSmallApp;
  11. use App\Models\OfficialAccount;
  12. use App\Models\PlatformPlaylet;
  13. use App\Models\TencentAdConf;
  14. class IntelligentMassSendingService
  15. {
  16. public static function platformIndex()
  17. {
  18. $data = config('platform.platform');
  19. return array_values($data);
  20. }
  21. public static function addAccount($params)
  22. {
  23. # 判断账号是否已经使用
  24. $row = IntelligentMassSendingAccount::query()
  25. ->where('platform_id', $params['platform_id'])
  26. ->where('enable', 1)
  27. ->where('account', $params['account'])
  28. ->first();
  29. if(!empty($row)) {
  30. return ['账号名称已存在', 5101];
  31. }
  32. $res = IntelligentMassSendingAccount::query()
  33. ->insertGetId($params);
  34. $code = $res>0 ? 0 : 400;
  35. return [$res, $code];
  36. }
  37. public static function accountIndex($page, $pageSize, $sysGroupId)
  38. {
  39. $offset = ($page - 1) * $pageSize;
  40. $query = IntelligentMassSendingAccount::query()
  41. ->where('sys_group_id', $sysGroupId)
  42. ->where('enable', 1);
  43. $countQuery = clone $query;
  44. $count = $countQuery->count();
  45. $list = $query->selectRaw('id as account_id, account, description, status, active_status,'
  46. .' service_provider_id, platform_id')
  47. ->orderBy('id', 'desc')
  48. ->offset($offset)
  49. ->limit($pageSize)
  50. ->get();
  51. # 提取服务商ID
  52. $serviceProviderIdList = $list->pluck('service_provider_id')->toArray();
  53. # 根据服务商ID统一计算关联的剧集数
  54. $playletStat = PlatformPlaylet::query()
  55. ->whereIn('partner_id', $serviceProviderIdList)
  56. ->where('enable', 1)
  57. ->where('state', 1)
  58. ->selectRaw('partner_id, count(1) as count')
  59. ->groupBy('partner_id')
  60. ->get();
  61. $platformArr = array_values(config('platform.platform'));
  62. $platformArr = array_column($platformArr, 'platform_name', 'platform_id');
  63. foreach($list as $value) {
  64. $playletInfo = $playletStat->where('partner_id', $value->service_provider_id)->first();
  65. $value->playlet_num = $playletInfo->count ?? 0;
  66. unset($value->service_provider_id);
  67. $value->platform_name = $platformArr[$value->platform_id] ?? null;
  68. }
  69. return [$list, $count];
  70. }
  71. public static function editAccount($accountId, $params)
  72. {
  73. # 判断账号是否已经使用
  74. $row = IntelligentMassSendingAccount::query()
  75. ->where('platform_id', $params['platform_id'])
  76. ->where('enable', 1)
  77. ->where('account', $params['account'])
  78. ->where('id', '!=', $accountId)
  79. ->first();
  80. if(!empty($row)) {
  81. return ['账号名称已存在', 5101];
  82. }
  83. $res = IntelligentMassSendingAccount::query()
  84. ->where('id', $accountId)
  85. ->update($params);
  86. return [$res, 0];
  87. }
  88. public static function accountList($platformId, $sysGroupId)
  89. {
  90. $list = IntelligentMassSendingAccount::query()
  91. ->selectRaw('id as account_id, account')
  92. ->where('enable', 1)
  93. ->where('status', 1)
  94. ->where('active_status', 1)
  95. ->where('sys_group_id', $sysGroupId)
  96. ->where(function($query) use ($platformId){
  97. if(!empty($platformId)) $query->where('platform_id', $platformId);
  98. })->get();
  99. return $list;
  100. }
  101. public static function addSmallApp($params)
  102. {
  103. # 判断账号是否已经使用
  104. $row = IntelligentMassSendingSmallApp::query()
  105. ->where('sys_group_id', $params['sys_group_id'])
  106. ->where('enable', 1)
  107. ->where('app_id', $params['app_id'])
  108. ->first();
  109. if(!empty($row)) {
  110. return ['小程序已存在', 5102];
  111. }
  112. $res = IntelligentMassSendingSmallApp::query()
  113. ->insertGetId($params);
  114. $code = $res>0 ? 0 : 400;
  115. return [$res, $code];
  116. }
  117. public static function editSmallApp($dataId, $params)
  118. {
  119. # 判断账号是否已经使用
  120. $row = IntelligentMassSendingSmallApp::query()
  121. ->where('sys_group_id', $params['sys_group_id'])
  122. ->where('enable', 1)
  123. ->where('app_id', $params['app_id'])
  124. ->where('id', '!=', $dataId)
  125. ->first();
  126. if(!empty($row)) {
  127. return ['小程序已存在', 5102];
  128. }
  129. $res = IntelligentMassSendingSmallApp::query()
  130. ->where('id', $dataId)
  131. ->update($params);
  132. return [$res, 0];
  133. }
  134. public static function smallAppIndex($appName, $page, $pageSize, $sysGroupId)
  135. {
  136. $offset = ($page -1) * $pageSize;
  137. $query = IntelligentMassSendingSmallApp::query()
  138. ->where('sys_group_id', $sysGroupId)
  139. ->where('enable', 1);
  140. if($appName) {
  141. $query->where('app_name', 'like', '%'.$appName.'%');
  142. }
  143. $countQuery = clone $query;
  144. $count = $countQuery->count();
  145. $list = $query->selectRaw('id as data_id, platform_id, app_id, app_name, status, create_time')
  146. ->orderByDesc('id')
  147. ->offset($offset)
  148. ->limit($pageSize)
  149. ->get();
  150. return [$list, $count];
  151. }
  152. public static function appList($corpid)
  153. {
  154. $list = TencentAdConf::query()
  155. ->where('corp_id', $corpid)
  156. ->where('enable', 1)
  157. ->select(['app_id'])
  158. ->get();
  159. $appIdList = $list->isNotEmpty() ? $list->pluck('app_id')->toArray() : [];
  160. if(empty($appIdList)) {
  161. return [];
  162. } else {
  163. $list = OfficialAccount::query()
  164. ->whereIn('mp_app_id', $appIdList)
  165. ->where('enable', 1)
  166. ->select(['mp_app_id', 'mp_name'])
  167. ->get();
  168. return $list;
  169. }
  170. }
  171. public static function smallAppList($sysGroupId, $platformId)
  172. {
  173. $list = IntelligentMassSendingSmallApp::query()
  174. ->where('enable', 1)
  175. ->where('sys_group_id', $sysGroupId)
  176. ->where(function($query) use ($platformId){
  177. if(!empty($platformId)) $query->where('platform_id', $platformId);
  178. })
  179. ->where('status', 1)
  180. ->selectRaw('app_id, app_name, platform_id')
  181. ->get();
  182. # 添加平台信息
  183. $platformInfoList = config('platform.platform');
  184. foreach($list as $info) {
  185. $platformInfo = $platformInfoList[$info->platform_id] ?? 0;
  186. $info->platform_name = $platformInfo['platform_name'] ?? null;
  187. }
  188. return $list;
  189. }
  190. }