|
@@ -6,6 +6,7 @@ import (
|
|
|
"os"
|
|
|
qu "qfw/util"
|
|
|
"qfw/util/redis"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"sync"
|
|
@@ -41,16 +42,20 @@ func init() {
|
|
|
}
|
|
|
go func() {
|
|
|
time.Sleep(5 * time.Second)
|
|
|
+ c := cron.New()
|
|
|
cron1, _ := Sysconfig["cron-sendmail"].(string)
|
|
|
+ cronMonth, _ := Sysconfig["cron-sendmonth"].(string)
|
|
|
+ log.Debug("enter sendMailtask...")
|
|
|
+ log.Println("发送数据定时任务")
|
|
|
if len(cron1) > 2 {
|
|
|
- log.Debug("enter sendMailtask...")
|
|
|
- c := cron.New()
|
|
|
- log.Println("发送数据定时任务")
|
|
|
c.AddFunc(cron1, sm)
|
|
|
- c.Start()
|
|
|
- defer c.Stop()
|
|
|
- select {}
|
|
|
}
|
|
|
+ if len(cronMonth) > 2 {
|
|
|
+ c.AddFunc(cronMonth, SendMonth)
|
|
|
+ }
|
|
|
+ c.Start()
|
|
|
+ defer c.Stop()
|
|
|
+ select {}
|
|
|
}()
|
|
|
}
|
|
|
|
|
@@ -59,6 +64,30 @@ func sm() {
|
|
|
sendmail("", -1, 0, "", true, "", "", "")
|
|
|
}
|
|
|
|
|
|
+//按月发送汇总邮件2020-6-29
|
|
|
+func SendMonth() {
|
|
|
+ query := map[string]interface{}{
|
|
|
+ "sendmonth": map[string]interface{}{
|
|
|
+ "$exists": 1,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ res, b := Mgo.Find("user", query, nil, nil, false, -1, -1)
|
|
|
+ now := time.Now()
|
|
|
+ day := now.Day()
|
|
|
+ startTime := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, time.Local).Format(qu.Date_Short_Layout)
|
|
|
+ endTime := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local).Format(qu.Date_Short_Layout)
|
|
|
+ if b && res != nil && *res != nil && len(*res) > 0 {
|
|
|
+ for _, v := range *res {
|
|
|
+ sendmonth := qu.IntAll(v["sendmonth"])
|
|
|
+ if sendmonth == day {
|
|
|
+ appid, _ := v["appid"].(string)
|
|
|
+ go sendmail(appid, -1, 1, "", false, startTime, endTime, "")
|
|
|
+ log.Println("发送月度汇总邮件", appid)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
//调用url地址发送邮件
|
|
|
func SendMailByAppid(w http.ResponseWriter, r *http.Request) {
|
|
|
defer qu.Catch()
|
|
@@ -116,7 +145,7 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
//当cronday为-7时,日期必须是周一 2020-6-9
|
|
|
nowDays := int(time.Now().Weekday())
|
|
|
if cronday == -7 {
|
|
|
- if nowDays != 0 {
|
|
|
+ if nowDays != 1 {
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
@@ -179,7 +208,22 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
}
|
|
|
st, _ := GetDayMinMax(time.Now())
|
|
|
st1 := st + int64(days*86400)
|
|
|
- if bcron { //如果是定时任务取cronday的值只能为0或-1,-1只发昨天的数据,0发最近两天的bget不为1的数据
|
|
|
+ if startTime != "" && endTime != "" {
|
|
|
+ reg1 := regexp.MustCompile("^[0-9]{10}$")
|
|
|
+ reg2 := regexp.MustCompile("^[0-9-]{10}$")
|
|
|
+ if reg1.MatchString(startTime) {
|
|
|
+ st1, _ = strconv.ParseInt(startTime, 10, 64)
|
|
|
+ st, _ = strconv.ParseInt(endTime, 10, 64)
|
|
|
+ } else if reg2.MatchString(startTime) {
|
|
|
+ d1, _ := time.Parse(qu.Date_Short_Layout, startTime)
|
|
|
+ d2, _ := time.Parse(qu.Date_Short_Layout, endTime)
|
|
|
+ st1 = d1.Unix()
|
|
|
+ st = d2.Unix()
|
|
|
+ } else {
|
|
|
+ log.Println("发送邮件指定时间格式错误", startTime, endTime)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else if bcron { //如果是定时任务取cronday的值只能为0或-1,-1只发昨天的数据,0发最近两天的bget不为1的数据
|
|
|
//st1 = st - 86400 //昨天0点
|
|
|
if cronday == 0 {
|
|
|
st = time.Now().Unix() //当前时间
|
|
@@ -191,10 +235,7 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
st1 = st
|
|
|
st = time.Now().Unix()
|
|
|
}
|
|
|
- if startTime != "" && endTime != "" {
|
|
|
- st1, _ = strconv.ParseInt(startTime, 10, 64)
|
|
|
- st, _ = strconv.ParseInt(endTime, 10, 64)
|
|
|
- }
|
|
|
+
|
|
|
dataQuery["createtime"] = map[string]interface{}{
|
|
|
"$gte": st1,
|
|
|
"$lt": st,
|
|
@@ -287,8 +328,9 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
}
|
|
|
item, _ := v1["item"].(string)
|
|
|
if item == "" {
|
|
|
- log.Info("info err,item nil")
|
|
|
- continue
|
|
|
+ item = "all" //默认数据的item为all
|
|
|
+ log.Info("info err,item nil", item)
|
|
|
+ //continue
|
|
|
}
|
|
|
userMap := allUserData[item]
|
|
|
if userMap == nil {
|
|
@@ -330,7 +372,7 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
if sortStr != "" {
|
|
|
sortArr = strings.Split(sortStr, ",")
|
|
|
}
|
|
|
- send := qu.IntAll(v["send"])
|
|
|
+ send := qu.IntAll(v["send"]) //用户配置为1时不发送邮件
|
|
|
sendBool := true
|
|
|
if send == 1 {
|
|
|
sendBool = false
|
|
@@ -344,93 +386,162 @@ func sendmail(appid string, days int, force int, fn string, bcron bool, startTim
|
|
|
}
|
|
|
|
|
|
func GetBidInfoXlsx(appid string, fn string, mails map[string]interface{}, fields map[string]interface{}, allUserData map[string][]map[string]interface{}, sortArr []string, appointMail string, sendBool bool) {
|
|
|
- //信息
|
|
|
- for key, data := range allUserData {
|
|
|
- var email string
|
|
|
- if appointMail == "" {
|
|
|
- email, _ = mails[key].(string)
|
|
|
- if email == "" {
|
|
|
- log.Info("mail nil error", appid, key)
|
|
|
- continue
|
|
|
+ //信息,调整支持合并发送2020-6-29
|
|
|
+ old := true
|
|
|
+ if appointMail == "" {
|
|
|
+ for k, _ := range mails {
|
|
|
+ if len(strings.Split(k, ",")) > 1 {
|
|
|
+ old = false
|
|
|
+ break
|
|
|
}
|
|
|
- } else {
|
|
|
- email = appointMail
|
|
|
}
|
|
|
- log.Println("email", email)
|
|
|
- fx := xlsx.NewFile()
|
|
|
- sheet := xlsx.Sheet{}
|
|
|
-
|
|
|
- style := xlsx.NewStyle()
|
|
|
- border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
|
|
|
- style.Border = border
|
|
|
- style.ApplyBorder = true
|
|
|
- //style.Alignment.WrapText = true
|
|
|
-
|
|
|
- //生表头
|
|
|
- styleHead := xlsx.NewStyle()
|
|
|
- //styleHead.Alignment.WrapText = true
|
|
|
- styleHead.Font.Bold = true
|
|
|
- styleHead.Border = border
|
|
|
- styleHead.ApplyBorder = true
|
|
|
- head := sheet.AddRow()
|
|
|
- arr := []string{}
|
|
|
- if len(sortArr) > 0 {
|
|
|
- for _, t := range sortArr {
|
|
|
- if fields[t] != nil {
|
|
|
- arr = append(arr, t)
|
|
|
- cell := head.AddCell()
|
|
|
- cell.SetValue(Fiels[t])
|
|
|
- cell.SetStyle(styleHead)
|
|
|
+ } else {
|
|
|
+ mails = map[string]interface{}{
|
|
|
+ "-1": appointMail,
|
|
|
+ }
|
|
|
+ old = false
|
|
|
+ }
|
|
|
+ if old {
|
|
|
+ log.Println("老方式发送邮件..")
|
|
|
+ for key, data := range allUserData {
|
|
|
+ var email string
|
|
|
+ if appointMail == "" {
|
|
|
+ email, _ = mails[key].(string)
|
|
|
+ if email == "" {
|
|
|
+ log.Info("mail nil error", appid, key)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ email = appointMail
|
|
|
+ }
|
|
|
+ log.Println("email", email)
|
|
|
+ fx := xlsx.NewFile()
|
|
|
+ sheet := MakeSheet(fields, data, sortArr)
|
|
|
+ fx.AppendSheet(sheet, "剑鱼标讯")
|
|
|
+ //t := time.Now()
|
|
|
+ dir := "./xlsx/" + appid + "/" + key + "/"
|
|
|
+ if b, _ := PathExists(dir); !b {
|
|
|
+ err1 := os.MkdirAll(dir, os.ModePerm)
|
|
|
+ if err1 != nil {
|
|
|
+ log.Println("mkdir err", dir)
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- for _, t := range FielsArr {
|
|
|
- if fields[t] != nil {
|
|
|
- arr = append(arr, t)
|
|
|
- cell := head.AddCell()
|
|
|
- cell.SetValue(Fiels[t])
|
|
|
- cell.SetStyle(styleHead)
|
|
|
+ fname := key + "_" + fn + "_N" + qu.GetRandom(4) + ".xlsx"
|
|
|
+ err := fx.Save(dir + fname)
|
|
|
+ if err != nil {
|
|
|
+ log.Info("xls error", appid, fname)
|
|
|
+ } else {
|
|
|
+ if sendBool {
|
|
|
+ for i := 0; i < len(Gmails); i++ {
|
|
|
+ gmail := Gmails[i]
|
|
|
+ status := mail.GSendMail_q("剑鱼标讯", email, "", "", key+"_"+fn, "", dir+fname, fname, gmail)
|
|
|
+ if status {
|
|
|
+ log.Println("send mail success", appid, email)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- for _, v := range data {
|
|
|
- row := sheet.AddRow()
|
|
|
- for _, t := range arr {
|
|
|
- cell := row.AddCell()
|
|
|
- if v[t] != nil {
|
|
|
- cell.SetValue(v[t])
|
|
|
- cell.SetStyle(style)
|
|
|
+ } else {
|
|
|
+ log.Println("合并发送邮件..")
|
|
|
+ for k, emailObj := range mails {
|
|
|
+ email := emailObj.(string)
|
|
|
+ fx := xlsx.NewFile()
|
|
|
+ bdata := false
|
|
|
+ if k == "-1" {
|
|
|
+ for key, data := range allUserData {
|
|
|
+ sheet := MakeSheet(fields, data, sortArr)
|
|
|
+ fx.AppendSheet(sheet, key)
|
|
|
+ bdata = true
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ks := strings.Split(k, ",")
|
|
|
+ for _, item := range ks {
|
|
|
+ data := allUserData[item]
|
|
|
+ if data != nil {
|
|
|
+ sheet := MakeSheet(fields, data, sortArr)
|
|
|
+ fx.AppendSheet(sheet, item)
|
|
|
+ bdata = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if bdata {
|
|
|
+ dir := "./xlsx/" + appid + "/"
|
|
|
+ if b, _ := PathExists(dir); !b {
|
|
|
+ err1 := os.MkdirAll(dir, os.ModePerm)
|
|
|
+ if err1 != nil {
|
|
|
+ log.Println("mkdir err", dir)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fname := fn + "_N" + qu.GetRandom(4) + ".xlsx"
|
|
|
+ err := fx.Save(dir + fname)
|
|
|
+ if err != nil {
|
|
|
+ log.Info("xls error", appid, fname)
|
|
|
+ } else {
|
|
|
+ if sendBool {
|
|
|
+ for i := 0; i < len(Gmails); i++ {
|
|
|
+ gmail := Gmails[i]
|
|
|
+ status := mail.GSendMail_q("剑鱼标讯", email, "", "", fn, "", dir+fname, fname, gmail)
|
|
|
+ if status {
|
|
|
+ log.Println("send mail success", appid, email)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func MakeSheet(fields map[string]interface{}, data []map[string]interface{}, sortArr []string) xlsx.Sheet {
|
|
|
+ sheet := xlsx.Sheet{}
|
|
|
+ style := xlsx.NewStyle()
|
|
|
+ border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
|
|
|
+ style.Border = border
|
|
|
+ style.ApplyBorder = true
|
|
|
+ //style.Alignment.WrapText = true
|
|
|
|
|
|
- fx.AppendSheet(sheet, "剑鱼标讯")
|
|
|
- //t := time.Now()
|
|
|
- dir := "./xlsx/" + appid + "/" + key + "/"
|
|
|
- if b, _ := PathExists(dir); !b {
|
|
|
- err1 := os.MkdirAll(dir, os.ModePerm)
|
|
|
- if err1 != nil {
|
|
|
- log.Println("mkdir err", dir)
|
|
|
+ //生表头
|
|
|
+ styleHead := xlsx.NewStyle()
|
|
|
+ //styleHead.Alignment.WrapText = true
|
|
|
+ styleHead.Font.Bold = true
|
|
|
+ styleHead.Border = border
|
|
|
+ styleHead.ApplyBorder = true
|
|
|
+ head := sheet.AddRow()
|
|
|
+ arr := []string{}
|
|
|
+ if len(sortArr) > 0 {
|
|
|
+ for _, t := range sortArr {
|
|
|
+ if fields[t] != nil {
|
|
|
+ arr = append(arr, t)
|
|
|
+ cell := head.AddCell()
|
|
|
+ cell.SetValue(Fiels[t])
|
|
|
+ cell.SetStyle(styleHead)
|
|
|
}
|
|
|
}
|
|
|
- fname := key + "_" + fn + "_N" + qu.GetRandom(4) + ".xlsx"
|
|
|
- err := fx.Save(dir + fname)
|
|
|
- if err != nil {
|
|
|
- log.Info("xls error", appid, fname)
|
|
|
- } else {
|
|
|
- if sendBool {
|
|
|
- for i := 0; i < len(Gmails); i++ {
|
|
|
- gmail := Gmails[i]
|
|
|
- status := mail.GSendMail_q("剑鱼标讯", email, "", "", key+"_"+fn, "", dir+fname, fname, gmail)
|
|
|
- if status {
|
|
|
- log.Println("send mail success", appid, email)
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
+ } else {
|
|
|
+ for _, t := range FielsArr {
|
|
|
+ if fields[t] != nil {
|
|
|
+ arr = append(arr, t)
|
|
|
+ cell := head.AddCell()
|
|
|
+ cell.SetValue(Fiels[t])
|
|
|
+ cell.SetStyle(styleHead)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ for _, v := range data {
|
|
|
+ row := sheet.AddRow()
|
|
|
+ for _, t := range arr {
|
|
|
+ cell := row.AddCell()
|
|
|
+ cell.SetStyle(style)
|
|
|
+ if v[t] != nil {
|
|
|
+ cell.SetValue(v[t])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sheet
|
|
|
}
|
|
|
|
|
|
func PathExists(path string) (bool, error) {
|
|
@@ -443,3 +554,55 @@ func PathExists(path string) (bool, error) {
|
|
|
}
|
|
|
return false, err
|
|
|
}
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+xlsx.Sheet{}
|
|
|
+
|
|
|
+ style := xlsx.NewStyle()
|
|
|
+ border := *xlsx.NewBorder("thin", "thin", "thin", "thin")
|
|
|
+ style.Border = border
|
|
|
+ style.ApplyBorder = true
|
|
|
+ //style.Alignment.WrapText = true
|
|
|
+
|
|
|
+ //生表头
|
|
|
+ styleHead := xlsx.NewStyle()
|
|
|
+ //styleHead.Alignment.WrapText = true
|
|
|
+ styleHead.Font.Bold = true
|
|
|
+ styleHead.Border = border
|
|
|
+ styleHead.ApplyBorder = true
|
|
|
+ head := sheet.AddRow()
|
|
|
+ arr := []string{}
|
|
|
+ if len(sortArr) > 0 {
|
|
|
+ for _, t := range sortArr {
|
|
|
+ if fields[t] != nil {
|
|
|
+ arr = append(arr, t)
|
|
|
+ cell := head.AddCell()
|
|
|
+ cell.SetValue(Fiels[t])
|
|
|
+ cell.SetStyle(styleHead)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for _, t := range FielsArr {
|
|
|
+ if fields[t] != nil {
|
|
|
+ arr = append(arr, t)
|
|
|
+ cell := head.AddCell()
|
|
|
+ cell.SetValue(Fiels[t])
|
|
|
+ cell.SetStyle(styleHead)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range data {
|
|
|
+ row := sheet.AddRow()
|
|
|
+ for _, t := range arr {
|
|
|
+ cell := row.AddCell()
|
|
|
+ cell.SetStyle(style)
|
|
|
+ if v[t] != nil {
|
|
|
+ cell.SetValue(v[t])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+*/
|