@@ -0,0 +1,165 @@
|
||||
package sensitivewordmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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.SensitiveWord
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "word", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "category", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Add 新增单条
|
||||
func Add(w *SensitiveWord) error {
|
||||
now := time.Now()
|
||||
w.CreatedAt = now
|
||||
w.UpdatedAt = now
|
||||
w.Status = StatusEnabled
|
||||
if _, err := coll(nil).InsertOne(w); err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] Add fail: %+v", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpsertByWord 按主词 upsert(导入时使用)
|
||||
func UpsertByWord(w *SensitiveWord) error {
|
||||
now := time.Now()
|
||||
filter := bson.M{"word": w.Word}
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"category": w.Category,
|
||||
"updatedAt": now,
|
||||
},
|
||||
"$setOnInsert": bson.M{
|
||||
"status": StatusEnabled,
|
||||
"createdAt": now,
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).UpsertOne(filter, update); err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] UpsertByWord fail: %+v", err), log.Any("word", w.Word))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 更新单条
|
||||
func Update(id primitive.ObjectID, fields bson.M) error {
|
||||
fields["updatedAt"] = time.Now()
|
||||
cond := bson.M{"_id": id}
|
||||
if _, err := coll(nil).UpdateOne(cond, bson.M{"$set": fields}); err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] Update fail: %+v", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 批量删除
|
||||
func Delete(ids []primitive.ObjectID) (int64, error) {
|
||||
cond := bson.M{"_id": bson.M{"$in": ids}}
|
||||
result, err := coll(nil).DeleteMany(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] Delete fail: %+v", err))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, nil
|
||||
}
|
||||
|
||||
// List 分页查询
|
||||
func List(req *ListReq) ([]*SensitiveWord, int64, error) {
|
||||
cond := buildCond(req)
|
||||
total, err := coll(nil).Count(cond)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
page := req.Page
|
||||
size := req.Size
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 20
|
||||
}
|
||||
skip := (page - 1) * size
|
||||
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}}).
|
||||
SetSkip(skip).
|
||||
SetLimit(size)
|
||||
|
||||
var list []*SensitiveWord
|
||||
if err = coll(nil).Find(&list, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] List find fail: %+v", err))
|
||||
return nil, total, err
|
||||
}
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
// FindAll 查询全部(导出时使用)
|
||||
func FindAll(req *ListReq) ([]*SensitiveWord, error) {
|
||||
cond := buildCond(req)
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "category", Value: 1}, {Key: "createdAt", Value: 1}})
|
||||
|
||||
var list []*SensitiveWord
|
||||
if err := coll(nil).Find(&list, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[sensitivewordmod] FindAll fail: %+v", err))
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func buildCond(req *ListReq) bson.M {
|
||||
cond := bson.M{}
|
||||
if req.Category != "" {
|
||||
cond["category"] = req.Category
|
||||
}
|
||||
if req.Status != nil {
|
||||
cond["status"] = *req.Status
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
cond["word"] = bson.M{"$regex": req.Keyword, "$options": "i"}
|
||||
}
|
||||
return cond
|
||||
}
|
||||
Reference in New Issue
Block a user