Quellcode durchsuchen

fix:新增获取用户阅读数据报表接口

Roc vor 3 Jahren
Ursprung
Commit
57a1e7a359

+ 53 - 40
controllers/company_user.go

@@ -1,6 +1,7 @@
 package controllers
 
 import (
+	"hongze/hongze_open_api/logic"
 	"hongze/hongze_open_api/models/response/company_user"
 	"hongze/hongze_open_api/models/tables"
 	"hongze/hongze_open_api/models/tables/wx_user"
@@ -24,11 +25,11 @@ type CompanyUser struct {
 // @Param   email   query   string  false       "用户邮箱"
 // @Success 200 {object} company_user.UserReportListResp
 // @router /user_report_list [get]
-func (c *CompanyUser) GetUserReportList()  {
-	mobile:=c.GetString("mobile","")
-	email:=c.GetString("email","")
-	startDate:=c.GetString("start_date","")
-	endDate:=c.GetString("end_date","")
+func (c *CompanyUser) GetUserReportList() {
+	mobile := c.GetString("mobile", "")
+	email := c.GetString("email", "")
+	startDate := c.GetString("start_date", "")
+	endDate := c.GetString("end_date", "")
 
 	//}
 	//	return
@@ -41,29 +42,26 @@ func (c *CompanyUser) GetUserReportList()  {
 
 	startTime := ``
 	endTime := ``
-	if startDate != "" && endDate != ""{
-		startDateTime,err := time.Parse(utils.FormatDate,startDate)
-		if err != nil{
+	if startDate != "" && endDate != "" {
+		startDateTime, err := time.Parse(utils.FormatDate, startDate)
+		if err != nil {
 			c.FailWithMessage("start_date格式异常")
 			return
 		}
-		endDateTime,err := time.Parse(utils.FormatDate,endDate)
-		if err != nil{
+		endDateTime, err := time.Parse(utils.FormatDate, endDate)
+		if err != nil {
 			c.FailWithMessage("end_date格式异常")
 			return
 		}
-		startTime = startDateTime.Format(utils.FormatDate)+" 00:00:00"
-		endTime = endDateTime.Format(utils.FormatDate)+" 23:59:59"
+		startTime = startDateTime.Format(utils.FormatDate) + " 00:00:00"
+		endTime = endDateTime.Format(utils.FormatDate) + " 23:59:59"
 	}
 
-
 	pageSize := c.PageSize
 	startSize := c.StartSize
 	currentIndex := c.StartPage
 
-
-
-	total,list, err := tables.GetViewReportList(mobile,email,startTime,endTime,startSize,pageSize)
+	total, list, err := tables.GetViewReportList(mobile, email, startTime, endTime, startSize, pageSize)
 	if err != nil {
 		c.FailWithMessage("获取失败")
 		return
@@ -98,10 +96,10 @@ func (c *CompanyUser) GetUserReportList()  {
 	page := utils.GetPaging(currentIndex, pageSize, total)
 
 	resp := company_user.UserReportListResp{
-		List: list,
+		List:   list,
 		Paging: page,
 	}
-	c.OkDetailed(resp,"获取成功")
+	c.OkDetailed(resp, "获取成功")
 }
 
 // GetUserReportListV2
@@ -111,31 +109,31 @@ func (c *CompanyUser) GetUserReportList()  {
 // @Param   email   query   string  false       "用户邮箱"
 // @Success 200 {object} company_user.UserReportListResp
 
-func (c *CompanyUser) GetUserReportListV2()  {
-	mobile:=c.GetString("mobile","")
-	startDate:=c.GetString("start_date","")
-	endDate:=c.GetString("end_date","")
-	if startDate == ""{
+func (c *CompanyUser) GetUserReportListV2() {
+	mobile := c.GetString("mobile", "")
+	startDate := c.GetString("start_date", "")
+	endDate := c.GetString("end_date", "")
+	if startDate == "" {
 		c.FailWithMessage("start_date必传")
 		return
 	}
-	if endDate == ""{
+	if endDate == "" {
 		c.FailWithMessage("end_date必传")
 		return
 	}
-	startDateTime,err := time.Parse(utils.FormatDate,startDate)
-	if err != nil{
+	startDateTime, err := time.Parse(utils.FormatDate, startDate)
+	if err != nil {
 		c.FailWithMessage("start_date格式异常")
 		return
 	}
-	endDateTime,err := time.Parse(utils.FormatDate,endDate)
-	if err != nil{
+	endDateTime, err := time.Parse(utils.FormatDate, endDate)
+	if err != nil {
 		c.FailWithMessage("end_date格式异常")
 		return
 	}
 	var wxUserInfo *wx_user.WxUserItem
-	if mobile!=""{
-		item, err :=  wx_user.GetWxUserByMobile(mobile)
+	if mobile != "" {
+		item, err := wx_user.GetWxUserByMobile(mobile)
 		if err != nil {
 			c.FailWithMessage("找不到该用户")
 			return
@@ -143,16 +141,16 @@ func (c *CompanyUser) GetUserReportListV2()  {
 		wxUserInfo = item
 	}
 
-	email:=c.GetString("email","")
-	if wxUserInfo == nil && email!=""{
-		item, err :=  wx_user.GetWxUserByEmail(email)
+	email := c.GetString("email", "")
+	if wxUserInfo == nil && email != "" {
+		item, err := wx_user.GetWxUserByEmail(email)
 		if err != nil {
 			c.FailWithMessage("找不到该用户")
 			return
 		}
 		wxUserInfo = item
 	}
-	if wxUserInfo == nil{
+	if wxUserInfo == nil {
 		c.FailWithMessage("找不该用户")
 	}
 
@@ -160,11 +158,11 @@ func (c *CompanyUser) GetUserReportListV2()  {
 	startSize := c.StartSize
 	currentIndex := c.StartPage
 
-	total,list, err := tables.GetViewReportList(wxUserInfo.Mobile,wxUserInfo.Email,startDateTime.Format(utils.FormatDate)+" 00:00:00",endDateTime.Format(utils.FormatDate)+" 23:59:59",startSize,pageSize)
+	total, list, err := tables.GetViewReportList(wxUserInfo.Mobile, wxUserInfo.Email, startDateTime.Format(utils.FormatDate)+" 00:00:00", endDateTime.Format(utils.FormatDate)+" 23:59:59", startSize, pageSize)
 	if err != nil {
-			c.FailWithMessage("获取失败")
-			return
-		}
+		c.FailWithMessage("获取失败")
+		return
+	}
 
 	for k, v := range list {
 		if v.ReportType == "day" {
@@ -194,8 +192,23 @@ func (c *CompanyUser) GetUserReportListV2()  {
 	page := utils.GetPaging(currentIndex, pageSize, total)
 
 	resp := company_user.UserReportListResp{
-		List: list,
+		List:   list,
 		Paging: page,
 	}
-	c.OkDetailed(resp,"获取成功")
+	c.OkDetailed(resp, "获取成功")
+}
+
+// GetUserReportViewTotalList
+// @Title 获取用户阅读数据报表
+// @Description 获取用户阅读数据报表
+// @Success 200 {object} []logic.UserView
+// @router /view_total_list [get]
+func (c *CompanyUser) GetUserReportViewTotalList() {
+	list, err := logic.GetUserViewTotal()
+	if err != nil {
+		c.FailWithMessage("获取失败")
+		return
+	}
+	resp := list
+	c.OkDetailed(resp, "获取成功")
 }

+ 107 - 0
logic/company_user.go

@@ -0,0 +1,107 @@
+package logic
+
+import (
+	"encoding/json"
+	"fmt"
+	"hongze/hongze_open_api/models/tables"
+	"hongze/hongze_open_api/models/tables/user_view_statistics"
+	"hongze/hongze_open_api/utils"
+	"sort"
+	"time"
+)
+
+// UserView 用户阅读
+type UserView struct {
+	Phone string `json:"phone" description:"手机号"`
+	Count int    `json:"count" description:"阅读数"`
+}
+
+// GetUserViewTotal 获取用户阅读报告数量统计
+func GetUserViewTotal() (list []UserView, err error) {
+	dayStr := time.Now().AddDate(0, 0, -30).Format(utils.FormatDate)
+	key := "openapi:user_view_total:" + dayStr
+	redisJsonData, redisErr := utils.Rc.RedisString(key)
+	if redisErr == nil {
+		err = json.Unmarshal([]byte(redisJsonData), &list)
+		if err != nil {
+			fmt.Println("开放api获取用户阅读报告数量统计,json转换失败")
+		}
+		return
+	}
+
+	userViewMap := make(map[string]UserView)
+	mobileList := make([]string, 0)
+	historyUserViewList, err := user_view_statistics.GetUserViewListByDate(dayStr)
+	if err != nil {
+		return
+	}
+	for _, historyUserView := range historyUserViewList {
+		userViewMap[historyUserView.Mobile] = UserView{
+			Phone: historyUserView.Mobile,
+			Count: historyUserView.Total,
+		}
+		mobileList = append(mobileList, historyUserView.Mobile)
+	}
+
+	todayStr := time.Now().Format(utils.FormatDate)
+	tmpList, err := tables.GetCountUserViewHistoryByMobile(todayStr)
+	if err != nil {
+		return
+	}
+	for _, tmpUserView := range tmpList {
+		if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
+			userView.Count += tmpUserView.Total
+			userViewMap[tmpUserView.Mobile] = userView
+		} else {
+			userViewMap[tmpUserView.Mobile] = UserView{
+				Phone: tmpUserView.Mobile,
+				Count: tmpUserView.Total,
+			}
+			mobileList = append(mobileList, tmpUserView.Mobile)
+		}
+	}
+
+	tmpList, err = tables.GetReportViewMaxTimeByMobile(todayStr)
+	if err != nil {
+		return
+	}
+	for _, tmpUserView := range tmpList {
+		if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
+			userView.Count += tmpUserView.Total
+			userViewMap[tmpUserView.Mobile] = userView
+		} else {
+			userViewMap[tmpUserView.Mobile] = UserView{
+				Phone: tmpUserView.Mobile,
+				Count: tmpUserView.Total,
+			}
+			mobileList = append(mobileList, tmpUserView.Mobile)
+		}
+	}
+	tmpList, err = tables.GetAdvisoryCountUserViewHistoryByMobile(todayStr)
+	if err != nil {
+		return
+	}
+	for _, tmpUserView := range tmpList {
+		if userView, ok := userViewMap[tmpUserView.Mobile]; ok {
+			userView.Count += tmpUserView.Total
+			userViewMap[tmpUserView.Mobile] = userView
+		} else {
+			userViewMap[tmpUserView.Mobile] = UserView{
+				Phone: tmpUserView.Mobile,
+				Count: tmpUserView.Total,
+			}
+			mobileList = append(mobileList, tmpUserView.Mobile)
+		}
+	}
+
+	sort.Strings(mobileList)
+	for _, mobile := range mobileList {
+		list = append(list, userViewMap[mobile])
+		delete(userViewMap, mobile)
+	}
+	redisJsonByteData, err := json.Marshal(list)
+	if err == nil {
+		_ = utils.Rc.Put(key, string(redisJsonByteData), time.Minute*30)
+	}
+	return
+}

+ 63 - 0
models/tables/company_user.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"hongze/hongze_open_api/utils"
 	"rdluck_tools/orm"
+	"time"
 )
 
 type ViewReportList struct {
@@ -418,3 +419,65 @@ func GetViewReportCount(mobile, email string) (total int, err error) {
 
 	return
 }
+
+// UserViewMobileTotalSlice 根据用户手机号字符串获取用户的浏览数
+type UserViewMobileTotalSlice struct {
+	Mobile      string    `description:"用户手机号"`
+	Total       int       `description:"总阅读数"`
+	CreatedTime time.Time `description:"用户浏览时间"`
+}
+
+// UserViewEmailTotalSlice 根据用户邮箱字符串获取用户的浏览数
+type UserViewEmailTotalSlice struct {
+	Email       string    `description:"用户邮箱"`
+	Total       int       `description:"总阅读数"`
+	CreatedTime time.Time `description:"用户浏览时间"`
+}
+
+func GetCountUserViewHistoryByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,mobile,max(created_time) as created_time FROM user_view_history WHERE created_time >= ? and mobile !="" group by mobile `
+	_, err = o.Raw(sql, dayStr+" 00:00:00").QueryRows(&items)
+	return
+}
+
+func GetCountUserViewHistoryByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,email,max(created_time) as created_time FROM user_view_history WHERE email in (` + emails + `) group by email`
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+	//return items2,err
+}
+
+func GetReportViewMaxTimeByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	rddpSql := `SELECT mobile,MAX(create_time) AS created_time,COUNT(1) AS total FROM report_view_record WHERE create_time >= ? and mobile !="" group by mobile`
+	_, err = o.Raw(rddpSql, dayStr+" 00:00:00").QueryRows(&items)
+	return
+}
+
+func GetReportViewMaxTimeByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	rddpSql := `SELECT mobile,MAX(create_time) AS created_time,COUNT(1) AS total FROM report_view_record WHERE email in (` + emails + `) group by email`
+	_, err = o.Raw(rddpSql).QueryRows(&items)
+	return
+}
+
+// GetAdvisoryCountUserViewHistoryByMobile 每日资讯
+func GetAdvisoryCountUserViewHistoryByMobile(dayStr string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,mobile,max(create_time) as created_time FROM advisory_user_chart_article_record WHERE  create_time >= ? and mobile !="" group by mobile`
+	_, err = o.Raw(sql, dayStr+" 00:00:00").QueryRows(&items)
+	return
+}
+
+// GetAdvisoryCountUserViewHistoryByEmails 每日资讯
+func GetAdvisoryCountUserViewHistoryByEmails(emails string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,email,max(create_time) as created_time FROM advisory_user_chart_article_record WHERE email in (` + emails + `) group by email`
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+	//return items2,err
+}

+ 36 - 0
models/tables/user_view_statistics/user_view_statistics.go

@@ -0,0 +1,36 @@
+package user_view_statistics
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+// UserViewStatistics 用户报告报告阅读数量统计表
+type UserViewStatistics struct {
+	Id          int       `orm:"column(id)" description:"自增Id"`
+	Mobile      string    `description:"手机号"`
+	Email       string    `description:"手机号"`
+	RealName    string    `description:"联系人名称"`
+	CompanyName string    `description:"客户名称"`
+	ViewNum     int       `description:"阅读总数"`
+	Date        time.Time `description:"阅读日期"`
+	CreateTime  time.Time `description:"添加时间"`
+}
+
+// UserViewMobileTotalSlice 根据用户手机号字符串获取用户的浏览数
+type UserViewMobileTotalSlice struct {
+	Mobile      string `description:"用户手机号"`
+	CompanyName string `description:"客户名称"`
+	RealName    string `description:"用户名称"`
+	Total       int    `description:"总阅读数"`
+}
+
+// GetUserViewListByDate 根据日期获取用户阅读数
+func GetUserViewListByDate(dayStr string) (list []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT mobile,sum(view_num) total
+			FROM user_view_statistics
+			WHERE 1=1 and date >= ? group by mobile`
+	_, err = o.Raw(sql, dayStr).QueryRows(&list)
+	return
+}

+ 16 - 1
utils/config.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/beego/beego/v2/core/logs"
 	"github.com/beego/beego/v2/server/web"
+	"rdluck_tools/cache"
 	"rdluck_tools/log"
 )
 
@@ -11,11 +12,14 @@ var (
 	RunMode        string //运行模式
 	MYSQL_URL      string //数据库连接
 	MYSQL_URL_RDDP string //数据库连接
+
+	REDIS_CACHE string       //缓存地址
+	Rc          *cache.Cache //redis缓存
+	Re          error        //redis错误
 )
 
 var ApiLog *log.Log
 
-
 var (
 	Bucketname       string = "hongze"
 	Endpoint         string
@@ -41,6 +45,17 @@ func init() {
 	MYSQL_URL = config["mysql_url"]
 	MYSQL_URL_RDDP = config["mysql_url_rddp"]
 
+	// 初始化redis
+	REDIS_CACHE = config["beego_cache"]
+	if len(REDIS_CACHE) <= 0 {
+		panic("redis链接参数没有配置")
+	}
+	Rc, Re = cache.NewCache(REDIS_CACHE) //初始化缓存
+	if Re != nil {
+		fmt.Println(Re)
+		panic(Re)
+	}
+
 	//日志类
 	if RunMode == "release" {
 		logDir := `/data/rdlucklog/hongze_open_api`