where('id', $ruleId)->first(); if(empty($ruleInfo)) return 5221; $params['next_send_time'] = CircleMassMsgService::getNextSendTime( $params['interval_type'], $params['time_points'], $params['interval'], $ruleInfo->last_send_time ); if(!$params['next_send_time']) return 5220; $result = CircleMassMsg::where('id', $ruleId)->update($params); } else { # 获取下一次发送时间 $lastSendTime = date('Y-m-d H:i:s'); $nextSendTime = CircleMassMsgService::getNextSendTime( $params['interval_type'], $params['time_points'], $params['interval'], $lastSendTime ); if(!$nextSendTime) return 5220; # 设置新群发规则 $massMsgModel = new CircleMassMsg(); $massMsgModel->admin_id = $params['admin_id']; $massMsgModel->corpid = $params['corpid']; $massMsgModel->name = $params['name']; $massMsgModel->last_send_time = $lastSendTime; $massMsgModel->next_send_time = $nextSendTime; $massMsgModel->is_all = $params['is_all']; $massMsgModel->senders = $params['senders']; $massMsgModel->content = $params['content']; $massMsgModel->attachments = html_entity_decode($params['attachments']); $massMsgModel->gender = $params['gender']; $massMsgModel->interval_type = $params['interval_type']; $massMsgModel->interval_time = $params['interval_time']; $massMsgModel->interval = $params['interval']; $massMsgModel->time_points = $params['time_points']; $massMsgModel->tag_screen_type = $params['tag_screen_type']; $massMsgModel->tag_list = $params['tag_list']; $massMsgModel->exclude_tag_list = $params['exclude_tag_list']; // 付费情况 $massMsgModel->pay_status = $params['pay_status']; $massMsgModel->pay_num_min = $params['pay_num_min']; $massMsgModel->pay_num_max = $params['pay_num_max']; $result = $massMsgModel->save(); // $ruleId = isset($massMsgModel->id) ? $massMsgModel->id : ''; } if(!$result) return 5222; } catch (\Exception $e) { Log::logError('智能群发规则设置发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'params' => $params ], 'SetCircleMassMsgRule'); return 5223; } return 0; } /** * 获取智能群发列表 * @param $corpid string 企业ID * @param $creatorId integer 创建人ID集合 * @param $createTimeStart string 创建时间查询-结束时间 * @param $createTimeEnd string 创建时间查询-结束时间 * @param $page integer 当前页码数 * @param $pageSize integer 每页显示条数 * */ public static function ruleList($corpid, $creatorId, $createTimeStart,$createTimeEnd, $page, $pageSize, &$errno) { try { list($list, $count) = CircleMassMsg::getRuleLists($corpid, $creatorId, $createTimeStart, $createTimeEnd, $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 : ''; # 描述信息 $days = floor($datum->interval_time / 60 / 24); $hour = ($datum->interval_time / 60) % 24; $minute = $datum->interval_time % 60; $datum->desc = '客户加客服' .$days. '天' .$hour. '小时' .$minute. '分钟后,'; if($datum->interval_type == 1) { $datum->desc .= '当天'; $timePoints = json_decode($datum->time_points, true); foreach ($timePoints as $points) { $datum->desc .= $points . ' '; } $datum->desc .= "提醒发送"; } elseif ($datum->interval_type == 2) { $datum->desc .= '后台每'.$datum->interval.'小时分批提醒发送'; } unset($datum->admin_id); } } catch (\Exception $e) { Log::logError('获取智能群发列表过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), ], 'CircleMassMsgRuleList'); $errno = 2105; return [[], 0]; } return [$list, $count]; } /** * 获取群发详情 * @param $corpid string 企业ID * @param $ruleId integer 群发规则ID * */ public static function ruleDetail($corpid, $ruleId, &$errno) { try{ $detail = CircleMassMsg::selectRaw('id as rule_id, admin_id, corpid, name, interval_type, interval_time, is_all, senders, content, attachments, status, gender, `interval`, time_points, tag_screen_type, tag_list, exclude_tag_list, pay_status, pay_num_min, pay_num_max') ->where('corpid', $corpid)->where('id', $ruleId) ->first(); if(empty($detail)) return []; # 发送人 if(!$detail->is_all) { $detail->sender_name = DjUser::query()->where("corpid",$corpid) ->whereIn("user_id", explode(',',$detail->senders)) ->pluck("name"); } else { $detail->sender_name = []; } # 获取创建人信息 $detail->creator = Users::where('id', $detail->admin_id)->value('name'); $attachments = json_decode($detail->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; } } } $detail->time_points = json_decode($detail->time_points); $detail->attachments = json_encode($attachments, 256); } catch (\Exception $e) { Log::logError('智能群发详情获取过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'rule_id' => $ruleId ], 'CircleMassMsgRuleDetail'); $errno = 2104; return []; } return $detail; } /** * 更新群发状态 * */ public static function updateRuleStatus($corpid, $ruleId, $status) { # 验证规则是否存在 $isExist = CirCleMassMsg::where('corpid', $corpid)->where('id', $ruleId)->exists(); if(!$isExist) return 2106; # 变更规则状态 $result = CircleMassMsg::where('corpid', $corpid)->where('id', $ruleId)->update(['status' => $status]); if(!$result) return 2107; return 0; } /** * 获取下一次推送时间 * */ public static function getNextSendTime($intervalType, $timePoints, $interval, $lastSendTime) { $lastSendTime = strtotime($lastSendTime); $nextSendTime = null; if($intervalType == 1) { // 指定时间点检索 $timePointsData = json_decode($timePoints, true); foreach ($timePointsData as &$timePoint){ $timePoint = strtotime($timePoint); } sort($timePointsData); foreach ($timePointsData as $time) { if($time>$lastSendTime && $time>=time()) { $nextSendTime = date('Y-m-d H:i:s', $time); break; } } if(!$nextSendTime) { $nextSendTime = date('Y-m-d H:i:s', strtotime('+1 day', $timePointsData[0])); Log::logInfo('下次推送时间需要到隔天', [ 'intervalType' => $intervalType, 'timePoints' => $timePoints, 'interval' => $interval, 'lastSendTime' => $lastSendTime, 'nextSendTime' => $nextSendTime ], 'NextSendTimeTrace'); } } elseif ($intervalType == 2) { $nextSendTime = strtotime('+'.$interval.' hour', $lastSendTime); if($nextSendTime < time()) $nextSendTime = strtotime('+'.$interval.' hour'); $nextSendTime = date('Y-m-d H:i:s', $nextSendTime); } Log::logInfo('下次推送时间运算日志', [ 'intervalType' => $intervalType, 'timePoints' => $timePoints, 'interval' => $interval, 'lastSendTime' => $lastSendTime, 'nextSendTime' => $nextSendTime ], 'NextSendTimeTrace'); return $nextSendTime; } /** * 获取下一次补充邀请入群时间 * */ public static function getNextSupplementaryInviteTime($intervalType, $timePoints, $interval, $lastSendTime) { $lastSendTime = strtotime($lastSendTime); $nextSendTime = null; if($intervalType == 1) { // 指定时间点检索 $timePointsData = json_decode($timePoints, true); foreach ($timePointsData as &$timePoint){ $timePoint = strtotime($timePoint); } sort($timePointsData); foreach ($timePointsData as $time) { if($time>$lastSendTime && $time>=time()) { $nextSendTime = date('Y-m-d H:i:s', $time); break; } } if(!$nextSendTime) { $nextSendTime = date('Y-m-d H:i:s', strtotime('+'.$interval.' day', $timePointsData[0])); Log::logInfo('下次推送时间需要到隔天', [ 'intervalType' => $intervalType, 'timePoints' => $timePoints, 'interval' => $interval, 'lastSendTime' => $lastSendTime, 'nextSendTime' => $nextSendTime ], 'getNextSupplementaryInviteTimeTrace'); } } elseif ($intervalType == 2) { $nextSendTime = strtotime('+'.$interval.' hour', $lastSendTime); if($nextSendTime < time()) $nextSendTime = strtotime('+'.$interval.' hour'); $nextSendTime = date('Y-m-d H:i:s', $nextSendTime); } Log::logInfo('下次推送时间运算日志', [ 'intervalType' => $intervalType, 'timePoints' => $timePoints, 'interval' => $interval, 'lastSendTime' => $lastSendTime, 'nextSendTime' => $nextSendTime ], 'getNextSupplementaryInviteTimeTrace'); return $nextSendTime; } }