131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package advanceconfigmod
|
|
|
|
import (
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
"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"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.AdvanceConfig
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "sortCode", Value: -1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "status", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func InsertOne(mt *db.MongoTool, cfg *AdvanceConfig) error {
|
|
if _, err := coll(mt).InsertOne(cfg); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-InsertOne]==> Model %s InsertOne fail error:%+v:", table, err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindDataByID 根据ID查询
|
|
func FindDataByID(id string) (AdvanceConfig, error) {
|
|
objID, err := primitive.ObjectIDFromHex(id)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-FindDataByID]==> Model %s ObjectIDFromHex fail error:%+v:", table, err),
|
|
log.Any("id", id),
|
|
)
|
|
return AdvanceConfig{}, err
|
|
}
|
|
var aud AdvanceConfig
|
|
if err = coll(nil).FindOne(&aud, bson.M{"_id": objID}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-FindDataByID]==> Model %s FindOne fail error:%+v:", table, err),
|
|
log.Any("objID", objID),
|
|
)
|
|
return aud, err
|
|
}
|
|
return aud, nil
|
|
}
|
|
|
|
// FindDataByUID 根据UID查询
|
|
func FindDataByUID(uid uint64) ([]*AdvanceConfig, error) {
|
|
var aud []*AdvanceConfig
|
|
if err := coll(nil).Find(&aud, bson.M{"uid": uid}); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-FindDataByUID]==> Model %s FindOne fail error:%+v:", table, err),
|
|
log.Any("uid", uid),
|
|
)
|
|
return aud, err
|
|
}
|
|
return aud, nil
|
|
}
|
|
|
|
// QueryAllDocument 分页查询文档
|
|
func QueryAllDocument(filter primitive.M, opts ...*options.FindOptions) (out []*AdvanceConfig, err error) {
|
|
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllDocument", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// CountDocument 查询文档条目数
|
|
func CountDocument(filter primitive.M) (int64, error) {
|
|
if count, err := coll(nil).Count(filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CountDocument", table, "Count", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return 0, err
|
|
} else {
|
|
return count, nil
|
|
}
|
|
}
|
|
|
|
// IsExist 根据标签名获取标签
|
|
func IsExist(filter primitive.M) (data *AdvanceConfig, err error) {
|
|
if err = coll(nil).FindOne(&data, filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "isExist", table, "FindOne", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// Update 修改订单
|
|
func Update(t *db.MongoTool, id primitive.ObjectID, set bson.M) error {
|
|
if _, err := coll(t).UpdateOne(bson.M{"_id": id}, set); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-Update]==> Model %s UpdateOne fail error:%+v:", table, err),
|
|
log.Any("id", id),
|
|
log.Any("set", set),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|