article_collect.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type CygxArticleCollect struct {
  7. Id int `orm:"column(id);pk"`
  8. ArticleId int
  9. UserId int
  10. CreateTime time.Time
  11. Mobile string `description:"手机号"`
  12. Email string `description:"邮箱"`
  13. CompanyId int `description:"公司id"`
  14. CompanyName string `description:"公司名称"`
  15. RealName string `description:"用户实际名称"`
  16. }
  17. //添加收藏信息
  18. func AddCygxArticleCollect(item *CygxArticleCollect) (lastId int64, err error) {
  19. o := orm.NewOrm()
  20. lastId, err = o.Insert(item)
  21. return
  22. }
  23. type ArticleCollectReq struct {
  24. ArticleId int `description:"报告id"`
  25. }
  26. type ArticleCollectResp struct {
  27. Status int `description:"1:收藏,2:取消收藏"`
  28. CollectCount int `description:"收藏总数"`
  29. }
  30. func RemoveArticleCollect(userId, articleId int) (err error) {
  31. o := orm.NewOrm()
  32. sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND article_id=? `
  33. _, err = o.Raw(sql, userId, articleId).Exec()
  34. return
  35. }
  36. func GetArticleCollectUsersCount(articleId int) (count int, err error) {
  37. sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE article_id=? `
  38. err = orm.NewOrm().Raw(sql, articleId).QueryRow(&count)
  39. return
  40. }
  41. func GetArticleCollectCount(userId, articleId int) (count int, err error) {
  42. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND article_id=? `
  43. err = orm.NewOrm().Raw(sql, userId, articleId).QueryRow(&count)
  44. return
  45. }
  46. type ArticleCollectList struct {
  47. Id int `orm:"column(id);pk"`
  48. ArticleId int
  49. UserId int
  50. CreateTime time.Time
  51. Title string `description:"标题"`
  52. TitleEn string `description:"英文标题 "`
  53. UpdateFrequency string `description:"更新周期"`
  54. CreateDate string `description:"创建时间"`
  55. PublishDate string `description:"发布时间"`
  56. Body string `description:"内容"`
  57. Abstract string `description:"摘要"`
  58. CategoryName string `description:"一级分类"`
  59. SubCategoryName string `description:"二级分类"`
  60. }
  61. func GetCygxArticleCollectList(condition string) (items []*CygxArticleCollect, err error) {
  62. o := orm.NewOrm()
  63. sql := `SELECT * FROM cygx_article_collect WHERE 1 =1 ` + condition + ` GROUP BY user_id `
  64. _, err = o.Raw(sql).QueryRows(&items)
  65. return
  66. }
  67. //修改用户收藏文章的相关信息
  68. func UpdateCygxArticleCollect(wxUser *WxUserItem) (err error) {
  69. o := orm.NewOrm()
  70. var sql string
  71. if wxUser.Mobile != "" {
  72. sql = `UPDATE cygx_article_collect SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  73. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  74. } else if wxUser.Email != "" {
  75. sql = `UPDATE cygx_article_collect SET user_id=?,company_id=?,company_name=?,mobile=?,real_name=? WHERE email=? `
  76. _, err = o.Raw(sql, wxUser.UserId, wxUser.CompanyId, wxUser.CompanyName, wxUser.Mobile, wxUser.RealName, wxUser.Email).Exec()
  77. }
  78. return
  79. }
  80. type CygxArticleCollectCountRep struct {
  81. ArticleId int `description:"文章ID"`
  82. Num int `description:"数量"`
  83. }
  84. //获取文章被收藏的数量
  85. func GetUserArticleCollectList() (items []*CygxArticleCollectCountRep, err error) {
  86. o := orm.NewOrm()
  87. sql := `SELECT
  88. COUNT( 1 ) AS num,
  89. f.article_id
  90. FROM
  91. cygx_article_collect AS f
  92. INNER JOIN wx_user AS u ON u.user_id = f.user_id
  93. INNER JOIN cygx_article AS a ON a.article_id = f.article_id
  94. WHERE
  95. 1 = 1
  96. GROUP BY
  97. f.article_id
  98. ORDER BY
  99. num DESC
  100. LIMIT 30 `
  101. _, err = o.Raw(sql).QueryRows(&items)
  102. return
  103. }
  104. //修改文章收藏的数量
  105. func UpdateArticleCollectCountNum(num, articleId int) (err error) {
  106. o := orm.NewOrm()
  107. sql := `UPDATE cygx_article SET user_collection_num = ? WHERE article_id = ?`
  108. _, err = o.Raw(sql, num, articleId).Exec()
  109. return
  110. }