Browse Source

新增video接口

rdluck 4 years ago
parent
commit
053b197ad9
2 changed files with 219 additions and 0 deletions
  1. 142 0
      controllers/video.go
  2. 77 0
      models/video.go

+ 142 - 0
controllers/video.go

@@ -0,0 +1,142 @@
+package controllers
+
+import (
+	"hongze/hongze_api/models"
+	"hongze/hongze_api/utils"
+	"rdluck_tools/paging"
+	"time"
+)
+
+//报告
+type VideoController struct {
+	BaseAuthController
+}
+
+// @Title 获取视频标签列表
+// @Description 获取视频标签列表接口
+// @Success 200 {object} video.VideoTagsListResp
+// @router /tags/list [get]
+func (this *VideoController) TagsList() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	user := this.User
+	if user == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,用户信息为空"
+		br.Ret = 408
+		return
+	}
+	list, err := models.GetVideoTagsList()
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+
+	resp := new(models.VideoTagsListResp)
+	resp.List = list
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}
+
+// @Title 视频列表
+// @Description 视频列表接口
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
+// @Param   KeyWord   query   string  true       "搜索关键词"
+// @Param   Tags   query   string  true       "标签名称"
+// @Param   DateType   query   string  true       "时间筛选组合:1:近一个月,2:近三个月,3:近半年,4:全部"
+// @Success 200 {object} video.VideoListResp
+// @router /list [get]
+func (this *VideoController) List() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	user := this.User
+	if user == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,用户信息为空"
+		br.Ret = 408
+		return
+	}
+
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	keyWord := this.GetString("KeyWord")
+	tags := this.GetString("Tags")
+	dateType, _ := this.GetInt("DateType")
+
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = paging.StartIndex(currentIndex, pageSize)
+
+	var condition string
+	var pars []interface{}
+
+	if tags != "" {
+		videoIdStr, err := models.GetVideoIdByKeyWord(keyWord)
+		if err != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取失败,Err:" + err.Error()
+			return
+		}
+		if videoIdStr != "" {
+			condition += ` AND a.video_id IN (` + videoIdStr + `) `
+		}
+	}
+
+	if keyWord != "" {
+		condition += ` AND (a.title LIKE '%` + keyWord + `%' ) `
+	}
+
+	publishDate := ""
+	if dateType == 1 {
+		publishDate = time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
+	} else if dateType == 2 {
+		publishDate = time.Now().AddDate(0, -3, 0).Format(utils.FormatDate)
+	} else if dateType == 3 {
+		publishDate = time.Now().AddDate(0, -6, 0).Format(utils.FormatDate)
+	} else if dateType == 4 {
+		publishDate = time.Now().AddDate(-100, 0, 0).Format(utils.FormatDate)
+	}
+
+	if publishDate != "" {
+		condition += ` AND a.publish_time >=? `
+		pars = append(pars, publishDate)
+	}
+
+	total, err := models.GetVideoListCount(condition, pars)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
+		return
+	}
+
+	list, err := models.GetVideoList(condition, pars, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+
+	page := paging.GetPaging(currentIndex, pageSize, total)
+	resp := new(models.VideoListResp)
+	resp.List = list
+	resp.Paging = page
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 77 - 0
models/video.go

@@ -0,0 +1,77 @@
+package models
+
+import (
+	"rdluck_tools/orm"
+	"rdluck_tools/paging"
+	"time"
+)
+
+type VideoTagsItems struct {
+	TagsId   int    `description:"标签id"`
+	TagsName string `description:"标签名称"`
+}
+
+type VideoTagsListResp struct {
+	List []*VideoTagsItems
+}
+
+func GetVideoTagsList() (items []*VideoTagsItems, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	sql := `SELECT * FROM tags WHERE tags_type=1 ORDER BY sort ASC `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
+type VideoList struct {
+	VideoId       int       `description:"视频id"`
+	Title         string    `description:"视频标题"`
+	VideoUrl      string    `description:"视频地址"`
+	VideoCoverUrl string    `description:"视频封面地址"`
+	Status        int       `description:"视频状态:0:未发布,1:已发布"`
+	PublishTime   time.Time `description:"视频发布时间"`
+	PlaySeconds   uint32    `description:"视频播放时长"`
+	CreateTime    time.Time `description:"视频创建时间"`
+	ModifyTime    time.Time `description:"视频修改时间"`
+}
+
+func GetVideoIdByKeyWord(tagName string) (video_id string, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	sql := ` SELECT GROUP_CONCAT(DISTINCT a.video_id) AS video_id FROM video_tags AS a
+             INNER JOIN tags AS b ON a.tags_id=b.tags_id
+			 WHERE b.tags_name=? `
+	err = o.Raw(sql,tagName).QueryRow(&video_id)
+	return
+}
+
+func GetVideoListCount(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	sql := `SELECT COUNT(DISTINCT b.company_id) AS count
+			FROM video AS a  `
+	if condition != "" {
+		sql += condition
+	}
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
+func GetVideoList(condition string, pars []interface{}, startSize, pageSize int) (items []*VideoList, err error) {
+	o := orm.NewOrm()
+	o.Using("rddp")
+	sql := `SELECT a.*,GROUP_CONCAT(c.tags_name ORDER BY b.product_id ASC SEPARATOR '/') AS tags_name FROM video AS a
+			INNER JOIN video_tags AS b ON a.video_id=b.video_id
+			INNER JOIN tags AS c ON b.tags_id=c.tags_id `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` GROUP BY a.video_id  ORDER BY  a.modify_time DESC LIMIT ?,? `
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+type VideoListResp struct {
+	Paging *paging.PagingItem
+	List   []*VideoList
+}