@@ -0,0 +1,151 @@
|
||||
package rchgamtmod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
"91porn-server/models"
|
||||
"91porn-server/web/webg"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const table = models.Gold
|
||||
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "coins", Value: 1}, {Key: "price", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
var trueActiveList = "trueActiveList"
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InsertOne 插入一条数据
|
||||
func InsertGold(g *Gold) error {
|
||||
defer redisCachDel(trueActiveList)
|
||||
if _, err := coll(nil).InsertOne(g); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertGold", table, "InsertOne", err), log.Any("g", g))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func DeleteGold(cond bson.M) (int64, error) {
|
||||
defer redisCachDel(trueActiveList)
|
||||
result, err := coll(nil).DeleteMany(cond)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteGold", table, "DeleteMany", err), log.Any("cond", cond))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, err
|
||||
}
|
||||
|
||||
// Update 金币配置更新
|
||||
func UpdateGold(cond bson.M, updt bson.M) (int64, error) {
|
||||
defer redisCachDel(trueActiveList)
|
||||
result, err := coll(nil).UpdateMany(cond, bson.M{"$set": updt})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateGold", table, "UpdateMany", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("updt", updt),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// GetGoldListByActiveTrue 条件获取金币配置表
|
||||
func GetGoldByID(id primitive.ObjectID) (*Gold, error) {
|
||||
var g Gold
|
||||
if err := coll(nil).FindOneByID(&g, id); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldByID", table, "FindOneByID", err), log.Any("id", id))
|
||||
return nil, err
|
||||
}
|
||||
return &g, nil
|
||||
}
|
||||
|
||||
// GetGoldListByActiveTrue 条件获取金币配置表
|
||||
func GetGoldListByActiveTrue() ([]*Gold, error) {
|
||||
var back []*Gold
|
||||
redisKey := redisconst.DataCachKey(table, trueActiveList)
|
||||
var redisc *redis.Client
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "coins", Value: 1}})
|
||||
err := coll(nil).Find(&back, bson.M{"active": true}, &opts)
|
||||
if err != nil {
|
||||
log.ZapLog.Warn("recharge GetGoldListByActiveTrue Find fail", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
if redisc == nil {
|
||||
return back, err
|
||||
}
|
||||
jsonBytes, err := json.Marshal(back)
|
||||
if err != nil {
|
||||
return back, err
|
||||
}
|
||||
_ = redisc.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire)
|
||||
return back, err
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldListByActiveTrue", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
if str == nil {
|
||||
return back, errors.New("redis key is null")
|
||||
}
|
||||
return back, json.Unmarshal([]byte(*str), &back)
|
||||
}
|
||||
|
||||
// GetGoldList 条件获取金币配置表
|
||||
func GetGoldList() ([]*Gold, error) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.M{"coins": 1})
|
||||
var back []*Gold
|
||||
err := coll(nil).Find(&back, bson.M{}, &opts)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldList", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
func redisCachDel(m string) {
|
||||
redisKey := redisconst.DataCachKey(table, m)
|
||||
_, _ = getRedis().Del(redisKey)
|
||||
}
|
||||
|
||||
func getRedis() *redis.Client {
|
||||
if appg.Redis != nil {
|
||||
return appg.Redis
|
||||
}
|
||||
if webg.Redis != nil {
|
||||
return webg.Redis
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user