package services

import (
	"fmt"
	"eta/eta_api/models"
	"eta/eta_api/services/alarm_msg"
	"eta/eta_api/utils"
	"strconv"
	"time"
)

//func init() {
//	report, _ := models.GetReportById(572)
//	SendReportToThs(report)
//}

// SendReportToEmail 发送报告邮件
func SendReportToEmail(report *models.ReportDetail) (err error) {
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox && utils.BusinessCode != utils.BusinessCodeDebug {
		// 客户不做报告邮件推送
		return
	}
	fmt.Println("SendReportToEmail")
	defer func() {
		if err != nil {
			//fmt.Println(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "发送消息至同花顺失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
			go alarm_msg.SendAlarmMsg("发送报告至邮件失败,SendReportToEmail ErrMsg："+err.Error(), 3)
			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendReportToEmail ErrMsg:"+err.Error(), utils.EmailSendToUsers)
		}
	}()
	toUser := ``
	jumpUrl := ``
	if utils.RunMode == "debug" {
		//toUser = `glji@hzinsights.com`
		toUser = `317699326@qq.com`
		jumpUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id=1578" + strconv.Itoa(report.Id)
	} else {
		toUser = "lijun011112@gtjas.com"
		jumpUrl = "https://ficc.hzinsights.com/reportdtl?id=" + strconv.Itoa(report.Id)
	}

	createDate, err := time.Parse(utils.FormatDateTime, report.CreateTime)
	createDateFrom := createDate.Format("060102")
	emailBody :=
		`<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email</title>
</head>
<body>
    <div>
        <h2 style="font-size: 16px;">弘则研报：<span>` + report.Title + "_" + createDateFrom + `</span><span></span></h2>
        <div style="font-size: 14px;margin-left: 40px;">
            <p>你好，</p>
            <p>今日报告已推送。</p>
            <p><span>报告类型：</span><span>` + report.ClassifyNameFirst + `</span></p>
            <p><span>报告标题：</span><span>` + report.Title + `</span></p>
            <p><span>报告链接：</span><a href="` + jumpUrl + `">弘则研究</a></p>
        </div>
    </div>
</body>
</html>`
	fmt.Println("start SendReportToEmail")
	result, err := utils.SendEmailByHz("弘则研报报告", emailBody, toUser)
	fmt.Println("send result:", result, err)
	return
}

// SendReportMiniToThs 发送报告-研报小程序到同花顺
func SendReportMiniToThs(report *models.ReportDetail) (err error) {
	defer func() {
		if err != nil {
			go alarm_msg.SendAlarmMsg("SendReportMiniToThs 发送报告-研报小程序到同花顺失败, ReportId:"+strconv.Itoa(report.Id)+", ErrMsg:"+err.Error(), 3)
			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "SendReportMiniToThs发送报告至同花顺失败, ReportId:" + strconv.Itoa(report.Id) + ", ErrMsg:" + err.Error(), utils.EmailSendToUsers)
		}
	}()
	if report.HasChapter == 0 {
		go SendReportToEmail(report)
	}
	return
}
