初始化定时任务
continuous-integration/drone/push Build is passing Details

于相涌/mail
YXY 2 years ago
parent 83ff42c7a8
commit 592bf3dcbf

@ -13,7 +13,8 @@ public enum EventOperateType {
CREATE("CREATE", "创建"), CREATE("CREATE", "创建"),
UPDATE("UPDATE", "更新"), UPDATE("UPDATE", "更新"),
DELETE("DELETE", "删除"), DELETE("DELETE", "删除"),
STOP("STOP", "终止"); STOP("STOP", "终止"),
SYNC_MAIL("SYNC_MAIL", "同步邮件"),;
private final String code; private final String code;
private final String info; private final String info;

@ -12,7 +12,8 @@ public enum TableRelationTypeEnum {
*/ */
MAINTENANCE_ROW_RELATION("MAINTENANCE_ROW_RELATION", "维护表之间的字段关系"), MAINTENANCE_ROW_RELATION("MAINTENANCE_ROW_RELATION", "维护表之间的字段关系"),
SINGLE_DIRECTION("SINGLE_DIRECTION", "单向传递"), SINGLE_DIRECTION("SINGLE_DIRECTION", "单向传递"),
DOUBLE_DIRECTION("DOUBLE_DIRECTION", "双向传递"); DOUBLE_DIRECTION("DOUBLE_DIRECTION", "双向传递"),
SYNC_EMAIL("SYNC_EMAIL", "同步邮箱"),;
private String code; private String code;
private String info; private String info;

@ -60,7 +60,7 @@
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId> <artifactId>javax.mail</artifactId>
<version>1.5.5</version> <version>1.6.2</version>
</dependency> </dependency>
</dependencies> </dependencies>

@ -0,0 +1,25 @@
package com.ruoyi.flyingbook.domain;
import lombok.Data;
import java.io.Serializable;
/**
* lark_table_relation
*
* @author ruoyi
* @date 2023-03-15
*/
@Data
public class LarkCompanyTableInfo implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String appId;
private String appSecret;
private String fromAppToken;
private String fromTableId;
private String toAppToken;
private String toTableId;
}

@ -13,6 +13,8 @@ public class MailInfo extends BaseEntity {
private Long id; private Long id;
private Long larkTableId;
/** 邮件服务器地址 */ /** 邮件服务器地址 */
private String mailServer; private String mailServer;

@ -15,6 +15,8 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class LarkRequest { public class LarkRequest {
private Long id;
private String appId; private String appId;
private String appSecret; private String appSecret;

@ -1,107 +1,79 @@
package com.ruoyi.flyingbook.mail; package com.ruoyi.flyingbook.mail;
import com.ruoyi.flyingbook.mail.request.MailRequest; import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
import com.ruoyi.common.enums.EventOperateType;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
import com.ruoyi.flyingbook.domain.Event;
import com.ruoyi.flyingbook.domain.EventLog;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mapper.EventLogMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.mail.*; import javax.mail.Flags;
import javax.mail.Message;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility; import javax.mail.internet.MimeUtility;
import javax.mail.search.*; import java.util.ArrayList;
import java.security.Security; import java.util.HashMap;
import java.util.Properties; import java.util.List;
import java.util.Map;
/** /**
* @author yuxiangyong * @author yuxiangyong
* @create 2023-04-09 15:28 * @create 2023-04-09 20:27
*/ */
@Slf4j @Slf4j
@Component
public class MailHelper { public class MailHelper {
/** @Autowired
* ssl private LarkTableHelper larkTableHelper;
*/ @Autowired
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private EventLogMapper eventLogMapper;
public static void main(String[] args) { protected void parseMsg(Message[] messages,LarkTableRequest add) {
MailHelper mailHelper = new MailHelper();
MailRequest req = new MailRequest("imap.qq.com",993,"932687738","ohziejkxqvsgbbag");
mailHelper.receiveMail(req);
}
public void receiveMail(MailRequest req){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// 定义连接imap服务器的属性信息
String portStr = String.valueOf(req.getEmailPort());
Properties props = new Properties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.transport.protocol", req.getEmailProtocol());
props.setProperty("mail.imap.port", portStr);
props.setProperty("mail.imap.socketFactory.port", portStr);
//创建会话 /**
Session session = Session.getInstance(props, new Authenticator() { *
@Override */
protected PasswordAuthentication getPasswordAuthentication() { List<EventLog> logs = new ArrayList<>();
return new PasswordAuthentication(req.getUserName(),req.getAuthorizationCode()); for (Message message : messages) {
}
});
Store store = null;
Folder folder = null;
try { try {
//存储对象 System.out.println("消息:" + message.getSubject());
store = session.getStore(req.getEmailProtocol()); String from = MimeUtility.decodeText(message.getFrom()[0].toString());
//连接 InternetAddress internetAddress = new InternetAddress(from);
store.connect(req.getEmailServer(),req.getEmailPort(),req.getUserName(),req.getAuthorizationCode()); System.out.println("发件人:" + internetAddress.getPersonal() + '(' + internetAddress.getAddress() + ')');
// 获得收件箱 String recordId = syncLarkTable(message,add);
folder = store.getFolder(req.getReceiveFolder()); this.buildLog(logs,message,recordId,null);
// 以读写模式打开收件箱 message.setFlag(Flags.Flag.SEEN, true);
folder.open(Folder.READ_WRITE);
//false 表示未读
FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN),false);
//获得收件箱的邮件列表
Message[] messages = folder.search(flagTerm);
// 打印不同状态的邮件数量
log.info("收件箱中共{}封邮件!\n共{}封未读邮件!\n共{}封新邮件!\n共{}封已删除邮件!",
messages.length,
folder.getUnreadMessageCount(),
folder.getNewMessageCount(),
folder.getDeletedMessageCount());
log.info("------------------------开始解析邮件----------------------------------");
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
parseMsg(message);
//设置已读
message.setFlag(Flags.Flag.SEEN,true);
}
} catch (Exception e) { } catch (Exception e) {
log.error("邮件解析失败",e); this.buildLog(logs,message,null,e);
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
} }
} catch (MessagingException e) {
log.error("邮件解析失败",e);
} }
if (CollectionUtils.isNotEmpty(logs)){
eventLogMapper.insertBatchEventLog(logs);
} }
} }
private void parseMsg(Message message){ private void buildLog(List<EventLog> logs,Message message,String recordId,Exception e){
try { EventLog log = e != null ?
System.out.println("消息:"+ message.getSubject()); new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message.toString())
String from = MimeUtility.decodeText(message.getFrom()[0].toString()); : new EventLog(null, EventOperateType.SYNC_MAIL.getCode(), message.toString(),null,e.getMessage());
InternetAddress internetAddress = new InternetAddress(from); log.setRemark(recordId);
System.out.println("发件人:" + internetAddress.getPersonal() + '(' + internetAddress.getAddress() + ')'); logs.add(log);
}
}catch (Exception e){
private String syncLarkTable(Message message,LarkTableRequest add){
Map<String, Object> body = this.buildBody(message);
add.setBody(body);
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(add);
return respBody.getRecord().getRecordId();
} }
protected Map<String,Object> buildBody(Message message){
return new HashMap<>();
} }
} }

@ -0,0 +1,108 @@
package com.ruoyi.flyingbook.mail;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mail.request.MailRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.mail.search.FlagTerm;
import java.security.Security;
import java.util.Properties;
/**
* @author yuxiangyong
* @create 2023-04-09 15:28
*/
@Slf4j
@Component
public class MailHttpHelper extends MailHelper{
public static void main(String[] args) {
MailHttpHelper mailHelper = new MailHttpHelper();
MailRequest req = new MailRequest("xjia.synology.me",5000,"xjzsb","12345678");
// req = new MailRequest("imap.qq.com",993,"932687738","lwoapikuenmzbfaj");
mailHelper.test(req);
}
public void test(MailRequest req){
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.debug","true");
// props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.transport.protocol", "pop3");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(req.getUserName(),req.getAuthorizationCode());
}
});
Store store = session.getStore("pop3");
store.connect(req.getEmailServer(),req.getEmailPort(),req.getUserName(), req.getAuthorizationCode());
Folder inbox = store.getFolder("INBOX");
}catch (Exception e){
log.info("",e);
}
}
public void receiveMail(MailRequest req, LarkTableRequest add){
// 定义连接imap服务器的属性信息
String portStr = String.valueOf(req.getEmailPort());
Properties props = new Properties();
props.setProperty("mail.smtp.ssl.enable","false");
props.setProperty("mail.imap.starttls.enable","true");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.transport.protocol", req.getEmailProtocol());
props.setProperty("mail.imap.port", portStr);
props.setProperty("mail.imap.socketFactory.port", portStr);
props.setProperty("mail.debug","false");
//创建会话
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(req.getUserName(),req.getAuthorizationCode());
}
});
Store store = null;
Folder folder = null;
try {
//存储对象
store = session.getStore(req.getEmailProtocol());
//连接
store.connect(req.getEmailServer(),req.getEmailPort(),req.getUserName(),req.getAuthorizationCode());
// 获得收件箱
folder = store.getFolder(req.getReceiveFolder());
// 以读写模式打开收件箱
folder.open(Folder.READ_WRITE);
//false 表示未读
FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN),false);
//获得收件箱的邮件列表
Message[] messages = folder.search(flagTerm);
// 打印不同状态的邮件数量
log.info("收件箱中共{}封邮件!\n共{}封未读邮件!\n共{}封新邮件!\n共{}封已删除邮件!",
messages.length,
folder.getUnreadMessageCount(),
folder.getNewMessageCount(),
folder.getDeletedMessageCount());
log.info("------------------------开始解析邮件----------------------------------");
// this.parseMsg(messages,add);
} catch (Exception e) {
log.error("邮件解析失败",e);
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
log.error("邮件解析失败",e);
}
}
}
}

@ -0,0 +1,107 @@
package com.ruoyi.flyingbook.mail;
import com.ruoyi.flyingbook.mail.request.MailRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.mail.search.*;
import java.security.Security;
import java.util.Properties;
/**
* @author yuxiangyong
* @create 2023-04-09 15:28
*/
@Slf4j
@Component
public class MailTencentHelper {
/**
* ssl
*/
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
public static void main(String[] args) {
MailTencentHelper mailHelper = new MailTencentHelper();
MailRequest req = new MailRequest("imap.qq.com",993,"932687738","ohziejkxqvsgbbag");
mailHelper.receiveMail(req);
}
public void receiveMail(MailRequest req){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// 定义连接imap服务器的属性信息
String portStr = String.valueOf(req.getEmailPort());
Properties props = new Properties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.transport.protocol", req.getEmailProtocol());
props.setProperty("mail.imap.port", portStr);
props.setProperty("mail.imap.socketFactory.port", portStr);
//创建会话
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(req.getUserName(),req.getAuthorizationCode());
}
});
Store store = null;
Folder folder = null;
try {
//存储对象
store = session.getStore(req.getEmailProtocol());
//连接
store.connect(req.getEmailServer(),req.getEmailPort(),req.getUserName(),req.getAuthorizationCode());
// 获得收件箱
folder = store.getFolder(req.getReceiveFolder());
// 以读写模式打开收件箱
folder.open(Folder.READ_WRITE);
//false 表示未读
FlagTerm flagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN),false);
//获得收件箱的邮件列表
Message[] messages = folder.search(flagTerm);
// 打印不同状态的邮件数量
log.info("收件箱中共{}封邮件!\n共{}封未读邮件!\n共{}封新邮件!\n共{}封已删除邮件!",
messages.length,
folder.getUnreadMessageCount(),
folder.getNewMessageCount(),
folder.getDeletedMessageCount());
log.info("------------------------开始解析邮件----------------------------------");
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
parseMsg(message);
//设置已读
message.setFlag(Flags.Flag.SEEN,true);
}
} catch (Exception e) {
log.error("邮件解析失败",e);
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
log.error("邮件解析失败",e);
}
}
}
private void parseMsg(Message message){
try {
System.out.println("消息:"+ message.getSubject());
String from = MimeUtility.decodeText(message.getFrom()[0].toString());
InternetAddress internetAddress = new InternetAddress(from);
System.out.println("发件人:" + internetAddress.getPersonal() + '(' + internetAddress.getAddress() + ')');
}catch (Exception e){
}
}
}

@ -1,5 +1,6 @@
package com.ruoyi.flyingbook.mail.request; package com.ruoyi.flyingbook.mail.request;
import com.ruoyi.flyingbook.domain.MailInfo;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -53,15 +54,6 @@ public class MailRequest {
} }
} }
public MailRequest(String emailServer, Integer emailPort, String userName, String authorizationCode, Boolean readFeedback) {
this.emailServer = emailServer;
this.emailPort = emailPort;
this.userName = userName;
this.authorizationCode = authorizationCode;
this.readFeedback = readFeedback;
this.fillDefault(readFeedback);
}
public MailRequest(String emailServer, Integer emailPort, String userName, String authorizationCode) { public MailRequest(String emailServer, Integer emailPort, String userName, String authorizationCode) {
this.emailServer = emailServer; this.emailServer = emailServer;
this.emailPort = emailPort; this.emailPort = emailPort;
@ -69,4 +61,14 @@ public class MailRequest {
this.authorizationCode = authorizationCode; this.authorizationCode = authorizationCode;
this.fillDefault(Boolean.TRUE); this.fillDefault(Boolean.TRUE);
} }
public MailRequest(MailInfo info,Boolean readFeedback) {
this.emailServer = info.getMailServer();
this.emailPort = Integer.valueOf(info.getMailPort());
this.userName = info.getMailUserName();
this.authorizationCode = info.getMailAuthorizationCode();
this.emailProtocol = info.getMailProtocol();
this.receiveFolder = info.getMailReceiveFolder();
this.readFeedback = Boolean.TRUE.equals(readFeedback);
}
} }

@ -1,6 +1,7 @@
package com.ruoyi.flyingbook.mapper; package com.ruoyi.flyingbook.mapper;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.LarkTableRelation; import com.ruoyi.flyingbook.domain.LarkTableRelation;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -62,4 +63,6 @@ public interface LarkTableRelationMapper
*/ */
public int deleteLarkTableRelationByIds(Long[] ids); public int deleteLarkTableRelationByIds(Long[] ids);
public List<LarkCompanyTableInfo> queryListByIdListJoinLarkCompany(@Param("ids") List<Long> tableIds, @Param("flag") Long flag, @Param("relationType") String relationType);
} }

@ -1,39 +1,63 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper <!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.flyingbook.mapper.LarkTableRelationMapper"> <mapper namespace="com.ruoyi.flyingbook.mapper.LarkTableRelationMapper">
<resultMap type="com.ruoyi.flyingbook.domain.LarkTableRelation" id="LarkTableRelationResult"> <resultMap type="com.ruoyi.flyingbook.domain.LarkTableRelation" id="LarkTableRelationResult">
<result property="id" column="id" /> <result property="id" column="id"/>
<result property="larkCompanyRelationId" column="lark_company_relation_id" /> <result property="larkCompanyRelationId" column="lark_company_relation_id"/>
<result property="fromAppToken" column="from_app_token" /> <result property="fromAppToken" column="from_app_token"/>
<result property="fromTableId" column="from_table_id" /> <result property="fromTableId" column="from_table_id"/>
<result property="toAppToken" column="to_app_token" /> <result property="toAppToken" column="to_app_token"/>
<result property="toTableId" column="to_table_id" /> <result property="toTableId" column="to_table_id"/>
<result property="createBy" column="create_by" /> <result property="createBy" column="create_by"/>
<result property="createTime" column="create_time" /> <result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by" /> <result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time" /> <result property="updateTime" column="update_time"/>
<result property="flag" column="flag" /> <result property="flag" column="flag"/>
<result property="remark" column="remark" /> <result property="remark" column="remark"/>
<result property="relationType" column="relation_type" /> <result property="relationType" column="relation_type"/>
</resultMap>
<resultMap type="com.ruoyi.flyingbook.domain.LarkCompanyTableInfo" id="LarkCompanyTableResult">
<result property="id" column="id"/>
<result property="appId" column="app_id"/>
<result property="appSecret" column="secret"/>
<result property="fromAppToken" column="from_app_token"/>
<result property="fromTableId" column="from_table_id"/>
<result property="toAppToken" column="to_app_token"/>
<result property="toTableId" column="to_table_id"/>
</resultMap> </resultMap>
<sql id="selectLarkTableRelationVo"> <sql id="selectLarkTableRelationVo">
select id, lark_company_relation_id, from_app_token, from_table_id, to_app_token, to_table_id, create_by, create_time, update_by, update_time, flag, remark,relation_type from lark_table_relation select id,
lark_company_relation_id,
from_app_token,
from_table_id,
to_app_token,
to_table_id,
create_by,
create_time,
update_by,
update_time,
flag,
remark,
relation_type
from lark_table_relation
</sql> </sql>
<select id="selectLarkTableRelationList" parameterType="com.ruoyi.flyingbook.domain.LarkTableRelation" resultMap="LarkTableRelationResult"> <select id="selectLarkTableRelationList" parameterType="com.ruoyi.flyingbook.domain.LarkTableRelation"
resultMap="LarkTableRelationResult">
<include refid="selectLarkTableRelationVo"/> <include refid="selectLarkTableRelationVo"/>
<where> <where>
<if test="larkCompanyRelationId != null "> and lark_company_relation_id = #{larkCompanyRelationId}</if> <if test="larkCompanyRelationId != null ">and lark_company_relation_id = #{larkCompanyRelationId}</if>
<if test="relationType != null and relationType != ''"> and relation_type = #{relationType}</if> <if test="relationType != null and relationType != ''">and relation_type = #{relationType}</if>
<if test="fromAppToken != null and fromAppToken != ''"> and from_app_token = #{fromAppToken}</if> <if test="fromAppToken != null and fromAppToken != ''">and from_app_token = #{fromAppToken}</if>
<if test="fromTableId != null and fromTableId != ''"> and from_table_id = #{fromTableId}</if> <if test="fromTableId != null and fromTableId != ''">and from_table_id = #{fromTableId}</if>
<if test="toAppToken != null and toAppToken != ''"> and to_app_token = #{toAppToken}</if> <if test="toAppToken != null and toAppToken != ''">and to_app_token = #{toAppToken}</if>
<if test="toTableId != null and toTableId != ''"> and to_table_id = #{toTableId}</if> <if test="toTableId != null and toTableId != ''">and to_table_id = #{toTableId}</if>
<if test="flag != null "> and flag = #{flag}</if> <if test="flag != null ">and flag = #{flag}</if>
</where> </where>
</select> </select>
@ -42,7 +66,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where id = #{id} where id = #{id}
</select> </select>
<insert id="insertLarkTableRelation" parameterType="com.ruoyi.flyingbook.domain.LarkTableRelation" useGeneratedKeys="true" keyProperty="id"> <insert id="insertLarkTableRelation" parameterType="com.ruoyi.flyingbook.domain.LarkTableRelation"
useGeneratedKeys="true" keyProperty="id">
insert into lark_table_relation insert into lark_table_relation
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="larkCompanyRelationId != null">lark_company_relation_id,</if> <if test="larkCompanyRelationId != null">lark_company_relation_id,</if>
@ -94,7 +119,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</update> </update>
<delete id="deleteLarkTableRelationById" parameterType="Long"> <delete id="deleteLarkTableRelationById" parameterType="Long">
delete from lark_table_relation where id = #{id} delete
from lark_table_relation
where id = #{id}
</delete> </delete>
<delete id="deleteLarkTableRelationByIds" parameterType="String"> <delete id="deleteLarkTableRelationByIds" parameterType="String">
@ -103,4 +130,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id} #{id}
</foreach> </foreach>
</delete> </delete>
<select id="queryListByIdListJoinLarkCompany" resultMap="LarkCompanyTableResult">
SELECT
cr.app_id,
cr.secret,
tr.id,
tr.from_app_token,
tr.from_table_id,
tr.to_app_token,
tr.to_table_id
FROM
lark_table_relation tr
LEFT JOIN lark_company_relation cr ON tr.lark_company_relation_id = cr.id
WHERE
tr.id IN
<foreach collection="tableIds" open="(" close=")" separator="," item="id">
#{id}
</foreach>
AND tr.flag = #{flag}
AND tr.relation_type = #{relationType};
</select>
</mapper> </mapper>

@ -6,6 +6,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<resultMap type="com.ruoyi.flyingbook.domain.MailInfo" id="MailInfoResult"> <resultMap type="com.ruoyi.flyingbook.domain.MailInfo" id="MailInfoResult">
<result property="id" column="id" /> <result property="id" column="id" />
<result property="larkTableId" column="lark_table_id" />
<result property="mailServer" column="mail_server" /> <result property="mailServer" column="mail_server" />
<result property="mailPort" column="mail_port" /> <result property="mailPort" column="mail_port" />
<result property="mailProtocol" column="mail_protocol" /> <result property="mailProtocol" column="mail_protocol" />
@ -21,12 +22,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap> </resultMap>
<sql id="selectMailInfoVo"> <sql id="selectMailInfoVo">
select id, mail_server, mail_port, mail_protocol, mail_user_name, mail_authorization_code, mail_receive_folder, create_by, create_time, update_by, update_time, flag, remark from mail_info select id,lark_table_id, mail_server, mail_port, mail_protocol, mail_user_name, mail_authorization_code, mail_receive_folder, create_by, create_time, update_by, update_time, flag, remark from mail_info
</sql> </sql>
<select id="selectMailInfoList" parameterType="MailInfo" resultMap="MailInfoResult"> <select id="selectMailInfoList" parameterType="com.ruoyi.flyingbook.domain.MailInfo" resultMap="MailInfoResult">
<include refid="selectMailInfoVo"/> <include refid="selectMailInfoVo"/>
<where> <where>
<if test="larkTableId != null"> and lark_table_id = #{larkTableId}</if>
<if test="mailServer != null and mailServer != ''"> and mail_server = #{mailServer}</if> <if test="mailServer != null and mailServer != ''"> and mail_server = #{mailServer}</if>
<if test="mailPort != null and mailPort != ''"> and mail_port = #{mailPort}</if> <if test="mailPort != null and mailPort != ''"> and mail_port = #{mailPort}</if>
<if test="mailProtocol != null and mailProtocol != ''"> and mail_protocol = #{mailProtocol}</if> <if test="mailProtocol != null and mailProtocol != ''"> and mail_protocol = #{mailProtocol}</if>
@ -42,9 +44,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where id = #{id} where id = #{id}
</select> </select>
<insert id="insertMailInfo" parameterType="MailInfo" useGeneratedKeys="true" keyProperty="id"> <insert id="insertMailInfo" parameterType="com.ruoyi.flyingbook.domain.MailInfo" useGeneratedKeys="true" keyProperty="id">
insert into mail_info insert into mail_info
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id,</if>
<if test="mailServer != null and mailServer != ''">mail_server,</if> <if test="mailServer != null and mailServer != ''">mail_server,</if>
<if test="mailPort != null and mailPort != ''">mail_port,</if> <if test="mailPort != null and mailPort != ''">mail_port,</if>
<if test="mailProtocol != null and mailProtocol != ''">mail_protocol,</if> <if test="mailProtocol != null and mailProtocol != ''">mail_protocol,</if>
@ -59,6 +62,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="remark != null">remark,</if> <if test="remark != null">remark,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">#{larkTableId},</if>
<if test="mailServer != null and mailServer != ''">#{mailServer},</if> <if test="mailServer != null and mailServer != ''">#{mailServer},</if>
<if test="mailPort != null and mailPort != ''">#{mailPort},</if> <if test="mailPort != null and mailPort != ''">#{mailPort},</if>
<if test="mailProtocol != null and mailProtocol != ''">#{mailProtocol},</if> <if test="mailProtocol != null and mailProtocol != ''">#{mailProtocol},</if>
@ -74,9 +78,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</trim> </trim>
</insert> </insert>
<update id="updateMailInfo" parameterType="MailInfo"> <update id="updateMailInfo" parameterType="com.ruoyi.flyingbook.domain.MailInfo">
update mail_info update mail_info
<trim prefix="SET" suffixOverrides=","> <trim prefix="SET" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id = #{larkTableId},</if>
<if test="mailServer != null and mailServer != ''">mail_server = #{mailServer},</if> <if test="mailServer != null and mailServer != ''">mail_server = #{mailServer},</if>
<if test="mailPort != null and mailPort != ''">mail_port = #{mailPort},</if> <if test="mailPort != null and mailPort != ''">mail_port = #{mailPort},</if>
<if test="mailProtocol != null and mailProtocol != ''">mail_protocol = #{mailProtocol},</if> <if test="mailProtocol != null and mailProtocol != ''">mail_protocol = #{mailProtocol},</if>

@ -34,6 +34,10 @@
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId> <artifactId>ruoyi-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-flyingbook</artifactId>
</dependency>
</dependencies> </dependencies>

@ -0,0 +1,94 @@
package com.ruoyi.quartz.task;
import com.ruoyi.common.enums.FlagStatus;
import com.ruoyi.common.enums.TableRelationTypeEnum;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.MailInfo;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mail.MailHttpHelper;
import com.ruoyi.flyingbook.mail.request.MailRequest;
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
import com.ruoyi.flyingbook.service.IMailInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
*
* @author ruoyi
*/
@Slf4j
@Component("mailSyncTask")
public class MailSyncTask {
@Autowired
private MailHttpHelper mailHttpHelper;
@Autowired
private IMailInfoService iMailInfoService;
@Autowired
private LarkTableRelationMapper larkTableRelationMapper;
public void syncMail() {
log.info("MailSyncTask start");
MailInfo mailInfo = new MailInfo();
mailInfo.setFlag(FlagStatus.OK.getCode());
List<MailInfo> mailInfos = iMailInfoService.selectMailInfoList(mailInfo);
log.info("MailSyncTask execute size:{}", mailInfos.size());
Map<Long, LarkTableRequest> larkMap = this.getLarkMap(mailInfos);
for (MailInfo info : mailInfos) {
LarkTableRequest addRequest = larkMap.get(info.getLarkTableId());
if (addRequest == null) {
log.info("待同步邮箱未配置飞书相关信息,mailInfo:{}", info);
continue;
}
MailRequest request = new MailRequest(info, Boolean.TRUE);
mailHttpHelper.receiveMail(request,addRequest);
}
log.info("MailSyncTask end");
}
private Map<Long, LarkTableRequest> getLarkMap(List<MailInfo> mailInfos) {
List<Long> tableIds = mailInfos.stream()
.filter(r -> {
return r.getLarkTableId() != null;
})
.map(MailInfo::getLarkTableId)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(tableIds)) {
return new HashMap<>();
}
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListByIdListJoinLarkCompany(tableIds, FlagStatus.OK.getCode(), TableRelationTypeEnum.SYNC_EMAIL.getCode());
return larkCompanyTableInfos.stream()
.filter(r -> {
return r.getId() != null
&& StringUtils.isNotBlank(r.getAppId())
&& StringUtils.isNotBlank(r.getAppSecret())
&& StringUtils.isNotBlank(r.getToAppToken())
&& StringUtils.isNotBlank(r.getToTableId());
})
.map(r -> {
LarkTableRequest request = new LarkTableRequest();
request.setId(r.getId());
request.setAppId(r.getAppId());
request.setAppSecret(r.getAppSecret());
request.setAppToken(r.getToAppToken());
request.setAppTable(r.getToTableId());
return request;
})
.collect(
Collectors.toMap(LarkTableRequest::getId, Function.identity(), (k1, k2) -> k1)
);
}
}
Loading…
Cancel
Save