Browse Source

合并Redis统计PV的方式

xingzai 3 years ago
parent
commit
9647014914
3 changed files with 66 additions and 0 deletions
  1. 18 0
      controllers/article.go
  2. 43 0
      services/activity.go
  3. 5 0
      utils/constants.go

+ 18 - 0
controllers/article.go

@@ -688,6 +688,15 @@ func (this *ArticleController) AddStopTime() {
 					if !utils.Rc.IsExist(key) || outType != 2 {
 						//新增浏览记录
 						go models.AddCygxArticleViewRecordNewpv(record)
+						recordRedis := new(services.ReportViewRecord)
+						recordRedis.UserId = user.UserId
+						recordRedis.ReportId = articleId
+						recordRedis.Mobile = user.Mobile
+						recordRedis.Email = user.Email
+						recordRedis.RealName = user.RealName
+						recordRedis.CompanyName = user.CompanyName
+						recordRedis.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
+						go services.PushViewRecordNewRedisData(recordRedis, user.CompanyId)
 					} else {
 						go models.UpdateCygxArticleViewRecordNewpv(record, stopTime)
 					}
@@ -1276,6 +1285,15 @@ func (this *ArticleCommonController) AddStopTimePublic() {
 					if !utils.Rc.IsExist(key) || outType != 2 {
 						//新增浏览记录
 						go models.AddCygxArticleViewRecordNewpv(record)
+						recordRedis := new(services.ReportViewRecord)
+						recordRedis.UserId = user.UserId
+						recordRedis.ReportId = articleId
+						recordRedis.Mobile = user.Mobile
+						recordRedis.Email = user.Email
+						recordRedis.RealName = user.RealName
+						recordRedis.CompanyName = companyName
+						recordRedis.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
+						go services.PushViewRecordNewRedisData(recordRedis, user.CompanyId)
 					} else {
 						go models.UpdateCygxArticleViewRecordNewpv(record, stopTime)
 					}

+ 43 - 0
services/activity.go

@@ -881,3 +881,46 @@ func AddCygxActivityRestrictSignupByAdmin(activityId int) (err error) {
 	}
 	return
 }
+
+// UserViewRedisData 阅读数据
+type UserViewRedisData struct {
+	Mobile      string `json:"mobile"`
+	Email       string `json:"email"`
+	RealName    string `json:"real_name"`
+	CompanyName string `json:"company_name"`
+	ViewTime    string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
+	ProductId   int    `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
+	CompanyId   int    `json:"company_id" description:"客户id"`
+}
+
+type ReportViewRecord struct {
+	Id          int       `orm:"column(id);pk"`
+	UserId      int       `description:"用户id"`
+	ReportId    int       `description:"报告id"`
+	Mobile      string    `description:"手机号"`
+	Email       string    `description:"邮箱"`
+	RealName    string    `description:"用户实际姓名"`
+	CompanyName string    `description:"公司名称"`
+	CreateTime  time.Time `description:"创建时间"`
+}
+
+// PushViewRecordNewRedisData 阅读数据加入到redis
+func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
+	data := &UserViewRedisData{
+		Mobile:      reportViewRecord.Mobile,
+		Email:       reportViewRecord.Email,
+		RealName:    reportViewRecord.RealName,
+		CompanyName: reportViewRecord.CompanyName,
+		ViewTime:    reportViewRecord.CreateTime.Format(utils.FormatDateTime),
+		ProductId:   2,
+		CompanyId:   companyId,
+	}
+	if utils.Re == nil {
+		err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
+		if err != nil {
+			fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
+		}
+		return true
+	}
+	return false
+}

+ 5 - 0
utils/constants.go

@@ -55,3 +55,8 @@ var (
 	AccessKeyId     string = "LTAIFMZYQhS2BTvW"
 	AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
 )
+
+//缓存key
+const (
+	CACHE_KEY_USER_VIEW = "user_view_record" //用户阅读数据
+)