143 lines
5.1 KiB
Go
143 lines
5.1 KiB
Go
package srv_homeconfig
|
|
|
|
import (
|
|
"context"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/homeconfigmod"
|
|
"videoweb/pub/models/playletthememod"
|
|
"videoweb/web/api/proto"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// validateHomeConfigTopics 校验题材配置、去重,并阻止启用模块重复占用题材
|
|
func validateHomeConfigTopics(ctx context.Context, config *homeconfigmod.HomeConfig) *srvdef.SrvErr {
|
|
if config.Style != "" && (len(config.Style) != 1 || config.Style[0] < 'A' || config.Style[0] > 'H') {
|
|
return srvdef.New(srvdef.ErrParamError, "样式仅支持 A-H", "")
|
|
}
|
|
if config.CategoryId < 0 || config.RecommendType < 0 || config.RecommendType > 5 {
|
|
return srvdef.New(srvdef.ErrParamError, "推荐配置无效", "")
|
|
}
|
|
if config.RecommendType == 5 && config.CategoryId == 0 {
|
|
return srvdef.New(srvdef.ErrParamError, "按分类推荐时必须填写分类ID", "")
|
|
}
|
|
if config.Type != homeconfigmod.TypeTheme {
|
|
if len(config.TopicIds) != 0 {
|
|
return srvdef.New(srvdef.ErrParamError, "短剧模块不能配置题材ID", "")
|
|
}
|
|
return nil
|
|
}
|
|
seen := map[int64]bool{}
|
|
ids := make([]int64, 0, len(config.TopicIds))
|
|
for _, id := range config.TopicIds {
|
|
if id <= 0 {
|
|
return srvdef.New(srvdef.ErrParamError, "题材ID无效", "")
|
|
}
|
|
if !seen[id] {
|
|
ids = append(ids, id)
|
|
seen[id] = true
|
|
}
|
|
}
|
|
config.TopicIds = ids
|
|
if !config.Status {
|
|
return nil
|
|
}
|
|
if len(ids) == 0 {
|
|
if config.Id == 0 {
|
|
return srvdef.New(srvdef.ErrParamError, "启用的题材模块必须配置题材ID", "")
|
|
}
|
|
return nil // 历史题材配置沿用自动选题材
|
|
}
|
|
themes, err := playletthememod.FindManyPlayletTheme(ctx, bson.M{playletthememod.FN.PlayletTheme.Id: bson.M{"$in": ids}, playletthememod.FN.PlayletTheme.Status: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(themes) != len(ids) {
|
|
return srvdef.New(srvdef.ErrParamError, "题材不存在或未上架", "")
|
|
}
|
|
others, err := homeconfigmod.FindManyHomeConfig(ctx, bson.M{homeconfigmod.FN.HomeConfig.Status: true, homeconfigmod.FN.HomeConfig.Type: homeconfigmod.TypeTheme, homeconfigmod.FN.HomeConfig.TopicIds: bson.M{"$in": ids}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, other := range others {
|
|
if other != nil && other.Id != config.Id {
|
|
return srvdef.New(srvdef.ErrParamError, "题材已被其他启用模块使用", "")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListHomeTopicOptions 分页查询可选的已上架短剧题材,并回显当前配置已选题材
|
|
func ListHomeTopicOptions(ctx context.Context, req *proto.HomeTopicOptionsReq) (*proto.HomeTopicOptionsResp, *srvdef.SrvErr) {
|
|
if req == nil || req.ConfigId < 0 {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "配置ID无效", "")
|
|
}
|
|
pageNum, pageSize := normalizeHomeConfigPage(req.PageNum, req.PageSize)
|
|
resp := &proto.HomeTopicOptionsResp{List: make([]*proto.HomeTopicOption, 0), SelectedList: make([]*proto.HomeTopicOption, 0), PageNum: pageNum, PageSize: pageSize}
|
|
var selected []int64
|
|
if req.ConfigId > 0 {
|
|
current, err := homeconfigmod.FindOneHomeConfig(ctx, bson.M{homeconfigmod.FN.HomeConfig.Id: req.ConfigId})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if current.Type != homeconfigmod.TypeTheme {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "当前配置不是题材模块", "")
|
|
}
|
|
selected = current.TopicIds
|
|
}
|
|
occupied := map[int64]bool{}
|
|
configs, err := homeconfigmod.FindManyHomeConfig(ctx, bson.M{homeconfigmod.FN.HomeConfig.Status: true, homeconfigmod.FN.HomeConfig.Type: homeconfigmod.TypeTheme})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, config := range configs {
|
|
if config != nil && config.Id != req.ConfigId {
|
|
for _, id := range config.TopicIds {
|
|
occupied[id] = true
|
|
}
|
|
}
|
|
}
|
|
filter := bson.M{playletthememod.FN.PlayletTheme.Status: true}
|
|
if len(occupied) > 0 {
|
|
ids := make([]int64, 0, len(occupied))
|
|
for id := range occupied {
|
|
ids = append(ids, id)
|
|
}
|
|
filter[playletthememod.FN.PlayletTheme.Id] = bson.M{"$nin": ids}
|
|
}
|
|
resp.TotalCount, err = playletthememod.CountPlayletTheme(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list, err := playletthememod.FindManyPlayletTheme(ctx, filter, options.Find().SetSort(bson.D{{Key: playletthememod.FN.PlayletTheme.Rank, Value: -1}, {Key: playletthememod.FN.PlayletTheme.Id, Value: -1}}).SetSkip((pageNum-1)*pageSize).SetLimit(pageSize))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range list {
|
|
if item != nil {
|
|
resp.List = append(resp.List, &proto.HomeTopicOption{Id: item.Id, Name: item.Name, Status: item.Status})
|
|
}
|
|
}
|
|
if len(selected) > 0 {
|
|
themes, err := playletthememod.FindManyPlayletTheme(ctx, bson.M{playletthememod.FN.PlayletTheme.Id: bson.M{"$in": selected}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byID := map[int64]*proto.HomeTopicOption{}
|
|
for _, item := range themes {
|
|
if item != nil {
|
|
byID[item.Id] = &proto.HomeTopicOption{Id: item.Id, Name: item.Name, Status: item.Status}
|
|
}
|
|
}
|
|
for _, id := range selected {
|
|
if item := byID[id]; item != nil {
|
|
resp.SelectedList = append(resp.SelectedList, item)
|
|
} else {
|
|
resp.SelectedList = append(resp.SelectedList, &proto.HomeTopicOption{Id: id})
|
|
}
|
|
}
|
|
}
|
|
return resp, nil
|
|
}
|