123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- package collection
- import (
- "errors"
- "fmt"
- "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/tables/rddp/report"
- "hongze/hongze_yb/models/tables/rddp/report_chapter"
- "hongze/hongze_yb/models/tables/yb_community_video"
- "hongze/hongze_yb/models/tables/yb_road_video"
- "hongze/hongze_yb/models/tables/yb_user_collection"
- "hongze/hongze_yb/utils"
- "sync"
- "time"
- )
- // 收藏类型
- const (
- CollectionTypeReport = iota + 1 // 研报
- CollectionTypeVideo // 视频社区
- CollectionTypeRoadVideo // 微路演视频
- )
- // AddCollection 加入收藏
- func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int) (err error) {
- title := ""
- nowTime := time.Now().Local()
- publishTime := nowTime
- // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
- switch collectionType {
- case CollectionTypeReport:
- // 晨周报章节
- if extendId > 0 {
- chapter, e := report_chapter.GetItemById(extendId)
- if e != nil {
- err = errors.New("获取章节失败, Err: " + e.Error())
- return
- }
- title = chapter.Title
- publishTime = chapter.PublishTime
- break
- }
- rp, e := report.GetPublishByReportId(primaryId)
- if e != nil {
- err = errors.New("获取报告失败, Err: " + e.Error())
- return
- }
- title = rp.Title
- publishTime = rp.PublishTime
- case CollectionTypeVideo:
- video, e := yb_community_video.GetItemById(primaryId)
- if e != nil {
- err = errors.New("获取视频失败, Err: " + e.Error())
- return
- }
- title = video.Title
- publishTime = video.PublishTime
- case CollectionTypeRoadVideo:
- roadVideo, e := yb_road_video.GetItemById(primaryId)
- if e != nil {
- err = errors.New("获取路演视频失败, Err: " + e.Error())
- return
- }
- title = roadVideo.Title
- publishTime = roadVideo.PublishTime
- default:
- err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型%d", collectionType))
- return
- }
- item := &yb_user_collection.YbUserCollection{
- CollectionType: collectionType,
- UserID: userId,
- PrimaryID: primaryId,
- ExtendID: extendId,
- State: 1,
- SourceAgent: sourceAgent,
- Title: title,
- PublishTime: publishTime,
- CreateTime: nowTime,
- ModifyTime: nowTime,
- }
- if e := item.Create(); e != nil {
- err = errors.New("新增收藏失败, Err: " + e.Error())
- return
- }
- return
- }
- // CancelCollection 取消收藏
- func CancelCollection(userId, collectionId int) (err error) {
- item, e := yb_user_collection.GetItemById(collectionId)
- if e != nil {
- err = errors.New("获取收藏失败, Err: " + e.Error())
- return
- }
- if item.CollectionID <= 0 {
- err = errors.New("收藏信息有误")
- return
- }
- if item.State != 1 {
- err = errors.New("收藏状态有误")
- return
- }
- if item.UserID != userId {
- err = errors.New(fmt.Sprintf("收藏人信息有误, 操作人ID: %d, 被操作人ID: %d", userId, item.UserID))
- return
- }
- updateCols := []string{"State", "ModifyTime"}
- item.State = 0
- item.ModifyTime = time.Now().Local()
- if e = item.Update(updateCols); e != nil {
- err = errors.New("更新收藏失败, Err: " + e.Error())
- return
- }
- return
- }
- // GetCollectionList 收藏列表
- func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string) (total int, respList []*response.CollectionList, err error) {
- respList = make([]*response.CollectionList, 0)
- if fromType <= 0 {
- fromType = 0
- }
- // 查询收藏列表
- var cond string
- var pars []interface{}
- cond += `user_id = ?`
- pars = append(pars, userId)
- if fromType > 0 {
- cond += ` AND collection_type = ?`
- pars = append(pars, fromType)
- }
- if keywords != "" {
- keywords = "%" + keywords + "%"
- cond += ` AND title LIKE ?`
- pars = append(pars, keywords)
- }
- collections, e := yb_user_collection.GetPageListByCondition(cond, pars, currPage, pageSize)
- if e != nil {
- err = errors.New("获取收藏列表失败, Err: " + e.Error())
- return
- }
- // 遍历收藏列表取出各类型的ID
- reportIdArr := make([]int, 0)
- chapterIdArr := make([]int, 0)
- videoIdArr := make([]int, 0)
- roadVideoIdArr := make([]int, 0)
- for i := range collections {
- switch collections[i].CollectionType {
- case CollectionTypeReport:
- if collections[i].ExtendID > 0 {
- chapterIdArr = append(chapterIdArr, collections[i].ExtendID)
- break
- }
- reportIdArr = append(reportIdArr, collections[i].PrimaryID)
- case CollectionTypeVideo:
- videoIdArr = append(videoIdArr, collections[i].PrimaryID)
- case CollectionTypeRoadVideo:
- roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
- }
- }
- // 查询相应收藏类型详情
- wg := sync.WaitGroup{}
- // 章节
- var chapterErr, reportErr, videoErr, roadVideoErr error
- chapterMap := make(map[int]*report_chapter.ReportChapter, 0)
- reportMap := make(map[int]*report.Report, 0)
- videoMap := make(map[int]*yb_community_video.YbCommunityVideo, 0)
- roadVideoMap := make(map[int]*yb_road_video.YbRoadVideo, 0)
- if len(chapterIdArr) > 0 {
- wg.Add(1)
- go func() {
- wg.Done()
- chapters, e := report_chapter.GetListByChapterIds(chapterIdArr)
- if e != nil {
- chapterErr = errors.New("获取章节失败, Err: " + e.Error())
- return
- }
- for i := range chapters {
- chapterMap[chapters[i].ReportChapterId] = chapters[i]
- fmt.Println("1", chapterMap)
- }
- }()
- }
- fmt.Println("2", chapterMap)
- // 报告
- if len(reportIdArr) > 0 {
- wg.Add(1)
- go func() {
- wg.Done()
- reports, e := report.GetListByReportIds(reportIdArr)
- if e != nil {
- reportErr = errors.New("获取报告失败, Err: " + e.Error())
- return
- }
- for i := range reports {
- reportMap[reports[i].Id] = reports[i]
- }
- }()
- }
- // 视频
- if len(videoIdArr) > 0 {
- wg.Add(1)
- go func() {
- wg.Done()
- videos, e := yb_community_video.GetListByVideoIds(videoIdArr)
- if e != nil {
- videoErr = errors.New("获取视频失败, Err: " + e.Error())
- return
- }
- for i := range videos {
- videoMap[videos[i].CommunityVideoID] = videos[i]
- }
- }()
- }
- // 路演视频
- if len(roadVideoIdArr) > 0 {
- wg.Add(1)
- go func() {
- wg.Done()
- roadVideos, e := yb_road_video.GetListByVideoIds(roadVideoIdArr)
- if e != nil {
- roadVideoErr = errors.New("获取视频失败, Err: " + e.Error())
- return
- }
- for i := range roadVideos {
- roadVideoMap[roadVideos[i].RoadVideoID] = roadVideos[i]
- }
- }()
- }
- wg.Wait()
- if chapterErr != nil {
- err = chapterErr
- return
- }
- if reportErr != nil {
- err = reportErr
- return
- }
- if videoErr != nil {
- err = videoErr
- return
- }
- if roadVideoErr != nil {
- err = roadVideoErr
- return
- }
- fmt.Println("3", chapterMap)
- // 响应列表
- for i := range collections {
- v := &response.CollectionList{
- CollectionId: collections[i].CollectionID,
- CollectionType: collections[i].CollectionType,
- PrimaryId: collections[i].PrimaryID,
- ExtendId: collections[i].ExtendID,
- CreateTime: collections[i].CreateTime.Format(utils.FormatDate),
- }
- // 收藏类型:1-研报; 2-视频社区; 3-微路演视频
- switch collections[i].CollectionType {
- case CollectionTypeReport:
- // 晨周报章节
- if collections[i].ExtendID > 0 {
- cp := chapterMap[collections[i].ExtendID]
- if cp != nil {
- v.PublishTime = cp.PublishTime.Format(utils.FormatDate)
- v.ClassifyName = utils.REPORT_CHAPTER_TYPE_NAME_MAP[cp.ReportType]
- v.Author = cp.Author
- }
- break
- }
- rp := reportMap[collections[i].PrimaryID]
- if rp != nil {
- v.PublishTime = rp.PublishTime.Format(utils.FormatDate)
- v.ClassifyName = rp.ClassifyNameFirst
- v.Author = rp.Author
- }
- case CollectionTypeVideo:
- vd := videoMap[collections[i].PrimaryID]
- if vd != nil {
- v.PublishTime = vd.PublishTime.Format(utils.FormatDate)
- v.ImgUrl = vd.CoverImgURL
- }
- case CollectionTypeRoadVideo:
- rv := roadVideoMap[collections[i].PrimaryID]
- if rv != nil {
- v.PublishTime = rv.PublishTime.Format(utils.FormatDate)
- v.ImgUrl = rv.CoverImgURL
- v.Author = rv.AdminRealName
- }
- default:
- break
- }
- respList = append(respList, v)
- }
- return
- }
|