@@ -0,0 +1,83 @@
|
||||
package checkinconfigmod
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
const table = models.CheckinConfig
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "enable", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// FindOne 查找签到配置
|
||||
func FindOne(filter primitive.M) (*CheckinConfig, error) {
|
||||
var config CheckinConfig
|
||||
if err := coll(nil).FindOne(&config, filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[checkinconfigmod:FindOne] failed: %+v", err))
|
||||
return nil, err
|
||||
}
|
||||
if config.ID.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// UpdateOne 更新签到配置
|
||||
func UpdateOne(filter primitive.M, update primitive.M) error {
|
||||
result, err := coll(nil).UpdateOne(filter, update)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[checkinconfigmod:UpdateOne] failed: %+v", err))
|
||||
return err
|
||||
}
|
||||
if result.MatchedCount == 0 {
|
||||
return mongo.ErrNoDocuments
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertOne 插入签到配置
|
||||
func InsertOne(config *CheckinConfig) error {
|
||||
if _, err := coll(nil).InsertOne(config); err != nil {
|
||||
log.Error(fmt.Sprintf("[checkinconfigmod:InsertOne] failed: %+v", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Count 统计数量
|
||||
func Count(filter primitive.M) (int64, error) {
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[checkinconfigmod:Count] failed: %+v", err))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
Reference in New Issue
Block a user