@@ -0,0 +1,56 @@
|
||||
package tonemod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/pageopt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Matcher = pageopt.Matcher
|
||||
|
||||
// EnableMatch
|
||||
type EnableMatch struct {
|
||||
Enable *bool
|
||||
}
|
||||
|
||||
func (i *EnableMatch) New() Matcher {
|
||||
return pageopt.NewAssignMatch("enable", i.Enable)
|
||||
}
|
||||
|
||||
// List
|
||||
func List(sort D, skip, limit int64, matchers ...Matcher) ([]Tone, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
opt := &options.FindOptions{}
|
||||
if len(sort) != 0 {
|
||||
opt.SetSort(sort)
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(limit)
|
||||
toneList := make([]Tone, 0, limit)
|
||||
if err := coll(nil).Find(&toneList, filter, opt); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err),
|
||||
log.Any("sort", sort),
|
||||
log.Any("skip", skip),
|
||||
log.Any("limit", limit),
|
||||
log.Any("matchers", matchers),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return toneList, nil
|
||||
}
|
||||
|
||||
// Count
|
||||
func Count(matchers ...Matcher) (int64, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Count", table, "Count", err),
|
||||
log.Any("matchers", matchers),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package tonemod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Tone
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 设置index
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "types", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "sortKey", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateTo
|
||||
func UpdateTo(id ObjectID, doc ToneDoc) error {
|
||||
now := time.Now()
|
||||
doc.UpdatedAt = &now
|
||||
update, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("coll:%s UpdateTo ToBsonM fail, error:%+v ", models.Tone, err)
|
||||
}
|
||||
if _, err := coll(nil).UpdateOne(M{"_id": id}, bson.M{"$set": update}); err != nil {
|
||||
return fmt.Errorf("coll:%s UpdateTo fail, error:%+v id:%+v update:%+v", models.Tone, err, id, update)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TonePages TonePages
|
||||
func TonePages(sort D, skip int64, limit int64) (page TonePage, err error) {
|
||||
list, err := List(sort, skip, limit)
|
||||
if err != nil {
|
||||
return TonePage{}, err
|
||||
}
|
||||
count, err := Count()
|
||||
if err != nil {
|
||||
return TonePage{}, err
|
||||
}
|
||||
return TonePage{
|
||||
int(count),
|
||||
list,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package tonemod
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ToneValue
|
||||
type ToneValue struct {
|
||||
OfficialRecomPlayCount int64 `json:"officialRecomPlayCount"`
|
||||
GoldCoinAreaPayCount int64 `josn:"goldCoinAreaPayCount"`
|
||||
LatestUploadPlayCount int64 `json:"latestUploadPlayCount"`
|
||||
MostLikesPlayCount int64 `json:"mostLikesPlayCount"`
|
||||
PlayAtMostPlayCount int64 `json:"playAtMostPlayCount"`
|
||||
MostCommentsPlayCount int64 `json:"mostCommentsPlayCount"`
|
||||
FreeAreaPlayCount int64 `json:"freeAreaPlayCount"`
|
||||
}
|
||||
|
||||
func (v ToneValue) MarshalBinary() ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (v *ToneValue) UnmarshalBinary(b []byte) error {
|
||||
return json.Unmarshal(b, v)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package tonemod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
// M M from Map
|
||||
type M = bson.M
|
||||
|
||||
// D D from Doc
|
||||
type D = bson.D
|
||||
|
||||
// A A from Array
|
||||
type A = []interface{}
|
||||
|
||||
type TonePage struct {
|
||||
Total int `json:"total" bson:"total"`
|
||||
List []Tone `json:"list" bson:"list"`
|
||||
}
|
||||
|
||||
// 音色最热模型asd
|
||||
type Tone struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"` //主题名
|
||||
Types string `json:"types" bson:"types"` //类型
|
||||
Cover string `json:"cover" bson:"cover"` //封面
|
||||
SortKey int `json:"sortKey" bson:"sortKey"` //排序
|
||||
Enable bool `json:"enable" bson:"enable"` //使能
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //刷新时间
|
||||
}
|
||||
|
||||
type ToneDoc struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
Name *string `json:"name" bson:"name,omitempty"` //主题名
|
||||
Types *string `json:"types" bson:"types,omitempty"` //类型
|
||||
Cover *string `json:"cover" bson:"cover,omitempty"` //封面
|
||||
SortKey *int `json:"sortKey" bson:"sortKey,omitempty"` //排序
|
||||
Enable *bool `json:"enable" bson:"enable,omitempty"` //使能
|
||||
CreatedAt *time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user