collection.go 12 KB

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