collection.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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) (collectionId 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. // 是否已收藏
  68. isCollect, e := GetUserCollectByItem(userId, collectionType, primaryId, extendId)
  69. if e != nil {
  70. err = e
  71. return
  72. }
  73. if isCollect > 0 {
  74. collectionId = isCollect
  75. return
  76. }
  77. item := &yb_user_collection.YbUserCollection{
  78. CollectionType: collectionType,
  79. UserID: userId,
  80. PrimaryID: primaryId,
  81. ExtendID: extendId,
  82. State: 1,
  83. SourceAgent: sourceAgent,
  84. Title: title,
  85. PublishTime: publishTime,
  86. CreateTime: nowTime,
  87. ModifyTime: nowTime,
  88. }
  89. if e := item.Create(); e != nil {
  90. err = errors.New("新增收藏失败, Err: " + e.Error())
  91. return
  92. }
  93. collectionId = item.CollectionID
  94. return
  95. }
  96. // CancelCollection 取消收藏
  97. func CancelCollection(userId, collectionId int) (err error) {
  98. item, e := yb_user_collection.GetItemById(collectionId)
  99. if e != nil {
  100. err = errors.New("获取收藏失败, Err: " + e.Error())
  101. return
  102. }
  103. if item.CollectionID <= 0 {
  104. err = errors.New("收藏信息有误")
  105. return
  106. }
  107. if item.State != 1 {
  108. err = errors.New("收藏状态有误")
  109. return
  110. }
  111. if item.UserID != userId {
  112. err = errors.New(fmt.Sprintf("收藏人信息有误, 操作人ID: %d, 被操作人ID: %d", userId, item.UserID))
  113. return
  114. }
  115. updateCols := []string{"State", "ModifyTime"}
  116. item.State = 0
  117. item.ModifyTime = time.Now().Local()
  118. if e = item.Update(updateCols); e != nil {
  119. err = errors.New("更新收藏失败, Err: " + e.Error())
  120. return
  121. }
  122. return
  123. }
  124. // GetCollectionList 收藏列表
  125. func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string) (total int, respList []*response.CollectionList, err error) {
  126. respList = make([]*response.CollectionList, 0)
  127. if fromType <= 0 {
  128. fromType = 0
  129. }
  130. // 查询收藏列表
  131. var cond string
  132. var pars []interface{}
  133. cond += `user_id = ?`
  134. pars = append(pars, userId)
  135. if fromType > 0 {
  136. cond += ` AND collection_type = ?`
  137. pars = append(pars, fromType)
  138. }
  139. if keywords != "" {
  140. keywords = "%" + keywords + "%"
  141. cond += ` AND title LIKE ?`
  142. pars = append(pars, keywords)
  143. }
  144. collectionTotal, e := yb_user_collection.GetPageListTotalByCondition(cond, pars)
  145. if e != nil {
  146. err = errors.New("获取收藏列表总数失败, Err: " + e.Error())
  147. return
  148. }
  149. total = int(collectionTotal)
  150. collections, e := yb_user_collection.GetPageListByCondition(cond, pars, currPage, pageSize)
  151. if e != nil {
  152. err = errors.New("获取收藏列表失败, Err: " + e.Error())
  153. return
  154. }
  155. // 遍历收藏列表取出各类型的ID
  156. reportIdArr := make([]int, 0)
  157. chapterIdArr := make([]int, 0)
  158. videoIdArr := make([]int, 0)
  159. roadVideoIdArr := make([]int, 0)
  160. for i := range collections {
  161. switch collections[i].CollectionType {
  162. case CollectionTypeReport:
  163. if collections[i].ExtendID > 0 {
  164. chapterIdArr = append(chapterIdArr, collections[i].ExtendID)
  165. break
  166. }
  167. reportIdArr = append(reportIdArr, collections[i].PrimaryID)
  168. case CollectionTypeVideo:
  169. videoIdArr = append(videoIdArr, collections[i].PrimaryID)
  170. case CollectionTypeRoadVideo:
  171. roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
  172. }
  173. }
  174. // 查询相应收藏类型详情
  175. var chapterErr, reportErr, videoErr, roadVideoErr error
  176. chapterMap := make(map[int]*report_chapter.ReportChapter, 0)
  177. reportMap := make(map[int]*report.Report, 0)
  178. videoMap := make(map[int]*yb_community_video.YbCommunityVideo, 0)
  179. roadVideoMap := make(map[int]*yb_road_video.YbRoadVideo, 0)
  180. wg := sync.WaitGroup{}
  181. // 章节
  182. wg.Add(1)
  183. go func() {
  184. defer wg.Done()
  185. if len(chapterIdArr) == 0 {
  186. return
  187. }
  188. chapters, e := report_chapter.GetListByChapterIds(chapterIdArr)
  189. if e != nil {
  190. chapterErr = errors.New("获取章节失败, Err: " + e.Error())
  191. return
  192. }
  193. for i := range chapters {
  194. chapterMap[chapters[i].ReportChapterId] = chapters[i]
  195. }
  196. }()
  197. // 报告
  198. wg.Add(1)
  199. go func() {
  200. defer wg.Done()
  201. if len(reportIdArr) == 0 {
  202. return
  203. }
  204. reports, e := report.GetListByReportIds(reportIdArr)
  205. if e != nil {
  206. reportErr = errors.New("获取报告失败, Err: " + e.Error())
  207. return
  208. }
  209. for i := range reports {
  210. reportMap[reports[i].Id] = reports[i]
  211. }
  212. }()
  213. // 视频
  214. wg.Add(1)
  215. go func() {
  216. defer wg.Done()
  217. if len(videoIdArr) == 0 {
  218. return
  219. }
  220. videos, e := yb_community_video.GetListByVideoIds(videoIdArr)
  221. if e != nil {
  222. videoErr = errors.New("获取视频失败, Err: " + e.Error())
  223. return
  224. }
  225. for i := range videos {
  226. videoMap[videos[i].CommunityVideoID] = videos[i]
  227. }
  228. }()
  229. // 路演视频
  230. wg.Add(1)
  231. go func() {
  232. defer wg.Done()
  233. if len(roadVideoIdArr) == 0 {
  234. return
  235. }
  236. roadVideos, e := yb_road_video.GetListByVideoIds(roadVideoIdArr)
  237. if e != nil {
  238. roadVideoErr = errors.New("获取视频失败, Err: " + e.Error())
  239. return
  240. }
  241. for i := range roadVideos {
  242. roadVideoMap[roadVideos[i].RoadVideoID] = roadVideos[i]
  243. }
  244. }()
  245. wg.Wait()
  246. if chapterErr != nil {
  247. err = chapterErr
  248. return
  249. }
  250. if reportErr != nil {
  251. err = reportErr
  252. return
  253. }
  254. if videoErr != nil {
  255. err = videoErr
  256. return
  257. }
  258. if roadVideoErr != nil {
  259. err = roadVideoErr
  260. return
  261. }
  262. // 响应列表
  263. for i := range collections {
  264. v := &response.CollectionList{
  265. CollectionId: collections[i].CollectionID,
  266. CollectionType: collections[i].CollectionType,
  267. PrimaryId: collections[i].PrimaryID,
  268. ExtendId: collections[i].ExtendID,
  269. CreateTime: collections[i].CreateTime.Format(utils.FormatDate),
  270. }
  271. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  272. switch collections[i].CollectionType {
  273. case CollectionTypeReport:
  274. // 晨周报章节
  275. if collections[i].ExtendID > 0 {
  276. cp := chapterMap[collections[i].ExtendID]
  277. if cp != nil {
  278. v.PublishTime = cp.PublishTime.Format(utils.FormatDate)
  279. v.ClassifyName = utils.REPORT_CHAPTER_TYPE_NAME_MAP[cp.ReportType]
  280. v.Author = cp.Author
  281. }
  282. break
  283. }
  284. rp := reportMap[collections[i].PrimaryID]
  285. if rp != nil {
  286. v.PublishTime = rp.PublishTime.Format(utils.FormatDate)
  287. v.ClassifyName = rp.ClassifyNameFirst
  288. v.Author = rp.Author
  289. }
  290. case CollectionTypeVideo:
  291. vd := videoMap[collections[i].PrimaryID]
  292. if vd != nil {
  293. v.PublishTime = vd.PublishTime.Format(utils.FormatDate)
  294. v.ImgUrl = vd.CoverImgURL
  295. }
  296. case CollectionTypeRoadVideo:
  297. rv := roadVideoMap[collections[i].PrimaryID]
  298. if rv != nil {
  299. v.PublishTime = rv.PublishTime.Format(utils.FormatDate)
  300. v.ImgUrl = rv.CoverImgURL
  301. v.Author = rv.AdminRealName
  302. }
  303. default:
  304. break
  305. }
  306. respList = append(respList, v)
  307. }
  308. return
  309. }
  310. // GetUserCollectByItem 获取用户是否已收藏
  311. func GetUserCollectByItem(userId, collectionType, primaryId, extendId int) (collectionId int, err error) {
  312. if userId <= 0 || collectionType <= 0 || primaryId <= 0 {
  313. return
  314. }
  315. var cond string
  316. var pars []interface{}
  317. cond += `user_id = ?`
  318. pars = append(pars, userId)
  319. cond += ` AND collection_type = ?`
  320. pars = append(pars, collectionType)
  321. cond += ` AND primary_id = ?`
  322. pars = append(pars, primaryId)
  323. if extendId > 0 {
  324. cond += ` AND extend_id = ?`
  325. pars = append(pars, extendId)
  326. }
  327. item, e := yb_user_collection.GetItemByCondition(cond, pars)
  328. if e != nil && e != utils.ErrNoRow {
  329. err = errors.New("获取用户收藏失败, Err: " + e.Error())
  330. return
  331. }
  332. if item != nil && item.CollectionID > 0 {
  333. collectionId = item.CollectionID
  334. }
  335. return
  336. }
  337. // GetUserCollectByItem 获取用户是否已收藏-列表(视频列表、路演视频列表)
  338. func GetUserCollectByList(userId, collectionType int, primaryIds []int) (collectMap map[int]int, err error) {
  339. collectMap = make(map[int]int, 0)
  340. if userId <= 0 || collectionType <= 0 || len(primaryIds) == 0 {
  341. return
  342. }
  343. var cond string
  344. var pars []interface{}
  345. cond += `user_id = ?`
  346. pars = append(pars, userId)
  347. cond += ` AND collection_type = ?`
  348. pars = append(pars, collectionType)
  349. cond += ` AND primary_id IN (?)`
  350. pars = append(pars, primaryIds)
  351. list, e := yb_user_collection.GetListByCondition(cond, pars)
  352. if e != nil {
  353. err = errors.New("获取用户收藏列表失败, Err: " + e.Error())
  354. return
  355. }
  356. for i := range list {
  357. if utils.InArrayByInt(primaryIds, list[i].PrimaryID) {
  358. collectMap[list[i].PrimaryID] = list[i].CollectionID
  359. }
  360. }
  361. return
  362. }