86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package settingmod
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
// SettingUpdateReq 配置信息
|
|
type SettingSelector struct {
|
|
BackGround *string `json:"backGround,omitempty" bson:"backGround,omitempty"` //背景图
|
|
}
|
|
|
|
const table = models.Setting
|
|
|
|
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: "uid", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("setting model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
func InsertSetting(t *db.MongoTool, s *Setting) error {
|
|
if _, err := coll(t).InsertOne(s); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertSetting", table, "InsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateSetting 修改用户信息
|
|
func UpdateSetting(uid uint64, set SettingSelector) error {
|
|
if _, err := coll(nil).UpsertOne(bson.M{"uid": uid}, bson.M{
|
|
"$set": set,
|
|
}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateSetting", table, "UpsertOne", err),
|
|
log.Any("uid", uid),
|
|
log.Any("set", set),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindSetting ()查询
|
|
func FindSetting(uid uint64) (s *Setting, err error) {
|
|
if err = coll(nil).FindOne(s, bson.M{"uid": uid}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindSetting", table, "FindOne", err),
|
|
log.Any("uid", uid),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// FindSettingByID ()查询
|
|
func FindSettingByID(id primitive.ObjectID) (s *Setting, err error) {
|
|
if err = coll(nil).FindOneByID(s, id); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindSettingByID", table, "FindOneByID", err),
|
|
log.Any("id", id),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|