Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

157 lines
4.1 KiB
Go

package rchgamegoldmod
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.GameGold
func initIndex() {
coll := coll(nil)
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "coins", Value: 1}, {Key: "price", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "active", Value: 1}, {Key: "coins", Value: -1}},
},
}
if _, err := coll.CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
var trueActiveList = "gameGoldTrueActiveList"
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)
redisc := getRedis()
if redisc == nil || !redisc.Exists(redisKey) {
opts := options.FindOptions{}
opts.SetSort(bson.D{{Key: "coins", Value: 1}})
if err := coll(nil).Find(&back, bson.M{"active": true}, &opts); err != nil {
log.ZapLog.Warn("recharge GetGoldListByActiveTrue 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.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 nil, errors.New("redis key is null")
}
if err = json.Unmarshal([]byte(*str), &back); err != nil {
log.Error(fmt.Sprintf("解析数据异常[%v]", err))
return nil, err
}
return back, nil
}
// GetGoldList 条件获取金币配置表
func GetGoldList() ([]*Gold, error) {
opts := options.FindOptions{}
opts.SetSort(bson.M{"coins": 1})
var back []*Gold
if err := coll(nil).Find(&back, bson.M{}, &opts); 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
}