Browse Source

首页头部导航、微路演列表

hsun 2 years ago
parent
commit
70b96509b1

+ 1 - 1
controllers/home.go

@@ -583,7 +583,7 @@ func (this *HomeController) HeaderTab() {
 	}
 	searchPage, _ := this.GetInt("SearchPage")
 
-	key := models.HomeHeaderTabConfigCode
+	key := models.HomeHeaderTabConfigKey
 	conf, e := models.GetConfigByCode(key)
 	if e != nil {
 		br.Msg = "获取失败"

+ 29 - 3
controllers/micro_roadshow.go

@@ -1,7 +1,10 @@
 package controllers
 
 import (
+	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/services"
+	"hongze/hongze_cygx/utils"
 )
 
 // 微路演
@@ -14,6 +17,8 @@ type MicroRoadShowController struct {
 // @Param   PageSize		query	int		true	"每页数据条数"
 // @Param   CurrentIndex	query	int		true	"当前页页码,从1开始"
 // @Param   KeyWord			query	string	false	"搜索关键词"
+// @Param   AudioId			query	int		false	"音频ID"
+// @Param   VideoId			query	int		false	"视频ID"
 // @Success 200 {object} models.HomeListResp
 // @router /list [get]
 func (this *MicroRoadShowController) List() {
@@ -29,12 +34,33 @@ func (this *MicroRoadShowController) List() {
 		br.Ret = 408
 		return
 	}
-	//pageSize, _ := this.GetInt("PageSize")
-	//currentIndex, _ := this.GetInt("CurrentIndex")
-	//keywords := this.GetString("KeyWord")
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	keywords := this.GetString("KeyWord")
+	audioId, _ := this.GetInt("AudioId")
+	videoId, _ := this.GetInt("VideoId")
 
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+
+	list, total, e := services.GetMicroRoadShowPageList(pageSize, currentIndex, audioId, videoId, keywords)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取微路演列表失败, Err: " + e.Error()
+		return
+	}
+
+	resp := new(models.MicroRoadShowListResp)
+	page := paging.GetPaging(currentIndex, pageSize, total)
+	resp.List = list
+	resp.Paging = page
 
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
+	br.Data = resp
 }

+ 2 - 1
models/config.go

@@ -6,7 +6,8 @@ import (
 )
 
 var (
-	HomeHeaderTabConfigCode = "home_header_tab"
+	HomeHeaderTabConfigKey              = "home_header_tab"
+	MicroRoadShowListDataRatioConfigKey = "micro_roadshow_list_data_ratio"
 )
 
 type CygxConfig struct {

+ 89 - 0
models/micro_roadshow.go

@@ -0,0 +1,89 @@
+package models
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"github.com/rdlucklib/rdluck_tools/paging"
+)
+
+// MicroRoadShowListResp 微路演列表响应体
+type MicroRoadShowListResp struct {
+	Paging *paging.PagingItem
+	List   []*MicroRoadShowPageList
+}
+
+// MicroRoadShowPageList 微路演列表
+type MicroRoadShowPageList struct {
+	Id                  int    `description:"音视频ID"`
+	Title               string `description:"标题"`
+	ResourceUrl         string `description:"链接"`
+	Type                int    `description:"类型: 1-音频; 2-视频"`
+	PublishTime         string `description:"发布时间"`
+	BackgroundImg       string `description:"背景图"`
+	ChartPermissionId   int    `description:"行业ID"`
+	ChartPermissionName string `description:"行业名称"`
+	PlaySeconds         string `description:"音视频时长"`
+	//AuthOk              bool   `description:"是否有权限"`
+}
+
+// GetMicroRoadShowAudioPageList 获取微路演音频列表-分页
+func GetMicroRoadShowAudioPageList(startSize, pageSize int, condition string, pars []interface{}) (total int, list []*MicroRoadShowPageList, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+				a.activity_voice_id AS id,
+				a.voice_name AS title,
+				a.voice_url AS resource_url,
+				1 AS type,
+				b.activity_time AS publish_time,
+				b.chart_permission_id,
+				b.chart_permission_name,
+				a.voice_play_seconds AS play_seconds
+			FROM
+				cygx_activity_voice AS a
+			JOIN cygx_activity AS b ON a.activity_id = b.activity_id `
+	if condition != `` {
+		sql += condition
+	}
+	sql += ` ORDER BY publish_time DESC`
+
+	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
+	err = o.Raw(totalSql, pars).QueryRow(&total)
+	if err != nil {
+		return
+	}
+
+	sql += ` LIMIT ?,?`
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
+	return
+}
+
+// GetMicroRoadShowVideoPageList 获取微路演视频列表-分页
+func GetMicroRoadShowVideoPageList(startSize, pageSize int, condition string, pars []interface{}) (total int, list []*MicroRoadShowPageList, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+				video_id AS id,
+				video_name AS title,
+				video_url AS resource_url,
+				2 AS type,
+				publish_date AS publish_time,
+				chart_permission_id,
+				chart_permission_name,
+				video_duration AS play_seconds
+			FROM
+				cygx_micro_roadshow_video
+			WHERE
+				publish_status = 1 `
+	if condition != `` {
+		sql += condition
+	}
+	sql += ` ORDER BY publish_time DESC`
+
+	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
+	err = o.Raw(totalSql, pars).QueryRow(&total)
+	if err != nil {
+		return
+	}
+
+	sql += ` LIMIT ?,?`
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
+	return
+}

+ 18 - 0
routers/commentsRouter.go

@@ -547,6 +547,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:HomeController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:HomeController"],
+        beego.ControllerComments{
+            Method: "HeaderTab",
+            Router: `/header_tab`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:HomeController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:HomeController"],
         beego.ControllerComments{
             Method: "ListHome",
@@ -556,6 +565,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:MicroRoadShowController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:MicroRoadShowController"],
+        beego.ControllerComments{
+            Method: "List",
+            Router: `/list`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:ReportBillboardController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:ReportBillboardController"],
         beego.ControllerComments{
             Method: "FllowList",

+ 125 - 23
services/micro_roadshow.go

@@ -2,26 +2,36 @@ package services
 
 import (
 	"errors"
+	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/utils"
+	"math"
+	"strconv"
+	"strings"
 	"sync"
 )
 
-type MicroRoadShowPageList struct {
-	Title          string `description:"标题"`
-	ResourceUrl    string `description:"链接"`
-	Type           int    `description:"类型: 1-音频; 2-视频"`
-	PublishTime    string `description:"发布时间"`
-	BackgroundImg  string `description:"背景图"`
-	PermissionName string `description:"行业名称"`
-	PlaySeconds    string `description:"音视频时长"`
-}
-
 // GetMicroRoadShowPageList 获取微路演列表
-func GetMicroRoadShowPageList(keywords string) (err error) {
-	// 根据
-
+func GetMicroRoadShowPageList(pageSize, currentIndex, audioId, videoId int, keywords string) (respList []*models.MicroRoadShowPageList, total int, err error) {
 	var e error
-	//audioList := make([]*MicroRoadShowPageList, 0)
-	//videoList := make([]*MicroRoadShowPageList, 0)
+	// 根据每页数据量获取音视频配比
+	audioRatio, videoRatio, audioPageNum, videoPageNum, sliceNum, e := getMicroRoadShowDataRatio(pageSize)
+	if e != nil {
+		err = errors.New("获取微路演列表数据音视频配比失败, Err: " + e.Error())
+		return
+	}
+
+	audioTotal := 0
+	videoTotal := 0
+	audioList := make([]*models.MicroRoadShowPageList, 0)
+	videoList := make([]*models.MicroRoadShowPageList, 0)
+	if keywords != "" {
+		keywords = "%" + keywords + "%"
+	}
+	// 查询指定音频/视频时, 调整比例为1方便后面组合数据
+	if audioId > 0 || videoId > 0 {
+		audioRatio = 1
+		videoRatio = 1
+	}
 
 	wg := sync.WaitGroup{}
 
@@ -30,15 +40,49 @@ func GetMicroRoadShowPageList(keywords string) (err error) {
 	// 分页查询音频
 	go func() {
 		defer wg.Done()
-		// TODO: 查询音频
-
+		// 如果筛选条件为指定视频ID则不做音频查询
+		if videoId > 0 {
+			return
+		}
+		var audioCond string
+		var audioPars []interface{}
+		if keywords != "" {
+			audioCond += ` AND a.voice_name LIKE ? OR b.label LIKE ?`
+			audioPars = append(audioPars, keywords, keywords)
+		}
+		if audioId > 0 {
+			audioCond += ` AND a.activity_voice_id = ?`
+			audioPars = append(audioPars, audioId)
+		}
+		audioStartSize := 0
+		if currentIndex > 1 {
+			audioStartSize = (currentIndex - 1) * audioPageNum
+		}
+		audioTotal, audioList, e = models.GetMicroRoadShowAudioPageList(audioStartSize, audioPageNum, audioCond, audioPars)
 	}()
 
 	// 分页查询视频
 	go func() {
 		defer wg.Done()
-		// TODO: 查询视频
-
+		// 如果筛选条件为指定音频ID则不做视频查询
+		if audioId > 0 {
+			return
+		}
+		var videoCond string
+		var videoPars []interface{}
+		if keywords != "" {
+			videoCond += ` AND video_name LIKE ?`
+			videoPars = append(videoPars, keywords)
+		}
+		if videoId > 0 {
+			videoCond += ` AND video_id = ?`
+			videoPars = append(videoPars, videoId)
+		}
+		videoStartSize := 0
+		if currentIndex > 1 {
+			videoStartSize = (currentIndex - 1) * videoPageNum
+		}
+		videoTotal, videoList, e = models.GetMicroRoadShowVideoPageList(videoStartSize, audioPageNum, videoCond, videoPars)
 	}()
 
 	wg.Wait()
@@ -49,14 +93,72 @@ func GetMicroRoadShowPageList(keywords string) (err error) {
 	}
 
 	// 按比例组合列表
-
+	audioLen := len(audioList)
+	videoLen := len(videoList)
+	for i := 0; i < sliceNum; i++ {
+		// 音频
+		a := i * audioRatio		// 0 4 8 12
+		oa := a + audioRatio	// 4 8 12 16
+		if oa <= audioLen {
+			for k1 := a; k1 < oa; k1++ {
+				respList = append(respList, audioList[k1])
+			}
+		}
+		// 视频
+		b := i * videoRatio		// 0 1 2 3
+		ob := b + videoRatio	// 1 2 3 4
+		if ob <= videoLen {
+			for k2 := b; k2 < ob; k2++ {
+				respList = append(respList, videoList[k2])
+			}
+		}
+	}
+	total = audioTotal + videoTotal
 	return
 }
 
-func GetMicroRoadShowAudioPageList() {
+// getMicroRoadShowDataRatio 获取微路演列表数据音视频配比
+func getMicroRoadShowDataRatio(pageSize int) (audioRatio, videoRatio, audioPageNum, videoPageNum, sliceNum int, err error) {
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	key := models.MicroRoadShowListDataRatioConfigKey
+	config, e := models.GetConfigByCode(key)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		err = errors.New("获取微路演列表数据量配置失败, Err: " + e.Error())
+		return
+	}
 
-}
+	// 默认音频视频展示比例为4:1
+	ratio := "4:1"
+	if config != nil {
+		ratio = config.ConfigValue
+	}
+	ratioArr := strings.Split(ratio, ":")
+	if len(ratioArr) != 2 {
+		err = errors.New("微路演列表数据量配比有误")
+		return
+	}
+	audioRatio, e = strconv.Atoi(ratioArr[0])
+	if e != nil {
+		err = errors.New("微路演列表数据量配比有误")
+		return
+	}
+	videoRatio, e = strconv.Atoi(ratioArr[1])
+	if e != nil {
+		err = errors.New("微路演列表数据量配比有误")
+		return
+	}
+	totalRatio := audioRatio + videoRatio
+	if totalRatio == 0 {
+		err = errors.New("微路演列表数据量配比有误")
+		return
+	}
 
-func GetMicroRoadShowVideoPageList() {
+	// 每比率对应数量(向上取整)
+	sliceNum = int(math.Ceil(float64(pageSize) / float64(totalRatio)))
+	audioPageNum = audioRatio * sliceNum
+	videoPageNum = videoRatio * sliceNum
 
+	return
 }