80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
package systemmod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/models/commod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
func configColl(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return cdb.Coll(cTable)
|
|
}
|
|
return t.Coll(cTable)
|
|
}
|
|
|
|
func configIndex() {
|
|
cdb = db.Init(cTable)
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}, {Key: "location", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "isActive", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := configColl(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("system_config model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
func GetByTypeLocation(t commod.SystemConfigType, lc commod.SystemLocationCode) (*Config, error) {
|
|
var (
|
|
now = time.Now()
|
|
c = &Config{
|
|
Type: t,
|
|
Location: lc,
|
|
IsActive: true,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
)
|
|
if err := configColl(nil).FindOne(&c, bson.M{"type": t, "location": lc}); err != nil {
|
|
return nil, err
|
|
}
|
|
if c.ID.IsZero() {
|
|
result, err := configColl(nil).InsertOne(&c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.InsertedID == nil {
|
|
return nil, errors.New("result.InsertedID is nil")
|
|
}
|
|
c.ID = result.InsertedID.(primitive.ObjectID)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func List() ([]*Config, error) {
|
|
items := make([]*Config, 0)
|
|
return items, configColl(nil).Find(&items, bson.M{"isActive": true})
|
|
}
|
|
|
|
func Edit(filter, update primitive.M) error {
|
|
result, err := configColl(nil).UpdateOne(filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.ModifiedCount == 0 {
|
|
return errors.New("ModifiedCount is nil")
|
|
}
|
|
return nil
|
|
}
|