123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\DjUser;
- use App\Models\MassMsgLinkRecord;
- use App\Models\System\Users;
- use App\Models\WelcomeMsg;
- use App\Models\WelcomeMsgRelation;
- use Illuminate\Support\Facades\DB;
- class WelcomeMsgService
- {
- /**
- * 设置新用户欢迎语发送规则
- * */
- public static function setRule($ruleId, $params)
- {
- try {
- # 验证欢迎语内容
- $msgData = isset($params['msg_data']) ? $params['msg_data'] : [];
- if(!is_array($msgData)) $msgData = json_decode($msgData, true);
- $errno = WelcomeMsgService::msgDataVerify($msgData);
- if($errno) return $errno;
- $adminId = $params['admin_id'];
- $corpid = $params['corpid'];
- $isForAll = $params['is_for_all'];
- $users = $params['users'];
- $name = $params['name'];
- $sysGroupId = $params['sys_group_id'];
- if(!$isForAll && empty($users)) {
- return 2204;
- }
- DB::beginTransaction();
- # 写入数据到主表
- $relationId = WelcomeMsgRelation::setMsgRelation($ruleId, $name, $corpid, $adminId, $isForAll, $users, $sysGroupId);
- if(!$relationId) {
- DB::rollBack();
- return 2203;
- }
- # 设置时间规则及消息内容到消息表
- foreach ($msgData as $msgRule) {
- $msgId = isset($msgRule['msg_id']) ? $msgRule['msg_id'] : 0;
- $weeks = $msgRule['weeks'];
- $isDayParting = $msgRule['is_day_parting'];
- $startTime = $msgRule['start_time'];
- $endTime = $msgRule['end_time'];
- $content = $msgRule['content'];
- $attachments = html_entity_decode($msgRule['attachments']);
- $operate = isset($msgRule['operate']) ? $msgRule['operate'] : '';
- if($msgId && $operate=='del') { // 删除子规则操作
- $result = WelcomeMsg::where('id', $msgId)->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;
- }
- }
|