collection.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package collection
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/models/tables/rddp/report"
  6. "hongze/hongze_yb/models/tables/rddp/report_chapter"
  7. "hongze/hongze_yb/models/tables/yb_community_video"
  8. "hongze/hongze_yb/models/tables/yb_road_video"
  9. "hongze/hongze_yb/models/tables/yb_user_collection"
  10. "time"
  11. )
  12. // 收藏类型
  13. const (
  14. CollectionTypeReport = iota + 1 // 研报
  15. CollectionTypeVideo // 视频社区
  16. CollectionTypeRoadVideo // 微路演视频
  17. )
  18. // AddCollection 加入收藏
  19. func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (err error) {
  20. title := ""
  21. nowTime := time.Now().Local()
  22. publishTime := nowTime
  23. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  24. switch collectionType {
  25. case CollectionTypeReport:
  26. // 晨周报章节
  27. if extendId > 0 {
  28. chapter, e := report_chapter.GetItemById(extendId)
  29. if e != nil {
  30. err = errors.New("获取章节失败, Err: " + e.Error())
  31. return
  32. }
  33. title = chapter.Title
  34. publishTime = chapter.PublishTime
  35. break
  36. }
  37. rp, e := report.GetPublishByReportId(primaryId)
  38. if e != nil {
  39. err = errors.New("获取报告失败, Err: " + e.Error())
  40. return
  41. }
  42. title = rp.Title
  43. publishTime = rp.PublishTime
  44. case CollectionTypeVideo:
  45. video, e := yb_community_video.GetItemById(primaryId)
  46. if e != nil {
  47. err = errors.New("获取视频失败, Err: " + e.Error())
  48. return
  49. }
  50. title = video.Title
  51. publishTime = video.PublishTime
  52. case CollectionTypeRoadVideo:
  53. roadVideo, e := yb_road_video.GetItemById(primaryId)
  54. if e != nil {
  55. err = errors.New("获取路演视频失败, Err: " + e.Error())
  56. return
  57. }
  58. title = roadVideo.Title
  59. publishTime = roadVideo.PublishTime
  60. default:
  61. err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型"))
  62. return
  63. }
  64. item := &yb_user_collection.YbUserCollection{
  65. CollectionType: collectionType,
  66. UserID: userId,
  67. PrimaryID: primaryId,
  68. ExtendID: extendId,
  69. State: 1,
  70. SourceAgent: sourceAgent,
  71. Title: title,
  72. PublishTime: publishTime,
  73. CreateTime: nowTime,
  74. ModifyTime: nowTime,
  75. }
  76. if e := item.Create(); e != nil {
  77. err = errors.New("新增收藏失败, Err: " + e.Error())
  78. return
  79. }
  80. return
  81. }
  82. // CancelCollection 取消收藏
  83. func CancelCollection(userId, collectionId int) (err error) {
  84. item, e := yb_user_collection.GetItemById(collectionId)
  85. if e != nil {
  86. err = errors.New("获取收藏失败, Err: " + e.Error())
  87. return
  88. }
  89. if item.CollectionID <= 0 {
  90. err = errors.New("收藏信息有误")
  91. return
  92. }
  93. if item.State != 1 {
  94. err = errors.New("收藏状态有误")
  95. return
  96. }
  97. if item.UserID != userId {
  98. err = errors.New(fmt.Sprintf("收藏人信息有误, 操作人ID: %d, 被操作人ID: %d", userId, item.UserID))
  99. return
  100. }
  101. updateCols := []string{"State", "ModifyTime"}
  102. item.State = 0
  103. item.ModifyTime = time.Now().Local()
  104. if e = item.Update(updateCols); e != nil {
  105. err = errors.New("更新收藏失败, Err: " + e.Error())
  106. return
  107. }
  108. return
  109. }
  110. // GetCollectionList 收藏列表
  111. // @Param from_type query int false "来源类型:0-全部; 1-研报; 2-线上路演; 3-视频社区"
  112. // @Param keywords query string false "搜索关键词"
  113. // @Param curr_page query int false "当前页码"
  114. // @Param page_size query int false "每页数量"
  115. func GetCollectionList(fromType, currPage, pageSize int, keywords string) (total int, err error) {
  116. if fromType <= 0 {
  117. fromType = 0
  118. }
  119. // TODO:查询收藏列表
  120. var cond string
  121. var pars []interface{}
  122. if fromType > 0 {
  123. cond += ` AND collection_type = ?`
  124. pars = append(pars, fromType)
  125. }
  126. if keywords != "" {
  127. keywords = "%" + keywords + "%"
  128. cond += ` AND title LIKE ?`
  129. pars = append(pars, keywords)
  130. }
  131. collections, e := yb_user_collection.GetPageListByCondition(cond, pars, currPage, pageSize)
  132. if e != nil {
  133. err = errors.New("获取收藏列表失败, Err: " + e.Error())
  134. return
  135. }
  136. // TODO:遍历收藏列表取出各类型的ID
  137. reportIdArr := make([]int, 0)
  138. chapterIdArr := make([]int, 0)
  139. videoIdArr := make([]int, 0)
  140. roadVideoIdArr := make([]int, 0)
  141. for i := range collections {
  142. switch collections[i].CollectionType {
  143. case CollectionTypeReport:
  144. if collections[i].ExtendID > 0 {
  145. chapterIdArr = append(chapterIdArr, collections[i].ExtendID)
  146. break
  147. }
  148. reportIdArr = append(reportIdArr, collections[i].PrimaryID)
  149. case CollectionTypeVideo:
  150. videoIdArr = append(videoIdArr, collections[i].PrimaryID)
  151. case CollectionTypeRoadVideo:
  152. roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
  153. }
  154. }
  155. // TODO:各类型ID大于0则进行相应的查询
  156. // TODO:根据收藏类型组合list
  157. return
  158. }