企微短剧业务系统

AndroidToolService.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AndroidBindCorp;
  5. use App\Models\AuthorizeCorp;
  6. use App\Models\CustomerDetails;
  7. use App\Models\CustomerShareRelation;
  8. use App\Models\DjUser;
  9. use App\Models\Es\ChatGroupMember;
  10. use App\Models\ForwardChatGroupRule;
  11. use App\Models\ForwardMsgRecord;
  12. use App\Models\System\Users;
  13. use App\RedisModel;
  14. use App\Support\EmailQueue;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Mail;
  17. use Illuminate\Support\Facades\Storage;
  18. use App\Support\Email\PHPMailer;
  19. class AndroidToolService
  20. {
  21. CONST ANDROID_UPLOAD_DATA_STATUS = 'Playlet::androidUploadDataStatus';
  22. CONST ANDROID_LAST_UPLOAD_DATA = 'Playlet::androidLastUploadData';
  23. CONST FORWARD_CHAT_GROUP_COUNT = 'Playlet::forwardChatGroupCount';
  24. /**
  25. * 安卓设备绑定登录的企微以及对应客服
  26. * */
  27. public static function editCorpBind($title, $deviceId, $bindList, $adminId, $sysGroupId, $forwardStatus)
  28. {
  29. if(!is_array($bindList)) $bindList = json_decode($bindList, true);
  30. try {
  31. DB::beginTransaction();
  32. # 禁用当前设备绑定的所有企微信息
  33. AndroidBindCorp::where('device_id', $deviceId)->where('enable', 1)->update(['enable' => 0]);
  34. if(!empty($bindList)) {
  35. foreach ($bindList as $item) {
  36. $corpid = $item['corpid'] ?? '';
  37. $userId = $item['user_id'] ?? '';
  38. if(empty($corpid) || empty($userId)) {
  39. DB::rollback();
  40. return 2812;
  41. }
  42. AndroidBindCorp::updateOrCreate(['device_id' => $deviceId, 'corpid' => $corpid, 'user_id' => $userId], [
  43. 'enable' => 1, 'admin_id' => $adminId, 'title' => $title, 'sys_group_id' => $sysGroupId, 'forward_status' => $forwardStatus
  44. ]);
  45. }
  46. }
  47. DB::commit();
  48. } catch (\Exception $e) {
  49. DB::rollback();
  50. Log::logError('安卓设备绑定企微及客服流程发生异常', [
  51. 'title' => $title,
  52. 'device_id' => $deviceId,
  53. 'bind_list' => $bindList,
  54. 'line' => $e->getLine(),
  55. 'msg' => $e->getMessage()
  56. ], 'EditCorpBind');
  57. EmailQueue::rPush('安卓设备绑定企微及客服流程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  58. return 2813;
  59. }
  60. return 0;
  61. }
  62. /**
  63. * 安卓设备绑定信息详情
  64. * */
  65. public static function bindDetail($deviceId, $sysGroupId)
  66. {
  67. $bindList = AndroidBindCorp::select('corpid', 'user_id')->where('device_id', $deviceId)
  68. ->where('sys_group_id', $sysGroupId)->where('enable', 1)->get();
  69. $confInfo = AndroidBindCorp::where('device_id', $deviceId)
  70. ->where('sys_group_id', $sysGroupId)->where('enable', 1)->select(['title', 'forward_status'])->first();
  71. return [
  72. 'title' => $confInfo->title ?? '',
  73. 'device_id' => $deviceId,
  74. 'forward_status' => $confInfo->forward_status ?? 0,
  75. 'bind_list' => $bindList
  76. ];
  77. }
  78. /**
  79. * 获取安卓设备列表
  80. * */
  81. public static function getDeviceList($keyword, $corpid, $userId, $sysGroupId, $page, $pageSize)
  82. {
  83. list($deviceList, $count) = AndroidBindCorp::getDeviceList($keyword, $corpid, $userId, $sysGroupId, $page, $pageSize);
  84. $adminIds = $deviceList->pluck('admin_id');
  85. $adminData = Users::query()
  86. ->select(['id','name'])
  87. ->whereIn('id', $adminIds)
  88. ->get();
  89. foreach ($deviceList as $datum) {
  90. # 获取操作人信息
  91. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  92. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  93. }
  94. return [$deviceList, $count];
  95. }
  96. /**
  97. * 绑定好友共享的目标客服信息
  98. * */
  99. public static function takeoverUserBind($title, $corpid, $userId, $takeoverUsers, $adminId, $sysGroupId)
  100. {
  101. try {
  102. $configId = CustomerShareRelation::where('user_id', $userId)->where('corpid', $corpid)->value('id');
  103. # 共享目标客服关系绑定
  104. $errno = CustomerShareRelation::takeoverUserBind($title, $corpid, $userId, $takeoverUsers, $configId, $adminId, $sysGroupId);
  105. } catch (\Exception $e) {
  106. Log::logError('绑定好友共享关系流程发生异常', [
  107. 'line' => $e->getLine(),
  108. 'msg' => $e->getMessage()
  109. ], 'TakeoverUserBind');
  110. $errno = 2332;
  111. }
  112. return $errno;
  113. }
  114. /**
  115. * 共享的目标客服绑定关系列表
  116. * */
  117. public static function takeoverUserBindList($corpid, $userId, $page, $pageSize, $sysGroupId)
  118. {
  119. try {
  120. list($list, $count) = CustomerShareRelation::takeoverUserBindList($corpid, $userId, $page, $pageSize, $sysGroupId);
  121. if(!$count) return [[], 0];
  122. $userList = DjUser::select('user_id', 'name')->where('corpid', $corpid)->where('status', 1)->where('enable', 1)->get();
  123. foreach ($list as $item) {
  124. # 获取转移客服信息
  125. $handoverUser = $userList->where('user_id', $item->user_id)->first();
  126. $item->handover_user = $handoverUser->name ?? '-';
  127. # 获取接收客服信息
  128. $takeoverUserIdList = explode(',', $item->takeover_users);
  129. $takeOverUserList = '';
  130. foreach ($takeoverUserIdList as $datum) {
  131. $takeoverUser = $userList->where('user_id', $datum)->first();
  132. $takeOverUserList .= ($takeoverUser->name ?? '-') . ',';
  133. }
  134. $item->takeover_users_name = trim($takeOverUserList, ',');
  135. }
  136. } catch (\Exception $e) {
  137. Log::logError('共享的目标客服绑定关系列表获取发生异常', [
  138. 'line' => $e->getLine(),
  139. 'msg' => $e->getMessage()
  140. ], 'TakeoverUserBindList');
  141. return [[], 0];
  142. }
  143. return [$list, $count];
  144. }
  145. /**
  146. * 修改共享配置状态
  147. * */
  148. public static function shareConfigStatusChange($corpid, $userId, $status, $sysGroupId, $adminId)
  149. {
  150. # 校验规则是否存在
  151. $configInfo = CustomerShareRelation::where('sys_group_id', $sysGroupId)->where('corpid', $corpid)
  152. ->where('user_id', $userId)
  153. ->first();
  154. if(empty($configInfo)) return 2330;
  155. if($configInfo->status == $status) return 2333;
  156. $configInfo->status = $status;
  157. $configInfo->admin_id = $adminId;
  158. $result = $configInfo->save();
  159. return $result ? 0 : 2334;
  160. }
  161. public static function getUploadDataStatus() {
  162. $status = RedisModel::get(self::ANDROID_UPLOAD_DATA_STATUS);
  163. if(empty($status)) {
  164. return 0;
  165. }
  166. return $status;
  167. }
  168. public static function getHistoryData($deviceId) {
  169. $data = RedisModel::hGet(self::ANDROID_LAST_UPLOAD_DATA, $deviceId);
  170. if(empty($data)) {
  171. return ['company' => '', 'date' => ''];
  172. } else {
  173. return json_decode($data, 1);
  174. }
  175. }
  176. public static function receiveUploadData($deviceId, $companyName, $userName, $endUpload, $oneways, &$errcode) {
  177. try {
  178. # 校验公司名称,并获取企业id
  179. $corpInfoList = AuthorizeCorp::getCorpInfoByName($companyName);
  180. if ($corpInfoList->isEmpty() || $corpInfoList->count() > 1) {
  181. Log::logError('安卓端上传单向好友异常', [
  182. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  183. 'end_upload' => $endUpload, 'one_ways' => $oneways, 'message' => '企业名称查询为空或者查询结果为多个',
  184. ], 'AndroidUploadDataError');
  185. $errcode = 6000;
  186. return;
  187. }
  188. $corpInfo = $corpInfoList->first();
  189. $corpid = $corpInfo->corpid ?? null;
  190. if (empty($corpid)) {
  191. Log::logError('安卓端上传单向好友异常', [
  192. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  193. 'end_upload' => $endUpload, 'one_ways' => $oneways, 'corp_info' => $corpInfo, 'message' => '企业id查询为空',
  194. ], 'AndroidUploadDataError');
  195. $errcode = 6002;
  196. return;
  197. }
  198. # 校验客服名称,并获取客服user_id
  199. $userInfoList = DjUser::getUserBySearch(['user_name' => $userName, 'corpid' => $corpid]);
  200. if ($userInfoList->isEmpty() || $userInfoList->count() > 1) {
  201. Log::logError('安卓端上传单向好友异常', [
  202. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  203. 'end_upload' => $endUpload, 'one_ways' => $oneways, 'message' => '客服名称查询为空或者查询结果为多个',
  204. ], 'AndroidUploadDataError');
  205. $errcode = 6001;
  206. return;
  207. }
  208. $userInfo = $userInfoList->first();
  209. $userId = $userInfo->user_id ?? null;
  210. if (empty($userId)) {
  211. Log::logError('安卓端上传单向好友异常', [
  212. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  213. 'end_upload' => $endUpload, 'one_ways' => $oneways, 'corp_id' => $corpid, 'user_info' => $userInfo,
  214. 'message' => '根据客服名称查询客服id结果为空',
  215. ], 'AndroidUploadDataError');
  216. $errcode = 6002;
  217. return;
  218. }
  219. # 根据添加日期以及客户名称尝试获取客户的外部联系人id
  220. $externalUserIdArr = [];
  221. $date = null;
  222. $oneways = json_decode($oneways, 1);
  223. foreach ($oneways as $values) {
  224. $externalUserInfoList = CustomerDetails::getCustomerDetailsByName($corpid, $userId, $values);
  225. if (is_null($externalUserInfoList) || $externalUserInfoList->isEmpty() || $externalUserInfoList->count() > 1) {
  226. Log::logError('安卓端上传单向好友异常', [
  227. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  228. 'end_upload' => $endUpload, 'one_ways' => $oneways, 'corp_id' => $corpid, 'user_info' => $userInfo,
  229. 'message' => '根据客户名称查询客户id结果为空',
  230. ], 'AndroidUploadDataError');
  231. continue;
  232. }
  233. $date = $values['date'];
  234. $externalUserInfo = $externalUserInfoList->first();
  235. $externalUserId = $externalUserInfo->external_userid ?? null;
  236. if (!empty($externalUserId)) $externalUserIdArr[] = $externalUserId;
  237. }
  238. # 批量更新客户状态
  239. $updateData = [
  240. 'enable' => 1,
  241. 'loss_status' => 0,
  242. 'loss_time' => date('Y-m-d H:i:s')
  243. ];
  244. if (!empty($externalUserIdArr)) {
  245. CustomerDetails::updateData($corpid, $userId, $externalUserIdArr, $updateData);
  246. }
  247. # 保存完成后,更新缓存中的信息
  248. RedisModel::hSet(self::ANDROID_LAST_UPLOAD_DATA, $deviceId, json_encode(['company' => $companyName, 'date' => $date], 256));
  249. } catch (\Exception $exception) {
  250. Log::logError('安卓端上传单向好友程序异常', [
  251. 'device_id' => $deviceId, 'company_name' => $companyName, 'user_name' => $userName,
  252. 'end_upload' => $endUpload, 'one_ways' => $oneways,
  253. 'message' => $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage(),
  254. 'trace' => $exception->getTraceAsString()
  255. ], 'AndroidUploadDataError');
  256. }
  257. }
  258. public static function sendLogEmail($uploadFiles, $subject, $userName, $corpName, $deviceId, $msgType, $timeInterval, $description) {
  259. $localPathArr = [];
  260. # 接收上传文件
  261. if(!empty($uploadFiles) && is_array($uploadFiles)) {
  262. foreach ($uploadFiles as $uploadFile) {
  263. if (empty($uploadFile) || !$uploadFile->isValid()) {
  264. return 2311;
  265. }
  266. $realPath = $uploadFile->getRealPath(); //临时文件的绝对路径
  267. $filename = $uploadFile->getClientOriginalName();
  268. // 使用我们新建的uploads本地存储空间(目录)
  269. $result = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
  270. if(!$result)
  271. return 2304;
  272. $localPathArr[] = storage_path('uploads').'/'.$filename;
  273. }
  274. }
  275. // $address = 'kaijia.li@kuxuan-inc.com';
  276. $address = 'song.shen@kuxuan-inc.com';
  277. $content = [
  278. '客服名称' => $userName,
  279. '主体名称' => $corpName,
  280. '设备号' => $deviceId,
  281. '问题类型' => $msgType,
  282. '时间节点' => $timeInterval,
  283. '问题描述' => $description,
  284. ];
  285. # 邮件发送以及删除本地文件
  286. if(!empty($localPathArr)) {
  287. $res = Mail::send('emails.log', ['content' => $content,], function ($email) use ($address, $subject, $localPathArr){
  288. $email->to($address)->subject($subject);
  289. foreach($localPathArr as $localPath) {
  290. $email->attach($localPath);
  291. }
  292. });
  293. foreach ($localPathArr as $localPath) {
  294. unlink($localPath);
  295. }
  296. return $res;
  297. }
  298. }
  299. public static function getForwardMemberList($sysGroupId, $keyword) {
  300. $bindList = AndroidBindCorp::select('corpid', 'user_id')->where('forward_status', 1)
  301. ->where('sys_group_id', $sysGroupId)->where('enable', 1)->get();
  302. if($bindList->isEmpty()) return [];
  303. $userSql = [];
  304. $corpData = [];
  305. foreach($bindList as $bindUser) {
  306. $userSql[] = "('".$bindUser->corpid."','".$bindUser->user_id."')";
  307. $corpData[] = $bindUser->corpid;
  308. }
  309. $accountUserDataList = DjUser::query()->where('enable', 1)->where(function($query) use ($keyword) {
  310. if(!empty($keyword)) $query->where('name', 'like', '%'.$keyword.'%');
  311. })->where('status', 1)
  312. ->whereRaw("(`corpid`, `user_id`) in (" . implode(",", $userSql).")")->get();
  313. $corpDataList = AuthorizeCorp::query()->whereIn('corpid', $corpData)->where('enable', 1)
  314. ->get();
  315. foreach($bindList as $key => $bindUser) {
  316. $userInfo = $accountUserDataList->where('corpid', $bindUser->corpid)->where('user_id', $bindUser->user_id)->first();
  317. $bindUser->user_name = $userInfo->name ?? null;
  318. $bindUser->system_user_id = $userInfo->id ?? null;
  319. $corpInfo = $corpDataList->where('corpid', $bindUser->corpid)->first();
  320. $bindUser->corp_name = $corpInfo->corp_name ?? '';
  321. if(empty($bindUser->user_name)) unset($bindList[$key]);
  322. }
  323. return $bindList;
  324. }
  325. public static function waitCreate($deviceId) {
  326. # 查询设备对应的企微及客服信息
  327. $userList = AndroidBindCorp::select('corpid', 'user_id', 'forward_status')->where('device_id', $deviceId)
  328. ->where('enable', 1)->get();
  329. if(empty($userList)) {
  330. Log::logError('此设备号未绑定任何企微信息', [
  331. 'device_id' => $deviceId
  332. ], 'waitCreate');
  333. return [];
  334. }
  335. # 查询支持无障碍模式的企微列表
  336. $corpIds = $userList->pluck('corpid');
  337. $corpList = AuthorizeCorp::select('corpid', 'corp_name')->whereIn('corpid', $corpIds)->where('enable', 1)->get();
  338. $data = [];
  339. $forwardStatusData = [];
  340. # 查询企微和客服是否配置了邀请入群功能
  341. foreach ($userList as $userInfo) {
  342. $corpid = $userInfo->corpid ?? null;
  343. $userId = $userInfo->user_id ?? null;
  344. if(!$corpid || !$userId) {
  345. Log::logError('设备绑定客服基本信息数据异常', [
  346. 'user_info' => $userInfo->toArray(),
  347. 'device_id' => $deviceId
  348. ], 'waitCreate');
  349. continue;
  350. }
  351. $corpInfo = $corpList->where('corpid', $corpid)->first();
  352. $corpName = $corpInfo->corp_name ?? null;
  353. if(is_null($corpName)) {
  354. Log::logError('企微名称获取为空', [
  355. 'device_id' => $deviceId,
  356. 'corpid' => $corpid,
  357. 'userId' => $userId,
  358. ], 'waitCreate');
  359. continue;
  360. }
  361. $forwardStatusData[] = [
  362. 'companyName' => $corpName,
  363. 'corpid' => $corpid,
  364. 'forward_status' => $userInfo->forward_status,
  365. ];
  366. # 使用更新时间最近的配置
  367. $inviteConfig = ForwardChatGroupRule::query()->where('corpid', $corpid)
  368. ->whereRaw("FIND_IN_SET('".$userId."', `users`)")
  369. ->where('status', 1)->where('enable', 1)
  370. ->orderBy('updated_at', 'desc')
  371. ->first();
  372. if(empty($inviteConfig)) {
  373. $inviteConfig = ForwardChatGroupRule::query()->where('corpid', $corpid)
  374. ->where('is_for_all', 1)
  375. ->where('enable', 1)->where('status', 1)
  376. ->orderBy('updated_at', 'desc')
  377. ->first();
  378. }
  379. if(empty($inviteConfig)) {
  380. Log::logError('该企微未配置创建转发消息群聊规则', [
  381. 'user_info' => $userInfo->toArray(),
  382. 'device_id' => $deviceId
  383. ], 'waitCreate');
  384. continue;
  385. }
  386. if($inviteConfig->create_type == 2 && strtotime($inviteConfig->do_time) > time()) {
  387. Log::logError('未到执行时间', [
  388. 'user_info' => $userInfo->toArray(),
  389. 'device_id' => $deviceId,
  390. 'config_info' => $inviteConfig->toArray()
  391. ], 'waitCreate');
  392. continue;
  393. }
  394. # 满足邀请入群时间,在执行完成后将规则状态改为关闭
  395. // if('9d8eea27-88a5-36ae-ab53-b6baad22ab5a' != $deviceId) {
  396. ForwardChatGroupRule::query()->where('id', $inviteConfig->id)->update(['status' => 0]);
  397. // }
  398. # 规则ID
  399. $ruleId = $inviteConfig->id;
  400. $ruleInfo = ForwardChatGroupRule::getInfo($ruleId);
  401. $forwardUserList = explode(',', $ruleInfo->forward_users);
  402. $forwardUserData = [];
  403. # 查询客服信息
  404. $userList = DjUser::getUserBySearch(['system_user_ids' => $forwardUserList]);
  405. $corpidList = $userList->isNotEmpty() ? array_unique(array_column($userList->toArray(), 'corpid')) : [];
  406. $corpList = AuthorizeCorp::getCorpInfoByCorpId($corpidList);
  407. foreach ($forwardUserList as $systemUserId) {
  408. $userInfo = $userList->where('id', $systemUserId)->first();
  409. $userCorpId = $userInfo->corpid ?? null;
  410. $corpInfo = $corpList->where('corpid', $userCorpId)->first();
  411. if($userInfo->user_id == $userId && $userCorpId == $corpid) {
  412. # 当群主和转发客服为同一个人的时候跳过不处理
  413. continue;
  414. }
  415. $forwardUserData[] = [
  416. 'corpid' => $userCorpId,
  417. 'corp_name' => $corpInfo->corp_name ?? '',
  418. 'user_id' => $userInfo->user_id ?? '',
  419. 'user_name' => $userInfo->name ?? '',
  420. 'user_alias' => $userInfo->alias ?? '',
  421. 'system_user_id' => $systemUserId,
  422. ];
  423. }
  424. $data[] = [
  425. 'companyName' => $corpName,
  426. 'corpid' => $corpid,
  427. 'group_name' => $ruleInfo->template_group_name ?? '',
  428. 'group_num' => $ruleInfo->group_num ?? 10,
  429. 'forward_user_list' => $forwardUserData,
  430. ];
  431. }
  432. return [
  433. 'create_task_list' => $data,
  434. 'forward_status_list' => $forwardStatusData
  435. ];
  436. }
  437. public static function forwardMessageRecord($deviceId, $requestId, $userName, $corpName, $chatList, $message, $messageFrom)
  438. {
  439. $chatCount = count(explode(",", $chatList));
  440. # 获取缓存中的转发群聊个数
  441. $totalChatCount = RedisModel::get(self::FORWARD_CHAT_GROUP_COUNT.'-'.$requestId);
  442. if(!empty($totalChatCount)) {
  443. $newTotalChatCount = $totalChatCount + $chatCount;
  444. } else {
  445. $newTotalChatCount = $chatCount;
  446. }
  447. RedisModel::set(self::FORWARD_CHAT_GROUP_COUNT.'-'.$requestId, $newTotalChatCount);
  448. RedisModel::expire(self::FORWARD_CHAT_GROUP_COUNT.'-'.$requestId, 86400);
  449. # 查询企微
  450. $corpid = AuthorizeCorp::where('corp_name', $corpName)->where('enable', 1)->value('corpid');
  451. Log::logInfo('查询到的企微id:', [
  452. 'corpid' => $corpid
  453. ], 'debug');
  454. if(!$corpid) {
  455. EmailQueue::rPush('转发日志中的企微不存在于系统', $corpName, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  456. Log::logError('转发日志中的企微不存在于系统', [
  457. 'device_id' => $deviceId, 'request_id' => $requestId, 'user_name' => $userName, 'corpName' => $corpName,
  458. 'chat_list' => $chatList, 'message' => $message, 'message_from' => $messageFrom
  459. ], 'ForwardMessageRecord');
  460. return false;
  461. }
  462. # 使用deviceId查询绑定的客服信息
  463. $userInfo = AndroidBindCorp::getBindUserByDeviceId($deviceId, $corpid);
  464. Log::logInfo('查询到的客服信息:', [
  465. 'user_info' => $userInfo
  466. ], 'debug');
  467. if(empty($userInfo)) {
  468. // EmailQueue::rPush('未查询到该设备有对应的企微成员绑定信息', $corpName . '-' . $corpid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  469. Log::logError('未查询到该设备有对应的企微成员绑定信息', [
  470. 'device_id' => $deviceId, 'request_id' => $requestId, 'user_name' => $userName, 'corpName' => $corpName,
  471. 'chat_list' => $chatList, 'message' => $message, 'message_from' => $messageFrom, 'corpid' => $corpid
  472. ], 'ForwardMessageRecord');
  473. return false;
  474. }
  475. # 查询已有记录信息
  476. $recordInfo = ForwardMsgRecord::getRecordDetail($deviceId, $requestId);
  477. Log::logInfo('查询到已有的记录:', [
  478. 'record_info' => $recordInfo
  479. ], 'debug');
  480. $timeInterval = '';
  481. if(!empty($recordInfo)) {
  482. $timeInterval = self::getForwardTimeInterval($recordInfo->time_interval, $recordInfo->updated_at);
  483. Log::logInfo('时间间隔信息:', [
  484. 'time_interval' => $timeInterval
  485. ], 'debug');
  486. }
  487. $chatGroupTotal = ChatGroupMember::getGroupCountByUserName($userName);
  488. $insertData = [
  489. 'time_interval' => $timeInterval, 'chat_group_total' => $chatGroupTotal, 'forward_total' => $newTotalChatCount
  490. ];
  491. $rst = ForwardMsgRecord::updateOrCreate([
  492. 'record_id' => $requestId, 'device_id' => $deviceId, 'corpid' => $corpid, 'user_id' => $userInfo->user_id
  493. ], $insertData);
  494. Log::logInfo('模拟点击工具转发消息记录', [
  495. 'device_id' => $deviceId, 'request_id' => $requestId, 'user_name' => $userName, 'corp_name' => $corpName,
  496. 'chat_list' => $chatList, 'message' => $message, 'message_from' => $messageFrom, 'chat_count' => $chatCount,
  497. 'total_chat_count' => $newTotalChatCount, 'insert_data' => $insertData, 'rst' => $rst
  498. ], 'forwardMessageRecord');
  499. return $rst;
  500. }
  501. public static function getForwardTimeInterval($timeIntervalOld, $lastForwardTime)
  502. {
  503. $second = time() - strtotime($lastForwardTime);
  504. $timeIntervalOld = $timeIntervalOld ? $timeIntervalOld . ',' . $second : $second;
  505. $timeIntervalOldArr = explode(',', $timeIntervalOld);
  506. if(count($timeIntervalOldArr) > 10) {
  507. $timeIntervalOldArr = array_slice($timeIntervalOldArr, '-10');
  508. }
  509. return trim(implode(',', $timeIntervalOldArr), ',');
  510. }
  511. public static function getForwardMsgRecordList($corpid, $page, $pageSize)
  512. {
  513. list($list, $count) = ForwardMsgRecord::getForwardMsgRecordList($corpid, $page, $pageSize);
  514. if(!$count) return [[], 0];
  515. # 获取设备信息
  516. $deviceIds = $list->pluck('device_id');
  517. $deviceConfigList = AndroidBindCorp::select('device_id', 'title')->whereIn('device_id', $deviceIds)->where('corpid', $corpid)->get();
  518. # 获取企微信息
  519. $corpName = AuthorizeCorp::where('corpid', $corpid)->value('corp_name');
  520. # 获取客服信息
  521. $userIds = $list->pluck('user_id');
  522. $userList = DjUser::select('user_id', 'name')->whereIn('user_id', $userIds)->where('corpid', $corpid)->get();
  523. foreach ($list as $item) {
  524. # 设备配置信息
  525. $deviceInfo = $deviceConfigList->where('device_id', $item->device_id)->first();
  526. $item->device_title = $deviceInfo->title;
  527. # 企微信息
  528. $item->corp_name = $corpName;
  529. # 成员信息
  530. $userInfo = $userList->where('user_id', $item->user_id)->first();
  531. $item->user_name = $userInfo->name ?? '';
  532. }
  533. return [$list, $count];
  534. }
  535. }