init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
package playletser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"videoweb/app/api/proto"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/playletcache"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/homeconfigmod"
|
||||
"videoweb/pub/models/playletmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func homeConfigToModule(item *proto.HomeConfigData) *proto.HomeModuleData {
|
||||
moduleType := int32(1)
|
||||
if item.Type == homeconfigmod.TypeTheme {
|
||||
moduleType = 2
|
||||
}
|
||||
return &proto.HomeModuleData{Id: item.Id, Slug: item.Slug, Title: item.Name, Desc: item.Desc, Keyword: item.Keyword, Label: item.Label,
|
||||
Style: item.Style, CardBackgroundImage: item.CardBackgroundImage, ModuleType: moduleType, DataType: 1,
|
||||
RecommendType: item.RecommendType, CategoryId: item.CategoryId, Rank: item.Rank, Limit: item.Limit, SeoInfo: item.SeoInfo,
|
||||
List: item.List, TopicList: item.ThemeList}
|
||||
}
|
||||
|
||||
// recommendType=0 保留历史 sortType 的含义
|
||||
func homeConfigPlayletQueryForConfig(config *homeconfigmod.HomeConfig) (bson.M, bson.D, int64) {
|
||||
categoryID := config.CategoryId
|
||||
if categoryID == 0 && config.RecommendType == 0 && config.Type == homeconfigmod.TypePlaylet {
|
||||
categoryID = homeConfigCategoryID(config.SortType, config.Id)
|
||||
}
|
||||
if config.RecommendType == 0 {
|
||||
filter, sort := homeConfigPlayletQuery(config.SortType, categoryID)
|
||||
return filter, sort, categoryID
|
||||
}
|
||||
filter := homeUpPlayletFilter(nil)
|
||||
field := playletmod.FN.Playlet.UpdatedAt
|
||||
switch config.RecommendType {
|
||||
case 2:
|
||||
field = playletmod.FN.Playlet.CountLike
|
||||
case 3:
|
||||
field = playletmod.FN.Playlet.CountBrowse
|
||||
case 4:
|
||||
field = playletmod.FN.Playlet.HeatValue
|
||||
case 5:
|
||||
if categoryID > 0 {
|
||||
filter[playletmod.FN.Playlet.CategoryIds] = categoryID
|
||||
}
|
||||
field = playletmod.FN.Playlet.RecommendRank
|
||||
}
|
||||
return filter, bson.D{{Key: field, Value: -1}, {Key: playletmod.FN.Playlet.Id, Value: -1}}, categoryID
|
||||
}
|
||||
|
||||
func findHomeConfigPlayletListForConfig(ctx context.Context, config *homeconfigmod.HomeConfig, limit int64) (playletmod.PlayletList, *srvdef.SrvErr) {
|
||||
if config.RecommendType == 0 {
|
||||
_, _, categoryID := homeConfigPlayletQueryForConfig(config)
|
||||
return findHomeConfigPlayletList(ctx, config.SortType, categoryID, limit)
|
||||
}
|
||||
filter, sort, categoryID := homeConfigPlayletQueryForConfig(config)
|
||||
if config.RecommendType == 5 {
|
||||
if categoryID <= 0 {
|
||||
return make(playletmod.PlayletList, 0), nil
|
||||
}
|
||||
return playletmod.FindManyPlayletByCategoryRank(ctx, filter, categoryID, 0, limit, playletmod.CategoryRankSort(), playletHomeProjection)
|
||||
}
|
||||
return findHomePlaylets(ctx, filter, sort, limit)
|
||||
}
|
||||
|
||||
func findHomeConfigThemePlayletsForConfig(ctx context.Context, config *homeconfigmod.HomeConfig, skip, limit int64) ([]homeConfigThemePlaylets, playletmod.PlayletList, *srvdef.SrvErr) {
|
||||
if len(config.TopicIds) == 0 {
|
||||
if skip == 0 {
|
||||
return findHomeConfigThemePlaylets(ctx, limit)
|
||||
}
|
||||
groups, _, all, err := findHomeConfigThemePlayletsPage(ctx, skip, limit)
|
||||
return groups, all, err
|
||||
}
|
||||
selected := map[int64]bool{}
|
||||
for _, id := range config.TopicIds {
|
||||
selected[id] = true
|
||||
}
|
||||
result := make([]homeConfigThemePlaylets, 0)
|
||||
all := make(playletmod.PlayletList, 0)
|
||||
var offset int64
|
||||
for _, theme := range playletcache.ListEnabledThemesByPlayletCount(0) {
|
||||
if theme == nil || !selected[theme.Id] {
|
||||
continue
|
||||
}
|
||||
filter := topicDetailPlayletFilter(theme.Id)
|
||||
count, err := playletmod.CountPlaylet(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
continue
|
||||
}
|
||||
if offset < skip {
|
||||
offset++
|
||||
continue
|
||||
}
|
||||
if int64(len(result)) >= limit {
|
||||
break
|
||||
}
|
||||
list, err := findHomePlaylets(ctx, filter, playletTopicDetailSort(topicDetailSortEdit), topicListPlayletLimit)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
result = append(result, homeConfigThemePlaylets{Theme: theme, Playlets: list, TotalCount: count})
|
||||
all = append(all, list...)
|
||||
offset++
|
||||
}
|
||||
return result, all, nil
|
||||
}
|
||||
|
||||
func countHomeConfigThemes(ctx context.Context, config *homeconfigmod.HomeConfig) (int64, *srvdef.SrvErr) {
|
||||
if len(config.TopicIds) == 0 {
|
||||
_, count, _, err := findHomeConfigThemePlayletsPage(ctx, 0, 0)
|
||||
return count, err
|
||||
}
|
||||
selected := map[int64]bool{}
|
||||
for _, id := range config.TopicIds {
|
||||
selected[id] = true
|
||||
}
|
||||
var count int64
|
||||
for _, theme := range playletcache.ListEnabledThemesByPlayletCount(0) {
|
||||
if theme == nil || !selected[theme.Id] {
|
||||
continue
|
||||
}
|
||||
filter := topicDetailPlayletFilter(theme.Id)
|
||||
total, err := playletmod.CountPlaylet(ctx, filter)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if total > 0 {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func HomeConfigList(ctx context.Context, req *proto.PlayletHomeConfigListReq) (*proto.PlayletHomeConfigListResp, *srvdef.SrvErr) {
|
||||
if req == nil || (req.Id <= 0 && strings.TrimSpace(req.Slug) == "") {
|
||||
return nil, srvdef.New(srvdef.ErrParamError, "首页配置ID或slug不能为空", "")
|
||||
}
|
||||
filter := bson.M{}
|
||||
if slug := strings.TrimSpace(req.Slug); slug != "" {
|
||||
filter[homeconfigmod.FN.HomeConfig.Slug] = slug
|
||||
} else {
|
||||
filter[homeconfigmod.FN.HomeConfig.Id] = req.Id
|
||||
}
|
||||
config, err := homeconfigmod.FindOneHomeConfig(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.Type != homeconfigmod.TypePlaylet && config.Type != homeconfigmod.TypeTheme {
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "首页配置不存在", "")
|
||||
}
|
||||
pageNum, pageSize := req.PageNum, req.PageSize
|
||||
if pageNum <= 0 {
|
||||
pageNum = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > maxPageSize {
|
||||
pageSize = maxPageSize
|
||||
}
|
||||
module := &proto.HomeModuleData{Id: config.Id, Slug: config.Slug, Title: config.Name, Desc: config.Desc, Keyword: homeconfigmod.NormalizeKeywords(config.Keyword), Label: config.Label,
|
||||
Style: config.Style, CardBackgroundImage: withImageDomain(ctx, config.CardBackgroundImage), DataType: 1,
|
||||
RecommendType: config.RecommendType, CategoryId: config.CategoryId, Rank: config.Rank, Limit: homeConfigDisplayLimit(config), SeoInfo: config.SeoInfo,
|
||||
List: make([]*proto.CommonData, 0), TopicList: make([]*proto.HomeConfigThemeData, 0)}
|
||||
if config.Type == homeconfigmod.TypeTheme {
|
||||
module.ModuleType = 2
|
||||
} else {
|
||||
module.ModuleType = 1
|
||||
}
|
||||
resp := &proto.PlayletHomeConfigListResp{Config: module, PageNum: int(pageNum), PageSize: int(pageSize),
|
||||
List: make([]*proto.CommonData, 0), TopicList: make([]*proto.HomeConfigThemeData, 0), Advitise: configser.GetAdvitise()}
|
||||
if !config.Status {
|
||||
return resp, nil
|
||||
}
|
||||
skip := (pageNum - 1) * pageSize
|
||||
if config.Type == homeconfigmod.TypePlaylet {
|
||||
playletFilter, sort, categoryID := homeConfigPlayletQueryForConfig(config)
|
||||
if (config.RecommendType == 5 || (config.RecommendType == 0 && config.SortType == homeconfigmod.SortTypeCategory)) && categoryID <= 0 {
|
||||
return resp, nil
|
||||
}
|
||||
resp.TotalCount, err = playletmod.CountPlaylet(ctx, playletFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list playletmod.PlayletList
|
||||
if config.RecommendType == 5 || (config.RecommendType == 0 && config.SortType == homeconfigmod.SortTypeCategory) {
|
||||
list, err = playletmod.FindManyPlayletByCategoryRank(ctx, playletFilter, categoryID, skip, pageSize, playletmod.CategoryRankSort(), playletHomeProjection)
|
||||
} else {
|
||||
list, err = findHomePlaylets(ctx, playletFilter, sort, pageSize, options.Find().SetSkip(skip))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
themeNames, err := buildHomeThemeNameMap(ctx, list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tagNames, err := buildHomeTagNameMap(ctx, list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.List = playletsToHomeCommonData(ctx, list, themeNames, tagNames)
|
||||
return resp, nil
|
||||
}
|
||||
resp.TotalCount, err = countHomeConfigThemes(ctx, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups, all, err := findHomeConfigThemePlayletsForConfig(ctx, config, skip, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
themeNames, err := buildHomeThemeNameMap(ctx, all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tagNames, err := buildHomeTagNameMap(ctx, all)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.TopicList = homeConfigThemesToHomeData(ctx, groups, themeNames, tagNames)
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user