rdluck 4 лет назад
Родитель
Сommit
0900a0ed59
3 измененных файлов с 194 добавлено и 196 удалено
  1. 40 6
      models/report_view.go
  2. 154 188
      services/report_view.go
  3. 0 2
      services/task.go

+ 40 - 6
models/report_view.go

@@ -47,10 +47,10 @@ type ResearchReportViewersDetail struct {
 	ResearchReportName string
 	ReportVariety      string
 	ResearchReportType string
-	ReportCreateDate string
+	ReportCreateDate   string
 }
 
-func GetResearchReportViewersDetail(startTime,endTime, reportType string) (items []*ResearchReportViewersDetail, err error) {
+func GetResearchReportViewersDetail(startTime, endTime, reportType string) (items []*ResearchReportViewersDetail, err error) {
 	sql := `
 			select u.real_name,c.company_name,uvh.created_time,rr.research_report_name,ifnull(rct.report_chapter_type_name,rr.report_variety) as report_variety,rr.type as research_report_type
 			from user_view_history uvh
@@ -65,11 +65,11 @@ func GetResearchReportViewersDetail(startTime,endTime, reportType string) (items
 			and c.company_id not in (1,16)
 `
 	o := orm.NewOrm()
-	_, err = o.Raw(sql, startTime,endTime, reportType).QueryRows(&items)
+	_, err = o.Raw(sql, startTime, endTime, reportType).QueryRows(&items)
 	return
 }
 
-func GetRddpReportViewersDetail(startTime,endTime string) (items []*ResearchReportViewersDetail, err error) {
+func GetRddpReportViewersDetail(startTime, endTime string) (items []*ResearchReportViewersDetail, err error) {
 	sql := `
 			SELECT u.real_name,c.company_name,uvh.create_time as created_time,REPLACE(SUBSTRING(r.create_time,6,5),'-','') AS report_create_date,r.title as research_report_name,r.classify_name_second as research_report_type
 			FROM hongze_rddp.report_view_record AS uvh
@@ -82,6 +82,40 @@ func GetRddpReportViewersDetail(startTime,endTime string) (items []*ResearchRepo
 			ORDER BY uvh.create_time DESC
 `
 	o := orm.NewOrm()
-	_, err = o.Raw(sql, startTime,endTime).QueryRows(&items)
+	_, err = o.Raw(sql, startTime, endTime).QueryRows(&items)
 	return
-}
+}
+
+type HistoryViewTimes struct {
+	RealName    string
+	CompanyName string
+	ViewCount   int
+	CreateTime  string
+	UserId      int
+}
+
+func GetHistoryViewTimes() (items []*HistoryViewTimes, err error) {
+	sql := `select u.user_id,u.real_name,c.company_name,count(distinct(DATE_FORMAT(uvh.created_time,'%Y-%m-%d'))) AS view_count,max(uvh.created_time) AS create_time
+	from user_view_history uvh
+	inner join wx_user u on u.user_id = uvh.user_id
+	inner join company c on c.company_id = u.company_id
+	and c.company_id not in (1,16)
+	group by uvh.user_id 
+	order by max(uvh.created_time) desc `
+	o := orm.NewOrm()
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
+func GetRddpHistoryViewTimes() (items []*HistoryViewTimes, err error) {
+	sql := `select u.user_id,u.real_name,c.company_name,count(distinct(DATE_FORMAT(rvr.create_time,'%Y-%m-%d'))) AS view_count ,max(rvr.create_time) AS create_time
+	from hongze_rddp.report_view_record rvr
+	inner join wx_user u on u.user_id = rvr.user_id
+	inner join company c on c.company_id = u.company_id
+	and c.company_id not in (1,16)
+	group by rvr.user_id 
+	order by max(rvr.create_time) desc `
+	o := orm.NewOrm()
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 154 - 188
services/report_view.go

@@ -13,249 +13,215 @@ import (
 //报告历史访问次数
 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())
+		if err != nil {
+			fmt.Println("crete ReportViewTimes err:", err.Error())
+			utils.FileLog.Info("crete ReportViewTimes err: %s", err.Error())
 		}
 	}()
-	typeList:=make([]*models.ReportType,0)
-	day:=new(models.ReportType)
-	day.TypeName="晨报"
-	day.TypeValue="day"
-	typeList=append(typeList,day)
-
-	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)
 
 	//创建excel
-	file:=xlsx.NewFile()
-	for _, v := range typeList {
-		fmt.Println(v.TypeValue,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="历史访问人数"
+	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.Value = strconv.Itoa(item.ViewCount)
+		cellD := row.AddCell()
+		cellD.Value = item.CreateTime
+	}
 
-		fmt.Println(v.TypeName, v.TypeValue)
-		if v.TypeValue == "rddp" {
-			items, err := models.GetRddpReportViewers()
-			if err != nil {
-				return err
-			}
-			for _, item := range items {
-				row := sheet.AddRow()
-				cellA:=row.AddCell()
-				cellA.Value=item.ResearchReportName+"("+item.CreateDate+")"
-				cellB:=row.AddCell()
-				cellB.Value=v.TypeName
-				cellC:=row.AddCell()
-				cellC.Value=strconv.Itoa(item.Count)
-				cellD:=row.AddCell()
-				cellD.Value=strconv.Itoa(item.UserCount)
-			}
-		} else {
-			items, err := models.GetResearchReportViewers(v.TypeValue)
-			if err != nil {
-				return err
-			}
-			for _, item := range items {
-				row := sheet.AddRow()
-				cellA:=row.AddCell()
-				cellA.Value=item.ResearchReportName
-				cellB:=row.AddCell()
-				cellB.Value=v.TypeName
-				cellC:=row.AddCell()
-				cellC.Value=strconv.Itoa(item.Count)
-				cellD:=row.AddCell()
-				cellD.Value=strconv.Itoa(item.UserCount)
-			}
-		}
+	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.Value = strconv.Itoa(item.ViewCount)
+		cellD := row.AddCell()
+		cellD.Value = item.CreateTime
 	}
-	savePath:="report_view_times"+time.Now().Format(utils.FormatDateTimeUnSpace)+".xlsx"
-	err=file.Save("./"+savePath)
-	if err!=nil {
+
+	savePath := "report_view_times" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	err = file.Save("./" + savePath)
+	if err != nil {
 		return
 	}
 	//发送邮件
-	title:="用户访问次数',\"你好,上周用户访问次数见附件。"
+	title := "用户访问次数"
+	content := "你好,上周用户访问次数见附件。"
 	fmt.Println("start send email")
-	sendResult:=utils.SendEmailByHongze(title,"",utils.EmailSendToHzUsers,savePath)
+	//sendResult := utils.SendEmailByHongze(title, content, utils.EmailSendToHzUsers, savePath)
+	sendResult:=utils.SendEmailByHongze(title,content,utils.EmailSendToMe,savePath)
 	if sendResult {
 		os.Remove(savePath)
 	}
-	fmt.Println("send result:",sendResult)
+	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())
+		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)
-
-	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)
+	typeList := make([]*models.ReportType, 0)
+	day := new(models.ReportType)
+	day.TypeName = "晨报"
+	day.TypeValue = "day"
+	typeList = append(typeList, day)
+
+	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()
+	file := xlsx.NewFile()
 	for _, v := range typeList {
 		fmt.Println(v.TypeName, v.TypeValue)
 		if v.TypeValue == "rddp" {
-			sheet,err:=file.AddSheet(v.TypeName+"阅读统计")
-			if err!=nil {
+			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)
+			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
+				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 {
-			sheet,err:=file.AddSheet(v.TypeName+"研报阅读统计")
-			if err!=nil {
+			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)
+			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
+				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
 			}
 		}
 	}
-	savePath:="report_viewer_details"+time.Now().Format(utils.FormatDateTimeUnSpace)+".xlsx"
-	err=file.Save("./"+savePath)
-	if err!=nil {
+	savePath := "report_viewer_details" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	err = file.Save("./" + savePath)
+	if err != nil {
 		return
 	}
 	//发送邮件
-	title:="研报阅读统计报表"
+	title := "研报阅读统计报表"
 	fmt.Println("start send email")
-	sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToHzUsers,savePath)
+	sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath)
 	//sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
 	if sendResult {
 		os.Remove(savePath)
 	}
-	fmt.Println("send result:",sendResult)
+	fmt.Println("send result:", sendResult)
 	fmt.Println("end send email")
 	return nil
 }

+ 0 - 2
services/task.go

@@ -7,7 +7,6 @@ import (
 
 func Task() {
 	fmt.Println("start")
-
 	//报告历史访问次数
 	reportViewTimes := toolbox.NewTask("reportViewTimes", "0 0 12 * * 0 ", ReportViewTimes)
 	toolbox.AddTask("reportViewTimes", reportViewTimes)
@@ -16,6 +15,5 @@ func Task() {
 	toolbox.AddTask("reportViewDetail", reportViewDetail)
 
 	toolbox.StartTask()
-
 	fmt.Println("end")
 }