article_collect.go 8.8 KB

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