package adminmod import ( "fmt" "time" "91porn-server/common" "91porn-server/common/db" "91porn-server/common/log" "91porn-server/common/pageopt" "91porn-server/models" "91porn-server/models/v/authoritymod" "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.Admin 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: "name", Value: 1}}, Options: options.Index().SetUnique(true), }, { Keys: bson.D{{Key: "secret", Value: 1}}, Options: options.Index().SetUnique(true), }, { Keys: bson.D{{Key: "createdAt", Value: -1}}, }, { Keys: bson.D{{Key: "lastLoginAt", Value: -1}}, }, } if _, err := coll(nil).CreateIndex(many); err != nil { panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err)) } } func FindOneByName(name string) (doc Admin, err error) { return findOne(AdminDoc{ Name: &name, }) } func FindOneByID(id ObjectID) (doc Admin, err error) { return findOne(AdminDoc{ ID: &id, }) } func findOne(filter AdminDoc) (doc Admin, err error) { filterM, err := common.ToBsonM(filter) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findOne", table, "ToBsonM", err)) return } if err = coll(nil).FindOne(&doc, filterM); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "findOne", table, "FindOne", err)) return } return } func ExistsByID(id ObjectID) (existed bool, err error) { doc, err := FindOneByID(id) existed = !doc.ID.IsZero() return } func ExistsByName(name string) (existed bool, err error) { doc, err := FindOneByName(name) existed = !doc.ID.IsZero() return } // Insert 插入记录 func Insert(tool *db.MongoTool, doc AdminDoc) (err error) { now := time.Now() doc.CreatedAt = &now doc.UpdatedAt = &now if doc.Role != nil { authority, err := authoritymod.FindOne(authoritymod.AuthorityDoc{Role: doc.Role}) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "FindOne", err)) return err } if authority.ID.IsZero() { return fmt.Errorf("coll:%s update Role is not exist", table) } } if _, err = coll(tool).InsertOne(doc); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err)) return } return } func Bind(name string) error { hasBind := true return update(AdminDoc{Name: &name}, AdminDoc{HasBind: &hasBind}) } // UpdateTo UpdateTo func UpdateByID(id ObjectID, updat AdminDoc) error { return update(AdminDoc{ID: &id}, updat) } // UpdateTo UpdateTo func update(filter AdminDoc, update AdminDoc) error { filterM, err := common.ToBsonM(filter) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "update", table, "filter ToBsonM", err)) return err } now := time.Now() update.UpdatedAt = &now if update.Role != nil { authority, err := authoritymod.FindOne(authoritymod.AuthorityDoc{Role: update.Role}) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "update", table, "FindOne", err)) return err } if authority.ID.IsZero() { return fmt.Errorf("coll:%s update Role is not exist", table) } } updateM, err := common.ToBsonM(update) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "update", table, "update ToBsonM", err)) return err } if _, err := coll(nil).UpdateOne(filterM, bson.M{"$set": updateM}); err != nil { log.Error(fmt.Sprintf( "[METHOD-%s]==> Model %s %s fail error:%+v:", "update", table, "UpdateOne", err), log.Any("filter", filter), log.Any("update", update), ) return err } return nil } func UnsetPrivilege(id ObjectID) error { filter := M{"_id": id} update := M{"$unset": M{ "role": 1, }} result, err := coll(nil).UpdateOne(filter, update) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UnsetPrivilege", table, "UpdateOne", err), log.Any("id", id)) return err } if result.ModifiedCount+result.UpsertedCount != 1 { return fmt.Errorf("coll:%s UpdateTo Count error, id:%+v", table, id) } return nil } // Delete Delete func Delete(doc AdminDoc) error { filer, err := common.ToBsonM(doc) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Delete", table, "ToBsonM", err)) return err } if _, err := coll(nil).DeleteOne(filer); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Delete", table, "ToBsonM", err), log.Any("filer", filer)) return err } return nil } // IsUsing // @return true:角色被使用 否则flase func IsUsing(role string) (bool, error) { filter := M{"role": role} cnt, err := coll(nil).Count(filter) if err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IsUsing", table, "Count", err)) return false, err } return cnt > 0, nil } func List(skip, limit int64, matcher ...pageopt.Matcher) ([]Admin, error) { filter := pageopt.MergeM(matcher) opt := (&options.FindOptions{}). SetSkip(skip). SetLimit(limit) adminList := make([]Admin, 0, limit) if err := coll(nil).Find(&adminList, filter, opt); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err)) return nil, err } return adminList, nil } func Count(matcher ...pageopt.Matcher) (int64, error) { filter := pageopt.MergeM(matcher) 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)) return 0, err } return count, nil } // GetNickByAccount 通过账号获取名称 func GetNickByAccount(accounts []string) (map[string]string, error) { m := make(map[string]string) datas := []Admin{} cond := bson.M{"name": bson.M{"$in": accounts}} if err := coll(nil).Find(&datas, cond); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetNickByAccount", table, "Find", err), log.Any("accounts", accounts)) return m, err } for _, d := range datas { m[d.Name] = d.Nickname } return m, nil } // GetNameByNick 通过昵称过去账号名 func GetNameByNick(nick string) (string, error) { data := Admin{} cond := bson.M{"nickname": nick} if err := coll(nil).FindOne(&data, cond); err != nil { log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetNameByNick", table, "FindOne", err), log.Any("nick", nick)) return data.Name, err } return data.Name, nil }