@@ -0,0 +1,136 @@
|
||||
package homeconfigmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var coll *mongos.Collection
|
||||
|
||||
// Init 初始化 home_config 集合和常用查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.HomeConfig)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HomeConfig.Type, Value: 1},
|
||||
{Key: FN.HomeConfig.SortType, Value: 1},
|
||||
{Key: FN.HomeConfig.Status, Value: 1},
|
||||
{Key: FN.HomeConfig.Rank, Value: -1},
|
||||
{Key: FN.HomeConfig.UpdatedAt, Value: -1},
|
||||
{Key: FN.HomeConfig.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HomeConfig.Name, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HomeConfig.Slug, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HomeConfig.CreatedAt, Value: -1},
|
||||
{Key: FN.HomeConfig.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.HomeConfig.Status, Value: 1},
|
||||
{Key: FN.HomeConfig.Rank, Value: -1},
|
||||
{Key: FN.HomeConfig.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(homeconfigmod:initIndex) Create Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneHomeConfig 插入一条首页配置数据
|
||||
func InsertOneHomeConfig(ctx context.Context, doc *HomeConfig) *srvdef.SrvErr {
|
||||
if doc.Id == 0 {
|
||||
doc.Id = mongos.Lid.HomeConfig.Get()
|
||||
}
|
||||
now := time.Now()
|
||||
if doc.CreatedAt.IsZero() {
|
||||
doc.CreatedAt = now
|
||||
}
|
||||
doc.UpdatedAt = now
|
||||
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
||||
log.Warn("homeconfigmod:InsertOneHomeConfig failed", log.Any("doc", doc), log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOneHomeConfig 删除一条首页配置数据
|
||||
func DelOneHomeConfig(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("homeconfigmod:DelOneHomeConfig failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindManyHomeConfig 按条件查询多条首页配置数据
|
||||
func FindManyHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (HomeConfigList, *srvdef.SrvErr) {
|
||||
data := make(HomeConfigList, 0)
|
||||
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("homeconfigmod:FindManyHomeConfig failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountHomeConfig 统计满足条件的首页配置数量
|
||||
func CountHomeConfig(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
log.Warn("homeconfigmod:CountHomeConfig failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOneHomeConfig 查询一条首页配置
|
||||
func FindOneHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (*HomeConfig, *srvdef.SrvErr) {
|
||||
var doc HomeConfig
|
||||
if err := coll.FindOneEx(ctx, &doc, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("homeconfigmod:FindOneHomeConfig failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// UpdateOneHomeConfig 更新一条首页配置数据
|
||||
func UpdateOneHomeConfig(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := coll.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("homeconfigmod:UpdateOneHomeConfig failed", log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package homeconfigmod
|
||||
|
||||
// TagHomeConfig 保存 home_config 集合字段名
|
||||
type TagHomeConfig struct {
|
||||
Id string
|
||||
Name string
|
||||
Slug string
|
||||
Desc string
|
||||
Keyword string
|
||||
Label string
|
||||
Style string
|
||||
CardBackgroundImage string
|
||||
TopicIds string
|
||||
CategoryId string
|
||||
RecommendType string
|
||||
Type string
|
||||
Rank string
|
||||
Limit string
|
||||
SortType string
|
||||
Status string
|
||||
SeoInfo struct {
|
||||
Description string
|
||||
Keyworld string
|
||||
Title string
|
||||
}
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
HomeConfig TagHomeConfig
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
HomeConfig string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.HomeConfig.Id = "_id"
|
||||
FN.HomeConfig.Name = "name"
|
||||
FN.HomeConfig.Slug = "slug"
|
||||
FN.HomeConfig.Desc = "desc"
|
||||
FN.HomeConfig.Keyword = "keyword"
|
||||
FN.HomeConfig.Label = "label"
|
||||
FN.HomeConfig.Style = "style"
|
||||
FN.HomeConfig.CardBackgroundImage = "cardBackgroundImage"
|
||||
FN.HomeConfig.TopicIds = "topicIds"
|
||||
FN.HomeConfig.CategoryId = "categoryId"
|
||||
FN.HomeConfig.RecommendType = "recommendType"
|
||||
FN.HomeConfig.Type = "type"
|
||||
FN.HomeConfig.Rank = "rank"
|
||||
FN.HomeConfig.Limit = "limit"
|
||||
FN.HomeConfig.SortType = "sortType"
|
||||
FN.HomeConfig.Status = "status"
|
||||
FN.HomeConfig.SeoInfo.Description = "seoInfo.webDescription"
|
||||
FN.HomeConfig.SeoInfo.Keyworld = "seoInfo.webKeyworld"
|
||||
FN.HomeConfig.SeoInfo.Title = "seoInfo.webTitle"
|
||||
FN.HomeConfig.CreatedAt = "createdAt"
|
||||
FN.HomeConfig.UpdatedAt = "updatedAt"
|
||||
TN.HomeConfig = "home_config"
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package homeconfigmod
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
const (
|
||||
TypePlaylet int32 = 1 // 短剧
|
||||
TypeTheme int32 = 3 // 题材
|
||||
)
|
||||
|
||||
const (
|
||||
SortTypeRecommend int32 = 1 // 推荐
|
||||
SortTypeHot int32 = 2 // 热播
|
||||
SortTypeLatest int32 = 3 // 最近更新
|
||||
SortTypeOnlineAt int32 = 4 // 上架时间
|
||||
SortTypeCategory int32 = 5 // 分类
|
||||
)
|
||||
|
||||
// HomeConfig @table: home_config
|
||||
//
|
||||
// HomeConfig 首页配置集合模型
|
||||
type HomeConfig struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // ID
|
||||
Name string `json:"name" bson:"name"` // 名称
|
||||
Slug string `json:"slug" bson:"slug"` // slug,H5 更多页路径优先用它
|
||||
Desc string `json:"desc" bson:"desc"` // 描述
|
||||
Keyword []string `json:"keyword" bson:"keyword"` // 页面关键字
|
||||
Label string `json:"label" bson:"label"`
|
||||
Style string `json:"style" bson:"style"`
|
||||
CardBackgroundImage string `json:"cardBackgroundImage" bson:"cardBackgroundImage"`
|
||||
TopicIds []int64 `json:"topicIds" bson:"topicIds"`
|
||||
CategoryId int64 `json:"categoryId" bson:"categoryId"`
|
||||
RecommendType int64 `json:"recommendType" bson:"recommendType"` // 参考项目排序;0 沿用本项目 sortType
|
||||
Type int32 `json:"type" bson:"type"` // 首页配置类型 1-短剧 3-题材
|
||||
Rank int64 `json:"rank" bson:"rank"` // 排序,越大越靠前
|
||||
Limit int64 `json:"limit" bson:"limit"` // 首页展示数量,0 时短剧默认12条、题材默认8个
|
||||
SortType int32 `json:"sortType" bson:"sortType"` // 短剧模块排序 1-推荐 2-热播 3-最近更新 4-上架时间 5-分类,题材模块预览按专题详情默认排序
|
||||
Status bool `json:"status" bson:"status"` // 上下架状态 true-上架 false-下架
|
||||
SeoInfo pubdef.SeoValue `json:"seoInfo" bson:"seoInfo"` // Seo相关信息
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// HomeConfigList 首页配置列表
|
||||
type HomeConfigList []*HomeConfig
|
||||
|
||||
// NormalizeKeywords 去掉空白项和重复项,始终返回非 nil 数组
|
||||
func NormalizeKeywords(items []string) []string {
|
||||
out := make([]string, 0, len(items))
|
||||
seen := make(map[string]struct{}, len(items))
|
||||
for _, item := range items {
|
||||
item = strings.TrimSpace(item)
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[item]; ok {
|
||||
continue
|
||||
}
|
||||
seen[item] = struct{}{}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user