瀏覽代碼

fix:新增阅读记录加入redis

Roc 3 年之前
父節點
當前提交
c124c4356a
共有 4 個文件被更改,包括 43 次插入4 次删除
  1. 3 1
      cache/report_cache.go
  2. 2 2
      controllers/report.go
  3. 10 0
      models/company_product.go
  4. 28 1
      services/task.go

+ 3 - 1
cache/report_cache.go

@@ -14,10 +14,11 @@ type UserViewRedisData struct {
 	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"`
 }
 
 // PushViewRecordNewRedisData 阅读数据加入到redis
-func PushViewRecordNewRedisData(reportViewRecord *models.ReportViewRecord) bool {
+func PushViewRecordNewRedisData(reportViewRecord *models.ReportViewRecord,companyId int) bool {
 	data := &UserViewRedisData{
 		Mobile:      reportViewRecord.Mobile,
 		Email:       reportViewRecord.Email,
@@ -25,6 +26,7 @@ func PushViewRecordNewRedisData(reportViewRecord *models.ReportViewRecord) bool
 		CompanyName: reportViewRecord.CompanyName,
 		ViewTime:    reportViewRecord.CreateTime.Format(utils.FormatDateTime),
 		ProductId:   1,
+		CompanyId: companyId
 	}
 	if utils.Re == nil {
 		err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)

+ 2 - 2
controllers/report.go

@@ -213,7 +213,7 @@ func (this *ReportController) Detail() {
 		}
 
 		// 添加阅读日志的数据加入到redis
-		go cache.PushViewRecordNewRedisData(record)
+		go cache.PushViewRecordNewRedisData(record, user.CompanyId)
 
 		//修改联系人最后一次阅读报告时间
 		go models.SetWxUserReportLastViewTime(user.UserId)
@@ -524,7 +524,7 @@ func (this *ReportController) AddViewRecordReport() {
 	}
 
 	// 添加阅读日志的数据加入到redis
-	go cache.PushViewRecordNewRedisData(record)
+	go cache.PushViewRecordNewRedisData(record, user.CompanyId)
 
 	//修改联系人最后一次阅读报告时间
 	go models.SetWxUserReportLastViewTime(user.UserId)

+ 10 - 0
models/company_product.go

@@ -32,6 +32,8 @@ type CompanyProduct struct {
 	LoseReason       string    `description:"流失原因"`
 	LossTime         time.Time `description:"流失时间"`
 	CompanyType      string    `description:"客户类型"`
+	ViewTotal        int       `description:"总阅读次数"`
+	LastViewTime     time.Time `description:"最后一次阅读时间"`
 }
 
 //判断客户权限总数
@@ -83,3 +85,11 @@ func GetCompanyProductsByUserId(userId int) (items []*CompanyProductDetail, err
 	_, err = o.Raw(sql, userId).QueryRows(&items)
 	return
 }
+
+// UpdateCompanyProductViewData 更新客户产品的阅读记录(阅读时间、总阅读次数)
+func UpdateCompanyProductViewData(companyId, productId int, lastViewTime string) (err error) {
+	sql := ` update company_product set view_total=view_total+1,last_view_time=? WHERE company_id=? and product_id =? `
+	o := orm.NewOrm()
+	_, err = o.Raw(sql, lastViewTime, companyId, productId).Exec()
+	return
+}

+ 28 - 1
services/task.go

@@ -1,10 +1,37 @@
 package services
 
-import "fmt"
+import (
+	"fmt"
+)
 
 func Task() {
 	fmt.Println("start")
 	//InitSetUnionId()
 	//FixUnionId()
 	fmt.Println("end")
+
+	//go AutoUpdateUserView()
 }
+
+// AutoUpdateUserView 自动添加阅读记录
+//func AutoUpdateUserView() {
+//	defer func() {
+//		if err := recover(); err != nil {
+//			fmt.Println("[AutoUpdateUserView]", err)
+//		}
+//	}()
+//	for {
+//		utils.Rc.Brpop(utils.CACHE_KEY_USER_VIEW, func(b []byte) {
+//			fmt.Println(string(b))
+//			var userViewRedisData cache.UserViewRedisData
+//			if err := json.Unmarshal(b, &userViewRedisData); err != nil {
+//				fmt.Println("json unmarshal wrong!")
+//				//}
+//				//if _, err := models.AddLogs(&log); err != nil {
+//				//	fmt.Println("AddLogs:", err.Error(), log)
+//			} else {
+//				models.UpdateCompanyProductViewData(userViewRedisData.CompanyId, userViewRedisData.ProductId, userViewRedisData.ViewTime)
+//			}
+//		})
+//	}
+//}