package app

import (
	"chain33-pai/pkg/email"
	"fmt"
	"chain33-pai/models"
	"chain33-pai/pkg/util"
)

const (
	NODEERROR  = "NODEEERROR"
	NETWORKERROR = "NETWORKERROR"
	NODENOTSYNC = "NODENOTSYNC"
)
var mailMsg = map[string]string{
	"NODEERROR":"节点不能正常启动，请联系技术远程排查问题",
	"NETWORKERROR":"网络出现异常，请排查家里网络是否正常上网以及网线接口是否松动",
	"NODENOTSYNC":"节点不同步，尝试排查网络，一键回滚，一键恢复等功能",
}

func EmailJob() error {
	for {
		for v:= range JobChan {
			var rasp models.RaspEmailHistory
			rasp.Date = util.GetMonthDay()
			rasp.Addr = v.Addr
			rasp.Error = v.Name
			switch v.Name {
			case NODEERROR:
				err := email.SendToMail(mailMsg[NODEERROR],v.Email)
				if err != nil {
					fmt.Println("SendToMail",err)
				}
			case NETWORKERROR:
				err := email.SendToMail(mailMsg[NETWORKERROR],v.Email)
				if err != nil {
					fmt.Println("SendToMail",err)
				}
			case NODENOTSYNC:
				err := email.SendToMail(mailMsg[NODENOTSYNC],v.Email)
				if err != nil {
					fmt.Println("SendToMail",err)
				}
			}
			models.AddEmailHistory(&rasp)
		}
	}
}
