浏览代码

Merge branch 'open_api/view_report' into debug

# Conflicts:
#	models/db.go
#	services/task.go
Roc 3 年之前
父节点
当前提交
bdb45b18fc

+ 1 - 0
models/db.go

@@ -88,5 +88,6 @@ func init() {
 		new(yb.ActivityVoice),    //研报活动语音表
 		new(yb.ActivityRegister), //研报活动报名表
 		new(yb.Speaker),          //研报主持人表
+		new(UserViewStatistics),
 	)
 }

+ 102 - 0
models/user_view_statistics.go

@@ -0,0 +1,102 @@
+package models
+
+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:"添加时间"`
+}
+
+// AddMultiUserViewStatistics 添加用户报告报告阅读数量统计记录
+func AddMultiUserViewStatistics(list []*UserViewStatistics) (err error) {
+	if len(list) <= 0 {
+		return
+	}
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(len(list), list)
+	return
+}
+
+// GetUserViewStatisticsCount 获取某天的用户报告报告阅读数量统计记录数量
+func GetUserViewStatisticsCount(dayStr string) (count int, err error) {
+	o := orm.NewOrm()
+	//产品权限
+	sql := `SELECT count(1) count FROM user_view_statistics WHERE date = ?`
+
+	err = o.Raw(sql, dayStr).QueryRow(&count)
+	return
+}
+
+// UserViewMobileTotalSlice 根据用户手机号字符串获取用户的浏览数
+type UserViewMobileTotalSlice struct {
+	Mobile      string `description:"用户手机号"`
+	CompanyName string `description:"客户名称"`
+	RealName    string `description:"用户名称"`
+	Total       int    `description:"总阅读数"`
+}
+
+// UserViewEmailTotalSlice 根据用户邮箱字符串获取用户的浏览数
+type UserViewEmailTotalSlice struct {
+	Email       string `description:"用户邮箱"`
+	CompanyName string `description:"客户名称"`
+	RealName    string `description:"用户名称"`
+	Total       int    `description:"总阅读数"`
+}
+
+func GetCountUserViewHistoryByMobiles(date string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,mobile,company_name,real_name FROM user_view_history WHERE created_time >= ? and created_time <= ? and mobile != "" group by mobile`
+	_, err = o.Raw(sql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+}
+
+func GetCountUserViewHistoryByEmails(date string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,email,company_name,real_name FROM user_view_history WHERE created_time >= ? and created_time <= ? and email != ""  group by email`
+	_, err = o.Raw(sql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+	//return items2,err
+}
+
+func GetReportViewMaxTimeByMobiles(date string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	rddpSql := `SELECT mobile,company_name,real_name,COUNT(1) AS total FROM report_view_record WHERE create_time >= ? and create_time <= ? and mobile != ""  group by mobile`
+	_, err = o.Raw(rddpSql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+}
+
+func GetReportViewMaxTimeByEmails(date string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	rddpSql := `SELECT mobile,company_name,real_name,COUNT(1) AS total FROM report_view_record WHERE create_time >= ? and create_time <= ? and email != "" group by email`
+	_, err = o.Raw(rddpSql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+}
+
+// GetAdvisoryCountUserViewHistoryByMobiles 每日资讯
+func GetAdvisoryCountUserViewHistoryByMobiles(date string) (items []*UserViewMobileTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,mobile,company_name,real_name FROM advisory_user_chart_article_record WHERE create_time >= ? and create_time <= ? and mobile != ""  group by mobile`
+	_, err = o.Raw(sql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+}
+
+// GetAdvisoryCountUserViewHistoryByEmails 每日资讯
+func GetAdvisoryCountUserViewHistoryByEmails(date string) (items []*UserViewEmailTotalSlice, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT count(1) total,email,company_name,real_name as created_time FROM advisory_user_chart_article_record WHERE create_time >= ? and create_time <= ? and email != "" group by email`
+	_, err = o.Raw(sql, date+" 00:00:00", date+" 23:59:59").QueryRows(&items)
+	return
+	//return items2,err
+}

+ 1 - 1
services/data/base_from_smm.go

@@ -347,7 +347,7 @@ func SyncSmmIndexDataBase() {
 		fmt.Println(err)
 		return
 	}
-	endDate := time.Now().Format(utils.FormatDate)
+	endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT,0,0).Format(utils.FormatDate)
 	smmIndexAll, err := data_manage.GetBaseFromSmmIndex()
 	if err != nil {
 		fmt.Println("GetBaseFromSmmIndex Err:" + err.Error())

+ 14 - 14
services/data/edb_info.go

@@ -31,7 +31,7 @@ func RefreshDataFromThs(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDate)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -148,7 +148,7 @@ func RefreshDataFromPb(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDateUnSpace)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -286,7 +286,7 @@ func RefreshDataFromCalculateAll() (err error) {
 				edbInfo, _ := data_manage.GetEdbInfoById(v.FromEdbInfoId)
 				edbInfoList = append(edbInfoList, edbInfo)
 			}
-			fmt.Println("RefreshCalculate:", edbInfoList, edbInfoId, edbCode, v.CalculateFormula, startDate, endDate, edbInfoIdBytes)
+			endDate = ""
 			err = RefreshCalculate(edbInfoList, edbInfoId, edbCode, v.CalculateFormula, startDate, endDate, edbInfoIdBytes)
 			if err != nil {
 				return errors.New("RefreshCalculate Err:" + err.Error())
@@ -302,7 +302,7 @@ func RefreshDataFromCalculateAll() (err error) {
 			}
 			year := time.Now().Year() - 1
 			startDate := strconv.Itoa(year) + "-01" + "-01"
-			endDate := time.Now().Format(utils.FormatDate)
+			endDate := ""//time.Now().Format(utils.FormatDate)
 			err = data_manage.RefreshCalculateLjzzy(edbInfoId, fromEdbInfo, edbCode, startDate, endDate)
 			if err != nil {
 				return errors.New("RefreshCalculateLjzzy Err:" + err.Error())
@@ -317,7 +317,7 @@ func RefreshDataFromCalculateAll() (err error) {
 				return errors.New("GetEdbInfoById Err:" + err.Error())
 			}
 			startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate)
-			endDate := time.Now().Format(utils.FormatDate)
+			endDate := ""//time.Now().Format(utils.FormatDate)
 			err = data_manage.RefreshCalculateTbz(edbInfoId, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
 			if err != nil {
 				return errors.New("RefreshCalculateTbz Err:" + err.Error())
@@ -332,7 +332,7 @@ func RefreshDataFromCalculateAll() (err error) {
 				return errors.New("GetEdbInfoById Err:" + err.Error())
 			}
 			startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate)
-			endDate := time.Now().Format(utils.FormatDate)
+			endDate := ""//time.Now().Format(utils.FormatDate)
 			err = data_manage.RefreshCalculateTcz(edbInfoId, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
 			if err != nil {
 				return errors.New("RefreshCalculateTcz Err:" + err.Error())
@@ -361,7 +361,7 @@ func RefreshDataFromCalculateAll() (err error) {
 				return errors.New("GetEdbInfoById Err:" + err.Error())
 			}
 			startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate)
-			endDate := time.Now().Format(utils.FormatDate)
+			endDate := ""//time.Now().Format(utils.FormatDate)
 			err = data_manage.RefreshCalculateHbz(edbInfoId, source, fromEdbInfo, calculateHbz.EdbCode, startDate, endDate)
 			if err != nil {
 				return errors.New("RefreshCalculateTbz Err:" + err.Error())
@@ -376,7 +376,7 @@ func RefreshDataFromCalculateAll() (err error) {
 				return errors.New("GetEdbInfoById Err:" + err.Error())
 			}
 			startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate)
-			endDate := time.Now().Format(utils.FormatDate)
+			endDate := ""//time.Now().Format(utils.FormatDate)
 			err = data_manage.RefreshCalculateHcz(edbInfoId, source, fromEdbInfo, calculateHcz.EdbCode, startDate, endDate)
 			if err != nil {
 				return errors.New("RefreshCalculateTbz Err:" + err.Error())
@@ -392,7 +392,7 @@ func RefreshDataFromCalculateAll() (err error) {
 					return errors.New("GetEdbInfoById Err:" + err.Error())
 				}
 				startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate)
-				endDate := time.Now().Format(utils.FormatDate)
+				endDate := ""//time.Now().Format(utils.FormatDate)
 				err = data_manage.RefreshCalculateBp(edbInfoId, fromEdbInfo, calculateBp.EdbCode, startDate, endDate)
 				if err != nil {
 					return errors.New("RefreshCalculateTbz Err:" + err.Error())
@@ -437,7 +437,7 @@ func RefreshDataFromManual(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDate)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -490,7 +490,7 @@ func RefreshDataFromLz(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDateUnSpace)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -544,7 +544,7 @@ func RefreshDataFromYs(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDateUnSpace)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -598,7 +598,7 @@ func RefreshDataFromGl(wg *sync.WaitGroup) (err error) {
 	}
 
 	var endDate string
-	endDate = time.Now().Format(utils.FormatDateUnSpace)
+	endDate = time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
 	for _, v := range items {
 		startDate := ""
 		if v.Frequency == "日度" {
@@ -636,4 +636,4 @@ func RefreshDataFromGl(wg *sync.WaitGroup) (err error) {
 func ResetEdbInfoIsUpdate(cont context.Context) (err error) {
 	go data_manage.ResetEdbInfoIsUpdate()
 	return nil
-}
+}

+ 1 - 1
services/data_source_longzhong.go

@@ -1082,7 +1082,7 @@ func GetLzSurveyProductData(cont context.Context) (err error) {
 	//startDate := time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6
 	startDateTime, _ := time.Parse(utils.FormatDate, "2018-01-01")
 	startDate := startDateTime.UnixNano() / 1e6
-	endDate := time.Now().UnixNano() / 1e6
+	endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).UnixNano() / 1e6
 
 	for k, v := range priceInfoList {
 		existList, err := data_manage.GetLzSurveyDataExistByTradeCode(v.SurveyProductId)

+ 7 - 0
services/report_push.go

@@ -274,6 +274,12 @@ func SendThs(title, labelStr, abstract, jumpBaseUrl, logoUrl string) (err error)
 		return
 	}
 
+	dataType := "1" //内容类型:1文字 2小程序
+	dataTypeEncript, err := gorsa.PublicEncrypt(dataType, pubKey)
+	if err != nil {
+		return
+	}
+
 	//开始发送
 	client := http.Client{}
 	form := url.Values{}
@@ -282,6 +288,7 @@ func SendThs(title, labelStr, abstract, jumpBaseUrl, logoUrl string) (err error)
 	form.Add("label", label)
 	form.Add("url", jumpUrl)
 	form.Add("icon", picUrl)
+	form.Add("dataType", dataTypeEncript)
 
 	utils.FileLog.Info("SendThs parms:%s", form.Encode())
 	resp, err := client.PostForm(sendUrl, form)

+ 5 - 1
services/task.go

@@ -76,7 +76,11 @@ func Task() {
 	task.AddTask("定时新增手工指标数据提醒", addEdbTask)
 	//每次服务启动都需要执行一次的
 	_ = AddEdbTask(nil)
-
+	
+	//每日用户阅读数据统计
+	statisticsUserView := task.NewTask("statisticsUserView", "0 5 0 * * *", StatisticsUserView)
+	task.AddTask("每日用户阅读数据统计", statisticsUserView)
+	
 	task.StartTask()
 
 	fmt.Println("task end")

+ 154 - 0
services/user_view_statistics.go

@@ -0,0 +1,154 @@
+package services
+
+import (
+	"context"
+	"hongze/hongze_task/models"
+	"hongze/hongze_task/utils"
+	"time"
+)
+
+// StatisticsUserView 汇总统计表
+func StatisticsUserView(cont context.Context) (err error) {
+	dateTime := time.Now().AddDate(0, 0, 0)
+	dateStr := dateTime.Format(utils.FormatDate)
+	count, err := models.GetUserViewStatisticsCount(dateStr)
+	if err != nil {
+		return
+	}
+	if count > 0 {
+		return
+	}
+
+	{
+		mobileViewMap := make(map[string]*models.UserViewMobileTotalSlice)
+
+		list, tmpErr := models.GetCountUserViewHistoryByMobiles(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			mobileViewMap[v.Mobile] = v
+		}
+
+		list, tmpErr = models.GetReportViewMaxTimeByMobiles(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
+				userViewStatistics.Total += v.Total
+			} else {
+				mobileViewMap[v.Mobile] = v
+			}
+		}
+
+		list, tmpErr = models.GetAdvisoryCountUserViewHistoryByMobiles(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
+				userViewStatistics.Total += v.Total
+			} else {
+				mobileViewMap[v.Mobile] = v
+			}
+		}
+
+		if len(mobileViewMap) > 0 {
+			tmpList := make([]*models.UserViewStatistics, 0)
+			for mobile, userViewStatistics := range mobileViewMap {
+				item := &models.UserViewStatistics{
+					Mobile:      mobile,
+					RealName:    userViewStatistics.RealName,
+					CompanyName: userViewStatistics.CompanyName,
+					ViewNum:     userViewStatistics.Total,
+					Date:        dateTime,
+					CreateTime:  time.Now(),
+				}
+				//小于200条的时候,不插入数据库
+				if len(tmpList) < 200 {
+					tmpList = append(tmpList, item)
+					continue
+				}
+				//大于200条的时候,才去插入数据库
+				_ = models.AddMultiUserViewStatistics(tmpList)
+				tmpList = make([]*models.UserViewStatistics, 0)
+			}
+			//最后出来后,如果还有数据未插入,那么再去批量插入数据库
+			if len(tmpList) > 0 {
+				_ = models.AddMultiUserViewStatistics(tmpList)
+			}
+		}
+	}
+
+	//邮箱
+	{
+		emailViewMap := make(map[string]*models.UserViewEmailTotalSlice)
+
+		list, tmpErr := models.GetCountUserViewHistoryByEmails(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			emailViewMap[v.Email] = v
+		}
+
+		list, tmpErr = models.GetReportViewMaxTimeByEmails(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			if userViewStatistics, ok := emailViewMap[v.Email]; ok {
+				userViewStatistics.Total += v.Total
+			} else {
+				emailViewMap[v.Email] = v
+			}
+		}
+
+		list, tmpErr = models.GetAdvisoryCountUserViewHistoryByEmails(dateStr)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
+		for _, v := range list {
+			if userViewStatistics, ok := emailViewMap[v.Email]; ok {
+				userViewStatistics.Total += v.Total
+			} else {
+				emailViewMap[v.Email] = v
+			}
+		}
+
+		if len(emailViewMap) > 0 {
+			tmpList := make([]*models.UserViewStatistics, 0)
+			for email, userViewStatistics := range emailViewMap {
+				item := &models.UserViewStatistics{
+					Email:       email,
+					RealName:    userViewStatistics.RealName,
+					CompanyName: userViewStatistics.CompanyName,
+					ViewNum:     userViewStatistics.Total,
+					Date:        dateTime,
+					CreateTime:  time.Now(),
+				}
+				//小于200条的时候,不插入数据库
+				if len(tmpList) < 200 {
+					tmpList = append(tmpList, item)
+					continue
+				}
+				//大于200条的时候,才去插入数据库
+				_ = models.AddMultiUserViewStatistics(tmpList)
+				tmpList = make([]*models.UserViewStatistics, 0)
+			}
+			//最后出来后,如果还有数据未插入,那么再去批量插入数据库
+			if len(tmpList) > 0 {
+				_ = models.AddMultiUserViewStatistics(tmpList)
+			}
+		}
+	}
+
+	return
+}

+ 2 - 1
utils/constants.go

@@ -89,7 +89,8 @@ const (
 
 //数据刷新频率
 const (
-	DATA_REFRESH = 7 //7个单位,日/周/月/季度/年
+	DATA_REFRESH        = 7 //7个单位,日/周/月/季度/年
+	DATA_END_DATE_LIMIT = 4 //数据结束日期为,当前日期,加上4年时间
 )
 
 var (