179 lines
4.7 KiB
Go
Executable File
179 lines
4.7 KiB
Go
Executable File
package sysconfmod
|
|
|
|
import (
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
// GetAllConfig 获取全部配置
|
|
func GetAllConfig() (res ConfMap, err error) {
|
|
var data []SysConf
|
|
if err = coll(nil).Find(&data, bson.M{}, options.Find().SetSort(bson.D{{Key: "_id", Value: -1}})); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByGCode", table, "Find", err))
|
|
return res, err
|
|
}
|
|
if len(data) == 0 {
|
|
return res, errors.New("record not found")
|
|
}
|
|
|
|
res = make(ConfMap, len(data))
|
|
for _, v := range data {
|
|
res[v.VCode] = v.Value
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// BatchGetInfoByVCode 按 vCode 列表批量获取配置项
|
|
func BatchGetInfoByVCode(vCodes []VCode) (res []SysConf, err error) {
|
|
if err = coll(nil).Find(&res, bson.M{"vCode": bson.M{"$in": vCodes}}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "BatchGetInfoByVCode", table, "Find", err),
|
|
log.Any("vCodes", vCodes),
|
|
)
|
|
return nil, err
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
return nil, errors.New("record not found")
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetByVCode 获取单个配置项。
|
|
func GetByVCode(code VCode) (*SysConf, error) {
|
|
var out SysConf
|
|
if err := coll(nil).FindOne(&out, bson.M{"vCode": string(code)}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByVCode", table, "FindOne", err),
|
|
log.Any("vCode", code),
|
|
)
|
|
return nil, err
|
|
}
|
|
if out.ID.IsZero() {
|
|
return nil, errors.New("record not found")
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// GetByGpCode 获取分组配置
|
|
func GetByGpCode(code GPCode) (res ConfMap, err error) {
|
|
var data []SysConf
|
|
if err = coll(nil).Find(&data, bson.M{"gpCode": code}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByGCode", table, "Find", err),
|
|
log.Any("gpCode", code),
|
|
)
|
|
return nil, err
|
|
}
|
|
if len(data) == 0 {
|
|
return nil, errors.New("record not found")
|
|
}
|
|
|
|
res = make(ConfMap, len(data))
|
|
for _, v := range data {
|
|
res[v.VCode] = v.Value
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetAll 查询全部文档
|
|
func GetAll(filter primitive.M, sort bson.D) (out []*SysConf, err error) {
|
|
if len(sort) == 0 {
|
|
sort = bson.D{{Key: "_id", Value: -1}}
|
|
}
|
|
opts := options.Find().SetSort(sort)
|
|
|
|
// 检查 filter 是否为空
|
|
if filter == nil {
|
|
filter = primitive.M{}
|
|
}
|
|
// 使用切片初始化 out
|
|
out = make([]*SysConf, 0)
|
|
if err = coll(nil).Find(&out, filter, opts); err != nil {
|
|
log.Error("[METHOD-GetAll] Model "+table+" Find fail error:"+err.Error(),
|
|
log.Any("filter", filter),
|
|
log.Any("opts", opts),
|
|
)
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetInfo 通过id获取详细信息
|
|
func GetInfo(id primitive.ObjectID) (SysConf, error) {
|
|
v := SysConf{}
|
|
if err := coll(nil).FindOne(&v, bson.M{"_id": id}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return v, err
|
|
}
|
|
|
|
if v.ID.IsZero() {
|
|
return v, errors.New("record not found")
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
// Insert 插入记录
|
|
func Insert(t *db.MongoTool, d SysConf) (data primitive.ObjectID, err error) {
|
|
result, err := coll(t).InsertOne(&d)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return
|
|
}
|
|
byteID, err := json.Marshal(result.InsertedID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Marshal", err))
|
|
return
|
|
}
|
|
if err = data.UnmarshalJSON(byteID); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "UnmarshalJSON", err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// UpdateByID 根据id更新数据
|
|
func UpdateByID(t *db.MongoTool, id primitive.ObjectID, data map[string]interface{}) (int64, error) {
|
|
cond := bson.M{"_id": id}
|
|
return update(t, cond, data)
|
|
}
|
|
|
|
// update 更新数据
|
|
func update(t *db.MongoTool, cond primitive.M, data map[string]interface{}) (int64, error) {
|
|
result, err := coll(t).UpdateMany(cond, bson.M{"$set": bson.M(data)})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateMany", err),
|
|
log.Any("cond", cond),
|
|
log.Any("update", data),
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// DeleteByID 删除数据
|
|
func DeleteByID(t *db.MongoTool, id primitive.ObjectID) error {
|
|
_, err := coll(t).DeleteOne(bson.M{"_id": id})
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil
|
|
} else {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteOne", err), log.Any("id", id))
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|