|
@@ -0,0 +1,87 @@
|
|
|
+package com.fjhx.email.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.io.IORuntimeException;
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import com.fjhx.email.config.base.R;
|
|
|
+import com.fjhx.email.config.exception.ServiceException;
|
|
|
+import com.fjhx.email.entity.dto.MailSyncInfo;
|
|
|
+import com.fjhx.email.entity.dto.MailboxInfo;
|
|
|
+import com.fjhx.email.service.IMailService;
|
|
|
+import com.fjhx.email.utils.HxHttpUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/mailService")
|
|
|
+public class MailController {
|
|
|
+
|
|
|
+ private static final Object syncLock = new Object();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IMailService mailService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每小时同步一次在线用户
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0 0/1 * * ?")
|
|
|
+ public void checkStart() {
|
|
|
+ try {
|
|
|
+ synchronized (syncLock) {
|
|
|
+ List<Long> onlineUserIdList = HxHttpUtil.getOnlineUserIdList();
|
|
|
+ MailSyncInfo.mailboxInfoList = mailService.getMailboxInfoListByUserId(onlineUserIdList);
|
|
|
+ }
|
|
|
+ } catch (IORuntimeException e) {
|
|
|
+ log.error("获取主服务在线用户超时");
|
|
|
+ ThreadUtil.sleep(1000 * 5);
|
|
|
+ checkStart();
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ log.error("主服务返回信息错误:{}", e.getMessage());
|
|
|
+ ThreadUtil.sleep(1000 * 5);
|
|
|
+ checkStart();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("解析错误", e);
|
|
|
+ ThreadUtil.sleep(1000 * 5);
|
|
|
+ checkStart();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/userLogin/{userId}")
|
|
|
+ public R userLogin(@PathVariable("userId") Long userId) {
|
|
|
+ try {
|
|
|
+ synchronized (syncLock) {
|
|
|
+
|
|
|
+ List<MailboxInfo> mailboxInfoList = mailService.getMailboxInfoListByUserId(Collections.singletonList(userId));
|
|
|
+
|
|
|
+ if (mailboxInfoList.size() == 0) {
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Long> mailBoxInfoIdList = MailSyncInfo.mailboxInfoList.stream()
|
|
|
+ .map(MailboxInfo::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (MailboxInfo mailboxInfo : mailboxInfoList) {
|
|
|
+ if (!mailBoxInfoIdList.contains(mailboxInfo.getId())) {
|
|
|
+ MailSyncInfo.mailboxInfoList.add(mailboxInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("登录信息异常", e);
|
|
|
+ return userLogin(userId);
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|