123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/12/28
- * Time: 11:04
- */
- namespace App\Service;
- use App\Log;
- use App\Models\AbnormalAccountConfDetail;
- use App\Models\AbnormalAccountWarnConf;
- use App\Models\AbnormalAccountWarnConfRecord;
- use App\Models\WarnGroup;
- use App\Models\WarnUser;
- class AbnormalAccountWarnConfService
- {
- # 设置客服异常预警规则
- public static function setConf($confId, $params, $content, &$errno)
- {
- try{
- if(!is_array($content)) {
- $content = json_decode($content, 1);
- }
- \DB::begintransaction();
- if(empty($confId)) { // 添加配置
- $confId = AbnormalAccountWarnConf::query()->insertGetId($params);
- list($data, $err) = self::addDetail($confId, $params, $content);
- } else { // 编辑配置
- list($data, $err) = self::updateDetail($confId, $params, $content);
- unset($params['warn_type']);
- AbnormalAccountWarnConf::query()->where('id', $confId)->update($params);
- }
- if(0 != $err){
- \DB::rollBack();
- $errno = $err;
- return $data;
- }
- \DB::commit();
- return ['请求成功'];
- } catch (\Exception $exception) {
- Log::logError('配置异常客服异常', [
- 'request' => [
- 'conf_id' => $confId,
- 'params' => $params,
- 'content' => $content,
- ],
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'msg' => $exception->getMessage(),
- 'trace'=> $exception->getTraceAsString(),
- ], 'interface');
- $errno = 500;
- return ['程序异常'];
- }
- }
- public static function addDetail($confId, $confParams, $content) {
- if(2 == $confParams['warn_type']) {
- if(count($content) > 1) return ['仅可添加单条预警规则', 3210];
- list($data, $err) = AbnormalAccountWarnConfRecord::addDetail($confId, $confParams, $content);
- } else {
- list($data, $err) = AbnormalAccountConfDetail::addDetail($confId, $content);
- }
- return [$data, $err];
- }
- public static function updateDetail($confId, $confParams, $content) {
- if(2 == $confParams['warn_type']) {
- if(count($content) > 1) return ['仅可添加单条预警规则', 3210];
- list($data, $err) = AbnormalAccountWarnConfRecord::updateDetail($confId, $confParams, $content);
- } else {
- list($data, $err) = AbnormalAccountConfDetail::updateDetail($confId, $content);
- }
- return [$data, $err];
- }
- public static function confList($sysGroupId, $warnType, $page, $pageSize)
- {
- $offset = ($page - 1) * $pageSize;
- $query = AbnormalAccountWarnConf::query()->where('sys_group_id', $sysGroupId)
- ->where('enable', 1)->where('warn_type', $warnType);
- $countQuery = clone $query;
- $count = $countQuery->count();
- $list = $query->selectRaw('`id` as conf_id, `monitor_user_list`, `notice_list`, `status`, `notice_type`,'
- .' `create_time`, `time_interval`, `time_interval_unit`')
- ->offset($offset)->limit($pageSize)->orderBy('id', 'desc')->get();
- # 监控客服号
- $monitorUserList = AbnormalAccountWarnConf::getMonitorUserData($list->toArray());
- $confIdList = array_column($list->toArray(), 'conf_id');
- # 配置规则
- if(1 == $warnType) {
- $contentList = AbnormalAccountConfDetail::query()->where('enable', 1)->whereIn('conf_id', $confIdList)
- ->select(['minute', 'check_type', 'num', 'conf_id'])->get();
- } else {
- $contentList = AbnormalAccountWarnConfRecord::query()->where('enable', 1)->whereIn('conf_id', $confIdList)
- ->select(['minute', 'check_type', 'num', 'conf_id'])->groupBy(['conf_id'])->get();
- }
- # 查询预警人与预警组
- $warnGroupList = WarnGroup::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
- ->select(['id', 'name'])->get();
- $warnUserList = WarnUser::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
- ->select(['id', 'name', 'phone'])->get();
- foreach ($list as $item) {
- $item->monitor_user_list = $monitorUserList[$item->conf_id];
- $contentArr = [];
- $content = $contentList->where('conf_id', $item->conf_id)->all();
- foreach ($content as $v) {
- $type = $v['check_type'] == 1 ? '小于等于' : '大于等于';
- $contentArr[] = 1 == $warnType ? "{$v['minute']}分钟内添加人数{$type}{$v['num']}人" : "总粉丝数{$type}{$v['num']}人";
- }
- $item->content_arr = $contentArr;
- if(1 == $item->notice_type) {
- $noticeIdList = explode(',', $item->notice_list);
- $noticeData = $warnUserList->whereIn('id', $noticeIdList)->all();
- } else {
- $noticeIdList = explode(',', $item->notice_list);
- $noticeData = $warnGroupList->whereIn('id', $noticeIdList)->all();
- }
- $item->notice_list = array_values($noticeData);
- }
- return [$list, $count];
- }
- public static function confDetail($sysGroupId, $confId)
- {
- $confInfo = AbnormalAccountWarnConf::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
- ->where('id', $confId)->selectRaw('`id` as conf_id, `notice_list`, `notice_type`, `monitor_user_list`,'
- .' `time_interval`, `warn_type`, `time_interval_unit`')->first();
- if(empty($confInfo)) {
- return [];
- }
- # 查询监控客服列表
- $monitorUserList = AbnormalAccountWarnConf::getMonitorUserData([json_decode(json_encode($confInfo), 1)]);
- $monitorUserData = $monitorUserList[$confId];
- $confInfo->monitor_user_list = $monitorUserData;
- # 查询预警人员ID列表或者预警组ID列表
- $noticeIdList = explode(',', $confInfo->notice_list);
- if(1 == $confInfo->notice_type) {
- $noticeData = WarnUser::query()->whereIn('id', $noticeIdList)->where('enable', 1)
- ->select(['name', 'phone', 'id'])->get();
- } else {
- $noticeData = WarnGroup::query()->whereIn('id', $noticeIdList)->where('enable', 1)
- ->select(['name', 'id'])->get();
- }
- $confInfo->notice_list = $noticeData;
- # 查询预警规则
- if(1 == $confInfo->warn_type) {
- $content = AbnormalAccountConfDetail::query()->where('enable', 1)->where('conf_id', $confId)
- ->selectRaw('id as data_id, minute, check_type, num')->get();
- } else {
- $content = AbnormalAccountWarnConfRecord::query()->where('enable', 1)->where('conf_id', $confId)
- ->selectRaw('id as data_id, minute, check_type, num')->limit(1)->get();
- }
- $confInfo->content = $content->toArray();
- return $confInfo;
- }
- public static function updateConfStatus($confId, $status, &$errno)
- {
- $confInfo = AbnormalAccountWarnConf::query()->where('id', $confId)->first();
- if(empty($confInfo)){
- $errno = 1102;
- return ['参数有误,配置ID不合法'];
- }
- if($confInfo->status != $status) {
- $res = AbnormalAccountWarnConf::query()->where('id', $confId)->update(['status' => $status]);
- if(!$res) {
- $errno = 400;
- return ['处理失败,稍候重试'];
- }
- }
- return ['请求成功'];
- }
- }
|