Browse Source

fix:pdf阅读记录添加uv,pv

zqbao 8 months ago
parent
commit
9c723258ce
3 changed files with 44 additions and 0 deletions
  1. 21 0
      controllers/user.go
  2. 16 0
      models/report_pdf.go
  3. 7 0
      models/user_read_record.go

+ 21 - 0
controllers/user.go

@@ -527,6 +527,27 @@ func (this *UserAuthController) AddReportPdfRecord() {
 			br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
 			return
 		}
+		count, err := models.GetUserReadRecordCountByReportPdfIdAndUserId(req.ReportPdfId, user.UserId)
+		if err != nil {
+			br.Msg = "添加阅读记录失败"
+			br.ErrMsg = "获取阅读记录失败,Err:" + err.Error()
+			return
+		}
+		if count > 0 {
+			err = models.UpdateReportPdfPv(req.ReportPdfId)
+			if err != nil {
+				br.Msg = "添加研报阅读记录失败"
+				br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
+				return
+			}
+		} else {
+			err = models.UpdateReportPdfUvAndPv(req.ReportPdfId)
+			if err != nil {
+				br.Msg = "添加研报阅读记录失败"
+				br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
+				return
+			}
+		}
 	} else {
 		// 如果存在就计算停留时间
 		userRecord, err := models.GetUserReadRecordListById(req.RecordId)

+ 16 - 0
models/report_pdf.go

@@ -88,3 +88,19 @@ func GetReportPdfById(id int) (item *ReportPdfView, err error) {
 	err = o.Raw(sql, id).QueryRow(&item)
 	return
 }
+
+// 更新pv
+func UpdateReportPdfPv(id int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE report_pdf SET pv = pv + 1 WHERE report_pdf_id = ?`
+	_, err = o.Raw(sql, id).Exec()
+	return
+}
+
+// 更新uv和pv
+func UpdateReportPdfUvAndPv(id int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE report_pdf SET uv = uv + 1 AND pv = pv + 1 WHERE report_pdf_id = ?`
+	_, err = o.Raw(sql, id).Exec()
+	return
+}

+ 7 - 0
models/user_read_record.go

@@ -47,6 +47,13 @@ func GetUserReadRecordListById(recordId int) (items *UserReadRecord, err error)
 	return
 }
 
+func GetUserReadRecordCountByReportPdfIdAndUserId(reportId, userId int) (count int, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT COUNT(*) AS count FROM user_read_record WHERE 1=1 AND report_id=? AND user_id = ? AND report_type = 2`
+	err = o.Raw(sql, reportId, userId).QueryRow(&count)
+	return
+}
+
 func GetUserReadRecordListByRcordIds(recordIds []string) (items []*UserReadRecord, err error) {
 	o := orm.NewOrm()
 	sql := `SELECT * FROM user_read_record WHERE 1=1  `