article_collect.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_clpt/utils"
  5. "time"
  6. )
  7. type CygxArticleCollect struct {
  8. Id int `orm:"column(id);pk"`
  9. ArticleId int
  10. ActivityVoiceId int
  11. ActivityVideoId int
  12. VideoId int
  13. UserId int
  14. CreateTime time.Time
  15. Mobile string `description:"手机号"`
  16. Email string `description:"邮箱"`
  17. CompanyId int `description:"公司id"`
  18. CompanyName string `description:"公司名称"`
  19. RealName string `description:"用户实际名称"`
  20. }
  21. // 添加收藏信息
  22. func AddCygxArticleCollect(item *CygxArticleCollect) (lastId int64, err error) {
  23. o := orm.NewOrm()
  24. lastId, err = o.Insert(item)
  25. return
  26. }
  27. type ArticleCollectReq struct {
  28. ArticleId int `description:"报告id"`
  29. }
  30. type ArticleCollectResp struct {
  31. Status int `description:"1:收藏,2:取消收藏"`
  32. CollectCount int `description:"收藏总数"`
  33. }
  34. func RemoveArticleCollect(userId, articleId int) (err error) {
  35. o := orm.NewOrm()
  36. sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND article_id=? `
  37. _, err = o.Raw(sql, userId, articleId).Exec()
  38. return
  39. }
  40. func GetArticleCollectUsersCount(articleId int) (count int, err error) {
  41. sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE article_id=? `
  42. err = orm.NewOrm().Raw(sql, articleId).QueryRow(&count)
  43. return
  44. }
  45. func GetArticleCollectCount(userId, articleId int) (count int, err error) {
  46. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND article_id=? `
  47. err = orm.NewOrm().Raw(sql, userId, articleId).QueryRow(&count)
  48. return
  49. }
  50. type ArticleCollectList struct {
  51. Id int `orm:"column(id);pk"`
  52. ArticleId int
  53. UserId int
  54. CreateTime time.Time
  55. Title string `description:"标题"`
  56. TitleEn string `description:"英文标题 "`
  57. UpdateFrequency string `description:"更新周期"`
  58. CreateDate string `description:"创建时间"`
  59. PublishDate string `description:"发布时间"`
  60. Body string `description:"内容"`
  61. Abstract string `description:"摘要"`
  62. CategoryName string `description:"一级分类"`
  63. SubCategoryName string `description:"二级分类"`
  64. }
  65. func GetCygxArticleCollectList(condition string) (items []*CygxArticleCollect, err error) {
  66. o := orm.NewOrm()
  67. sql := `SELECT * FROM cygx_article_collect WHERE 1 =1 ` + condition + ` GROUP BY user_id `
  68. _, err = o.Raw(sql).QueryRows(&items)
  69. return
  70. }
  71. func GetCygxArticleCollectListByUser(userId int) (items []*CygxArticleCollect, err error) {
  72. o := orm.NewOrm()
  73. sql := `SELECT * FROM cygx_article_collect WHERE 1 =1 AND article_id > 0 AND user_id = ? `
  74. _, err = o.Raw(sql, userId).QueryRows(&items)
  75. return
  76. }
  77. // 修改用户收藏文章的相关信息
  78. func UpdateCygxArticleCollect(wxUser *WxUserItem) (err error) {
  79. o := orm.NewOrm()
  80. var sql string
  81. if wxUser.Mobile != "" {
  82. sql = `UPDATE cygx_article_collect SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  83. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  84. } else if wxUser.Email != "" {
  85. sql = `UPDATE cygx_article_collect SET user_id=?,company_id=?,company_name=?,mobile=?,real_name=? WHERE email=? `
  86. _, err = o.Raw(sql, wxUser.UserId, wxUser.CompanyId, wxUser.CompanyName, wxUser.Mobile, wxUser.RealName, wxUser.Email).Exec()
  87. }
  88. return
  89. }
  90. type CygxArticleCollectCountRep struct {
  91. ArticleId int `description:"文章ID"`
  92. Num int `description:"数量"`
  93. }
  94. // 获取文章被收藏的数量
  95. func GetUserArticleCollectList() (items []*CygxArticleCollectCountRep, err error) {
  96. o := orm.NewOrm()
  97. sql := `SELECT
  98. COUNT( 1 ) AS num,
  99. f.article_id
  100. FROM
  101. cygx_article_collect AS f
  102. INNER JOIN wx_user AS u ON u.user_id = f.user_id
  103. INNER JOIN cygx_article AS a ON a.article_id = f.article_id
  104. WHERE
  105. 1 = 1
  106. GROUP BY
  107. f.article_id
  108. ORDER BY
  109. num DESC
  110. LIMIT 30 `
  111. _, err = o.Raw(sql).QueryRows(&items)
  112. return
  113. }
  114. // 修改文章收藏的数量
  115. func UpdateArticleCollectCountNum(num, articleId int) (err error) {
  116. o := orm.NewOrm()
  117. sql := `UPDATE cygx_article SET user_collection_num = ? WHERE article_id = ?`
  118. _, err = o.Raw(sql, num, articleId).Exec()
  119. return
  120. }
  121. type CygxArticleNum struct {
  122. ArticleId int `description:"文章ID"`
  123. IsCollect bool `description:"本人是否收藏"`
  124. Pv int `description:"PV"`
  125. CollectNum int `description:"收藏人数"`
  126. }
  127. // GetArticleCollectNum 根据文章ID获取收藏数量的列表
  128. func GetArticleCollectNum(articleId []string, uid int) (items []*CygxArticleNum, err error) {
  129. lenarticleId := len(articleId)
  130. if lenarticleId == 0 {
  131. return
  132. }
  133. sql := `SELECT
  134. a.article_id,
  135. ( SELECT count( 1 ) FROM cygx_article_history_record_newpv AS h WHERE h.article_id = a.article_id ) AS pv,
  136. ( 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,
  137. ( 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,
  138. ( SELECT count( 1 ) FROM cygx_article_collect AS ac WHERE ac.article_id = a.article_id AND user_id = ? ) AS is_collect
  139. FROM
  140. cygx_article AS a WHERE 1 = 1 AND article_id IN (` + utils.GetOrmInReplace(lenarticleId) + `) `
  141. o := orm.NewOrm()
  142. _, err = o.Raw(sql, uid, articleId).QueryRows(&items)
  143. return
  144. }
  145. func GetVoiceCollectCount(userId, voiceId int) (count int, err error) {
  146. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND activity_voice_id=? `
  147. err = orm.NewOrm().Raw(sql, userId, voiceId).QueryRow(&count)
  148. return
  149. }
  150. func GetActivityVideoCollectCount(userId, activityVideoId int) (count int, err error) {
  151. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND activity_video_id=? `
  152. err = orm.NewOrm().Raw(sql, userId, activityVideoId).QueryRow(&count)
  153. return
  154. }
  155. func GetVideoCollectCount(userId, videoId int) (count int, err error) {
  156. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND video_id=? `
  157. err = orm.NewOrm().Raw(sql, userId, videoId).QueryRow(&count)
  158. return
  159. }
  160. func RemoveVoiceCollect(userId, voiceId int) (err error) {
  161. o := orm.NewOrm()
  162. sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND activity_voice_id=? `
  163. _, err = o.Raw(sql, userId, voiceId).Exec()
  164. return
  165. }
  166. func RemoveActivityVideoCollect(userId, activityVideoId int) (err error) {
  167. o := orm.NewOrm()
  168. sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND activity_video_id=? `
  169. _, err = o.Raw(sql, userId, activityVideoId).Exec()
  170. return
  171. }
  172. func GetActivityVideoCollectUsersCount(videoId int) (count int, err error) {
  173. sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE activity_video_id=? `
  174. err = orm.NewOrm().Raw(sql, videoId).QueryRow(&count)
  175. return
  176. }
  177. func GetVoiceCollectUsersCount(voiceId int) (count int, err error) {
  178. sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE activity_voice_id=? `
  179. err = orm.NewOrm().Raw(sql, voiceId).QueryRow(&count)
  180. return
  181. }
  182. func RemoveVideoCollect(userId, videoId int) (err error) {
  183. o := orm.NewOrm()
  184. sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND video_id=? `
  185. _, err = o.Raw(sql, userId, videoId).Exec()
  186. return
  187. }
  188. func GetVideoCollectUsersCount(videoId int) (count int, err error) {
  189. sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE video_id=? `
  190. err = orm.NewOrm().Raw(sql, videoId).QueryRow(&count)
  191. return
  192. }