123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\AuthorizeCorp;
- use App\Models\DjUser;
- use App\Models\OperationGroup;
- use App\Models\OperationGroupDetail;
- use App\Models\System\Users;
- use Illuminate\Support\Facades\DB;
- class OperationGroupService
- {
- /**
- * 配置运营组
- * */
- public static function setConfig($groupId, $groupName, $operatorId, $corpUserList, $sysGroupId, $adminId)
- {
- try {
- $errno = 0;
- # 验证运营人员ID
- $enableUserIdList = Users::getCorpUserList($sysGroupId);
- if($groupId) {
- $groupConfig = OperationGroup::getConfig($groupId, $sysGroupId);
- if(empty($groupConfig)) {
- $errno = 5250;
- return $errno;
- }
- $enableUserIdList[] = $groupConfig->operator_id ?? 0;
- }
- if(!in_array($operatorId, $enableUserIdList)){
- $errno = 5254;
- return $errno;
- }
- DB::beginTransaction();
- # 更新数据到dj_operation_group
- $groupId = OperationGroup::setConfig($groupId, $groupName, $operatorId, $sysGroupId, $adminId, $errno);
- if($groupId === false) {
- DB::rollBack();
- return $errno;
- }
- # 保存配置详情到dj_operation_group_detail
- $corpUserList = json_decode($corpUserList, true);
- if(empty($corpUserList)) {
- DB::rollBack();
- return 5251;
- }
- # 删除未保留的成员配置信息
- OperationGroupDetail::where('enable', 1)->where('group_id', $groupId)
- ->update(['enable' => 0]);
-
- foreach ($corpUserList as $corpUser) {
- $corpid = $corpUser['corpid'];
- $userId = $corpUser['user_id'];
- # 数据入库
- OperationGroupDetail::updateOrCreate(['group_id' => $groupId, 'corpid' => $corpid, 'user_id' => $userId], ['enable' => 1]);
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::logError('设置运营组过程发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'data' => [
- 'group_name' => $groupName, 'operator_id' => $operatorId, 'corp_u_list' => $corpUserList,
- 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId, 'id' => $groupId
- ]
- ], 'OperationGroup');
- }
- return 0;
- }
- /**
- * 获取运营组列表
- * */
- public static function groupList($sysGroupId, $adminId, $operatorId, $page, $pageSize)
- {
- list($list, $count) = OperationGroup::groupList($sysGroupId, $adminId, $operatorId, $page, $pageSize);
- if(!$count) return [[], 0];
- # 获取运营组对应的成员信息
- $groupIdLIst = $list->pluck('group_id');
- $configData = OperationGroupDetail::select('group_id', 'corpid', 'user_id')->whereIn('group_id', $groupIdLIst)->where('enable', 1)->get();
- # 获取所需的企微信息和成员信息
- $corpidList = array_unique($configData->pluck('corpid')->toArray());
- $useridList = array_unique($configData->pluck('user_id')->toArray());
- $corpData = AuthorizeCorp::select('corpid', 'corp_name')->whereIn('corpid', $corpidList)->get();
- $corpUserList = DjUser::select('corpid', 'user_id', 'name')->whereIn('corpid', $corpidList)
- ->whereIn('user_id', $useridList)->where('status', 1)->get();
- # 获取运营组对应的运营信息
- $operatorIds = $list->pluck('operator_id');
- $operatorList = Users::select('id', 'name')->whereIn('id', $operatorIds)->get();
- foreach ($list as $item) {
- # 获取运营名称
- $operatorInfo = $operatorList->where('id', $item->operator_id)->first();
- $item->operator_name = $operatorInfo->name ?? null;
- # 获取运营组内的成员信息
- $configList = $configData->where('group_id', $item->group_id)->all();
- foreach ($configList as $k => $config) {
- # 获取对应的企微名称和成员名称
- $corpInfo = $corpData->where('corpid', $config->corpid)->first();
- $corpName = $corpInfo->corp_name ?? null;
- $userInfo = $corpUserList->where('corpid', $config->corpid)->where('user_id', $config->user_id)->first();
- $userName = $userInfo->name ?? null;
- if(empty($userName)) {
- unset($configList[$k]);
- continue;
- }
- $config->user_info = $corpName . '-' . $userName;
- }
- $item->corp_user_list = array_values($configList);
- }
- return [$list, $count];
- }
- /**
- * 获取运营组详情信息
- * */
- public static function groupDetail($groupId, $sysGroupId, &$errno)
- {
- $groupBasicInfo = OperationGroup::where('id', $groupId)->first();
- if($sysGroupId != -1)
- $groupBasicInfo->where('sys_group_id', $sysGroupId);
- if(empty($groupBasicInfo)) {
- $errno = 5250;
- return [];
- }
- # 获取运营组对应的企微成员信息
- $groupDetail = OperationGroupDetail::select('corpid', 'user_id')->where('enable', 1)->where('group_id', $groupId)
- ->get();
- $corpidList = array_unique($groupDetail->pluck('corpid')->toArray());
- # 数据处理
- $userList = [];
- foreach ($corpidList as $corpid) {
- # 获取企微对应的成员user_id
- $userIds = $groupDetail->where('corpid', $corpid)->pluck('user_id')->toArray();
- $midData = [
- 'app_id' => $corpid,
- 'user_list' => $userIds
- ];
- array_push($userList, $midData);
- }
- $groupBasicInfo->corp_user_list = $userList;
- return $groupBasicInfo;
- }
- /**
- * 更新运营组状态
- * */
- public static function changeStatus($sysGroupId, $groupId, $status)
- {
- # 判断运营组是否存在
- $groupInfo = OperationGroup::where('sys_group_id', $sysGroupId)
- ->where('id', $groupId)->first();
- if(empty($groupInfo)) return 5250;
- if($groupInfo->enable == $status) return 5252;
- $groupInfo->enable = $status;
- $result = $groupInfo->save();
- return $result ? 0 : 5253;
- }
- }
|