123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package collection
- import (
- "errors"
- "fmt"
- "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"
- "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("收藏类型有误, 当前收藏类型"))
- 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 收藏列表
- // @Param from_type query int false "来源类型:0-全部; 1-研报; 2-线上路演; 3-视频社区"
- // @Param keywords query string false "搜索关键词"
- // @Param curr_page query int false "当前页码"
- // @Param page_size query int false "每页数量"
- func GetCollectionList(fromType, currPage, pageSize int, keywords string) (total int, err error) {
- if fromType <= 0 {
- fromType = 0
- }
- // TODO:查询收藏列表
- var cond string
- var pars []interface{}
- 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
- }
- // TODO:遍历收藏列表取出各类型的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)
- }
- }
- // TODO:各类型ID大于0则进行相应的查询
- // TODO:根据收藏类型组合list
- return
- }
|