@@ -0,0 +1,109 @@
|
||||
package ipblockmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.IpBlock
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
Options: options.Index(),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "ip", 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 Insert(l *IPBlock) (err error) {
|
||||
l.UpdatedAt = time.Now()
|
||||
l.CreatedAt = time.Now()
|
||||
if _, err = coll(nil).InsertOne(l); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 删除
|
||||
func DeleteOne(objID primitive.ObjectID) (err error) {
|
||||
if _, err = coll(nil).DeleteOne(bson.M{"_id": objID}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOne", table, "DeleteOne", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteMany(objID []primitive.ObjectID) (err error) {
|
||||
if _, err = coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": objID}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err), log.Any("objID", objID))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Update 修改
|
||||
func Update(id primitive.ObjectID, set IPBlockEdit) (err error) {
|
||||
set.UpdatedAt = time.Now()
|
||||
update, _ := common.ToBsonM(&set)
|
||||
if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": update}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("set", set),
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FindOneByType 根据Type查询ip信息
|
||||
func FindOneByType(blockType string) (data IPBlock, err error) {
|
||||
if err = coll(nil).FindOne(&data, bson.M{"type": blockType}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneByType", table, "FindOne", err), log.Any("blockType", blockType))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AggregateForIPArray(blockType string) (webAggre WebAggregateResp, err error) {
|
||||
if err = coll(nil).AggregateDecode(&webAggre, []bson.M{
|
||||
{
|
||||
"$match": bson.M{"type": blockType},
|
||||
},
|
||||
{
|
||||
"$group": bson.M{"_id": bson.M{"blockType": "$type"}, "ips": bson.M{"$addToSet": "$ip"}},
|
||||
}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AggregateForIPArray", table, "FindOne", err), log.Any("blockType", blockType))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user