浏览代码

rule调整

Roc 1 月之前
父节点
当前提交
401cbef345
共有 3 个文件被更改,包括 60 次插入24 次删除
  1. 9 0
      models/report/outside_report.go
  2. 40 22
      services/pcsg/mail.go
  3. 11 2
      utils/mail/imap.go

+ 9 - 0
models/report/outside_report.go

@@ -126,6 +126,15 @@ func GetMaxOutsideReportByEmailMessageId() (maxEmailMessageUid int, err error) {
 	return
 }
 
+// GetAllOutsideReportByEmailMessageId
+// @Description: 获取所有的邮件id
+// @return maxEmailMessageUid
+// @return err
+func GetAllOutsideReportByEmailMessageId() (emailMessageUidList []int, err error) {
+	err = global.DEFAULT_MYSQL.Model(OutsideReport{}).Select("email_message_uid").Find(&emailMessageUidList).Error
+	return
+}
+
 // GetNoSysUserNameOutsideReport
 // @Description: 获取没有用户的信息
 // @return items

+ 40 - 22
services/pcsg/mail.go

@@ -22,6 +22,7 @@ import (
 
 // 同步用户锁,防止重复同步,不管是全量还是增量,都是同一时间只能一个同步
 var lockListenEmail sync.Mutex
+
 // 邮件改名规则
 var ruleList []report.MailRule
 
@@ -66,12 +67,26 @@ func ListenMail() {
 	emailMessageUID := global.CONFIG.Email.StartReadIndex
 	if emailMessageUID <= 0 {
 		emailMessageUID, err = report.GetMaxOutsideReportByEmailMessageId()
-		// 已经存在了,那么就返回
+		// 查找失败了,那么就记录
 		if err != nil {
 			global.FILE_LOG.Errorf("获取已入库的最大邮件id失败:%s", err.Error())
 		}
 	}
 
+	{
+		idList, err := report.GetAllOutsideReportByEmailMessageId()
+		// 已经存在了,那么就返回
+		if err != nil {
+			global.FILE_LOG.Errorf("获取已入库的所有邮件id失败:%s", err.Error())
+		} else {
+			idMap := make(map[int]bool)
+			for _, v := range idList {
+				idMap[v] = true
+			}
+			mail.IsHandleMessageIdMap = idMap
+		}
+	}
+
 	mailAddress := fmt.Sprintf("%s:%d", global.CONFIG.Email.Host, global.CONFIG.Email.Port)
 	mail.ListenMail(mailAddress, global.CONFIG.Email.Folder, global.CONFIG.Email.UserName, global.CONFIG.Email.Password, global.CONFIG.Email.ReadBatchSize, emailMessageUID, mailMessageChan, mailMessageDoneChan)
 
@@ -134,7 +149,7 @@ func handleMailMessage(emailMessage mail.MailMessage) (err error) {
 				global.FILE_LOG.Errorf("更新收件时间失败:%s", err.Error())
 				return
 			}
-			global.FILE_LOG.Debugf( "更新收件时间,名称:%s, 原时间:%s, 新收件时间: %s", outReport.Title, outReport.ReportUpdateTime, emailMessage.Date)
+			global.FILE_LOG.Debugf("更新收件时间,名称:%s, 原时间:%s, 新收件时间: %s", outReport.Title, outReport.ReportUpdateTime, emailMessage.Date)
 		}
 		global.FILE_LOG.Debugf("已存在,就不处理了,报告标题:%s;;邮件下标:%d", outReport.Title, emailMessage.Uid)
 		return
@@ -195,30 +210,33 @@ func handleMailMessage(emailMessage mail.MailMessage) (err error) {
 			classifyName = classify.ClassifyName
 
 			// 特殊规则
-			if strings.Contains(title, "海外分公司市场信息月报") || strings.Contains(title, "区域市场信息月报"){
-				if emailMessage.From == "report.pcanalyst00@petrochina-usa.com" {
-					sysUserName = "PCI"
-					break
-				} else {
-					v.Author = emailMessage.From
-				}
-			}
-
-			if strings.Contains(v.Author, "@") {
-				sysUser, tmpErr := eta.GetSysUserByEmail(v.Author)
-				if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
-					err = tmpErr
-					return
-				}
-				if sysUser.AdminId > 0 {
-					sysUserId = sysUser.AdminId
-					sysUserName = sysUser.RealName
+			//if strings.Contains(title, "海外分公司市场信息月报") || strings.Contains(title, "区域市场信息月报") {
+			//	if emailMessage.From == "report.pcanalyst00@petrochina-usa.com" {
+			//		sysUserName = "PCI"
+			//		break
+			//	} else {
+			//		v.Author = emailMessage.From
+			//	}
+			//}
+
+			if v.Author != `` {
+				if strings.Contains(v.Author, "@") {
+					sysUser, tmpErr := eta.GetSysUserByEmail(v.Author)
+					if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
+						err = tmpErr
+						return
+					}
+					if sysUser.AdminId > 0 {
+						sysUserId = sysUser.AdminId
+						sysUserName = sysUser.RealName
+					} else {
+						sysUserName = v.Author
+					}
 				} else {
 					sysUserName = v.Author
 				}
-			} else {
-				sysUserName = v.Author
 			}
+
 			break
 		}
 	}

+ 11 - 2
utils/mail/imap.go

@@ -30,6 +30,9 @@ type MailMessage struct {
 	Attachment  map[string]string `description:"附件资源"`
 }
 
+// IsHandleMessageIdMap 邮件id是否已经处理过了
+var IsHandleMessageIdMap = map[int]bool{}
+
 func ListenMail(mailAddress, folder, userName, password string, readBatchSize, fromEmailIndex int, mailMessageChan chan MailMessage, mailMessageDoneChan chan bool) (err error) { // 收件箱
 	defer func() {
 		// 处理结束
@@ -214,6 +217,12 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, ok bool, err err
 	emailMessage.Attachment = make(map[string]string) // 附件
 
 	emailMessage.Uid = msg.Uid
+	if IsHandleMessageIdMap != nil {
+		if _, has := IsHandleMessageIdMap[int(emailMessage.Uid)]; has {
+			fmt.Println("邮件已处理,邮件下标:", emailMessage.Uid)
+			return
+		}
+	}
 	htmlStr := ``
 	textStr := ``
 
@@ -451,10 +460,10 @@ func RuleCheck(ruleList []report.MailRule, emailMessage MailMessage) bool {
 	for _, v := range ruleList {
 		title := strings.ToLower(emailMessage.Title)
 		rule := strings.ToLower(v.Rule)
-		if strings.Contains(title, rule){
+		if strings.Contains(title, rule) {
 			return true
 		}
 	}
 
 	return false
-}
+}