package services import ( "errors" "fmt" "hongze/hongze_clpt/models" "hongze/hongze_clpt/utils" "strconv" "time" ) // 记录用户文章浏览记录 func ArticleHistory(articleId int, user *models.WxUserItem) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg("记录用户文章浏览记录,失败"+err.Error(), 2) } }() uid := user.UserId key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) if !utils.Rc.IsExist(key) { //新增浏览记录 //这个表貌似没怎么用了,暂时保留记录 record := new(models.CygxArticleViewRecord) record.UserId = uid record.ArticleId = articleId record.CreateTime = time.Now() record.Mobile = user.Mobile record.Email = user.Email record.CompanyId = user.CompanyId record.CompanyName = user.CompanyName _, e := models.AddCygxArticleViewRecord(record) if e != nil { err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error()) return } e = models.ModifyReportLastViewTime(uid) if e != nil { err = errors.New("ModifyReportLastViewTime, Err: " + e.Error()) return } utils.Rc.Put(key, 1, 2*time.Second) } return } // 记录用户文章浏览记录带时长 func ArticleHistoryStopTime(articleId, stopTime, outType int, user *models.WxUserItem) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg("记录用户文章浏览记录带时长,失败"+err.Error(), 2) } }() if stopTime < 3 { return } uid := user.UserId key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType) if !utils.Rc.IsExist(key) { record := new(models.CygxArticleHistoryRecordNewpv) record.UserId = uid record.ArticleId = articleId record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime)) record.ModifyTime = time.Now() record.Mobile = user.Mobile record.Email = user.Email record.CompanyId = user.CompanyId record.CompanyName = user.CompanyName record.StopTime = stopTime record.OutType = outType record.RegisterPlatform = utils.REGISTER_PLATFORM record.Source = "WEB" newId, e := models.AddCygxArticleViewRecordNewpv(record) if e != nil { err = errors.New("AddCygxArticleViewRecordNewpv, Err: " + e.Error()) return } recordRedis := new(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.StopTime = stopTime recordRedis.OutId = int(newId) recordRedis.CreateTime = record.CreateTime go PushViewRecordNewRedisData(recordRedis, user.CompanyId) utils.Rc.Put(key, 1, 2*time.Second) } go ArticleHistoryUserLabelLogAdd(articleId, uid) return } // GetArticleHistoryByArticleId 获取文章阅读 pv map func GetArticleHistoryByArticleId(articleIds []int) (mapResp map[int]int) { var err error defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("获取用户的阅读数据,信息失败,GetArticleHistoryByArticleId Err:"+err.Error(), 3) } }() lenIds := len(articleIds) if lenIds == 0 { return } var condition string var pars []interface{} condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenIds) + `) ` pars = append(pars, articleIds) listCy, e := models.GetCygxArticleHistoryRecordNewpvListPvCy(condition, pars) if e != nil { err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error()) return } mapResp = make(map[int]int, 0) for _, v := range listCy { mapResp[v.ArticleId] = v.Pv } listCl, e := models.GetCygxArticleHistoryRecordNewpvListPvCl(condition, pars) if e != nil { err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error()) return } for _, v := range listCl { mapResp[v.ArticleId] += v.Pv } return }