@@ -0,0 +1,131 @@
|
||||
package authoritymod
|
||||
|
||||
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/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Authority
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "role", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(doc AuthorityDoc) 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
|
||||
func UpdateTo(id ObjectID, doc AuthorityDoc) 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))
|
||||
return err
|
||||
}
|
||||
result, err := coll(nil).UpdateOne(M{"_id": id}, 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("update", update))
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount+result.UpsertedCount != 1 {
|
||||
return fmt.Errorf("coll:%s UpdateTo Count error, id:%+v update:%+v", table, id, update)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteMany DeleteMany
|
||||
func DeleteMany(idArray []ObjectID) error {
|
||||
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))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindOne(doc AuthorityDoc) (authority Authority, err error) {
|
||||
filter, err := common.ToBsonM(doc)
|
||||
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(&authority, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "FindOne", err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ExistsByID(id ObjectID) (existed bool, err error) {
|
||||
filter := M{
|
||||
"_id": id,
|
||||
}
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ExistsByID", table, "Count", err))
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// FineOneByRole 通过权限id获取权限json
|
||||
func FineOneByRole(role string) (Authority, error) {
|
||||
var auth Authority
|
||||
if err := coll(nil).FindOne(&auth, bson.M{"role": role}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FineOneByRole", table, "FindOne", err), log.Any("role", role))
|
||||
return Authority{}, err
|
||||
}
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
// IDMapByRoles role->id Map
|
||||
func IDMapByRoles(roleArray []string) (map[string]ObjectID, error) {
|
||||
filter := M{
|
||||
"role": M{"$in": roleArray},
|
||||
}
|
||||
authArray := make([]Authority, 0, len(roleArray))
|
||||
if err := coll(nil).Find(&authArray, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IDMapByRoles", table, "Find", err), log.Any("roleArray", roleArray))
|
||||
return nil, err
|
||||
}
|
||||
idMap := make(map[string]ObjectID, len(authArray))
|
||||
for _, auth := range authArray {
|
||||
idMap[auth.Role] = auth.ID
|
||||
}
|
||||
return idMap, nil
|
||||
}
|
||||
Reference in New Issue
Block a user