企微短剧业务系统

SystemService.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. use App\Models\Customer;
  6. use App\Models\CustomerDetails;
  7. use App\Models\DjUser;
  8. use App\Models\System\AdminManageCorp;
  9. use App\Models\UserOpinionFeedback;
  10. use App\Models\WarnGroup;
  11. use App\Models\WarnGroupDjuser;
  12. use App\Models\WarnGroupUser;
  13. use App\RedisModel;
  14. use App\Support\EmailQueue;
  15. use App\Support\YPSMS;
  16. class SystemService
  17. {
  18. public static function userOpinionFeedbackAction($corpid, $userId, $externalUserId, $type, $content, $attachments) {
  19. Log::logInfo('userOpinionFeedbackAction', ['corpid' => $corpid, 'user_id' => $userId, 'external_userid' => $externalUserId
  20. , 'type' => $type, 'content' => $content, 'attachments' => $attachments], 'interface');
  21. $res = UserOpinionFeedback::addRecord($corpid, $userId, $externalUserId, $type, $content, $attachments);
  22. # 反馈用户拉黑
  23. $userDetailDeal = CustomerDetails::suffix($corpid)->where('corpid', $corpid)->where('user_id', $userId)
  24. ->where('external_userid', $externalUserId)->update(['blacklist_status' => 1]);
  25. Log::logInfo('userOpinionFeedbackAction', ['corpid' => $corpid, 'user_id' => $userId, 'external_userid' => $externalUserId
  26. , 'type' => $type, 'content' => $content, 'attachments' => $attachments, 'blacklist_deal' => $userDetailDeal], 'interface');
  27. # 给反馈客户打标签
  28. RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_MARK_RDS, json_encode([
  29. 'corpid' => $corpid, 'user_id' => $userId, 'external_userid' => $externalUserId,
  30. 'type' => 2, 'tag_group_name' => '猎羽', 'tag_name_list' => ['投诉客户']
  31. ], 256));
  32. if($res) {
  33. # 发送短信提醒
  34. self::remindUser($corpid, $userId);
  35. return ['成功', 0];
  36. }
  37. return ['失败', 500];
  38. }
  39. public static function userOpinionFeedbackList($sysGroupId, $userId, $page, $pageSize) {
  40. # 查询当前登录账号的企微权限列表
  41. $corpIdList = AdminManageCorp::query()->where("is_delete",0)
  42. ->where("view_type",'helper')
  43. ->where("sys_user_id",$sysGroupId)
  44. ->pluck("corpid");
  45. $corpids = AuthorizeCorp::getCorpIdList($corpIdList);
  46. # 查询列表
  47. $params = ['corpid_list' => $corpids];
  48. $userSqlArr = [];
  49. if(!empty($userId) && is_array($userId)) {
  50. foreach($userId as $item) {
  51. $userSqlArr[] = "('" . $item['corpid'] . "','" . $item['user_id'] . "')";
  52. }
  53. }
  54. if(!empty($userSqlArr)) $params['user_sql'] = $userSqlArr;
  55. $query = UserOpinionFeedback::getDataQuery($params);
  56. # 记录条数
  57. $countQuery = clone $query;
  58. $count = $countQuery->count();
  59. # 数据格式化处理
  60. $list = $query->select(['id', 'corpid', 'user_id', 'external_userid', 'type', 'content', 'attachments', 'create_time'])
  61. ->orderBy('create_time', 'desc')->offset(($page - 1) * $pageSize)
  62. ->limit($pageSize)->get();
  63. $corpidList = array_unique(array_column($list->toArray(), 'corpid'));
  64. $corpDataList = AuthorizeCorp::getAllCorpList($corpidList);
  65. $userSql = [];
  66. foreach ($list as $item) {
  67. $key = $item->corpid . '#' . $item->user_id;
  68. $userSql[$key] = "('" . $item->corpid . "','" . $item->user_id . "')";
  69. }
  70. $userDataList = DjUser::getUserBySearch(['user_sql' => $userSql]);
  71. $complainTypeArr = config('wxad.complain');
  72. $typeList = [];
  73. foreach ($complainTypeArr['data'] as $item) {
  74. $typeList[$item['type']] = $item['title'];
  75. if(isset($item['child'])){
  76. foreach($item['child']['data'] as $i) {
  77. $pTitle = $typeList[$i['ptype']] ?? '';
  78. $typeList[$i['type']] = $pTitle.'--'.$i['title'];
  79. }
  80. }
  81. }
  82. foreach($list as $item) {
  83. # 企微名称
  84. $corpInfo = $corpDataList->where('corpid', $item->corpid)->first();
  85. $item->corp_name = $corpInfo->corp_name ?? null;
  86. # 客服名称
  87. $userInfo = $userDataList->where('corpid', $item->corpid)->where('user_id', $item->user_id)->first();
  88. $item->user_name = $userInfo->name ?? null;
  89. # 客户信息
  90. $customerInfo = Customer::getCustomerInfoByExternalUserId($item->corpid, $item->external_userid);
  91. $item->customer_name = $customerInfo->name ?? null;
  92. $item->customer_avatar = $customerInfo->avatar ?? null;
  93. # 反馈类型
  94. $item->type_title = $typeList[$item->type];
  95. # 附件
  96. $item->attachments = json_decode($item->attachments, 1);
  97. }
  98. return [$list, $count];
  99. }
  100. # 短信提醒
  101. public static function remindUser($corpid, $userId) {
  102. try{
  103. # 查询客服对应的预警人
  104. $groupIds = WarnGroupDjuser::getGroupIdByCorpUser($corpid, $userId);
  105. $groupIds = array_unique(array_column($groupIds->toArray(), 'group_id'));
  106. if(empty($groupIds)) {
  107. Log::logInfo('获取预警组为空', [
  108. 'type' => 'feed_back',
  109. 'res' => '没有待提醒的手机号码',
  110. 'corpId' => $corpid,
  111. 'userId' => $userId,
  112. ], 'feedBackRemindUser');
  113. return ;
  114. }
  115. $enableGroupIds = WarnGroup::query()->select(['id'])->whereIn('id', $groupIds)->where('enable', 1)
  116. ->get();
  117. $enableGroupIds = array_column($enableGroupIds->toArray(), 'id');
  118. if(empty($enableGroupIds)) {
  119. Log::logInfo('获取预警组为空', [
  120. 'type' => 'feed_back',
  121. 'res' => '没有待提醒的手机号码',
  122. 'corpId' => $corpid,
  123. 'userId' => $userId,
  124. ], 'feedBackRemindUser');
  125. return ;
  126. }
  127. // 获取预警人员
  128. $groupUserArr = WarnGroupUser::getUserList($enableGroupIds);
  129. // 提取手机号
  130. $phones = [];
  131. foreach($groupUserArr as $groupInfo) {
  132. foreach($groupInfo as $info) {
  133. if(!empty($info['user_phone']) && !in_array($info['user_phone'], $phones)) {
  134. $phones[] = $info['user_phone'];
  135. }
  136. }
  137. }
  138. if(empty($phones)) {
  139. Log::logInfo('短信发送结果', [
  140. 'type' => 'feed_back',
  141. 'res' => '没有待提醒的手机号码',
  142. 'corpId' => $corpid,
  143. 'userId' => $userId,
  144. ], 'feedBackRemindUser');
  145. return ;
  146. }
  147. // 发送短信
  148. $param['corp'] = AuthorizeCorp::query()->where('corpid', $corpid)->value('corp_name');
  149. $param['user'] = DjUser::query()->where('corpid', $corpid)->where('user_id', $userId)
  150. ->value('name');
  151. foreach ($phones as $phone) {
  152. $res = YPSMS::warn_check_rule($phone, $param, 'feed_back');
  153. Log::logInfo('短信发送结果', [
  154. 'type' => 'feed_back',
  155. 'res' => $res,
  156. 'corpId' => $corpid,
  157. 'userId' => $userId,
  158. 'warnUserPhone' => $phone,
  159. ], 'feedBackRemindUser');
  160. }
  161. } catch (\Exception $exception) {
  162. EmailQueue::rPush('客户意见反馈提醒客服异常', json_encode([
  163. 'corpid' => $corpid, 'user_id' => $userId,
  164. 'err_msg' => $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage()
  165. ]), ['song.shen@kuxuan-inc.com'], '猎羽');
  166. Log::logError('客户意见反馈提醒客服异常', [
  167. 'corpid' => $corpid, 'user_id' => $userId,
  168. 'err_msg' => $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage(),
  169. 'trace' => $exception->getTraceAsString()
  170. ], 'feedBackRemindUserError');
  171. }
  172. }
  173. }