Browse Source

新的PV统计方式,以及需要做的用户行为记录

xingzai 3 years ago
parent
commit
d668f51195

+ 35 - 0
controllers/activity.go

@@ -404,6 +404,17 @@ func (this *ActivityCoAntroller) ScheduleList() {
 			list[k].IsShowSustainable = true
 		}
 	}
+	//添加我的日程访问记录
+	item := new(models.CygxPageHistoryRecord)
+	item.UserId = user.UserId
+	item.CreateTime = time.Now()
+	item.Mobile = user.Mobile
+	item.Email = user.Email
+	item.CompanyId = user.CompanyId
+	item.CompanyName = user.CompanyName
+	item.PageType = "MySchedule"
+	go models.AddCygxPageHistoryRecord(item)
+
 	resp := new(models.GetCygxActivityListRep)
 	resp.List = list
 	resp.Paging = page
@@ -1989,6 +2000,20 @@ func (this *ActivityCoAntroller) LabelTypeList() {
 			itemsNew[3] = itemsNewCopy[2]
 		}
 	}
+
+	//记录用户搜索的筛选条件
+
+	item := new(models.CygxActivitySearchHistory)
+	item.UserId = user.UserId
+	item.Mobile = user.Mobile
+	item.CompanyId = user.CompanyId
+	item.CompanyName = user.CompanyName
+	item.CreateTime = time.Now()
+	item.ChartPermissionIds = chartPermissionIds
+	item.IsPower = isPower
+	item.WhichDay = whichDay
+	go models.AddCygxActivitySearchHistory(item)
+
 	resp.List = itemsNew
 	br.Ret = 200
 	br.Success = true
@@ -2106,6 +2131,16 @@ func (this *ActivityCoAntroller) LabelMoreList() {
 	for k, v := range list {
 		list[k].KeyWord = services.LabelStr(v.KeyWord)
 	}
+	//添加更多主题访问记录
+	item := new(models.CygxPageHistoryRecord)
+	item.UserId = user.UserId
+	item.CreateTime = time.Now()
+	item.Mobile = user.Mobile
+	item.Email = user.Email
+	item.CompanyId = user.CompanyId
+	item.CompanyName = user.CompanyName
+	item.PageType = "LabelMore"
+	go models.AddCygxPageHistoryRecord(item)
 	resp.List = list
 	br.Ret = 200
 	br.Success = true

+ 3 - 3
controllers/article.go

@@ -620,6 +620,7 @@ func (this *ArticleController) AddStopTime() {
 				record.UserId = uid
 				record.ArticleId = articleId
 				record.CreateTime = time.Now()
+				record.ModifyTime = time.Now()
 				record.Mobile = user.Mobile
 				record.Email = user.Email
 				record.CompanyId = user.CompanyId
@@ -630,11 +631,10 @@ func (this *ArticleController) AddStopTime() {
 					//新增浏览记录
 					go models.AddCygxArticleViewRecordNewpv(record)
 				} else {
-
+					go models.UpdateCygxArticleViewRecordNewpv(record, req.StopTime)
 				}
-				utils.Rc.Put(key, 1, 5*time.Second)
+				utils.Rc.Put(key, 1, 10*time.Minute)
 				models.ModifyReportLastViewTime(uid)
-
 			} else { //无该行业权限
 				hasPermission = 3
 			}

+ 25 - 0
models/activity_seaarch_history.go

@@ -0,0 +1,25 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"time"
+)
+
+type CygxActivitySearchHistory struct {
+	Id                 int `orm:"column(id);pk"`
+	UserId             int
+	CreateTime         time.Time
+	Mobile             string `description:"手机号"`
+	CompanyId          int    `description:"公司id"`
+	CompanyName        string `description:"公司名称"`
+	ChartPermissionIds string `description:"表chart_permission中id"`
+	WhichDay           string `description:"是否属于新标签,1是,0否"`
+	IsPower            int    `description:"是否属于深标签,1是,0否"`
+}
+
+//添加
+func AddCygxActivitySearchHistory(item *CygxActivitySearchHistory) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	return
+}

+ 10 - 0
models/article_history_record.go

@@ -17,6 +17,8 @@ type CygxArticleHistoryRecord struct {
 	CompanyId   int       `description:"公司id"`
 	CompanyName string    `description:"公司名称"`
 	ModifyTime  time.Time `description:"修改时间"`
+	StopTime    int       `description:"停留时间"`
+	OutType     int       `description:"退出方式,1正常退出,2强制关闭"`
 }
 
 //添加历史信息
@@ -101,3 +103,11 @@ func GetNoAddStoptimeArticleCount(uid, articleId int) (count int, err error) {
 	err = o.Raw(sqlCount, uid, articleId).QueryRow(&count)
 	return
 }
+
+//最新标的列表
+func GetArticleHistoryList() (items []*CygxArticleHistoryRecordNewpv, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT *  FROM cygx_article_history_record WHERE stop_time >0 `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 34 - 2
models/article_history_record_newpv.go

@@ -2,6 +2,7 @@ package models
 
 import (
 	"rdluck_tools/orm"
+	"strconv"
 	"time"
 )
 
@@ -10,6 +11,7 @@ type CygxArticleHistoryRecordNewpv struct {
 	ArticleId   int
 	UserId      int
 	CreateTime  time.Time
+	ModifyTime  time.Time
 	Mobile      string `description:"手机号"`
 	Email       string `description:"邮箱"`
 	CompanyId   int    `description:"公司id"`
@@ -26,9 +28,39 @@ func AddCygxArticleViewRecordNewpv(item *CygxArticleHistoryRecordNewpv) (lastId
 }
 
 //获取最新的一条阅读记录
-func GetNewArticleHistoryRecordNewpv(uid, articleId int) (item *AddStopTimeNewRep, err error) {
+func GetNewArticleHistoryRecordNewpv(uid, articleId int, modifytime string) (item *AddStopTimeNewRep, err error) {
 	o := orm.NewOrm()
-	sql := `SELECT * FROM  cygx_article_history_record_newpv WHERE user_id = ? AND article_id = ? ORDER BY id DESC LIMIT 1;`
+	sql := `SELECT * FROM  cygx_article_history_record_newpv WHERE user_id = ? AND article_id = ? 	AND modify_time <='` + modifytime + `'  ORDER BY id DESC LIMIT 1;`
 	err = o.Raw(sql, uid, articleId).QueryRow(&item)
 	return
 }
+
+//把十分钟之内的阅读记录进行累加
+func UpdateCygxArticleViewRecordNewpv(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_article_history_record_newpv 
+			SET stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
+			WHERE
+			article_id = ? 
+			AND user_id = ? 
+			AND modify_time = ? 
+			AND timestampdiff(MINUTE,modify_time,NOW()) < 10`
+	_, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId, itemRep.ModifyTime).Exec()
+
+	return
+}
+
+//把十分钟之内的阅读记录进行累加
+func UpdateCygxArticleViewRecordNewpvList(itemRep *CygxArticleHistoryRecordNewpv, stopTime int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_article_history_record_newpv 
+			SET stop_time = stop_time + ` + strconv.Itoa(stopTime) + `
+			WHERE
+			article_id = ? 
+			AND user_id = ? 
+			AND modify_time = ? 
+			AND id = ?`
+	_, err = o.Raw(sql, itemRep.ArticleId, itemRep.UserId, itemRep.ModifyTime, itemRep.Id).Exec()
+
+	return
+}

+ 2 - 0
models/db.go

@@ -66,5 +66,7 @@ func init() {
 		new(CygxArticleAuthor),
 		new(CygxUserRecord),
 		new(CygxReportIndustrialSeaarchHistory),
+		new(CygxArticleHistoryRecordNewpv),
+		new(CygxActivitySearchHistory),
 	)
 }

+ 2 - 2
models/report.go

@@ -164,8 +164,8 @@ func IndustrialToArticleWhichDepartment(condition string, pars []interface{}, ui
 	o := orm.NewOrm()
 	sql := `SELECT
 			art.* ,m.industry_name,d.nick_name,
-			(SELECT count(1) FROM cygx_article_history_record as h WHERE h.article_id = art.article_id ) as pv,
-			( SELECT COUNT( 1 ) FROM cygx_article_history_record AS rec WHERE rec.user_id =  ` + strconv.Itoa(uid) + ` AND rec.article_id = art.article_id ) AS readnum 
+			(SELECT count(1) FROM cygx_article_history_record_newpv as h WHERE h.article_id = art.article_id ) as pv,
+			( SELECT COUNT( 1 ) FROM cygx_article_history_record_newpv AS rec WHERE rec.user_id =  ` + strconv.Itoa(uid) + ` AND rec.article_id = art.article_id ) AS readnum 
 			FROM
 			cygx_article AS art
 			INNER JOIN cygx_industrial_article_group_management as mg ON mg.article_id = art.article_id 

+ 31 - 0
services/article.go

@@ -12,6 +12,7 @@ import (
 	nhttp "net/http"
 	"strconv"
 	"strings"
+	"time"
 )
 
 func GetReportContentSub(content string) (contentSub string, err error) {
@@ -563,3 +564,33 @@ func GetArticleListByApi(cont context.Context) (err error) {
 	}
 	return
 }
+
+func SynchronizationArtclehistory() {
+	fmt.Println("同步开始")
+	list, err := models.GetArticleHistoryList()
+	if err != nil {
+		fmt.Println("获取列表失败", err)
+	}
+	for _, v := range list {
+		endDate := v.ModifyTime.Add(+time.Minute * 10).Format(utils.FormatDateTime)
+		detail, err := models.GetNewArticleHistoryRecordNewpv(v.UserId, v.ArticleId, endDate)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			fmt.Println("获取信息失败", err)
+		}
+		fmt.Println(v.Id)
+		if detail == nil {
+			_, err = models.AddCygxArticleViewRecordNewpv(v)
+			if err != nil {
+				fmt.Println("新增失败", err)
+			}
+		} else {
+			err = models.UpdateCygxArticleViewRecordNewpvList(v, v.StopTime)
+			if err != nil {
+				fmt.Println("修改失败", err)
+			}
+		}
+
+	}
+	fmt.Println(len(list))
+	fmt.Println("同步结束")
+}

+ 5 - 1
services/task.go

@@ -54,7 +54,7 @@ func Task() {
 		chageIndustrialArticleNum := task.NewTask("chageIndustrialArticleNum", "0 01 00 * * *", ChageIndustrialArticleNum) //更改对应产业的文章阅读数量
 		task.AddTask("chageIndustrialArticleNum", chageIndustrialArticleNum)
 	}
-
+	//SynchronizationArthistory()//同步原有的阅读记录
 	//GetAddpArticle() //同步日度点评数据
 	task.StartTask()
 	fmt.Println("end")
@@ -406,3 +406,7 @@ func GetAddpArticle() {
 	fmt.Println(list)
 
 }
+
+func SynchronizationArthistory() {
+	go SynchronizationArtclehistory()
+}