24282 2 years ago
parent
commit
0f9305b982

+ 20 - 10
src/main/java/com/fjhx/email/controller/MailController.java

@@ -6,10 +6,7 @@ import com.fjhx.email.config.base.R;
 import com.fjhx.email.config.exception.ServiceException;
 import com.fjhx.email.entity.EnterpriseMailbox;
 import com.fjhx.email.entity.PersonalMailbox;
-import com.fjhx.email.entity.dto.GetMessagePageDto;
-import com.fjhx.email.entity.dto.MailSyncInfo;
-import com.fjhx.email.entity.dto.MailboxInfo;
-import com.fjhx.email.entity.dto.SendDto;
+import com.fjhx.email.entity.dto.*;
 import com.fjhx.email.entity.vo.MessageDetailVo;
 import com.fjhx.email.entity.vo.MessageVo;
 import com.fjhx.email.service.IEnterpriseMailboxService;
@@ -23,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.web.bind.annotation.*;
 
+import javax.mail.MessagingException;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -49,7 +47,7 @@ public class MailController {
         try {
             synchronized (this) {
                 List<Long> onlineUserIdList = HxHttpUtil.getOnlineUserIdList();
-                MailSyncInfo.mailboxInfoList = mailService.getMailboxInfoListByUserId(onlineUserIdList);
+                MailSyncInfo.mailboxInfoList = mailService.getMailboxInfoListByUserId(onlineUserIdList, MailSyncInfo.mailType);
             }
         } catch (IORuntimeException e) {
             log.error("获取主服务在线用户超时");
@@ -70,7 +68,7 @@ public class MailController {
         try {
             synchronized (this) {
 
-                List<MailboxInfo> mailboxInfoList = mailService.getMailboxInfoListByUserId(Collections.singletonList(userId));
+                List<MailboxInfo> mailboxInfoList = mailService.getMailboxInfoListByUserId(Collections.singletonList(userId), MailSyncInfo.mailType);
 
                 if (mailboxInfoList.size() == 0) {
                     return R.ok();
@@ -98,7 +96,7 @@ public class MailController {
      */
     @GetMapping("/getUserEmailList/{userId}")
     public R getUserEmailList(@PathVariable("userId") Long userId) {
-        List<MailboxInfo> mailboxInfoList = mailService.getMailboxInfoListByUserId(Collections.singletonList(userId));
+        List<MailboxInfo> mailboxInfoList = mailService.getMailboxInfoListByUserId(Collections.singletonList(userId), null);
 
         // 赋值文件夹未读邮件数量
         mailService.setUnreadMessageCount(mailboxInfoList);
@@ -134,7 +132,7 @@ public class MailController {
             SmtpUtil.sendMail(host, user, password, sendDto);
         } catch (Exception e) {
             log.error("发送邮件失败:", e);
-            throw new ServiceException("发送邮件失败");
+            throw new ServiceException("发送邮件失败:{}", e.getMessage());
         }
         return R.ok();
     }
@@ -142,7 +140,7 @@ public class MailController {
     /**
      * 添加企业邮箱
      */
-    @PostMapping("addEnterpriseMailbox")
+    @PostMapping("/addEnterpriseMailbox")
     public R addEnterpriseMailbox(@RequestBody EnterpriseMailbox enterpriseMailbox) {
         enterpriseMailboxService.add(enterpriseMailbox);
         userLogin(enterpriseMailbox.getUserId());
@@ -152,11 +150,23 @@ public class MailController {
     /**
      * 添加个人邮箱
      */
-    @PostMapping("addPersonalMailbox")
+    @PostMapping("/addPersonalMailbox")
     public R addPersonalMailbox(@RequestBody PersonalMailbox personalMailbox) {
         personalMailboxService.add(personalMailbox);
         userLogin(personalMailbox.getUserId());
         return R.ok();
     }
 
+    @PostMapping("/setSeen/{host}/{user}/{password}")
+    public R setSeen(@PathVariable("host") String host, @PathVariable("user") String user,
+                     @PathVariable("password") String password, @RequestBody SetSeenDto dto) {
+        try {
+            mailService.setSeen(host, user, password, dto);
+        } catch (MessagingException e) {
+            log.error("设置已读失败:", e);
+            throw new ServiceException("设置已读失败:{}", e.getMessage());
+        }
+        return R.ok();
+    }
+
 }

+ 5 - 0
src/main/java/com/fjhx/email/entity/dto/MailboxInfo.java

@@ -47,6 +47,11 @@ public class MailboxInfo {
     private String receiveProtocol;
 
     /**
+     * 地址类型 1国内 2国外
+     */
+    private Integer addressType;
+
+    /**
      * 文件夹
      */
     private List<MailFolderInfo> mailFolderInfoList;

+ 22 - 0
src/main/java/com/fjhx/email/entity/dto/SetSeenDto.java

@@ -0,0 +1,22 @@
+package com.fjhx.email.entity.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.List;
+
+@Getter
+@Setter
+public class SetSeenDto {
+
+    /**
+     * 1个人邮箱 2企业邮箱
+     */
+    private String folderName;
+
+    /**
+     * uid
+     */
+    private List<Long> uidList;
+
+}

+ 23 - 13
src/main/java/com/fjhx/email/mapper/xml/MailMapper.xml

@@ -13,6 +13,7 @@
         <result column="receive_host" property="receiveHost"/>
         <result column="receive_port" property="receivePort"/>
         <result column="receive_protocol" property="receiveProtocol"/>
+        <result column="address_type" property="addressType"/>
 
         <collection property="mailFolderInfoList"
                     autoMapping="true"
@@ -40,7 +41,8 @@
         folder_id,
         folder_name,
         last_uid,
-        last_received_date
+        last_received_date,
+        address_type
         from (
         (select 1 type,
         pm.id,
@@ -52,19 +54,23 @@
         pf.id folder_id,
         pf.name folder_name,
         pf.last_uid,
-        pf.last_received_date
+        pf.last_received_date,
+        pm.type address_type
         from personal_mailbox pm
         inner join personal_folder pf on pm.id = pf.mailbox_id
-        where pm.status = 1
+        where
+        pm.del_flag = 0
+        and pf.del_flag = 0
+        and pm.status = 1
         and pm.sync_status = 1
-        and pm.type = #{mailType}
-        and pm.del_flag = 0
+        and pf.sync_status = 1
+        <if test="mailType neq null">
+            and pm.type = #{mailType}
+        </if>
         and pm.user_id in
         <foreach collection="userIdList" item="userId" open="(" separator="," close=")">
             #{userId}
         </foreach>
-        and pf.sync_status = 1
-        and pf.del_flag = 0
         )
         union all
         (select 2 type,
@@ -77,21 +83,25 @@
         ef.id folder_id,
         ef.name folder_name,
         ef.last_uid,
-        ef.last_received_date
+        ef.last_received_date,
+        ed.type address_type
         from enterprise_domain ed
         inner join enterprise_mailbox em on ed.id = em.domain_id
         inner join enterprise_folder ef on em.id = ef.mailbox_id
-        where ed.type = #{mailType}
-        and ed.del_flag = 0
+        where
+        ed.del_flag = 0
+        and em.del_flag = 0
+        and ef.del_flag = 0
         and em.status = 1
         and em.sync_status = 1
-        and em.del_flag = 0
+        and ef.sync_status = 1
+        <if test="mailType neq null">
+            and ed.type = #{mailType}
+        </if>
         and em.user_id in
         <foreach collection="userIdList" item="userId" open="(" separator="," close=")">
             #{userId}
         </foreach>
-        and ef.sync_status = 1
-        and ef.del_flag = 0
         )
         ) t
         ORDER BY t.id, t.folder_id DESC

+ 8 - 1
src/main/java/com/fjhx/email/service/IMailService.java

@@ -2,10 +2,12 @@ package com.fjhx.email.service;
 
 import com.fjhx.email.entity.dto.GetMessagePageDto;
 import com.fjhx.email.entity.dto.MailboxInfo;
+import com.fjhx.email.entity.dto.SetSeenDto;
 import com.fjhx.email.entity.vo.MessageDetailVo;
 import com.fjhx.email.entity.vo.MessageVo;
 import com.fjhx.email.utils.PageWrapper;
 
+import javax.mail.MessagingException;
 import java.util.List;
 
 public interface IMailService {
@@ -13,7 +15,7 @@ public interface IMailService {
     /**
      * 获取邮箱信息
      */
-    List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList);
+    List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList, Integer mailType);
 
     /**
      * 获取用户邮箱列表
@@ -30,4 +32,9 @@ public interface IMailService {
      */
     void setUnreadMessageCount(List<MailboxInfo> mailboxInfoList);
 
+    /**
+     * 设置已读
+     */
+    void setSeen(String host, String user, String password, SetSeenDto dto) throws MessagingException;
+
 }

+ 1 - 1
src/main/java/com/fjhx/email/service/impl/CoreServiceImpl.java

@@ -100,7 +100,7 @@ public class CoreServiceImpl implements ApplicationRunner {
 
         try {
             List<Long> onlineUserIdList = HxHttpUtil.getOnlineUserIdList();
-            MailSyncInfo.mailboxInfoList = mailService.getMailboxInfoListByUserId(onlineUserIdList);
+            MailSyncInfo.mailboxInfoList = mailService.getMailboxInfoListByUserId(onlineUserIdList, MailSyncInfo.mailType);
         } catch (IORuntimeException e) {
             log.error("获取主服务在线用户超时");
             ThreadUtil.sleep(1000 * 5);

+ 25 - 8
src/main/java/com/fjhx/email/service/impl/MailServiceImpl.java

@@ -10,8 +10,8 @@ import com.fjhx.email.config.exception.ServiceException;
 import com.fjhx.email.entity.*;
 import com.fjhx.email.entity.dto.GetMessagePageDto;
 import com.fjhx.email.entity.dto.MailFolderInfo;
-import com.fjhx.email.entity.dto.MailSyncInfo;
 import com.fjhx.email.entity.dto.MailboxInfo;
+import com.fjhx.email.entity.dto.SetSeenDto;
 import com.fjhx.email.entity.vo.MessageDetailVo;
 import com.fjhx.email.entity.vo.MessageVo;
 import com.fjhx.email.mapper.MailMapper;
@@ -32,10 +32,7 @@ import org.springframework.transaction.PlatformTransactionManager;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionStatus;
 
-import javax.mail.Folder;
-import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.Part;
+import javax.mail.*;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -95,12 +92,11 @@ public class MailServiceImpl implements IMailService {
     private IEnterpriseFolderService enterpriseFolderService;
 
     @Override
-    public List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList) {
+    public List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList, Integer mailType) {
         if (ObjectUtil.isEmpty(userIdList)) {
             return Collections.emptyList();
         }
-
-        return mailMapper.getMailboxInfoListByUserId(userIdList, MailSyncInfo.mailType);
+        return mailMapper.getMailboxInfoListByUserId(userIdList, mailType);
     }
 
     @SneakyThrows
@@ -348,6 +344,27 @@ public class MailServiceImpl implements IMailService {
 
     }
 
+    @Override
+    public void setSeen(String host, String user, String password, SetSeenDto dto) throws MessagingException {
+        IMAPStore store = ImapUtil.getStore(host, user, password);
+        IMAPFolder folder = (IMAPFolder) store.getFolder(dto.getFolderName());
+        folder.open(Folder.READ_WRITE);
+
+        List<Long> uidList = dto.getUidList();
+
+        long[] uids = new long[uidList.size()];
+        for (int i = 0; i < uidList.size(); i++) {
+            uids[i] = uidList.get(i);
+        }
+
+        Message[] messages = folder.getMessagesByUID(uids);
+
+        Flags flags = new Flags();
+        flags.add(Flags.Flag.SEEN);
+
+        folder.setFlags(messages, flags, true);
+    }
+
     /**
      * 保存内容
      */