update(['enable' => 0]); if(!$result) { DB::rollBack(); return 2203; } continue; } $result = WelcomeMsg::setMsg( $msgId, $weeks, $startTime, $endTime, $content, $attachments, $relationId, $isDayParting, $sysGroupId ); if(!$result) { DB::rollBack(); return 2203; } } DB::commit(); } catch (\Exception $e) { Log::logError('设置新用户欢迎语发送规则过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'data' => $params ], 'WelcomeMsgRuleSet-Exception'); return 2202; } return 0; } /** * 获取新消息欢迎语规则列表 * @param $corpid string 企业ID * @param $page integer 当前页码数 * @param $pageSize integer 每页显示条数 * */ public static function ruleList($corpid, $userId, $status, $page, $pageSize, &$errno) { try { list($list, $count) = WelcomeMsgRelation::getRuleLists($corpid, $userId, $status, $page, $pageSize); # 获取创建人信息 $adminIds = $list->pluck('admin_id'); $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get(); foreach ($list as $datum) { # 创建人信息 $adminInfo = $adminData->where('id', $datum->admin_id)->first(); $datum->creator = isset($adminInfo->name) ? $adminInfo->name : ''; # 使用成员信息 $users = $datum->users; $isForAll = $datum->is_for_all; if($isForAll) { $userList = DjUser::where('corpid', $corpid)->pluck('name')->toArray(); } else { $userIds = explode(',', trim($users, ',')); $userList = DjUser::selectRaw("distinct(name)")->whereIn('user_id', $userIds) ->where('corpid', $corpid) ->get(); $userList = $userList->pluck('name'); } $datum->user_list = $userList; $datum->rule_id = $datum->id; unset($datum->admin_id, $datum->id); } } catch (\Exception $e) { Log::logError('获取新消息欢迎语规则列表发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'WelcomeMsgList-Exception'); $errno = 2205; return [[], 0]; } return [$list, $count]; } /** * 获取欢迎语详情 * */ public static function ruleDetail($corpid, $ruleId, &$errno) { try{ $detail = WelcomeMsgRelation::selectRaw('id as rule_id, admin_id, corpid, name, is_for_all, users, enable') ->where('corpid', $corpid)->where('id', $ruleId) ->where('is_del', 0) ->first(); if(empty($detail)) return []; # 获取消息列表 $msgList = WelcomeMsg::selectRaw("id as msg_id, weeks, start_time, end_time, content, attachments, is_day_parting") ->where('relation_id', $detail->rule_id)->where('enable', 1)->get(); # 附件信息处理 foreach($msgList as $msg) { $attachments = json_decode($msg->attachments, true); if(!empty($attachments)) { foreach ($attachments as $key=>&$attachment) { if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显 $radarId = $attachment['radar']['radar_id'] ?? 0; $radarInfo = RadarService::getRadarContent($corpid, $radarId); if(empty($radarInfo)) { unset($attachment[$key]); continue; } $attachment['radar'] = $radarInfo; } } } $msg->attachments = json_encode($attachments, 256); } $detail->msg_list = $msgList; } catch (\Exception $e) { Log::logError('欢迎语详情获取过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'rule_id' => $ruleId ], 'ruleDetail'); $errno = 2206; return []; } return $detail; } /** * 删除新用户欢迎语 * */ public static function updateStatus($corpid, $ruleId, $status) { try { # 验证规则是否存在 $isExist = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->exists(); if(!$isExist) return 2207; DB::beginTransaction(); # 变更规则状态 $result = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->update(['enable' => $status]); if(!$result) { DB::rollBack(); return 2208; } if(!$status) # 变更剧集组配置 MassMsgLinkRecord::where('msg_type', 2)->where('rule_id', $ruleId)->update(['enable' => $status]); DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('欢迎语状态变更过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'WelcomeMsgRuleUpdateStatus'); return 2208; } return 0; } public static function ruleDel($corpid, $ruleId) { try { # 验证规则是否存在 $isExist = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->exists(); if(!$isExist) return 2207; DB::beginTransaction(); # 软删除 $result = WelcomeMsgRelation::where('corpid', $corpid)->where('id', $ruleId)->update(['is_del' => 1]); if(!$result) { DB::rollBack(); return 2209; } # 变更剧集组配置 MassMsgLinkRecord::where('msg_type', 2)->where('rule_id', $ruleId)->update(['enable' => 0]); DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('欢迎语删除过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'WelcomeMsgRuleDel'); return 2209; } return 0; } /** * 校验新用户欢迎语消息体 * */ public static function msgDataVerify(&$msgData) { if(!empty($msgData)) { foreach ($msgData as $key=>&$msg) { # 判断附件格式是否合法 if(!empty($msg['attachments'])) { $attachments = json_decode(html_entity_decode($msg['attachments']), true); if(!is_array($attachments)) return 2210; } # 判断是否是分时段欢迎语 $isDayParting = isset($msg['is_day_parting']) ? $msg['is_day_parting'] : false; if($isDayParting === false) { unset($msgData[$key]); } } } else { return 2201; } if(empty($msgData)) return 2201; return 0; } }