@@ -0,0 +1,142 @@
|
||||
package noticefmtmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/pageopt"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// M M from Map
|
||||
type M = bson.M
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.NoticeFmt
|
||||
|
||||
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: "noticeCode", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "sender", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "receiver", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "noticeType", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func InsertOne(doc InsertDoc) error {
|
||||
sort.Sort(doc.UIDList)
|
||||
sort.Sort(doc.SpecifyNotice.SpecifyTimeList)
|
||||
docM, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "ToBsonM", err))
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
docM["updatedAt"] = now
|
||||
docM["createdAt"] = now
|
||||
if _, err = coll(nil).InsertOne(docM); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateOne(id ObjectID, doc UpdateDoc) error {
|
||||
docM, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateOne", table, "ToBsonM", err))
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
docM["updatedAt"] = now
|
||||
if _, err = coll(nil).UpsertOne(M{"_id": id}, M{"$set": docM}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateOne", table, "UpdateOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteOne(id ObjectID) error {
|
||||
filter := M{
|
||||
"_id": id,
|
||||
}
|
||||
if _, err := coll(nil).DeleteOne(filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOne", table, "DeleteOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllUserNotice(matchs ...Matcher) ([]NoticeFmt, error) {
|
||||
receiver := AllUser
|
||||
matchs = append(matchs, (&ReceiverMatch{&receiver}).New())
|
||||
filter := pageopt.MergeM(matchs)
|
||||
var list []NoticeFmt
|
||||
return list, coll(nil).Find(&list, filter)
|
||||
}
|
||||
|
||||
func GetSomeUserNotice(uid uint64, matchs ...Matcher) ([]NoticeFmt, error) {
|
||||
receiver := SomeUser
|
||||
matchs = append(matchs,
|
||||
(&ReceiverMatch{&receiver}).New(),
|
||||
(&UIDListMatch{&uid}).New(),
|
||||
)
|
||||
filter := pageopt.MergeM(matchs)
|
||||
var list []NoticeFmt
|
||||
return list, coll(nil).Find(&list, filter)
|
||||
}
|
||||
|
||||
func GetNoticeFmtListByUID(uid uint64, matchs ...Matcher) ([]NoticeFmt, error) {
|
||||
someUserNotice, err := GetSomeUserNotice(uid, matchs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allUserNotice, err := GetAllUserNotice(matchs...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append(someUserNotice, allUserNotice...), nil
|
||||
}
|
||||
|
||||
func DeleteBeforeCreatedAt(t *db.MongoTool, tm time.Time) error {
|
||||
_, err := coll(t).DeleteMany(bson.M{"createdAt": bson.M{"$lt": tm}})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user