@@ -0,0 +1,112 @@
|
||||
package currencymod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func currencyColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return currencyDB.Coll(currencyTable)
|
||||
}
|
||||
return t.Coll(currencyTable)
|
||||
}
|
||||
|
||||
func currencyIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}, {Key: "isActive", Value: 1}, {Key: "coins", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := currencyColl(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("currency model set index err ==>[%+v]", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Get 获取单个货币配置
|
||||
func Get(id primitive.ObjectID) (Currency, error) {
|
||||
var item Currency
|
||||
return item, currencyColl(nil).FindOne(&item, bson.M{"_id": id})
|
||||
}
|
||||
|
||||
// List 获取货币配置列表
|
||||
func List(t commod.CurrencyType) ([]Currency, error) {
|
||||
var items []Currency
|
||||
return items, currencyColl(nil).Find(&items, bson.M{"isActive": true, "type": t}, options.Find().SetSort(bson.M{"coins": 1}))
|
||||
}
|
||||
|
||||
// Add 新增
|
||||
func Add(document interface{}) error {
|
||||
_, err := currencyColl(nil).InsertOne(&document)
|
||||
return err
|
||||
}
|
||||
|
||||
// Edit 编辑
|
||||
func Edit(filter primitive.M, update primitive.M) error {
|
||||
result, err := currencyColl(nil).UpdateOne(filter, update)
|
||||
if err != nil {
|
||||
log.Warn("currency edit err", log.Any("filter", filter), log.Any("update", update), log.E(err))
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount == 0 {
|
||||
log.Warn("currency edit fail", log.Any("filter", filter), log.Any("update", update), log.E(err))
|
||||
return errors.New("ModifiedCount is nil")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAll 查询
|
||||
func QueryAll(filter primitive.M, opts ...*options.FindOptions) ([]*Currency, error) {
|
||||
var out []*Currency
|
||||
return out, currencyColl(nil).Find(&out, filter, opts...)
|
||||
}
|
||||
|
||||
// QueryCount 查询条目
|
||||
func QueryCount(filter primitive.M) (int64, error) {
|
||||
return currencyColl(nil).Count(filter)
|
||||
}
|
||||
|
||||
// GetCurrencyListByRedis 条件获取金币配置表,缓存拿
|
||||
func GetCurrencyListByRedis(t commod.CurrencyType) ([]*Currency, error) {
|
||||
var back []*Currency
|
||||
redisKey := redisconst.DataCachKey(currencyTable, "trueList")
|
||||
var redisc *redis.Client
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "coins", Value: 1}})
|
||||
if err := currencyColl(nil).Find(&back, bson.M{"isActive": true, "type": t}, &opts); err != nil {
|
||||
log.ZapLog.Warn("currencymod GetCurrencyListByRedis Find fail", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
if redisc == nil {
|
||||
return back, nil
|
||||
}
|
||||
jsonBytes, err := json.Marshal(back)
|
||||
if err != nil {
|
||||
return back, err
|
||||
}
|
||||
_ = redisc.Set(redisKey, string(jsonBytes), redisconst.EmailCaptchaExpire)
|
||||
return back, err
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCurrencyListByRedis", currencyTable, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
if str == nil {
|
||||
return back, errors.New("redis key is null")
|
||||
}
|
||||
return back, json.Unmarshal([]byte(*str), &back)
|
||||
}
|
||||
Reference in New Issue
Block a user