135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package navigationser
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"videoweb/app/api/proto"
|
|
"videoweb/app/service/configser"
|
|
"videoweb/pub/common/netutil"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/translationcategorymod"
|
|
"videoweb/pub/models/translationmod"
|
|
"videoweb/pub/pubdef"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const (
|
|
headerNavigationCategoryLimit int64 = 30
|
|
headerNavigationInfoLimit int64 = 50
|
|
)
|
|
|
|
var (
|
|
headerNavigationCategoryProjection = bson.D{
|
|
{Key: translationcategorymod.FN.TranslationCategory.Id, Value: 1},
|
|
{Key: translationcategorymod.FN.TranslationCategory.Name, Value: 1},
|
|
{Key: translationcategorymod.FN.TranslationCategory.Icon, Value: 1},
|
|
{Key: translationcategorymod.FN.TranslationCategory.CategoryType, Value: 1},
|
|
{Key: translationcategorymod.FN.TranslationCategory.UrlValue, Value: 1},
|
|
}
|
|
headerNavigationInfoProjection = bson.D{
|
|
{Key: translationmod.FN.TranslationInfo.Id, Value: 1},
|
|
{Key: translationmod.FN.TranslationInfo.Name, Value: 1},
|
|
{Key: translationmod.FN.TranslationInfo.TransCategoryId, Value: 1},
|
|
{Key: translationmod.FN.TranslationInfo.IsFriendAds, Value: 1},
|
|
{Key: translationmod.FN.TranslationInfo.Link, Value: 1},
|
|
}
|
|
)
|
|
|
|
// HeaderNavigation 查询头部导航配置
|
|
func HeaderNavigation(ctx context.Context) (proto.AppTranslationInfo, *srvdef.SrvErr) {
|
|
categoryList, err := translationcategorymod.FindManyTranslationCategorys(ctx, bson.M{
|
|
translationcategorymod.FN.TranslationCategory.Status: true,
|
|
translationcategorymod.FN.TranslationCategory.CategoryType: bson.M{
|
|
"$in": []pubdef.TranslationType{
|
|
pubdef.TranslationTypeUrl,
|
|
pubdef.TranslationTypeSub,
|
|
},
|
|
},
|
|
}, options.Find().
|
|
SetProjection(headerNavigationCategoryProjection).
|
|
SetSort(bson.D{
|
|
{Key: translationcategorymod.FN.TranslationCategory.Rank, Value: -1},
|
|
{Key: translationcategorymod.FN.TranslationCategory.Id, Value: -1},
|
|
}).
|
|
SetLimit(headerNavigationCategoryLimit))
|
|
if err != nil {
|
|
return proto.AppTranslationInfo{}, err
|
|
}
|
|
resp := proto.AppTranslationInfo{CategoryList: make([]proto.TranslationCategory, 0, len(categoryList))}
|
|
if len(categoryList) == 0 {
|
|
return resp, nil
|
|
}
|
|
categoryIds := make([]int64, 0, len(categoryList))
|
|
for _, item := range categoryList {
|
|
if item != nil && item.Id > 0 {
|
|
categoryIds = append(categoryIds, item.Id)
|
|
}
|
|
}
|
|
translationListByCategory, err := queryHeaderTranslationList(ctx, categoryIds)
|
|
if err != nil {
|
|
return proto.AppTranslationInfo{}, err
|
|
}
|
|
imgCdn := configser.ImageCdnFromCtx(ctx)
|
|
for _, item := range categoryList {
|
|
if item == nil {
|
|
continue
|
|
}
|
|
resp.CategoryList = append(resp.CategoryList, proto.TranslationCategory{
|
|
ID: item.Id,
|
|
Name: item.Name,
|
|
Icon: netutil.MergeURL(imgCdn, item.Icon),
|
|
UrlValue: item.UrlValue,
|
|
CategoryType: item.CategoryType,
|
|
TranslationList: translationListByCategory[item.Id],
|
|
})
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// queryHeaderTranslationList 批量查询头部导航二级配置并按分类ID分组
|
|
func queryHeaderTranslationList(ctx context.Context, categoryIds []int64) (map[int64][]proto.TranslationInfo, *srvdef.SrvErr) {
|
|
result := make(map[int64][]proto.TranslationInfo, len(categoryIds))
|
|
if len(categoryIds) == 0 {
|
|
return result, nil
|
|
}
|
|
list, err := translationmod.FindManyTranslationInfos(ctx, bson.M{
|
|
translationmod.FN.TranslationInfo.Status: true,
|
|
translationmod.FN.TranslationInfo.TransCategoryId: bson.M{"$in": categoryIds},
|
|
}, options.Find().
|
|
SetProjection(headerNavigationInfoProjection).
|
|
SetSort(bson.D{
|
|
{Key: translationmod.FN.TranslationInfo.Rank, Value: -1},
|
|
{Key: translationmod.FN.TranslationInfo.Id, Value: -1},
|
|
}).
|
|
SetLimit(headerNavigationInfoLimit))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range list {
|
|
if item == nil || item.IsFriendAds {
|
|
continue
|
|
}
|
|
info := proto.TranslationInfo{
|
|
ID: item.Id,
|
|
Name: item.Name,
|
|
TransCategoryId: item.TransCategoryId,
|
|
UrlValue: headerTranslationURL(item),
|
|
}
|
|
if info.UrlValue == "" {
|
|
continue
|
|
}
|
|
result[item.TransCategoryId] = append(result[item.TransCategoryId], info)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// headerTranslationURL 解析导航明细跳转地址
|
|
func headerTranslationURL(item *translationmod.TranslationInfo) string {
|
|
if item == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(item.Link)
|
|
}
|