123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- package services
- import (
- "fmt"
- "github.com/tealeg/xlsx"
- "hongze/hongze_task/models"
- "hongze/hongze_task/utils"
- "os"
- "time"
- )
- // 报告历史访问次数
- func ReportViewTimes() (err error) {
- defer func() {
- if err != nil {
- fmt.Println("crete ReportViewTimes err:", err.Error())
- utils.FileLog.Info("crete ReportViewTimes err: %s", err.Error())
- }
- }()
- //创建excel
- file := xlsx.NewFile()
- sheet, err := file.AddSheet("用户访问次数")
- 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 = "最近一次访问日期"
- rddpItems, err := models.GetRddpHistoryViewTimes()
- items, err := models.GetHistoryViewTimes()
- if err != nil {
- return err
- }
- 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.SetInt(item.ViewCount)
- cellD := row.AddCell()
- cellD.Value = item.CreateTime
- }
- for _, item := range rddpItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.SetInt(item.ViewCount)
- cellD := row.AddCell()
- cellD.Value = item.CreateTime
- }
- title := "用户访问次数"
- savePath := "report_view_times" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- err = file.Save("./" + savePath)
- if err != nil {
- return
- }
- //发送邮件
- content := "你好,上周用户访问次数见附件。"
- fmt.Println("start send email")
- sendResult := utils.SendEmailByHongze(title, content, utils.EmailSendToHzUsers, savePath, title+".xlsx")
- //sendResult:=utils.SendEmailByHongze(title,content,utils.EmailSendToMe,savePath)
- if sendResult {
- os.Remove(savePath)
- }
- fmt.Println("send result:", sendResult)
- fmt.Println("end send email")
- return nil
- }
- // 报告访问详情
- func ReportViewDetail() (err error) {
- defer func() {
- if err != nil {
- fmt.Println("crete ReportViewDetail err:", err.Error())
- utils.FileLog.Info("crete ReportViewDetail err: %s", err.Error())
- }
- }()
- typeList := make([]*models.ReportType, 0)
- day := new(models.ReportType)
- day.TypeName = "晨报"
- day.TypeValue = "day"
- typeList = append(typeList, day)
- advisory := new(models.ReportType)
- advisory.TypeName = "每日商品聚焦"
- advisory.TypeValue = "advisory"
- typeList = append(typeList, advisory)
- week := new(models.ReportType)
- week.TypeName = "周报"
- week.TypeValue = "week"
- typeList = append(typeList, week)
- two_week := new(models.ReportType)
- two_week.TypeName = "双周报"
- two_week.TypeValue = "two_week"
- typeList = append(typeList, two_week)
- month := new(models.ReportType)
- month.TypeName = "月报"
- month.TypeValue = "month"
- typeList = append(typeList, month)
- other := new(models.ReportType)
- other.TypeName = "数据点评"
- other.TypeValue = "other"
- typeList = append(typeList, other)
- rddp := new(models.ReportType)
- rddp.TypeName = "日度点评"
- rddp.TypeValue = "rddp"
- typeList = append(typeList, rddp)
- startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
- endTime := time.Now().Format(utils.FormatDateTime)
- //创建excel
- file := xlsx.NewFile()
- for _, v := range typeList {
- fmt.Println(v.TypeName, v.TypeValue)
- if v.TypeName == `晨报` {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- //新晨报的数据
- weekItems, err := models.GetRddpDayReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- for _, item := range weekItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.Value = item.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- continue
- } else if v.TypeName == `周报` {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- //新周报的数据
- weekItems, err := models.GetRddpWeekReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- for _, item := range weekItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.Value = item.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- continue
- } else if v.TypeName == `双周报` {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- //新双周报的数据
- weekItems, err := models.GetRddpTwoWeekReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- for _, item := range weekItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.Value = item.CreatedTime
- cellD := row.AddCell()
- cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ClassifyNameSecond
- cellF := row.AddCell()
- cellF.Value = `大宗商品`
- }
- continue
- } else if v.TypeName == `月报` {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- //新月报的数据
- monthItems, err := models.GetRddpMonthReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- for _, item := range monthItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.Value = item.CreatedTime
- cellD := row.AddCell()
- cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ClassifyNameSecond
- cellF := row.AddCell()
- cellF.Value = `月报`
- }
- continue
- } else if v.TypeName == `数据点评` {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- //新数据点评的数据
- monthItems, err := models.GetRddpDataReviewReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- for _, item := range monthItems {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CompanyName
- cellC := row.AddCell()
- cellC.Value = item.CreatedTime
- cellD := row.AddCell()
- cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ClassifyNameSecond
- cellF := row.AddCell()
- cellF.Value = item.ClassifyNameFirst
- }
- continue
- }
- if v.TypeValue == "rddp" {
- sheet, err := file.AddSheet(v.TypeName + "阅读统计")
- 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 = "访问标题"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetRddpReportViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
- cellE := row.AddCell()
- cellE.Value = v.TypeName
- }
- } else if v.TypeValue == "advisory" {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "报告类型"
- items, err := models.GetAdvisoryViewersDetail(startTime, endTime)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = v.TypeName
- }
- } else {
- sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
- 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 = "访问页面"
- cellF := rowTitle.AddCell()
- cellF.Value = "报告类型"
- items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
- if err != nil {
- return err
- }
- 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.CreatedTime
- cellD := row.AddCell()
- cellD.Value = item.ResearchReportName
- cellE := row.AddCell()
- cellE.Value = item.ReportVariety
- cellF := row.AddCell()
- cellF.Value = v.TypeName
- }
- }
- }
- title := "研报阅读统计报表"
- savePath := "report_view_detail" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- err = file.Save("./" + savePath)
- fmt.Println(err)
- 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
- }
|