collection.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package collection
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/models/response"
  6. "hongze/hongze_yb/models/tables/rddp/report"
  7. "hongze/hongze_yb/models/tables/rddp/report_chapter"
  8. "hongze/hongze_yb/models/tables/yb_community_video"
  9. "hongze/hongze_yb/models/tables/yb_road_video"
  10. "hongze/hongze_yb/models/tables/yb_user_collection"
  11. "hongze/hongze_yb/utils"
  12. "sync"
  13. "time"
  14. )
  15. // 收藏类型
  16. const (
  17. CollectionTypeReport = iota + 1 // 研报
  18. CollectionTypeVideo // 视频社区
  19. CollectionTypeRoadVideo // 微路演视频
  20. )
  21. // AddCollection 加入收藏
  22. func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (err error) {
  23. title := ""
  24. nowTime := time.Now().Local()
  25. publishTime := nowTime
  26. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  27. switch collectionType {
  28. case CollectionTypeReport:
  29. // 晨周报章节
  30. if extendId > 0 {
  31. chapter, e := report_chapter.GetItemById(extendId)
  32. if e != nil {
  33. err = errors.New("获取章节失败, Err: " + e.Error())
  34. return
  35. }
  36. title = chapter.Title
  37. publishTime = chapter.PublishTime
  38. break
  39. }
  40. rp, e := report.GetPublishByReportId(primaryId)
  41. if e != nil {
  42. err = errors.New("获取报告失败, Err: " + e.Error())
  43. return
  44. }
  45. title = rp.Title
  46. publishTime = rp.PublishTime
  47. case CollectionTypeVideo:
  48. video, e := yb_community_video.GetItemById(primaryId)
  49. if e != nil {
  50. err = errors.New("获取视频失败, Err: " + e.Error())
  51. return
  52. }
  53. title = video.Title
  54. publishTime = video.PublishTime
  55. case CollectionTypeRoadVideo:
  56. roadVideo, e := yb_road_video.GetItemById(primaryId)
  57. if e != nil {
  58. err = errors.New("获取路演视频失败, Err: " + e.Error())
  59. return
  60. }
  61. title = roadVideo.Title
  62. publishTime = roadVideo.PublishTime
  63. default:
  64. err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型%d", collectionType))
  65. return
  66. }
  67. item := &yb_user_collection.YbUserCollection{
  68. CollectionType: collectionType,
  69. UserID: userId,
  70. PrimaryID: primaryId,
  71. ExtendID: extendId,
  72. State: 1,
  73. SourceAgent: sourceAgent,
  74. Title: title,
  75. PublishTime: publishTime,
  76. CreateTime: nowTime,
  77. ModifyTime: nowTime,
  78. }
  79. if e := item.Create(); e != nil {
  80. err = errors.New("新增收藏失败, Err: " + e.Error())
  81. return
  82. }
  83. return
  84. }
  85. // CancelCollection 取消收藏
  86. func CancelCollection(userId, collectionId int) (err error) {
  87. item, e := yb_user_collection.GetItemById(collectionId)
  88. if e != nil {
  89. err = errors.New("获取收藏失败, Err: " + e.Error())
  90. return
  91. }
  92. if item.CollectionID <= 0 {
  93. err = errors.New("收藏信息有误")
  94. return
  95. }
  96. if item.State != 1 {
  97. err = errors.New("收藏状态有误")
  98. return
  99. }
  100. if item.UserID != userId {
  101. err = errors.New(fmt.Sprintf("收藏人信息有误, 操作人ID: %d, 被操作人ID: %d", userId, item.UserID))
  102. return
  103. }
  104. updateCols := []string{"State", "ModifyTime"}
  105. item.State = 0
  106. item.ModifyTime = time.Now().Local()
  107. if e = item.Update(updateCols); e != nil {
  108. err = errors.New("更新收藏失败, Err: " + e.Error())
  109. return
  110. }
  111. return
  112. }
  113. // GetCollectionList 收藏列表
  114. func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string) (total int, respList []*response.CollectionList, err error) {
  115. respList = make([]*response.CollectionList, 0)
  116. if fromType <= 0 {
  117. fromType = 0
  118. }
  119. // 查询收藏列表
  120. var cond string
  121. var pars []interface{}
  122. cond += `user_id = ?`
  123. pars = append(pars, userId)
  124. if fromType > 0 {
  125. cond += ` AND collection_type = ?`
  126. pars = append(pars, fromType)
  127. }
  128. if keywords != "" {
  129. keywords = "%" + keywords + "%"
  130. cond += ` AND title LIKE ?`
  131. pars = append(pars, keywords)
  132. }
  133. collections, e := yb_user_collection.GetPageListByCondition(cond, pars, currPage, pageSize)
  134. if e != nil {
  135. err = errors.New("获取收藏列表失败, Err: " + e.Error())
  136. return
  137. }
  138. // 遍历收藏列表取出各类型的ID
  139. reportIdArr := make([]int, 0)
  140. chapterIdArr := make([]int, 0)
  141. videoIdArr := make([]int, 0)
  142. roadVideoIdArr := make([]int, 0)
  143. for i := range collections {
  144. switch collections[i].CollectionType {
  145. case CollectionTypeReport:
  146. if collections[i].ExtendID > 0 {
  147. chapterIdArr = append(chapterIdArr, collections[i].ExtendID)
  148. break
  149. }
  150. reportIdArr = append(reportIdArr, collections[i].PrimaryID)
  151. case CollectionTypeVideo:
  152. videoIdArr = append(videoIdArr, collections[i].PrimaryID)
  153. case CollectionTypeRoadVideo:
  154. roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
  155. }
  156. }
  157. // 查询相应收藏类型详情
  158. wg := sync.WaitGroup{}
  159. // 章节
  160. var chapterErr, reportErr, videoErr, roadVideoErr error
  161. chapterMap := make(map[int]*report_chapter.ReportChapter, 0)
  162. reportMap := make(map[int]*report.Report, 0)
  163. videoMap := make(map[int]*yb_community_video.YbCommunityVideo, 0)
  164. roadVideoMap := make(map[int]*yb_road_video.YbRoadVideo, 0)
  165. if len(chapterIdArr) > 0 {
  166. wg.Add(1)
  167. go func() {
  168. wg.Done()
  169. chapters, e := report_chapter.GetListByChapterIds(chapterIdArr)
  170. if e != nil {
  171. chapterErr = errors.New("获取章节失败, Err: " + e.Error())
  172. return
  173. }
  174. for i := range chapters {
  175. chapterMap[chapters[i].ReportChapterId] = chapters[i]
  176. fmt.Println("1", chapterMap)
  177. }
  178. }()
  179. }
  180. fmt.Println("2", chapterMap)
  181. // 报告
  182. if len(reportIdArr) > 0 {
  183. wg.Add(1)
  184. go func() {
  185. wg.Done()
  186. reports, e := report.GetListByReportIds(reportIdArr)
  187. if e != nil {
  188. reportErr = errors.New("获取报告失败, Err: " + e.Error())
  189. return
  190. }
  191. for i := range reports {
  192. reportMap[reports[i].Id] = reports[i]
  193. }
  194. }()
  195. }
  196. // 视频
  197. if len(videoIdArr) > 0 {
  198. wg.Add(1)
  199. go func() {
  200. wg.Done()
  201. videos, e := yb_community_video.GetListByVideoIds(videoIdArr)
  202. if e != nil {
  203. videoErr = errors.New("获取视频失败, Err: " + e.Error())
  204. return
  205. }
  206. for i := range videos {
  207. videoMap[videos[i].CommunityVideoID] = videos[i]
  208. }
  209. }()
  210. }
  211. // 路演视频
  212. if len(roadVideoIdArr) > 0 {
  213. wg.Add(1)
  214. go func() {
  215. wg.Done()
  216. roadVideos, e := yb_road_video.GetListByVideoIds(roadVideoIdArr)
  217. if e != nil {
  218. roadVideoErr = errors.New("获取视频失败, Err: " + e.Error())
  219. return
  220. }
  221. for i := range roadVideos {
  222. roadVideoMap[roadVideos[i].RoadVideoID] = roadVideos[i]
  223. }
  224. }()
  225. }
  226. wg.Wait()
  227. if chapterErr != nil {
  228. err = chapterErr
  229. return
  230. }
  231. if reportErr != nil {
  232. err = reportErr
  233. return
  234. }
  235. if videoErr != nil {
  236. err = videoErr
  237. return
  238. }
  239. if roadVideoErr != nil {
  240. err = roadVideoErr
  241. return
  242. }
  243. fmt.Println("3", chapterMap)
  244. // 响应列表
  245. for i := range collections {
  246. v := &response.CollectionList{
  247. CollectionId: collections[i].CollectionID,
  248. CollectionType: collections[i].CollectionType,
  249. PrimaryId: collections[i].PrimaryID,
  250. ExtendId: collections[i].ExtendID,
  251. CreateTime: collections[i].CreateTime.Format(utils.FormatDate),
  252. }
  253. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  254. switch collections[i].CollectionType {
  255. case CollectionTypeReport:
  256. // 晨周报章节
  257. if collections[i].ExtendID > 0 {
  258. cp := chapterMap[collections[i].ExtendID]
  259. if cp != nil {
  260. v.PublishTime = cp.PublishTime.Format(utils.FormatDate)
  261. v.ClassifyName = utils.REPORT_CHAPTER_TYPE_NAME_MAP[cp.ReportType]
  262. v.Author = cp.Author
  263. }
  264. break
  265. }
  266. rp := reportMap[collections[i].PrimaryID]
  267. if rp != nil {
  268. v.PublishTime = rp.PublishTime.Format(utils.FormatDate)
  269. v.ClassifyName = rp.ClassifyNameFirst
  270. v.Author = rp.Author
  271. }
  272. case CollectionTypeVideo:
  273. vd := videoMap[collections[i].PrimaryID]
  274. if vd != nil {
  275. v.PublishTime = vd.PublishTime.Format(utils.FormatDate)
  276. v.ImgUrl = vd.CoverImgURL
  277. }
  278. case CollectionTypeRoadVideo:
  279. rv := roadVideoMap[collections[i].PrimaryID]
  280. if rv != nil {
  281. v.PublishTime = rv.PublishTime.Format(utils.FormatDate)
  282. v.ImgUrl = rv.CoverImgURL
  283. v.Author = rv.AdminRealName
  284. }
  285. default:
  286. break
  287. }
  288. respList = append(respList, v)
  289. }
  290. return
  291. }