浏览代码

发送邮件

rdluck 4 年之前
父节点
当前提交
924ca46a74
共有 8 个文件被更改,包括 230 次插入15 次删除
  1. 25 0
      models/free_viewer.go
  2. 23 0
      models/users.go
  3. 77 0
      services/free_viewer.go
  4. 5 5
      services/report_view.go
  5. 15 7
      services/task.go
  6. 73 0
      services/users.go
  7. 2 1
      utils/constants.go
  8. 10 2
      utils/email.go

+ 25 - 0
models/free_viewer.go

@@ -0,0 +1,25 @@
+package models
+
+import "rdluck_tools/orm"
+
+type FreeViewerDetails struct {
+	RealName       string
+	Mobile         string
+	Note           string
+	CreatedTime    string
+	MaxCreatedTime string
+}
+
+func GetFreeViewerDetails(startTime,endTime string) (items []*FreeViewerDetails, err error) {
+	sql := `select u.real_name,u.mobile,u.note,u.created_time,max(uvh.created_time) as max_created_time
+	from wx_user u
+	LEFT JOIN user_view_history uvh on u.user_id = uvh.user_id
+	where company_id = 1
+	and apply_method<>2
+	and mobile is not null
+	and u.created_time > ?
+	and u.created_time <= ?
+	group by u.user_id`
+	_, err = orm.NewOrm().Raw(sql, startTime,endTime).QueryRows(&items)
+	return
+}

+ 23 - 0
models/users.go

@@ -0,0 +1,23 @@
+package models
+
+import "rdluck_tools/orm"
+
+type HongzeUsers struct {
+	RealName    string
+	CompanyName string
+	Mobile      string
+	IsRegister  string
+	UserType    string
+}
+
+func GetHongzeUsers() (items []*HongzeUsers, err error) {
+	sql := `select wu.real_name,c.company_name,left(wu.mobile, 11) as mobile ,if(wu.open_id is null,'否','是') as is_register,if(c.type in (1),'是','否') as user_type
+	from wx_user wu
+	inner join company c on c.company_id = wu.company_id
+	where c.company_id <> 1 and c.company_id <> 16
+	and c.type in (1)
+	order by company_name desc `
+	_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
+	return
+
+}

+ 77 - 0
services/free_viewer.go

@@ -0,0 +1,77 @@
+package services
+
+import (
+	"fmt"
+	"github.com/tealeg/xlsx"
+	"hongze/hongze_task/models"
+	"hongze/hongze_task/utils"
+	"os"
+	"time"
+)
+
+//潜在客户回访记录
+func FreeViewerDetail() (err error) {
+	defer func() {
+		if err != nil {
+			fmt.Println("crete FreeViewerDetail err:", err.Error())
+			utils.FileLog.Info("crete FreeViewerDetail err: %s", err.Error())
+		}
+	}()
+
+	//endTime := time.Now().Format(utils.FormatDateTime)
+	startTime := "2020-06-07 12:00:00" //time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
+	endTime :="2020-06-14 12:00:00"
+	items,err:=models.GetFreeViewerDetails(startTime,endTime)
+	if err!=nil {
+		return
+	}
+	title:="潜在客户回访记录"
+	//创建excel
+	file := xlsx.NewFile()
+	sheet, err := file.AddSheet(title)
+	if err != nil {
+		return err
+	}
+	//标头
+	rowTitle := sheet.AddRow()
+	cellA := rowTitle.AddCell()
+	cellA.Value = "姓名"
+	cellB := rowTitle.AddCell()
+	cellB.Value = "手机号"
+	cellC := rowTitle.AddCell()
+	cellC.Value = "客户公司"
+	cellD := rowTitle.AddCell()
+	cellD.Value = "创建时间"
+	cellE := rowTitle.AddCell()
+	cellE.Value = "最后访问时间"
+
+	for _, item := range items {
+		row := sheet.AddRow()
+		cellA := row.AddCell()
+		cellA.Value = item.RealName
+		cellB := row.AddCell()
+		cellB.Value = item.Mobile
+		cellC := row.AddCell()
+		cellC.Value=item.Note
+		cellD := row.AddCell()
+		cellD.Value = item.CreatedTime
+		cellE := row.AddCell()
+		cellE.Value = item.MaxCreatedTime
+	}
+
+	savePath := "free_viewer_details"+ time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	err = file.Save("./" + savePath)
+	if err != nil {
+		return
+	}
+	//发送邮件
+	fmt.Println("start send email")
+	sendResult := utils.SendEmailByHongze(title, "潜在客户回访记录',\"你好,上周潜在客户回访记录见附件。", utils.EmailSendToHzUsers, savePath,title+".xlsx")
+	//sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
+	if sendResult {
+		os.Remove(savePath)
+	}
+	fmt.Println("send result:", sendResult)
+	fmt.Println("end send email")
+	return nil
+}

+ 5 - 5
services/report_view.go

@@ -67,16 +67,16 @@ func ReportViewTimes() (err error) {
 		cellD.Value = item.CreateTime
 	}
 
+	title := "用户访问次数"
 	savePath := "report_view_times" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
 	err = file.Save("./" + savePath)
 	if err != nil {
 		return
 	}
 	//发送邮件
-	title := "用户访问次数"
 	content := "你好,上周用户访问次数见附件。"
 	fmt.Println("start send email")
-	sendResult := utils.SendEmailByHongze(title, content, utils.EmailSendToHzUsers, savePath)
+	sendResult := utils.SendEmailByHongze(title, content, utils.EmailSendToHzUsers, savePath,title+".xlsx")
 	//sendResult:=utils.SendEmailByHongze(title,content,utils.EmailSendToMe,savePath)
 	if sendResult {
 		os.Remove(savePath)
@@ -207,15 +207,15 @@ func ReportViewDetail() (err error) {
 			}
 		}
 	}
-	savePath := "report_viewer_details" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	title := "研报阅读统计报表"
+	savePath := "report_view_detail"+ time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
 	err = file.Save("./" + savePath)
 	if err != nil {
 		return
 	}
 	//发送邮件
-	title := "研报阅读统计报表"
 	fmt.Println("start send email")
-	sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath)
+	sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath,title+".xlsx")
 	//sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
 	if sendResult {
 		os.Remove(savePath)

+ 15 - 7
services/task.go

@@ -7,13 +7,21 @@ import (
 
 func Task() {
 	fmt.Println("start")
+	//发送邮件
+	sendEmail := toolbox.NewTask("sendEmail", "0 0 12 * * 0 ", SendEmail)
+	toolbox.AddTask("sendEmail", sendEmail)
+	toolbox.StartTask()
+	fmt.Println("end")
+}
+
+func SendEmail() (err error) {
 	//报告历史访问次数
-	reportViewTimes := toolbox.NewTask("reportViewTimes", "0 0 12 * * 0 ", ReportViewTimes)
-	toolbox.AddTask("reportViewTimes", reportViewTimes)
+	go ReportViewTimes()
 	//报告访问详情
-	reportViewDetail := toolbox.NewTask("reportViewDetail", "0 0 12 * * 0 ", ReportViewDetail)
-	toolbox.AddTask("reportViewDetail", reportViewDetail)
-
-	//toolbox.StartTask()
-	fmt.Println("end")
+	go ReportViewDetail()
+	//潜在客户回访记录
+	go FreeViewerDetail()
+	//用户权限统计
+	go HongzeUsers()
+	return
 }

+ 73 - 0
services/users.go

@@ -0,0 +1,73 @@
+package services
+
+import (
+	"fmt"
+	"github.com/tealeg/xlsx"
+	"hongze/hongze_task/models"
+	"hongze/hongze_task/utils"
+	"os"
+	"time"
+)
+
+//用户权限统计
+func HongzeUsers() (err error) {
+	defer func() {
+		if err != nil {
+			fmt.Println("crete HongzeUsers err:", err.Error())
+			utils.FileLog.Info("crete HongzeUsers err: %s", err.Error())
+		}
+	}()
+
+	items,err:=models.GetHongzeUsers()
+	if err!=nil {
+		return
+	}
+	title:="用户权限统计"
+	//创建excel
+	file := xlsx.NewFile()
+	sheet, err := file.AddSheet(title)
+	if err != nil {
+		return err
+	}
+	//标头
+	rowTitle := sheet.AddRow()
+	cellA := rowTitle.AddCell()
+	cellA.Value = "用户名称"
+	cellB := rowTitle.AddCell()
+	cellB.Value = "公司名称"
+	cellC := rowTitle.AddCell()
+	cellC.Value = "手机号"
+	cellD := rowTitle.AddCell()
+	cellD.Value = "是否使用"
+	cellE := rowTitle.AddCell()
+	cellE.Value = "是否付费用户"
+
+	for _, item := range items {
+		row := sheet.AddRow()
+		cellA := row.AddCell()
+		cellA.Value = item.RealName
+		cellB := row.AddCell()
+		cellB.Value = item.CompanyName
+		cellC := row.AddCell()
+		cellC.Value=item.Mobile
+		cellD := row.AddCell()
+		cellD.Value = item.IsRegister
+		cellE := row.AddCell()
+		cellE.Value = item.UserType
+	}
+
+	savePath := "hongze_user"+ time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	err = file.Save("./" + savePath)
+	if err != nil {
+		return
+	}
+	//发送邮件
+	fmt.Println("start send email")
+	sendResult := utils.SendEmailByHongze(title, "用户权限统计',\"你好,用户权限统计见附件。", utils.EmailSendToHzUsers, savePath,title+".xlsx")
+	if sendResult {
+		os.Remove(savePath)
+	}
+	fmt.Println("send result:", sendResult)
+	fmt.Println("end send email")
+	return nil
+}

+ 2 - 1
utils/constants.go

@@ -56,6 +56,7 @@ const (
 )
 
 const (
-	EmailSendToHzUsers = "544935339@qq.com;lnyan@hzinsights.com;pdzhao@hzinsights.com;317699326@qq.com"
+	EmailSendToHzUsers = "544935339@qq.com;lnyan@hzinsights.com;pdzhao@hzinsights.com;glji@hzinsights.com"
+	//EmailSendToHzUsers = "317699326@qq.com"
 )
 

+ 10 - 2
utils/email.go

@@ -3,6 +3,7 @@ package utils
 import (
 	"fmt"
 	"gopkg.in/gomail.v2"
+	"mime"
 	"strings"
 )
 
@@ -32,7 +33,7 @@ func SendEmail(title, content string, touser string)bool {
 }
 
 //发送邮件
-func SendEmailByHongze(title, content string, touser,attachPath string)bool {
+func SendEmailByHongze(title, content string, touser,attachPath,attachName string)bool {
 	var arr []string
 	sub := strings.Index(touser, ";")
 	if sub >= 0 {
@@ -50,7 +51,14 @@ func SendEmailByHongze(title, content string, touser,attachPath string)bool {
 	m.SetBody("text/html", content)
 
 	//body := new(bytes.Buffer)
-	m.Attach(attachPath)
+	m.Attach(attachPath,
+		gomail.Rename(attachName),
+		gomail.SetHeader(map[string][]string{
+			"Content-Disposition": []string{
+				fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", attachName)),
+			},
+		}),)
+
 	d := gomail.NewDialer("smtp.mxhichina.com", 465, "public@hzinsights.com", "Hzinsights2018")
 	if err := d.DialAndSend(m); err != nil {
 		fmt.Println("send err:",err.Error())