@@ -0,0 +1,148 @@
|
||||
package locmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// M M from Map
|
||||
type M = bson.M
|
||||
|
||||
// ObjectID ObjectID
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.HotCity
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndexHot 设置index
|
||||
func initIndexHot() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "city", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
// GetHotCity 获取热门城市
|
||||
func GetHotCity() ([]HotCity, error) {
|
||||
var back []HotCity
|
||||
if err := coll(nil).Find(&back, bson.M{"enable": true}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetHotCity", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(doc HotCityDoc) error {
|
||||
now := time.Now()
|
||||
doc.CreatedAt = &now
|
||||
doc.UpdatedAt = &now
|
||||
if _, err := coll(nil).InsertOne(doc); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateTo UpdateTo
|
||||
func UpdateTo(id ObjectID, doc HotCityDoc) error {
|
||||
now := time.Now()
|
||||
doc.UpdatedAt = &now
|
||||
update, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "ToBsonM", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return err
|
||||
}
|
||||
result, err := coll(nil).UpdateOne(M{"_id": id}, bson.M{"$set": update})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount+result.UpsertedCount != 1 {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
log.Any("mCount", result.ModifiedCount),
|
||||
log.Any("uCount", result.UpsertedCount),
|
||||
)
|
||||
return fmt.Errorf("wrong update count")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteMany DeleteMany
|
||||
func DeleteMany(idArray []ObjectID) error {
|
||||
if idArray == nil {
|
||||
idArray = []ObjectID{}
|
||||
}
|
||||
if _, err := coll(nil).DeleteMany(M{"_id": M{"$in": idArray}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err), log.Any("idArray", idArray))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HotCityPages 热门城市
|
||||
func HotCityPages(sort bson.D, skip int64, limit int64, matchs ...pageopt.Matcher) (HotCityPage, error) {
|
||||
pipeline := []bson.M{
|
||||
{"$match": pageopt.MergeM(matchs)},
|
||||
{
|
||||
"$facet": bson.M{
|
||||
"total": bson.A{bson.M{"$count": "total"}},
|
||||
"list": bson.A{
|
||||
bson.M{"$sort": sort},
|
||||
bson.M{"$skip": skip},
|
||||
bson.M{"$limit": limit},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$project": bson.M{
|
||||
"total": bson.M{"$arrayElemAt": bson.A{"$total.total", 0}},
|
||||
"list": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
var page HotCityPage
|
||||
if err := coll(nil).AggregateDecode(&page, pipeline); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "HotCityPages", table, "AggregateDecode", err),
|
||||
log.Any("sort", sort),
|
||||
log.Any("matchs", matchs),
|
||||
)
|
||||
return page, err
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
Reference in New Issue
Block a user