소스 검색

temp commit

hsun 2 년 전
부모
커밋
8ad2c90554

+ 17 - 6
controller/collection/collection.go

@@ -1,9 +1,12 @@
 package collection
 
 import (
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"hongze/hongze_yb/controller/response"
 	"hongze/hongze_yb/models/request"
+	responseModel "hongze/hongze_yb/models/response"
+	"hongze/hongze_yb/services"
 	"hongze/hongze_yb/services/collection"
 	"hongze/hongze_yb/services/user"
 )
@@ -24,12 +27,21 @@ func List(c *gin.Context) {
 		return
 	}
 
-	//userInfo := user.GetInfoByClaims(c)
-	//page := services.GetCurrPageByClaims(c)
-	//pageSize := services.GetPageSizeByClaims(c)
+	userInfo := user.GetInfoByClaims(c)
+	page := services.GetCurrPageByClaims(c)
+	pageSize := services.GetPageSizeByClaims(c)
 
-	response.Ok("操作成功", c)
-	return
+	total, list, e := collection.GetCollectionList(int(userInfo.UserID), req.FromType, page, pageSize, req.Keywords)
+	if e != nil {
+		fmt.Println(e.Error())
+		response.FailMsg("获取收藏列表失败", "获取收藏列表失败, Err: "+e.Error(), c)
+		return
+	}
+
+	response.OkData("获取成功", &responseModel.CollectionListResp{
+		List:   list,
+		Paging: responseModel.GetPaging(page, pageSize, total),
+	}, c)
 }
 
 // Collect
@@ -58,7 +70,6 @@ func Collect(c *gin.Context) {
 	}
 
 	response.Ok("操作成功", c)
-	return
 }
 
 // Cancel

+ 2 - 0
init_serve/router.go

@@ -73,5 +73,7 @@ func InitRouter() (r *gin.Engine) {
 	routers.InitVoiceBroadcast(r)
 	//线上路演
 	routers.InitRoad(r)
+	// 收藏路由
+	routers.InitCollection(r)
 	return
 }

+ 5 - 5
models/request/collection.go

@@ -2,15 +2,15 @@ package request
 
 // CollectionCollectReq 收藏请求体
 type CollectionCollectReq struct {
-	CollectionType int `description:"收藏类型:1-研报; 2-视频社区; 3-微路演视频"`
-	PrimaryId      int `description:"收藏类型主ID"`
-	ExtendId       int `description:"扩展ID-如研报章节"`
-	SourceAgent    int `description:"操作来源:1-小程序 2-小程序 PC 3-弘则研究公众号 4-Web PC"`
+	CollectionType int `json:"collection_type" description:"收藏类型:1-研报; 2-视频社区; 3-微路演视频"`
+	PrimaryId      int `json:"primary_id" description:"收藏类型主ID"`
+	ExtendId       int `json:"extend_id" description:"扩展ID-如研报章节"`
+	SourceAgent    int `json:"source_agent" description:"操作来源:1-小程序 2-小程序 PC 3-弘则研究公众号 4-Web PC"`
 }
 
 // CollectionCancelReq 取消收藏请求体
 type CollectionCancelReq struct {
-	CollectionId int `description:"收藏ID"`
+	CollectionId int `json:"collection_id" description:"收藏ID"`
 }
 
 // CollectionListReq 收藏列表请求体

+ 9 - 2
models/response/collection.go

@@ -8,6 +8,13 @@ type CollectionListResp struct {
 
 // CollectionList 收藏列表
 type CollectionList struct {
-	CollectionId   int `description:"收藏ID"`
-	CollectionType int `description:"收藏类型: 1-研报; 2-视频社区; 3-微路演视频"`
+	CollectionId   int    `description:"收藏ID"`
+	CollectionType int    `description:"收藏类型: 1-研报; 2-视频社区; 3-微路演视频"`
+	PrimaryId      int    `description:"不同收藏类型的ID"`
+	ExtendId       int    `description:"扩展ID: 如晨周报章节的ID, 大于0则详情页跳转章节详情"`
+	PublishTime    string `description:"发布时间"`
+	CreateTime     string `description:"收藏时间"`
+	ClassifyName   string `description:"报告类型名称"`
+	Author         string `description:"作者"`
+	ImgUrl         string `description:"图片地址"`
 }

+ 20 - 1
models/tables/rddp/report/query.go

@@ -139,7 +139,10 @@ func GetByReportId(id int) (item *Report, err error) {
 
 // GetByReportIds 根据id获取报告
 func GetByReportIds(ids []int) (list []*Report, err error) {
-	err = global.MYSQL["rddp"].Model(Report{}).Where("id in (?) and state = 2", ids).Select("id, create_time").Scan(&list).Error
+	err = global.MYSQL["rddp"].Model(Report{}).
+		Where("id in (?) and state = 2", ids).
+		Select("id, create_time").
+		Scan(&list).Error
 	if err == utils.ErrNoRow {
 		err = nil
 	}
@@ -405,4 +408,20 @@ ORDER BY
 	sql = fmt.Sprintf(sql, firstName, secondId)
 	err = global.MYSQL["rddp"].Raw(sql).First(&items).Error
 	return
+}
+
+
+// GetListByReportIds 根据IDs获取列表
+func GetListByReportIds(reportIds []int) (list []*Report, err error) {
+	var where string
+	where = `state = 2`
+	if len(reportIds) > 0 {
+		where += ` AND id IN (?)`
+	}
+
+	err = global.MYSQL["rddp"].Model(Report{}).
+		Where(where, reportIds).
+		Order("id asc").
+		Scan(&list).Error
+	return
 }

+ 16 - 0
models/tables/rddp/report_chapter/query.go

@@ -178,3 +178,19 @@ func GetItemById(chapterId int) (item *ReportChapter, err error) {
 		First(&item).Error
 	return
 }
+
+// GetListByChapterIds 根据章节IDs获取列表
+func GetListByChapterIds(chapterIds []int) (list []*ReportChapter, err error) {
+	var where string
+	where = `publish_state = 2`
+	if len(chapterIds) > 0 {
+		where += ` AND report_chapter_id IN (?)`
+	}
+
+	err = global.MYSQL["rddp"].Model(ReportChapter{}).
+		//Select("report_id, type_id, report_chapter_id, classify_name_first, classify_id_first, video_url, video_name, video_play_seconds, video_size, sort").
+		Where(where, chapterIds).
+		Order("sort asc, report_chapter_id asc").
+		Scan(&list).Error
+	return
+}

+ 15 - 0
models/tables/yb_community_video/model.go

@@ -39,3 +39,18 @@ func GetItemById(videoId int) (item *YbCommunityVideo, err error) {
 		First(&item).Error
 	return
 }
+
+// GetListByVideoIds 根据视频IDs获取列表
+func GetListByVideoIds(videoIds []int) (list []*YbCommunityVideo, err error) {
+	var where string
+	where = `publish_state = 1 AND is_deleted = 0`
+	if len(videoIds) > 0 {
+		where += ` AND community_video_id IN (?)`
+	}
+
+	err = global.DEFAULT_MYSQL.Model(YbCommunityVideo{}).
+		Where(where, videoIds).
+		Order("community_video_id asc").
+		Scan(&list).Error
+	return
+}

+ 15 - 0
models/tables/yb_road_video/model.go

@@ -37,3 +37,18 @@ func GetItemById(videoId int) (item *YbRoadVideo, err error) {
 		First(&item).Error
 	return
 }
+
+// GetListByVideoIds 根据视频IDs获取列表
+func GetListByVideoIds(videoIds []int) (list []*YbRoadVideo, err error) {
+	var where string
+	where = `publish_state = 1 AND is_deleted = 0`
+	if len(videoIds) > 0 {
+		where += ` AND road_video_id IN (?)`
+	}
+
+	err = global.DEFAULT_MYSQL.Model(YbRoadVideo{}).
+		Where(where, videoIds).
+		Order("road_video_id asc").
+		Scan(&list).Error
+	return
+}

+ 14 - 0
routers/collection.go

@@ -0,0 +1,14 @@
+package routers
+
+import (
+	"github.com/gin-gonic/gin"
+	"hongze/hongze_yb/controller/collection"
+	"hongze/hongze_yb/middleware"
+)
+
+func InitCollection(r *gin.Engine) {
+	rGroup := r.Group("/api/collection").Use(middleware.Token())
+	rGroup.GET("/list", collection.List)
+	rGroup.POST("/collect", collection.Collect)
+	rGroup.POST("/cancel", collection.Cancel)
+}

+ 157 - 10
services/collection/collection.go

@@ -3,11 +3,14 @@ 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"
 )
 
@@ -62,7 +65,7 @@ func AddCollection(userId, collectionType, primaryId, extendId, sourceAgent int)
 		title = roadVideo.Title
 		publishTime = roadVideo.PublishTime
 	default:
-		err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型"))
+		err = errors.New(fmt.Sprintf("收藏类型有误, 当前收藏类型%d", collectionType))
 		return
 	}
 
@@ -115,17 +118,17 @@ func CancelCollection(userId, collectionId int) (err error) {
 }
 
 // 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) {
+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
 	}
-	// TODO:查询收藏列表
+
+	// 查询收藏列表
 	var cond string
 	var pars []interface{}
+	cond += `user_id = ?`
+	pars = append(pars, userId)
 	if fromType > 0 {
 		cond += ` AND collection_type = ?`
 		pars = append(pars, fromType)
@@ -140,7 +143,8 @@ func GetCollectionList(fromType, currPage, pageSize int, keywords string) (total
 		err = errors.New("获取收藏列表失败, Err: " + e.Error())
 		return
 	}
-	// TODO:遍历收藏列表取出各类型的ID
+
+	// 遍历收藏列表取出各类型的ID
 	reportIdArr := make([]int, 0)
 	chapterIdArr := make([]int, 0)
 	videoIdArr := make([]int, 0)
@@ -159,9 +163,152 @@ func GetCollectionList(fromType, currPage, pageSize int, keywords string) (total
 			roadVideoIdArr = append(roadVideoIdArr, collections[i].PrimaryID)
 		}
 	}
-	// TODO:各类型ID大于0则进行相应的查询
 
-	// TODO:根据收藏类型组合list
+	// 查询相应收藏类型详情
+	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
 }

+ 11 - 0
utils/constants.go

@@ -202,3 +202,14 @@ var SystemSourceList = []int{
 const (
 	SendTemplateMsgAuthorization = "dc855fce962a639faa779cbdd4cd332f"
 )
+
+// 研报类型标识
+var (
+	REPORT_TYPE_DAY  = "day"
+	REPORT_TYPE_WEEK = "week"
+)
+
+var REPORT_CHAPTER_TYPE_NAME_MAP = map[string]string{
+	REPORT_TYPE_DAY:  "晨报",
+	REPORT_TYPE_WEEK: "周报",
+}