企微短剧业务系统

OperationGroupService.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. use App\Models\DjUser;
  6. use App\Models\OperationGroup;
  7. use App\Models\OperationGroupDetail;
  8. use App\Models\System\Users;
  9. use Illuminate\Support\Facades\DB;
  10. class OperationGroupService
  11. {
  12. /**
  13. * 配置运营组
  14. * */
  15. public static function setConfig($groupId, $groupName, $operatorId, $corpUserList, $sysGroupId, $adminId)
  16. {
  17. try {
  18. $errno = 0;
  19. # 验证运营人员ID
  20. $enableUserIdList = Users::getCorpUserList($sysGroupId);
  21. if($groupId) {
  22. $groupConfig = OperationGroup::getConfig($groupId, $sysGroupId);
  23. if(empty($groupConfig)) {
  24. $errno = 5250;
  25. return $errno;
  26. }
  27. $enableUserIdList[] = $groupConfig->operator_id ?? 0;
  28. }
  29. if(!in_array($operatorId, $enableUserIdList)){
  30. $errno = 5254;
  31. return $errno;
  32. }
  33. DB::beginTransaction();
  34. # 更新数据到dj_operation_group
  35. $groupId = OperationGroup::setConfig($groupId, $groupName, $operatorId, $sysGroupId, $adminId, $errno);
  36. if($groupId === false) {
  37. DB::rollBack();
  38. return $errno;
  39. }
  40. # 保存配置详情到dj_operation_group_detail
  41. $corpUserList = json_decode($corpUserList, true);
  42. if(empty($corpUserList)) {
  43. DB::rollBack();
  44. return 5251;
  45. }
  46. # 删除未保留的成员配置信息
  47. OperationGroupDetail::where('enable', 1)->where('group_id', $groupId)
  48. ->update(['enable' => 0]);
  49. foreach ($corpUserList as $corpUser) {
  50. $corpid = $corpUser['corpid'];
  51. $userId = $corpUser['user_id'];
  52. # 数据入库
  53. OperationGroupDetail::updateOrCreate(['group_id' => $groupId, 'corpid' => $corpid, 'user_id' => $userId], ['enable' => 1]);
  54. }
  55. DB::commit();
  56. } catch (\Exception $e) {
  57. DB::rollBack();
  58. Log::logError('设置运营组过程发生异常', [
  59. 'line' => $e->getLine(),
  60. 'msg' => $e->getMessage(),
  61. 'data' => [
  62. 'group_name' => $groupName, 'operator_id' => $operatorId, 'corp_u_list' => $corpUserList,
  63. 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId, 'id' => $groupId
  64. ]
  65. ], 'OperationGroup');
  66. }
  67. return 0;
  68. }
  69. /**
  70. * 获取运营组列表
  71. * */
  72. public static function groupList($sysGroupId, $adminId, $operatorId, $page, $pageSize)
  73. {
  74. list($list, $count) = OperationGroup::groupList($sysGroupId, $adminId, $operatorId, $page, $pageSize);
  75. if(!$count) return [[], 0];
  76. # 获取运营组对应的成员信息
  77. $groupIdLIst = $list->pluck('group_id');
  78. $configData = OperationGroupDetail::select('group_id', 'corpid', 'user_id')->whereIn('group_id', $groupIdLIst)->where('enable', 1)->get();
  79. # 获取所需的企微信息和成员信息
  80. $corpidList = array_unique($configData->pluck('corpid')->toArray());
  81. $useridList = array_unique($configData->pluck('user_id')->toArray());
  82. $corpData = AuthorizeCorp::select('corpid', 'corp_name')->whereIn('corpid', $corpidList)->get();
  83. $corpUserList = DjUser::select('corpid', 'user_id', 'name')->whereIn('corpid', $corpidList)
  84. ->whereIn('user_id', $useridList)->where('status', 1)->get();
  85. # 获取运营组对应的运营信息
  86. $operatorIds = $list->pluck('operator_id');
  87. $operatorList = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
  88. foreach ($list as $item) {
  89. # 获取运营名称
  90. $operatorInfo = $operatorList->where('id', $item->operator_id)->first();
  91. $item->operator_name = $operatorInfo->name ?? null;
  92. # 获取运营组内的成员信息
  93. $configList = $configData->where('group_id', $item->group_id)->all();
  94. foreach ($configList as $k => $config) {
  95. # 获取对应的企微名称和成员名称
  96. $corpInfo = $corpData->where('corpid', $config->corpid)->first();
  97. $corpName = $corpInfo->corp_name ?? null;
  98. $userInfo = $corpUserList->where('corpid', $config->corpid)->where('user_id', $config->user_id)->first();
  99. $userName = $userInfo->name ?? null;
  100. if(empty($userName)) {
  101. unset($configList[$k]);
  102. continue;
  103. }
  104. $config->user_info = $corpName . '-' . $userName;
  105. }
  106. $item->corp_user_list = array_values($configList);
  107. }
  108. return [$list, $count];
  109. }
  110. /**
  111. * 获取运营组详情信息
  112. * */
  113. public static function groupDetail($groupId, $sysGroupId, &$errno)
  114. {
  115. $groupBasicInfo = OperationGroup::where('id', $groupId)->first();
  116. if($sysGroupId != -1)
  117. $groupBasicInfo->where('sys_group_id', $sysGroupId);
  118. if(empty($groupBasicInfo)) {
  119. $errno = 5250;
  120. return [];
  121. }
  122. # 获取运营组对应的企微成员信息
  123. $groupDetail = OperationGroupDetail::select('corpid', 'user_id')->where('enable', 1)->where('group_id', $groupId)
  124. ->get();
  125. $corpidList = array_unique($groupDetail->pluck('corpid')->toArray());
  126. # 数据处理
  127. $userList = [];
  128. foreach ($corpidList as $corpid) {
  129. # 获取企微对应的成员user_id
  130. $userIds = $groupDetail->where('corpid', $corpid)->pluck('user_id')->toArray();
  131. $midData = [
  132. 'app_id' => $corpid,
  133. 'user_list' => $userIds
  134. ];
  135. array_push($userList, $midData);
  136. }
  137. $groupBasicInfo->corp_user_list = $userList;
  138. return $groupBasicInfo;
  139. }
  140. /**
  141. * 更新运营组状态
  142. * */
  143. public static function changeStatus($sysGroupId, $groupId, $status)
  144. {
  145. # 判断运营组是否存在
  146. $groupInfo = OperationGroup::where('sys_group_id', $sysGroupId)
  147. ->where('id', $groupId)->first();
  148. if(empty($groupInfo)) return 5250;
  149. if($groupInfo->enable == $status) return 5252;
  150. $groupInfo->enable = $status;
  151. $result = $groupInfo->save();
  152. return $result ? 0 : 5253;
  153. }
  154. }