24282 2 jaren geleden
bovenliggende
commit
84abbae9cd

+ 31 - 0
pom.xml

@@ -82,5 +82,36 @@
 
     </dependencies>
 
+    <build>
+        <finalName>${project.artifactId}</finalName>
+        <plugins>
+            <!-- springBoot plugin -->
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+            <!-- 指定java版本 -->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <encoding>UTF-8</encoding>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+        <resources>
+            <resource>
+                <directory>src/main/java</directory>
+                <includes>
+                    <include>com/fjhx/email/mapper/xml/*.xml</include>
+                </includes>
+            </resource>
+            <resource>
+                <directory>src/main/resources</directory>
+            </resource>
+        </resources>
+    </build>
 
 </project>

+ 4 - 10
src/main/java/com/fjhx/email/config/base/R.java

@@ -9,14 +9,11 @@ import lombok.Data;
 @Data
 public class R {
 
-    // 是否成功
-    private Boolean success;
-
     // 返回码
     private Integer code;
 
     // 返回消息
-    private String message;
+    private String msg;
 
     // 返回数据
     private Object data;
@@ -24,9 +21,8 @@ public class R {
     // 成功
     public static R ok() {
         R r = new R();
-        r.setSuccess(true);
         r.setCode(HttpStatus.HTTP_OK);
-        r.setMessage("成功");
+        r.setMsg("成功");
         return r;
     }
 
@@ -40,18 +36,16 @@ public class R {
     // 失败
     public static R error(String errorMsg) {
         R r = new R();
-        r.setSuccess(false);
         r.setCode(HttpStatus.HTTP_BAD_REQUEST);
-        r.setMessage(errorMsg);
+        r.setMsg(errorMsg);
         return r;
     }
 
     // 失败
     public static R error(Integer errorCode, String errorMsg) {
         R r = new R();
-        r.setSuccess(false);
         r.setCode(errorCode);
-        r.setMessage(errorMsg);
+        r.setMsg(errorMsg);
         return r;
     }
 

+ 123 - 0
src/main/java/com/fjhx/email/config/mybatis/SqlLogInterceptor.java

@@ -0,0 +1,123 @@
+package com.fjhx.email.config.mybatis;
+
+import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.executor.statement.StatementHandler;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.ParameterMapping;
+import org.apache.ibatis.mapping.ParameterMode;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.plugin.Intercepts;
+import org.apache.ibatis.plugin.Invocation;
+import org.apache.ibatis.plugin.Signature;
+import org.apache.ibatis.reflection.MetaObject;
+import org.apache.ibatis.reflection.SystemMetaObject;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.type.TypeHandlerRegistry;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.sql.Statement;
+import java.text.DateFormat;
+import java.util.*;
+
+/**
+ * 打印执行的sql日志
+ */
+@Intercepts({
+        @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
+        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
+        @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})
+})
+@Slf4j
+@Component
+public class SqlLogInterceptor implements Interceptor {
+
+    @Value("${spring.profiles.active}")
+    private String active;
+
+    @Override
+    public Object intercept(Invocation invocation) throws Throwable {
+        if (!"dev".equals(active)) {
+            return invocation.proceed();
+        }
+
+        long start = System.currentTimeMillis();
+        Object result = invocation.proceed();
+        long end = System.currentTimeMillis();
+
+        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
+        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
+        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
+        BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
+        String sql = boundSql.getSql().replaceAll("\\s+", " ").toLowerCase();
+
+        List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings());
+        Object parameterObject = boundSql.getParameterObject();
+        if (parameterMappings.isEmpty() && parameterObject == null) {
+            log.warn("parameterMappings is empty or parameterObject is null");
+            return result;
+        }
+
+        Configuration configuration = mappedStatement.getConfiguration();
+        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
+
+        try {
+            String parameter = "";
+            MetaObject newMetaObject = configuration.newMetaObject(parameterObject);
+            for (ParameterMapping parameterMapping : parameterMappings) {
+                if (Objects.equals(parameterMapping.getMode(), ParameterMode.OUT)) {
+                    continue;
+                }
+                String propertyName = parameterMapping.getProperty();
+                if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
+                    parameter = getParameterValue(parameterObject);
+                } else if (newMetaObject.hasGetter(propertyName)) {
+                    parameter = getParameterValue(newMetaObject.getValue(propertyName));
+                } else if (boundSql.hasAdditionalParameter(propertyName)) {
+                    parameter = getParameterValue(boundSql.getAdditionalParameter(propertyName));
+                }
+
+                sql = sql.replaceFirst("\\?", parameter);
+            }
+
+            // 打印 sql
+            log.info("\n====================  Sql Start  ===================="
+                            + "\n mapper       : {}"
+                            + "\n execute sql  : {}"
+                            + "\n execute time : {} ms"
+                            + "\n====================  Sql  End   ====================\n",
+                    mappedStatement.getId(), sql, end - start);
+        } catch (Exception e) {
+            log.error("intercept sql error: {}", sql, e);
+        }
+
+        return result;
+    }
+
+    /**
+     * 获取参数
+     *
+     * @param obj Object类型参数
+     * @return 转换之后的参数
+     */
+    private String getParameterValue(Object obj) {
+        String value;
+        if (obj instanceof String) {
+            value = "'" + obj + "'";
+        } else if (obj instanceof Date) {
+            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
+            value = "'" + formatter.format(new Date()) + "'";
+        } else {
+            if (obj != null) {
+                value = obj.toString();
+            } else {
+                value = "";
+            }
+        }
+        return value.replace("$", "\\$");
+    }
+
+}

+ 9 - 3
src/main/java/com/fjhx/email/entity/EnterpriseMailbox.java

@@ -34,14 +34,19 @@ public class EnterpriseMailbox extends BaseEntity {
     private String mailPassword;
 
     /**
+     * 状态:1启用 0禁用
+     */
+    private Integer status;
+
+    /**
      * 同步邮件状态: 1同步 0不同步
      */
-    private Byte syncStatus;
+    private Integer syncStatus;
 
     /**
      * 默认邮箱: 1默认 0非默认
      */
-    private Byte defaultStatus;
+    private Integer defaultStatus;
 
     /**
      * 用户id
@@ -66,5 +71,6 @@ public class EnterpriseMailbox extends BaseEntity {
     /**
      * 逻辑删除 0未删除 1已删除
      */
-    private Byte delFlag;
+    private Integer delFlag;
+
 }

+ 12 - 0
src/main/java/com/fjhx/email/mapper/MailMapper.java

@@ -0,0 +1,12 @@
+package com.fjhx.email.mapper;
+
+import com.fjhx.email.entity.dto.MailboxInfo;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+public interface MailMapper {
+
+    List<MailboxInfo> getMailboxInfoListByUserId(@Param("userIdList") List<Long> userIdList, @Param("mailType") Integer mailType);
+
+}

+ 90 - 0
src/main/java/com/fjhx/email/mapper/xml/MailMapper.xml

@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fjhx.email.mapper.MailMapper">
+
+    <resultMap id="getMailboxInfoListByUserIdResultMap"
+               autoMapping="true"
+               type="com.fjhx.email.entity.dto.MailboxInfo">
+
+        <id column="id" property="id"/>
+        <result column="type" property="type"/>
+        <result column="skip" property="skip"/>
+        <result column="mail_user" property="mailUser"/>
+        <result column="mail_password" property="mailPassword"/>
+        <result column="receive_host" property="receiveHost"/>
+        <result column="receive_port" property="receivePort"/>
+        <result column="receive_protocol" property="receiveProtocol"/>
+
+        <collection property="mailFolderInfoList"
+                    autoMapping="true"
+                    ofType="com.fjhx.email.entity.dto.MailFolderInfo"
+                    javaType="java.util.List">
+
+            <id column="folder_id" property="id"/>
+            <result column="folder_name" property="name"/>
+            <result column="skip" property="skip"/>
+            <result column="last_message_number" property="lastMessageNumber"/>
+            <result column="last_received_date" property="lastReceivedDate"/>
+
+        </collection>
+
+    </resultMap>
+
+    <select id="getMailboxInfoListByUserId" resultMap="getMailboxInfoListByUserIdResultMap">
+        (select 1 type,
+        0 skip,
+        pm.id,
+        pm.mail_user,
+        pm.mail_password,
+        pm.receive_host,
+        pm.receive_port,
+        pm.receive_protocol,
+        pf.id folder_id,
+        pf.name folder_name,
+        pf.last_message_number,
+        pf.last_received_date
+        from personal_mailbox pm
+        inner join personal_folder pf on pm.id = pf.mailbox_id
+        where pm.status = 1
+        and pm.sync_status = 1
+        and pm.type = #{mailType}
+        and pm.del_flag = 0
+        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,
+        0 skip,
+        em.id,
+        em.mail_user_prefix + '@' + ed.domain_name mail_user,
+        em.mail_password,
+        ed.receive_host,
+        ed.receive_port,
+        ed.receive_protocol,
+        ef.id folder_id,
+        ef.name folder_name,
+        ef.last_message_number,
+        ef.last_received_date
+        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
+        and em.status = 1
+        and em.sync_status = 1
+        and em.del_flag = 0
+        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
+        )
+    </select>
+
+
+</mapper>

+ 14 - 0
src/main/java/com/fjhx/email/service/IMailService.java

@@ -0,0 +1,14 @@
+package com.fjhx.email.service;
+
+import com.fjhx.email.entity.dto.MailboxInfo;
+
+import java.util.List;
+
+public interface IMailService {
+
+    /**
+     * 获取邮箱信息
+     */
+    List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList);
+
+}

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

@@ -71,6 +71,9 @@ public class CoreServiceImpl implements ApplicationRunner {
     @Autowired
     private IEnterpriseMessageService enterpriseMessageService;
 
+    @Autowired
+    private IMailService mailService;
+
     /**
      * 累计同步次数
      */
@@ -96,7 +99,8 @@ public class CoreServiceImpl implements ApplicationRunner {
 
         try {
             List<Long> onlineUserIdList = HxHttpUtil.getOnlineUserIdList();
-
+            List<MailboxInfo> mailboxInfoListByUserId = mailService.getMailboxInfoListByUserId(onlineUserIdList);
+            MailSyncInfo.mailboxInfoList = mailboxInfoListByUserId;
         } catch (IORuntimeException e) {
             log.error("获取主服务在线用户超时");
             ThreadUtil.sleep(1000 * 5);
@@ -437,7 +441,7 @@ public class CoreServiceImpl implements ApplicationRunner {
                         personalMessage.setSendDate(mailInfo.getSendDate());
                         personalMessage.setContentSync(0);
                         personalMessage.setAddressSync(0);
-                        personalMessage.setAddressSync(0);
+                        personalMessage.setAttachmentSync(0);
                         return personalMessage;
                     }).collect(Collectors.toList());
                     personalMessageService.saveBatch(personalMessageList);
@@ -478,7 +482,7 @@ public class CoreServiceImpl implements ApplicationRunner {
                         enterpriseMessage.setSendDate(mailInfo.getSendDate());
                         enterpriseMessage.setContentSync(0);
                         enterpriseMessage.setAddressSync(0);
-                        enterpriseMessage.setAddressSync(0);
+                        enterpriseMessage.setAttachmentSync(0);
                         return enterpriseMessage;
                     }).collect(Collectors.toList());
                     enterpriseMessageService.saveBatch(enterpriseMessageList);

+ 23 - 0
src/main/java/com/fjhx/email/service/impl/MailServiceImpl.java

@@ -0,0 +1,23 @@
+package com.fjhx.email.service.impl;
+
+import com.fjhx.email.entity.dto.MailSyncInfo;
+import com.fjhx.email.entity.dto.MailboxInfo;
+import com.fjhx.email.mapper.MailMapper;
+import com.fjhx.email.service.IMailService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class MailServiceImpl implements IMailService {
+
+    @Autowired
+    private MailMapper mailMapper;
+
+    @Override
+    public List<MailboxInfo> getMailboxInfoListByUserId(List<Long> userIdList) {
+        return mailMapper.getMailboxInfoListByUserId(userIdList, MailSyncInfo.mailType);
+    }
+
+}

+ 0 - 1
src/main/java/com/fjhx/email/utils/EmailUtil.java

@@ -47,7 +47,6 @@ public class EmailUtil {
                 mailboxInfo.getReceivePort(),
                 mailboxInfo.getMailUser(),
                 mailboxInfo.getMailPassword());
-        store.connect();
         store.id(ImapStoreId);
         return store;
     }

+ 3 - 2
src/main/java/com/fjhx/email/utils/HxHttpUtil.java

@@ -8,6 +8,7 @@ import com.fjhx.email.config.exception.ServiceException;
 import com.fjhx.email.entity.dto.MailSyncInfo;
 
 import java.util.List;
+import java.util.Objects;
 
 public class HxHttpUtil {
 
@@ -18,8 +19,8 @@ public class HxHttpUtil {
         String url = MailSyncInfo.urlPrefix + "getOnlineUserIdList";
         String result = HttpUtil.get(url);
         R r = JSON.parseObject(result, R.class);
-        if (!r.getSuccess()) {
-            throw new ServiceException(r.getMessage());
+        if (!Objects.equals(r.getCode(), 200)) {
+            throw new ServiceException(r.getMsg());
         }
         return Convert.toList(Long.class, r.getData());
     }