ziwen 1 年之前
父节点
当前提交
2e00613f8f
共有 4 个文件被更改,包括 85 次插入405 次删除
  1. 0 142
      models/english_report_email_log.go
  2. 85 86
      services/aliyun_email.go
  3. 0 152
      services/english_report/english_report.go
  4. 0 25
      services/tencent_yun.go

+ 0 - 142
models/english_report_email_log.go

@@ -1,142 +0,0 @@
-package models
-
-import (
-	"github.com/beego/beego/v2/client/orm"
-	"github.com/rdlucklib/rdluck_tools/paging"
-	"hongze/hongze_yb_en_api/utils"
-	"time"
-)
-
-const (
-	EnglishReportEmailLogSourceAli     = 1 // 来源-阿里云
-	EnglishReportEmailLogSourceTencent = 2 // 来源-腾讯云
-
-	EnglishReportEmailLogStatusIng     = -1 // 推送状态-已发送
-	EnglishReportEmailLogStatusFail    = 0  // 推送状态-发送失败
-	EnglishReportEmailLogStatusSuccess = 1  // 推送状态-发送成功
-)
-
-// EnglishReportEmailLog 英文研报-邮件推送记录
-type EnglishReportEmailLog struct {
-	Id           int       `orm:"column(id);pk;auto" description:"邮箱ID"`
-	ReportId     int       `description:"报告ID或者线上路演ID"`
-	ReportType   int       `description:"类型:0英文研报,1英文线上路演"`
-	EmailId      int       `description:"邮箱ID"`
-	Email        string    `description:"邮箱地址"`
-	SendData     string    `description:"请求信息"`
-	Result       string    `description:"响应信息"`
-	SendStatus   int       `description:"发送状态:-1-已发送(一个中间状态,重新推送时可能会产生这种状态);0-发送失败;1-发送成功"`
-	CreateTime   time.Time `description:"请求时间"`
-	Source       int       `description:"服务商:1-阿里云;2-腾讯云"`
-	IsDeleted    int       `description:"是否已删除: 0-正常; 1-已删除"`
-	CallbackData string    `description:"回调信息"`
-	ErrMsg       string    `description:"错误信息(Result/CallbackData里面的取起来麻烦=_=!)"`
-}
-
-func (item *EnglishReportEmailLog) TableName() string {
-	return "english_report_email_log"
-}
-
-func (item *EnglishReportEmailLog) Create() (err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	_, err = o.Insert(item)
-	return
-}
-
-func (item *EnglishReportEmailLog) Update(cols []string) (err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	_, err = o.Update(item, cols...)
-	return
-}
-
-func (item *EnglishReportEmailLog) InsertMulti(items []*EnglishReportEmailLog) (err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	_, err = o.InsertMulti(len(items), items)
-	return
-}
-
-// EnglishReportEmailLogPageListResp 英文研报-邮件推送记录分页列表响应体
-type EnglishReportEmailLogPageListResp struct {
-	List   []*EnglishReportEmailLogPageList `description:"列表数据"`
-	Paging *paging.PagingItem               `description:"分页数据"`
-}
-
-// EnglishReportEmailLogPageList 英文研报-邮件推送记录分页列表
-type EnglishReportEmailLogPageList struct {
-	SendId     int    `description:"推送ID"`
-	ReportId   int    `description:"报告ID"`
-	EmailId    int    `description:"邮箱ID"`
-	Email      string `description:"邮箱地址"`
-	ResultMsg  string `description:"结果详情"`
-	SendStatus int    `description:"发送状态:-1-已发送;0-发送失败;1-发送成功"`
-	Source     int    `description:"服务商:1-阿里云;2-腾讯云"`
-	CreateTime string `description:"创建时间"`
-}
-
-// GetEnglishReportEmailLogPageList 获取日志列表-分页
-func GetEnglishReportEmailLogPageList(condition string, pars []interface{}, order string, startSize, pageSize int) (total int, list []*EnglishReportEmailLog, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT * FROM english_report_email_log WHERE is_deleted = 0 `
-	sql += condition
-	if order != "" {
-		sql += order
-	} else {
-		sql += ` ORDER BY send_status ASC, create_time DESC`
-	}
-	totalSQl := `SELECT COUNT(1) total FROM (` + sql + `) z`
-	if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
-		return
-	}
-	sql += ` LIMIT ?,?`
-	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
-	return
-}
-
-// GetEnglishReportEmailLogById 主键获取日志
-func GetEnglishReportEmailLogById(id int) (item *EnglishReportEmailLog, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT * FROM english_report_email_log WHERE is_deleted = 0 AND id = ? LIMIT 1`
-	err = o.Raw(sql, id).QueryRow(&item)
-	return
-}
-
-
-// EnglishReportEmailLogFail 群发消息日志失败列表
-type EnglishReportEmailLogFail struct {
-	HasFail  int `description:"是否有失败记录: 0-无; 1-有"`
-	ReportId int `description:"报告ID"`
-}
-
-// GetEnglishReportEmailLogFailList 获取群发消息日志失败列表
-func GetEnglishReportEmailLogFailList(reportType int) (list []*EnglishReportEmailLogFail, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT 1 AS has_fail, report_id FROM english_report_email_log WHERE report_type=? and is_deleted = 0 AND send_status = 0 GROUP BY report_id`
-	_, err = o.Raw(sql, reportType).QueryRows(&list)
-	return
-}
-
-// GetEnglishReportEmailLog 获取邮件推送日志
-func GetEnglishReportEmailLog(condition string, pars []interface{}) (item *EnglishReportEmailLog, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT * FROM english_report_email_log WHERE 1 = 1 `
-	sql += condition
-	sql += ` ORDER BY id DESC LIMIT 1`
-	err = o.Raw(sql, pars).QueryRow(&item)
-	return
-}
-
-// GetHasFailEmailLogReportIds 获取存在邮件推送失败记录的英文报告IDs
-func GetHasFailEmailLogReportIds() (reportIds []int, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT DISTINCT report_id FROM english_report_email_log WHERE is_deleted = 0 AND send_status = 0`
-	_, err = o.Raw(sql).QueryRows(&reportIds)
-	return
-}
-
-// GetSuccessEmailLogReportIds 获取邮件推送记录均为成功的英文报告IDs
-func GetSuccessEmailLogReportIds() (reportIds []int, err error) {
-	o := orm.NewOrmUsingDB("rddp")
-	sql := `SELECT DISTINCT report_id FROM english_report_email_log WHERE is_deleted = 0 AND (send_status != 0 AND send_status != -1)`
-	_, err = o.Raw(sql).QueryRows(&reportIds)
-	return
-}

+ 85 - 86
services/aliyun_email.go

@@ -8,7 +8,6 @@ import (
 	util "github.com/alibabacloud-go/tea-utils/v2/service"
 	"github.com/alibabacloud-go/tea/tea"
 	"hongze/hongze_yb_en_api/global"
-	"hongze/hongze_yb_en_api/services/english_report"
 	"hongze/hongze_yb_en_api/utils"
 	"time"
 )
@@ -60,91 +59,91 @@ type AliyunEmailResultData struct {
 	RequestId  string `description:"请求ID"`
 	StatusCode int    `description:"状态码"`
 }
-
-// SendEmail 邮件推送
-func (em *AliyunEmail) SendEmail(item *english_report.EnglishReportSendEmailRequest) (ok bool, result string, err error) {
-	if em.Client == nil {
-		if e := em.NewClient(); e != nil {
-			err = e
-			return
-		}
-	}
-	singleSendMailRequest := &dm.SingleSendMailRequest{}
-	singleSendMailRequest.SetAccountName(AliyunEmailAccountName)
-	singleSendMailRequest.SetAddressType(1)
-	singleSendMailRequest.SetReplyToAddress(false)
-	singleSendMailRequest.SetSubject(item.Subject)
-	singleSendMailRequest.SetToAddress(item.Email)
-	singleSendMailRequest.SetHtmlBody(item.HtmlBody)
-	singleSendMailRequest.SetFromAlias(item.FromAlias)
-
-	runtime := &util.RuntimeOptions{}
-	tryErr := func() error {
-		res, e := em.Client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
-		if e != nil {
-			return e
-		}
-		// 请求成功
-		if tea.Int32Value(res.StatusCode) == 200 {
-			ok = true
-		}
-		resByte, e := json.Marshal(res.Body)
-		if e != nil {
-			return e
-		}
-		result = string(resByte)
-		return nil
-	}()
-
-	if tryErr != nil {
-		var e = &tea.SDKError{}
-		if t, ok := tryErr.(*tea.SDKError); ok {
-			e = t
-		} else {
-			e.Message = tea.String(tryErr.Error())
-		}
-		err = e
-		errByte, _ := json.Marshal(err)
-		result = string(errByte)
-	}
-	return
-}
-
-// BatchSendEmail 批量推送邮件
-func (em *AliyunEmail) BatchSendEmail(list []*english_report.EnglishReportSendEmailRequest) (results []*english_report.EnglishReportSendEmailResult, err error) {
-	results = make([]*english_report.EnglishReportSendEmailResult, 0)
-	if len(list) == 0 {
-		return
-	}
-	if e := em.NewClient(); e != nil {
-		err = e
-		return
-	}
-
-	// sendSingleMail接口有QPS100的限制, 保险起见每秒只请求50次接口
-	max := 50
-	c := 0
-	for i := range list {
-		if c >= max {
-			time.Sleep(time.Second)
-			c = 0
-		}
-		c += 1
-
-		dataByte, _ := json.Marshal(list[i])
-		ok, result, _ := em.SendEmail(list[i])
-		results = append(results, &english_report.EnglishReportSendEmailResult{
-			ReportId:   list[i].ReportId,
-			EmailId:    list[i].EmailId,
-			Email:      list[i].Email,
-			Ok:         ok,
-			SendData:   string(dataByte),
-			ResultData: result,
-			Source:     1,
-		})
-	}
-	return
-}
+//
+//// SendEmail 邮件推送
+//func (em *AliyunEmail) SendEmail(item *english_report.EnglishReportSendEmailRequest) (ok bool, result string, err error) {
+//	if em.Client == nil {
+//		if e := em.NewClient(); e != nil {
+//			err = e
+//			return
+//		}
+//	}
+//	singleSendMailRequest := &dm.SingleSendMailRequest{}
+//	singleSendMailRequest.SetAccountName(AliyunEmailAccountName)
+//	singleSendMailRequest.SetAddressType(1)
+//	singleSendMailRequest.SetReplyToAddress(false)
+//	singleSendMailRequest.SetSubject(item.Subject)
+//	singleSendMailRequest.SetToAddress(item.Email)
+//	singleSendMailRequest.SetHtmlBody(item.HtmlBody)
+//	singleSendMailRequest.SetFromAlias(item.FromAlias)
+//
+//	runtime := &util.RuntimeOptions{}
+//	tryErr := func() error {
+//		res, e := em.Client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
+//		if e != nil {
+//			return e
+//		}
+//		// 请求成功
+//		if tea.Int32Value(res.StatusCode) == 200 {
+//			ok = true
+//		}
+//		resByte, e := json.Marshal(res.Body)
+//		if e != nil {
+//			return e
+//		}
+//		result = string(resByte)
+//		return nil
+//	}()
+//
+//	if tryErr != nil {
+//		var e = &tea.SDKError{}
+//		if t, ok := tryErr.(*tea.SDKError); ok {
+//			e = t
+//		} else {
+//			e.Message = tea.String(tryErr.Error())
+//		}
+//		err = e
+//		errByte, _ := json.Marshal(err)
+//		result = string(errByte)
+//	}
+//	return
+//}
+//
+//// BatchSendEmail 批量推送邮件
+//func (em *AliyunEmail) BatchSendEmail(list []*english_report.EnglishReportSendEmailRequest) (results []*english_report.EnglishReportSendEmailResult, err error) {
+//	results = make([]*english_report.EnglishReportSendEmailResult, 0)
+//	if len(list) == 0 {
+//		return
+//	}
+//	if e := em.NewClient(); e != nil {
+//		err = e
+//		return
+//	}
+//
+//	// sendSingleMail接口有QPS100的限制, 保险起见每秒只请求50次接口
+//	max := 50
+//	c := 0
+//	for i := range list {
+//		if c >= max {
+//			time.Sleep(time.Second)
+//			c = 0
+//		}
+//		c += 1
+//
+//		dataByte, _ := json.Marshal(list[i])
+//		ok, result, _ := em.SendEmail(list[i])
+//		results = append(results, &english_report.EnglishReportSendEmailResult{
+//			ReportId:   list[i].ReportId,
+//			EmailId:    list[i].EmailId,
+//			Email:      list[i].Email,
+//			Ok:         ok,
+//			SendData:   string(dataByte),
+//			ResultData: result,
+//			Source:     1,
+//		})
+//	}
+//	return
+//}
 
 // sendEmailVerificationCode 邮件验证码推送
 func (em *AliyunEmail) sendEmailVerificationCode(email, content string) (ok bool, result string, err error) {

+ 0 - 152
services/english_report/english_report.go

@@ -1,6 +1,5 @@
 package english_report
 
-
 // IEnglishEmailSend 英文研报-邮件推送接口
 type IEnglishEmailSend interface {
 	NewClient() (err error)
@@ -33,154 +32,3 @@ type EnglishReportSendEmailResult struct {
 	ResultData string `description:"推送结果-JSON"`
 	Source     int    `description:"服务来源:1-阿里云;2-腾讯云"`
 }
-
-// BatchSendAliEnglishReportEmail 批量推送英文研报邮件
-//func BatchSendAliEnglishReportEmail(list []*EnglishReportSendEmailRequest) (err error) {
-//	defer func() {
-//		if err != nil {
-//			go alarm_msg.SendAlarmMsg("阿里云群发英文研报邮件失败, Err: "+err.Error(), 3)
-//		}
-//	}()
-//	if len(list) == 0 {
-//		return
-//	}
-//	requestMap := make(map[int]*EnglishReportSendEmailRequest, 0)
-//	for i := range list {
-//		requestMap[list[i].EmailId] = list[i]
-//	}
-//
-//	// 请求阿里云接口批量推送
-//	aliEmail := new(services.AliyunEmail)
-//	resultList, e := aliEmail.BatchSendEmail(list)
-//	if e != nil {
-//		err = e
-//		return
-//	}
-//
-//	// 返回的结果更新日志
-//	resendList := make([]*EnglishReportSendEmailRequest, 0)
-//	failLogIds := make([]int, 0)
-//	updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
-//	for i := range resultList {
-//		var cond string
-//		var pars []interface{}
-//		cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
-//		pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceAli, models.EnglishReportEmailLogStatusIng)
-//		l, e := models.GetEnglishReportEmailLog(cond, pars)
-//		if e != nil {
-//			continue
-//		}
-//		l.SendData = resultList[i].SendData
-//		l.Result = resultList[i].ResultData
-//		if resultList[i].Ok {
-//			l.SendStatus = models.EnglishReportEmailLogStatusSuccess
-//		} else {
-//			l.SendStatus = models.EnglishReportEmailLogStatusFail
-//			failLogIds = append(failLogIds, l.Id)
-//			if requestMap[resultList[i].EmailId] != nil {
-//				resendList = append(resendList, requestMap[resultList[i].EmailId])
-//			}
-//			// 取出错误信息
-//			r := new(services.AliyunEmailResult)
-//			if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
-//				continue
-//			}
-//			rd := new(services.AliyunEmailResultData)
-//			res := strings.Replace(r.Data, `\`, ``, -1)
-//			if e = json.Unmarshal([]byte(res), &rd); e != nil {
-//				continue
-//			}
-//			l.ErrMsg = rd.Message
-//		}
-//		if e = l.Update(updateCols); e != nil {
-//			continue
-//		}
-//	}
-//
-//	// 推送失败的重新腾讯云, 若腾讯云也失败将不再自动重推, 用户手动去重推
-//	if len(resendList) > 0 && len(failLogIds) > 0 {
-//		_ = ResendTencentEnglishReportEmail(resendList, failLogIds)
-//	}
-//	return
-//}
-
-// ResendTencentEnglishReportEmail 腾讯云邮件重新推送
-//func ResendTencentEnglishReportEmail(resendList []*EnglishReportSendEmailRequest, failLogIds []int) (err error) {
-//	defer func() {
-//		if err != nil {
-//			go alarm_msg.SendAlarmMsg("腾讯云重发英文研报邮件失败, Err: "+err.Error(), 3)
-//		}
-//	}()
-//	if len(resendList) == 0 || len(failLogIds) == 0 {
-//		return
-//	}
-//
-//	// 标记原有日志为已删除
-//	if len(failLogIds) > 0 {
-//		if e := models.DeleteEnglishReportEmailLogByIds(failLogIds); e != nil {
-//			err = errors.New("删除原邮件日志失败, Err: " + e.Error())
-//			return
-//		}
-//	}
-//
-//	// 写入新的日志
-//	nowTime := time.Now().Local()
-//	logData := make([]*models.EnglishReportEmailLog, 0)
-//	for i := range resendList {
-//		sendByte, e := json.Marshal(resendList[i])
-//		if e != nil {
-//			err = errors.New("sendByte json.Marshal Err, Err: " + e.Error())
-//			return
-//		}
-//
-//		logData = append(logData, &models.EnglishReportEmailLog{
-//			ReportId:   resendList[i].ReportId,
-//			EmailId:    resendList[i].EmailId,
-//			Email:      resendList[i].Email,
-//			SendData:   string(sendByte),
-//			Source:     models.EnglishReportEmailLogSourceTencent,
-//			SendStatus: models.EnglishReportEmailLogStatusIng,
-//			CreateTime: nowTime,
-//		})
-//	}
-//	emailLog := new(models.EnglishReportEmailLog)
-//	if e := emailLog.InsertMulti(logData); e != nil {
-//		err = errors.New("批量写入群发邮件日志失败, Err: " + e.Error())
-//		return
-//	}
-//
-//	// 请求腾讯云
-//	tecentEmail := new(services.TencentEmail)
-//	resultList, e := tecentEmail.BatchSendEmail(resendList)
-//	if e != nil {
-//		err = e
-//		return
-//	}
-//	updateCols := []string{"SendData", "Result", "SendStatus", "ErrMsg"}
-//	for i := range resultList {
-//		var cond string
-//		var pars []interface{}
-//		cond = ` AND is_deleted = 0 AND report_id = ? AND email_id = ? AND source = ? AND send_status = ?`
-//		pars = append(pars, resultList[i].ReportId, resultList[i].EmailId, models.EnglishReportEmailLogSourceTencent, models.EnglishReportEmailLogStatusIng)
-//		l, e := models.GetEnglishReportEmailLog(cond, pars)
-//		if e != nil {
-//			continue
-//		}
-//		l.SendData = resultList[i].SendData
-//		l.Result = resultList[i].ResultData
-//		if resultList[i].Ok {
-//			l.SendStatus = models.EnglishReportEmailLogStatusSuccess
-//		} else {
-//			l.SendStatus = models.EnglishReportEmailLogStatusFail
-//			r := new(services.TencentEmailResult)
-//			if e = json.Unmarshal([]byte(resultList[i].ResultData), &r); e != nil {
-//				continue
-//			}
-//			l.ErrMsg = r.Message
-//		}
-//		if e = l.Update(updateCols); e != nil {
-//			continue
-//		}
-//	}
-//	return
-//}

+ 0 - 25
services/tencent_yun.go

@@ -156,28 +156,3 @@ type TencentEmailCallBack struct {
 	FromDomain string `description:"发信域名"`
 	TemplateId int    `description:"模板 Id"`
 }
-
-//func init() {
-//	fmt.Println("start email init")
-//
-//	te := new(TencentEmail)
-//	te.NewClient()
-//	req := new(EnglishReportSendEmailRequest)
-//	req.Subject = "这是一封邮件的主题"
-//	req.Email = ""
-//	req.ReportTitle = "Report Hello World"
-//	req.ReportAbstract = "This is abstract"
-//	req.ReportContent = "This is content"
-//	req.ReportShareLink = "https://share.hzinsights.com/reportEn?code=a226e450e214f350856e2980b6e55ac9"
-//	req.ReportTime = "2022/11/23"
-//
-//	ok, result, e := te.SendEmail(req)
-//	fmt.Println(ok)
-//	if e != nil {
-//		fmt.Println("出错了: ", e)
-//	} else {
-//		fmt.Println("没出错: ", result)
-//	}
-//
-//	fmt.Println("end email init")
-//}