You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.3 KiB
68 lines
1.3 KiB
package mgoService
|
|
|
|
import (
|
|
"GoClouds/models"
|
|
"GoClouds/service"
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"time"
|
|
)
|
|
|
|
func FindParam(id string) *models.MgoParam {
|
|
if len(id) <= 0 {
|
|
return nil
|
|
}
|
|
ses := service.MgoParamDao.GetSession()
|
|
e := new(models.MgoParam)
|
|
ids, _ := primitive.ObjectIDFromHex(id)
|
|
err := ses.C().Find(context.Background(), bson.M{"_id": ids}).One(e)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return e
|
|
}
|
|
func GetParam(key string) *models.MgoParam {
|
|
if len(key) <= 0 {
|
|
return nil
|
|
}
|
|
ses := service.MgoParamDao.GetSession()
|
|
e := new(models.MgoParam)
|
|
err := ses.C().Find(context.Background(), bson.M{"key": key}).One(e)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return e
|
|
}
|
|
func SetParam(key string, val map[string]interface{}, tits ...string) bool {
|
|
if len(key) <= 0 {
|
|
return false
|
|
}
|
|
isup := true
|
|
e := GetParam(key)
|
|
if e == nil {
|
|
isup = false
|
|
e = new(models.MgoParam)
|
|
e.Id = primitive.NewObjectID()
|
|
e.Key = key
|
|
e.Times = time.Now()
|
|
}
|
|
|
|
e.Value = val
|
|
if len(tits) > 0 {
|
|
e.Title = tits[0]
|
|
}
|
|
|
|
var err error
|
|
ses := service.MgoParamDao.GetSession()
|
|
if isup {
|
|
err = ses.C().UpdateId(context.Background(), e.Id, e)
|
|
} else {
|
|
_, err = ses.C().InsertOne(context.Background(), e)
|
|
}
|
|
if err != nil {
|
|
println("err:" + err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|