企微短剧业务系统

AbnormalAccountWarnConfService.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/12/28
  6. * Time: 11:04
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\Models\AbnormalAccountConfDetail;
  11. use App\Models\AbnormalAccountWarnConf;
  12. use App\Models\AbnormalAccountWarnConfRecord;
  13. use App\Models\WarnGroup;
  14. use App\Models\WarnUser;
  15. class AbnormalAccountWarnConfService
  16. {
  17. # 设置客服异常预警规则
  18. public static function setConf($confId, $params, $content, &$errno)
  19. {
  20. try{
  21. if(!is_array($content)) {
  22. $content = json_decode($content, 1);
  23. }
  24. \DB::begintransaction();
  25. if(empty($confId)) { // 添加配置
  26. $confId = AbnormalAccountWarnConf::query()->insertGetId($params);
  27. list($data, $err) = self::addDetail($confId, $params, $content);
  28. } else { // 编辑配置
  29. list($data, $err) = self::updateDetail($confId, $params, $content);
  30. unset($params['warn_type']);
  31. AbnormalAccountWarnConf::query()->where('id', $confId)->update($params);
  32. }
  33. if(0 != $err){
  34. \DB::rollBack();
  35. $errno = $err;
  36. return $data;
  37. }
  38. \DB::commit();
  39. return ['请求成功'];
  40. } catch (\Exception $exception) {
  41. Log::logError('配置异常客服异常', [
  42. 'request' => [
  43. 'conf_id' => $confId,
  44. 'params' => $params,
  45. 'content' => $content,
  46. ],
  47. 'file' => $exception->getFile(),
  48. 'line' => $exception->getLine(),
  49. 'msg' => $exception->getMessage(),
  50. 'trace'=> $exception->getTraceAsString(),
  51. ], 'interface');
  52. $errno = 500;
  53. return ['程序异常'];
  54. }
  55. }
  56. public static function addDetail($confId, $confParams, $content) {
  57. if(2 == $confParams['warn_type']) {
  58. if(count($content) > 1) return ['仅可添加单条预警规则', 3210];
  59. list($data, $err) = AbnormalAccountWarnConfRecord::addDetail($confId, $confParams, $content);
  60. } else {
  61. list($data, $err) = AbnormalAccountConfDetail::addDetail($confId, $content);
  62. }
  63. return [$data, $err];
  64. }
  65. public static function updateDetail($confId, $confParams, $content) {
  66. if(2 == $confParams['warn_type']) {
  67. if(count($content) > 1) return ['仅可添加单条预警规则', 3210];
  68. list($data, $err) = AbnormalAccountWarnConfRecord::updateDetail($confId, $confParams, $content);
  69. } else {
  70. list($data, $err) = AbnormalAccountConfDetail::updateDetail($confId, $content);
  71. }
  72. return [$data, $err];
  73. }
  74. public static function confList($sysGroupId, $warnType, $page, $pageSize)
  75. {
  76. $offset = ($page - 1) * $pageSize;
  77. $query = AbnormalAccountWarnConf::query()->where('sys_group_id', $sysGroupId)
  78. ->where('enable', 1)->where('warn_type', $warnType);
  79. $countQuery = clone $query;
  80. $count = $countQuery->count();
  81. $list = $query->selectRaw('`id` as conf_id, `monitor_user_list`, `notice_list`, `status`, `notice_type`,'
  82. .' `create_time`, `time_interval`, `time_interval_unit`')
  83. ->offset($offset)->limit($pageSize)->orderBy('id', 'desc')->get();
  84. # 监控客服号
  85. $monitorUserList = AbnormalAccountWarnConf::getMonitorUserData($list->toArray());
  86. $confIdList = array_column($list->toArray(), 'conf_id');
  87. # 配置规则
  88. if(1 == $warnType) {
  89. $contentList = AbnormalAccountConfDetail::query()->where('enable', 1)->whereIn('conf_id', $confIdList)
  90. ->select(['minute', 'check_type', 'num', 'conf_id'])->get();
  91. } else {
  92. $contentList = AbnormalAccountWarnConfRecord::query()->where('enable', 1)->whereIn('conf_id', $confIdList)
  93. ->select(['minute', 'check_type', 'num', 'conf_id'])->groupBy(['conf_id'])->get();
  94. }
  95. # 查询预警人与预警组
  96. $warnGroupList = WarnGroup::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
  97. ->select(['id', 'name'])->get();
  98. $warnUserList = WarnUser::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
  99. ->select(['id', 'name', 'phone'])->get();
  100. foreach ($list as $item) {
  101. $item->monitor_user_list = $monitorUserList[$item->conf_id];
  102. $contentArr = [];
  103. $content = $contentList->where('conf_id', $item->conf_id)->all();
  104. foreach ($content as $v) {
  105. $type = $v['check_type'] == 1 ? '小于等于' : '大于等于';
  106. $contentArr[] = 1 == $warnType ? "{$v['minute']}分钟内添加人数{$type}{$v['num']}人" : "总粉丝数{$type}{$v['num']}人";
  107. }
  108. $item->content_arr = $contentArr;
  109. if(1 == $item->notice_type) {
  110. $noticeIdList = explode(',', $item->notice_list);
  111. $noticeData = $warnUserList->whereIn('id', $noticeIdList)->all();
  112. } else {
  113. $noticeIdList = explode(',', $item->notice_list);
  114. $noticeData = $warnGroupList->whereIn('id', $noticeIdList)->all();
  115. }
  116. $item->notice_list = array_values($noticeData);
  117. }
  118. return [$list, $count];
  119. }
  120. public static function confDetail($sysGroupId, $confId)
  121. {
  122. $confInfo = AbnormalAccountWarnConf::query()->where('enable', 1)->where('sys_group_id', $sysGroupId)
  123. ->where('id', $confId)->selectRaw('`id` as conf_id, `notice_list`, `notice_type`, `monitor_user_list`,'
  124. .' `time_interval`, `warn_type`, `time_interval_unit`')->first();
  125. if(empty($confInfo)) {
  126. return [];
  127. }
  128. # 查询监控客服列表
  129. $monitorUserList = AbnormalAccountWarnConf::getMonitorUserData([json_decode(json_encode($confInfo), 1)]);
  130. $monitorUserData = $monitorUserList[$confId];
  131. $confInfo->monitor_user_list = $monitorUserData;
  132. # 查询预警人员ID列表或者预警组ID列表
  133. $noticeIdList = explode(',', $confInfo->notice_list);
  134. if(1 == $confInfo->notice_type) {
  135. $noticeData = WarnUser::query()->whereIn('id', $noticeIdList)->where('enable', 1)
  136. ->select(['name', 'phone', 'id'])->get();
  137. } else {
  138. $noticeData = WarnGroup::query()->whereIn('id', $noticeIdList)->where('enable', 1)
  139. ->select(['name', 'id'])->get();
  140. }
  141. $confInfo->notice_list = $noticeData;
  142. # 查询预警规则
  143. if(1 == $confInfo->warn_type) {
  144. $content = AbnormalAccountConfDetail::query()->where('enable', 1)->where('conf_id', $confId)
  145. ->selectRaw('id as data_id, minute, check_type, num')->get();
  146. } else {
  147. $content = AbnormalAccountWarnConfRecord::query()->where('enable', 1)->where('conf_id', $confId)
  148. ->selectRaw('id as data_id, minute, check_type, num')->limit(1)->get();
  149. }
  150. $confInfo->content = $content->toArray();
  151. return $confInfo;
  152. }
  153. public static function updateConfStatus($confId, $status, &$errno)
  154. {
  155. $confInfo = AbnormalAccountWarnConf::query()->where('id', $confId)->first();
  156. if(empty($confInfo)){
  157. $errno = 1102;
  158. return ['参数有误,配置ID不合法'];
  159. }
  160. if($confInfo->status != $status) {
  161. $res = AbnormalAccountWarnConf::query()->where('id', $confId)->update(['status' => $status]);
  162. if(!$res) {
  163. $errno = 400;
  164. return ['处理失败,稍候重试'];
  165. }
  166. }
  167. return ['请求成功'];
  168. }
  169. }