go-common/app/admin/main/tv/http/intervs.go
2019-04-22 18:49:16 +08:00

138 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package http
import (
"fmt"
"go-common/app/admin/main/tv/model"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
const (
_jsonErr = "Incorrect Json Format"
)
func intervsRank(c *bm.Context) {
var (
form = new(model.RankListReq)
request = new(model.IntervListReq)
)
if err := c.Bind(form); err != nil {
return
}
request.FromRank(form)
c.JSON(tvSrv.Intervs(request))
}
func intervsMod(c *bm.Context) {
var (
form = new(model.ModListReq)
request = new(model.IntervListReq)
)
if err := c.Bind(form); err != nil {
return
}
request.FromMod(form)
c.JSON(tvSrv.Intervs(request))
}
func intervsIndex(c *bm.Context) {
var (
form = new(model.IdxListReq)
request = new(model.IntervListReq)
)
if err := c.Bind(form); err != nil {
return
}
request.FromIndex(form)
c.JSON(tvSrv.Intervs(request))
}
func rankPub(c *bm.Context) {
var (
form = new(model.RankPubReq)
req = new(model.IntervPubReq)
)
if err := c.Bind(form); err != nil {
return
}
if err := req.FromRank(form); err != nil {
renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
return
}
intervPublish(c, req)
}
func indexPub(c *bm.Context) {
var (
form = new(model.IdxPubReq)
req = new(model.IntervPubReq)
)
if err := c.Bind(form); err != nil {
return
}
if err := req.FromIndex(form); err != nil {
renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
return
}
intervPublish(c, req)
}
func modPub(c *bm.Context) {
var (
form = new(model.ModPubReq)
req = new(model.IntervPubReq)
)
if err := c.Bind(form); err != nil {
return
}
if err := req.FromMod(form); err != nil {
renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
return
}
intervPublish(c, req)
}
// combine the alert msg for too many interventions and cut the slice
func alertMsg(items []*model.SimpleRank, nbLimit int) (msg string, restItems []*model.SimpleRank) {
var (
length = len(items)
)
if length <= nbLimit {
return "", items
}
msg = "以下内容因超量未发布干预:"
for i := nbLimit; i < length; i++ {
if i+1 == length {
msg = msg + fmt.Sprintf("id%d", items[i].ContID)
continue
}
msg = msg + fmt.Sprintf("id%d,", items[i].ContID)
}
return msg, items[:nbLimit]
}
func intervPublish(c *bm.Context, req *model.IntervPubReq) {
var (
err error
invalid *model.RankError
alertInfo string // used when too many interventions published
)
alertInfo, req.Items = alertMsg(req.Items, tvSrv.IntervLimit) // too many intervention treatment
invalid, err = tvSrv.RefreshIntervs(req)
if err != nil {
log.Error("RefreshIntervs Error %v", err)
c.JSON(nil, err)
return
}
if invalid != nil {
renderErrMsg(c, ecode.RequestErr.Code(), fmt.Sprintf("发布失败以下内容状态错误id%d", invalid.SeasonID))
return
}
if alertInfo != "" {
renderErrMsg(c, ecode.OK.Code(), alertInfo)
return
}
renderErrMsg(c, ecode.OK.Code(), "发布成功")
}