collection.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. "strings"
  13. "sync"
  14. "time"
  15. )
  16. // 收藏类型
  17. const (
  18. CollectionTypeReport = iota + 1 // 研报
  19. CollectionTypeVideo // 视频社区
  20. CollectionTypeRoadVideo // 微路演视频
  21. )
  22. // AddCollection 加入收藏
  23. func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (collectionId int, err error) {
  24. if userId <= 0 || collectionType <= 0 || primaryId <= 0 {
  25. return
  26. }
  27. title := ""
  28. nowTime := time.Now().Local()
  29. publishTime := nowTime
  30. // 判断是否曾收藏过
  31. var cond string
  32. var pars []interface{}
  33. cond += `user_id = ?`
  34. pars = append(pars, userId)
  35. cond += ` AND collection_type = ?`
  36. pars = append(pars, collectionType)
  37. cond += ` AND primary_id = ?`
  38. pars = append(pars, primaryId)
  39. if extendId > 0 {
  40. cond += ` AND extend_id = ?`
  41. pars = append(pars, extendId)
  42. }
  43. exist, e := yb_user_collection.GetItemByCondition(cond, pars)
  44. if e != nil && e != utils.ErrNoRow {
  45. err = errors.New("获取用户收藏失败, Err: " + e.Error())
  46. return
  47. }
  48. if exist != nil && exist.CollectionID > 0 {
  49. // 可能手快重复收藏
  50. collectionId = exist.CollectionID
  51. if exist.State == 1 {
  52. return
  53. }
  54. // 重新收藏
  55. exist.State = 1
  56. exist.CreateTime = nowTime
  57. exist.ModifyTime = nowTime
  58. updateCols := []string{"State", "CreateTime", "ModifyTime"}
  59. if e = exist.Update(updateCols); e != nil {
  60. collectionId = 0
  61. err = errors.New("重新收藏失败, Err: " + e.Error())
  62. return
  63. }
  64. return
  65. }
  66. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  67. switch collectionType {
  68. case CollectionTypeReport:
  69. // 晨周报章节
  70. if extendId > 0 {
  71. chapter, e := report_chapter.GetItemById(extendId)
  72. if e != nil {
  73. err = errors.New("获取章节失败, Err: " + e.Error())
  74. return
  75. }
  76. title = chapter.Title
  77. publishTime = chapter.PublishTime
  78. break
  79. }
  80. rp, e := report.GetPublishByReportId(primaryId)
  81. if e != nil {
  82. err = errors.New("获取报告失败, Err: " + e.Error())
  83. return
  84. }
  85. title = rp.Title
  86. publishTime = rp.PublishTime
  87. case CollectionTypeVideo:
  88. video, e := yb_community_video.GetItemById(primaryId)
  89. if e != nil {
  90. err = errors.New("获取视频失败, Err: " + e.Error())
  91. return
  92. }
  93. title = video.Title
  94. publishTime = video.PublishTime
  95. case CollectionTypeRoadVideo:
  96. roadVideo, e := yb_road_video.GetItemById(primaryId)
  97. if e != nil {
  98. err = errors.New("获取路演视频失败, Err: " + e.Error())
  99. return
  100. }
  101. title = roadVideo.Title
  102. publishTime = roadVideo.PublishTime
  103. default:
  104. err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型%d", collectionType))
  105. return
  106. }
  107. // 新增收藏
  108. item := &yb_user_collection.YbUserCollection{
  109. CollectionType: collectionType,
  110. UserID: userId,
  111. PrimaryID: primaryId,
  112. ExtendID: extendId,
  113. State: 1,
  114. SourceAgent: sourceAgent,
  115. Title: title,
  116. PublishTime: publishTime,
  117. CreateTime: nowTime,
  118. ModifyTime: nowTime,
  119. }
  120. if e := item.Create(); e != nil {
  121. err = errors.New("新增收藏失败, Err: " + e.Error())
  122. return
  123. }
  124. collectionId = item.CollectionID
  125. return
  126. }
  127. // CancelCollection 取消收藏
  128. func CancelCollection(userId, collectionId int) (err error) {
  129. item, e := yb_user_collection.GetItemById(collectionId)
  130. if e != nil {
  131. err = errors.New("获取收藏失败, Err: " + e.Error())
  132. return
  133. }
  134. if item.CollectionID <= 0 {
  135. err = errors.New("收藏信息有误")
  136. return
  137. }
  138. if item.State != 1 {
  139. err = errors.New("收藏状态有误")
  140. return
  141. }
  142. if item.UserID != userId {
  143. err = errors.New(fmt.Sprintf("收藏人信息有误, 操作人ID: %d, 被操作人ID: %d", userId, item.UserID))
  144. return
  145. }
  146. updateCols := []string{"State", "ModifyTime"}
  147. item.State = 0
  148. item.ModifyTime = time.Now().Local()
  149. if e = item.Update(updateCols); e != nil {
  150. err = errors.New("更新收藏失败, Err: " + e.Error())
  151. return
  152. }
  153. return
  154. }
  155. // GetCollectionList 收藏列表
  156. func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string) (total int, respList []*response.CollectionList, err error) {
  157. respList = make([]*response.CollectionList, 0)
  158. if fromType <= 0 {
  159. fromType = 0
  160. }
  161. // 查询收藏列表
  162. var cond string
  163. var pars []interface{}
  164. cond += `state = 1 AND user_id = ?`
  165. pars = append(pars, userId)
  166. if fromType > 0 {
  167. cond += ` AND collection_type = ?`
  168. pars = append(pars, fromType)
  169. }
  170. if keywords != "" {
  171. cond += ` AND title LIKE ?`
  172. pars = append(pars, fmt.Sprint("%", keywords, "%"))
  173. }
  174. collectionTotal, e := yb_user_collection.GetPageListTotalByCondition(cond, pars)
  175. if e != nil {
  176. err = errors.New("获取收藏列表总数失败, Err: " + e.Error())
  177. return
  178. }
  179. total = int(collectionTotal)
  180. collections, e := yb_user_collection.GetPageListByCondition(cond, pars, currPage, pageSize)
  181. if e != nil {
  182. err = errors.New("获取收藏列表失败, Err: " + e.Error())
  183. return
  184. }
  185. // 遍历收藏列表取出各类型的ID
  186. reportIdArr := make([]int, 0)
  187. chapterIdArr := make([]int, 0)
  188. videoIdArr := make([]int, 0)
  189. roadVideoIdArr := make([]int, 0)
  190. for i := range collections {
  191. switch collections[i].CollectionType {
  192. case CollectionTypeReport:
  193. if collections[i].ExtendID > 0 {
  194. chapterIdArr = append(chapterIdArr, collections[i].ExtendID)
  195. break
  196. }
  197. reportIdArr = append(reportIdArr, collections[i].PrimaryID)
  198. case CollectionTypeVideo:
  199. videoIdArr = append(videoIdArr, collections[i].PrimaryID)
  200. case CollectionTypeRoadVideo:
  201. roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
  202. }
  203. }
  204. // 查询相应收藏类型详情
  205. var chapterErr, reportErr, videoErr, roadVideoErr error
  206. chapterMap := make(map[int]*report_chapter.ReportChapter, 0)
  207. reportMap := make(map[int]*report.Report, 0)
  208. videoMap := make(map[int]*yb_community_video.YbCommunityVideo, 0)
  209. roadVideoMap := make(map[int]*yb_road_video.YbRoadVideo, 0)
  210. wg := sync.WaitGroup{}
  211. // 章节
  212. wg.Add(1)
  213. go func() {
  214. defer wg.Done()
  215. if len(chapterIdArr) == 0 {
  216. return
  217. }
  218. chapters, e := report_chapter.GetListByChapterIds(chapterIdArr)
  219. if e != nil {
  220. chapterErr = errors.New("获取章节失败, Err: " + e.Error())
  221. return
  222. }
  223. for i := range chapters {
  224. chapterMap[chapters[i].ReportChapterId] = chapters[i]
  225. }
  226. }()
  227. // 报告
  228. wg.Add(1)
  229. go func() {
  230. defer wg.Done()
  231. if len(reportIdArr) == 0 {
  232. return
  233. }
  234. reports, e := report.GetListByReportIds(reportIdArr)
  235. if e != nil {
  236. reportErr = errors.New("获取报告失败, Err: " + e.Error())
  237. return
  238. }
  239. for i := range reports {
  240. reportMap[reports[i].Id] = reports[i]
  241. }
  242. }()
  243. // 视频
  244. wg.Add(1)
  245. go func() {
  246. defer wg.Done()
  247. if len(videoIdArr) == 0 {
  248. return
  249. }
  250. videos, e := yb_community_video.GetListByVideoIds(videoIdArr)
  251. if e != nil {
  252. videoErr = errors.New("获取视频失败, Err: " + e.Error())
  253. return
  254. }
  255. for i := range videos {
  256. videoMap[videos[i].CommunityVideoID] = videos[i]
  257. }
  258. }()
  259. // 路演视频
  260. wg.Add(1)
  261. go func() {
  262. defer wg.Done()
  263. if len(roadVideoIdArr) == 0 {
  264. return
  265. }
  266. roadVideos, e := yb_road_video.GetListByVideoIds(roadVideoIdArr)
  267. if e != nil {
  268. roadVideoErr = errors.New("获取视频失败, Err: " + e.Error())
  269. return
  270. }
  271. for i := range roadVideos {
  272. roadVideoMap[roadVideos[i].RoadVideoID] = roadVideos[i]
  273. }
  274. }()
  275. wg.Wait()
  276. if chapterErr != nil {
  277. err = chapterErr
  278. return
  279. }
  280. if reportErr != nil {
  281. err = reportErr
  282. return
  283. }
  284. if videoErr != nil {
  285. err = videoErr
  286. return
  287. }
  288. if roadVideoErr != nil {
  289. err = roadVideoErr
  290. return
  291. }
  292. // 响应列表
  293. titlePre := `<div style="-webkit-line-clamp: 2;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;">`
  294. titleSuf := `</div>`
  295. highlightPre := `<span style="color:#E3B377">`
  296. highlightSuf := `</span>`
  297. for i := range collections {
  298. v := &response.CollectionList{
  299. CollectionId: collections[i].CollectionID,
  300. CollectionType: collections[i].CollectionType,
  301. PrimaryId: collections[i].PrimaryID,
  302. ExtendId: collections[i].ExtendID,
  303. CreateTime: collections[i].CreateTime.Format(utils.FormatDate),
  304. }
  305. // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
  306. switch collections[i].CollectionType {
  307. case CollectionTypeReport:
  308. // 晨周报章节
  309. if collections[i].ExtendID > 0 {
  310. cp := chapterMap[collections[i].ExtendID]
  311. if cp != nil {
  312. v.PublishTime = cp.PublishTime.Format(utils.FormatDate)
  313. v.ClassifyName = utils.REPORT_CHAPTER_TYPE_NAME_MAP[cp.ReportType]
  314. v.Author = cp.Author
  315. v.Title = cp.Title
  316. }
  317. break
  318. }
  319. rp := reportMap[collections[i].PrimaryID]
  320. if rp != nil {
  321. v.PublishTime = rp.PublishTime.Format(utils.FormatDate)
  322. v.ClassifyName = rp.ClassifyNameFirst
  323. v.ClassifySecondName = rp.ClassifyNameSecond
  324. v.Author = rp.Author
  325. v.Title = rp.Title
  326. }
  327. case CollectionTypeVideo:
  328. vd := videoMap[collections[i].PrimaryID]
  329. if vd != nil {
  330. v.PublishTime = vd.PublishTime.Format(utils.FormatDate)
  331. v.ImgUrl = vd.CoverImgURL
  332. v.Title = vd.Title
  333. }
  334. case CollectionTypeRoadVideo:
  335. rv := roadVideoMap[collections[i].PrimaryID]
  336. if rv != nil {
  337. v.PublishTime = rv.PublishTime.Format(utils.FormatDate)
  338. v.ImgUrl = rv.CoverImgURL
  339. v.Author = rv.AdminRealName
  340. v.Title = rv.Title
  341. }
  342. default:
  343. break
  344. }
  345. // 标题富文本及高亮
  346. v.Title = fmt.Sprint(titlePre, v.Title, titleSuf)
  347. if keywords != "" {
  348. kw := fmt.Sprint(highlightPre, keywords, highlightSuf)
  349. v.Title = strings.ReplaceAll(v.Title, keywords, kw)
  350. }
  351. respList = append(respList, v)
  352. }
  353. return
  354. }
  355. // GetUserCollectByItem 获取用户是否已收藏
  356. func GetUserCollectByItem(userId, collectionType, primaryId, extendId int) (collectionId int, err error) {
  357. if userId <= 0 || collectionType <= 0 || primaryId <= 0 {
  358. return
  359. }
  360. var cond string
  361. var pars []interface{}
  362. cond += `state = 1 AND user_id = ?`
  363. pars = append(pars, userId)
  364. cond += ` AND collection_type = ?`
  365. pars = append(pars, collectionType)
  366. cond += ` AND primary_id = ?`
  367. pars = append(pars, primaryId)
  368. if extendId > 0 {
  369. cond += ` AND extend_id = ?`
  370. pars = append(pars, extendId)
  371. }
  372. item, e := yb_user_collection.GetItemByCondition(cond, pars)
  373. if e != nil && e != utils.ErrNoRow {
  374. err = errors.New("获取用户收藏失败, Err: " + e.Error())
  375. return
  376. }
  377. if item != nil && item.CollectionID > 0 {
  378. collectionId = item.CollectionID
  379. }
  380. return
  381. }
  382. // GetUserCollectByItem 获取用户是否已收藏-列表(视频列表、路演视频列表)
  383. func GetUserCollectByList(userId, collectionType int, primaryIds []int) (collectMap map[int]int, err error) {
  384. collectMap = make(map[int]int, 0)
  385. if userId <= 0 || collectionType <= 0 || len(primaryIds) == 0 {
  386. return
  387. }
  388. var cond string
  389. var pars []interface{}
  390. cond += `state = 1 AND user_id = ?`
  391. pars = append(pars, userId)
  392. cond += ` AND collection_type = ?`
  393. pars = append(pars, collectionType)
  394. cond += ` AND primary_id IN (?)`
  395. pars = append(pars, primaryIds)
  396. list, e := yb_user_collection.GetListByCondition(cond, pars)
  397. if e != nil {
  398. err = errors.New("获取用户收藏列表失败, Err: " + e.Error())
  399. return
  400. }
  401. for i := range list {
  402. if utils.InArrayByInt(primaryIds, list[i].PrimaryID) {
  403. collectMap[list[i].PrimaryID] = list[i].CollectionID
  404. }
  405. }
  406. return
  407. }