浏览代码

no message

xingzai 2 年之前
父节点
当前提交
45025a2740
共有 2 个文件被更改,包括 69 次插入1 次删除
  1. 42 1
      controllers/user.go
  2. 27 0
      models/article_collect.go

+ 42 - 1
controllers/user.go

@@ -108,7 +108,7 @@ func (this *UserController) Login() {
 		}
 	} else {
 		br.Msg = "无效的登录方式"
-		br.ErrMsg = "无效的登录方式,Err:" + err.Error()
+		br.ErrMsg = "无效的登录方式,Err:"
 		return
 	}
 	if len(req.Mobile) >= 11 && req.CountryCode == "" {
@@ -462,6 +462,21 @@ func (this *UserController) CollectList() {
 			}
 		}
 	}
+
+	//处理文章PV收藏等数量
+	mapArticleCollectNum := make(map[int]*models.CygxArticleNum)
+	if len(articleIds) > 0 {
+		articleCollectNumList, err := models.GetArticleCollectNum(articleIds, userId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取失败,GetArticleCollectNum Err:" + err.Error()
+			return
+		}
+		for _, v := range articleCollectNumList {
+			mapArticleCollectNum[v.ArticleId] = v
+		}
+	}
+
 	lenList := len(list)
 	for i := 0; i < lenList; i++ {
 		item := list[i]
@@ -475,6 +490,11 @@ func (this *UserController) CollectList() {
 		} else {
 			list[i].Source = 2
 		}
+		if mapArticleCollectNum[article.ArticleId] != nil {
+			list[i].CollectNum = mapArticleCollectNum[article.ArticleId].CollectNum
+			list[i].Pv = mapArticleCollectNum[article.ArticleId].Pv
+			list[i].IsCollect = mapArticleCollectNum[article.ArticleId].IsCollect
+		}
 		//list[i].TitleEn = article.TitleEn
 		//list[i].UpdateFrequency = article.UpdateFrequency
 		//list[i].CreateDate = article.CreateDate
@@ -665,6 +685,21 @@ func (this *UserController) BrowseHistoryList() {
 			}
 		}
 	}
+
+	//处理文章PV收藏等数量
+	mapArticleCollectNum := make(map[int]*models.CygxArticleNum)
+	if len(articleIds) > 0 {
+		articleCollectNumList, err := models.GetArticleCollectNum(articleIds, userId)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取失败,GetArticleCollectNum Err:" + err.Error()
+			return
+		}
+		for _, v := range articleCollectNumList {
+			mapArticleCollectNum[v.ArticleId] = v
+		}
+	}
+
 	lenList := len(list)
 	for i := 0; i < lenList; i++ {
 		item := list[i]
@@ -679,6 +714,12 @@ func (this *UserController) BrowseHistoryList() {
 			} else {
 				list[i].Source = 2
 			}
+
+			if mapArticleCollectNum[article.ArticleId] != nil {
+				list[i].CollectNum = mapArticleCollectNum[article.ArticleId].CollectNum
+				list[i].Pv = mapArticleCollectNum[article.ArticleId].Pv
+				list[i].IsCollect = mapArticleCollectNum[article.ArticleId].IsCollect
+			}
 			//list[i].TitleEn = article.TitleEn
 			//list[i].UpdateFrequency = article.UpdateFrequency
 			//list[i].CreateDate = article.CreateDate

+ 27 - 0
models/article_collect.go

@@ -2,6 +2,7 @@ package models
 
 import (
 	"github.com/beego/beego/v2/client/orm"
+	"hongze/hongze_cygx/utils"
 	"time"
 )
 
@@ -122,3 +123,29 @@ func UpdateArticleCollectCountNum(num, articleId int) (err error) {
 	_, err = o.Raw(sql, num, articleId).Exec()
 	return
 }
+
+type CygxArticleNum struct {
+	ArticleId  int  `description:"文章ID"`
+	IsCollect  bool `description:"本人是否收藏"`
+	Pv         int  `description:"PV"`
+	CollectNum int  `description:"收藏人数"`
+}
+
+// GetArticleCollectNum 根据文章ID获取收藏数量的列表
+func GetArticleCollectNum(articleId []string, uid int) (items []*CygxArticleNum, err error) {
+	lenarticleId := len(articleId)
+	if lenarticleId == 0 {
+		return
+	}
+	sql := `SELECT
+			a.article_id,
+			( SELECT count( 1 ) FROM cygx_article_history_record_newpv AS h WHERE h.article_id = a.article_id ) AS pv,
+			( SELECT count( 1 ) FROM cygx_article_collect AS ac  INNER JOIN wx_user as u ON  u.user_id = ac.user_id  WHERE ac.article_id = a.article_id  ) AS collect_num, 
+			( SELECT count( 1 ) FROM cygx_article_collect AS ac  INNER JOIN wx_user as u ON  u.user_id = ac.user_id  WHERE ac.article_id = a.article_id AND DATE_SUB( CURDATE(), INTERVAL 30 DAY ) <= date( ac.create_time )  ) AS collect_num_order, 
+			( SELECT count( 1 ) FROM cygx_article_collect AS ac WHERE ac.article_id = a.article_id  AND user_id = ? ) AS is_collect
+		FROM
+			cygx_article AS a WHERE  1 = 1  AND  article_id IN  (` + utils.GetOrmInReplace(lenarticleId) + `) `
+	o := orm.NewOrm()
+	_, err = o.Raw(sql, uid, articleId).QueryRows(&items)
+	return
+}