177 lines
4.7 KiB
Go
177 lines
4.7 KiB
Go
package goldcfgmod
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/app/appg"
|
|
"91porn-server/common"
|
|
"91porn-server/common/constant/redisconst"
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/redis"
|
|
"91porn-server/models"
|
|
"91porn-server/models/commod"
|
|
"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"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.GoldConf
|
|
|
|
func initIndex() {
|
|
coll := coll(nil)
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "amount", Value: 1}, {Key: "type", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "type", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
var GoldConfigList = "goldConfigList"
|
|
|
|
// InsertOne 插入一条数据
|
|
func InsertOne(g *Incr) error {
|
|
defer redisCachDel(GoldConfigList)
|
|
g.CreatedAt = time.Now()
|
|
g.UpdatedAt = time.Now()
|
|
if _, err := coll(nil).InsertOne(g); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertOne", err), log.Any("g", g))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除数据
|
|
func Delete(ids []primitive.ObjectID) (int64, error) {
|
|
defer redisCachDel(GoldConfigList)
|
|
result, err := coll(nil).DeleteMany(bson.M{"_id": bson.M{"$in": ids}})
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Delete", table, "Delete", err))
|
|
return 0, err
|
|
}
|
|
return result.DeletedCount, nil
|
|
}
|
|
|
|
// Update 金币配置更新
|
|
func Update(id primitive.ObjectID, set EditDoc) (int64, error) {
|
|
defer redisCachDel(GoldConfigList)
|
|
updateAt := time.Now()
|
|
set.UpdatedAt = &updateAt
|
|
result, err := coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": set})
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
|
log.Any("id", id),
|
|
log.Any("set", set),
|
|
)
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// GetGoldConfigList 条件获取金币配置表
|
|
func GetGoldConfigList() ([]*DiscountConfig, error) {
|
|
var back []*DiscountConfig
|
|
redisKey := redisconst.DataCachKey(table, GoldConfigList)
|
|
redisc := getRedis()
|
|
if redisc == nil || !redisc.Exists(redisKey) {
|
|
opts := options.FindOptions{}
|
|
opts.SetSort(bson.D{{Key: "amount", Value: 1}})
|
|
if err := coll(nil).Find(&back, bson.M{}, &opts); err != nil {
|
|
log.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:", "GetGoldConfigList", table, "Find", err))
|
|
return nil, err
|
|
}
|
|
if str == nil {
|
|
return back, errors.New("redis key is null")
|
|
}
|
|
return back, json.Unmarshal([]byte(*str), &back)
|
|
}
|
|
|
|
// List 条件获取金币配置表
|
|
func List(param ListParam) (data DisConfRes, err error) {
|
|
var discountConfig []DiscountConfig
|
|
std := commod.StdQuery{
|
|
Page: &commod.PageBy{
|
|
CheckNext: false,
|
|
Num: param.PageNumber,
|
|
Size: param.PageSize,
|
|
},
|
|
Order: &[]commod.OrderBy{{Key: "updatedAt", Desc: true}},
|
|
}
|
|
opts := commod.ConvertToListQuery(std)
|
|
query, _ := common.ToBsonM(param.ListFilter)
|
|
total, err := coll(nil).Count(query)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Count", err))
|
|
return
|
|
}
|
|
if err = coll(nil).Find(&discountConfig, query, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "List", table, "Find", err))
|
|
return
|
|
}
|
|
data.List = discountConfig
|
|
data.Total = total
|
|
return data, 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
|
|
}
|
|
|
|
// 根据类型和基础金币额查找对应的优惠信息
|
|
func GetGoldCgfByAmountAndType(ctx context.Context, amount int64, cage string) (discountConfig *DiscountConfig, err error) {
|
|
if err = coll(nil).FindOne(&discountConfig, bson.M{"amount": amount, "type": cage}); err != nil {
|
|
log.ErrorX(ctx, fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "FindOne", err),
|
|
log.Any("amount", amount), log.Any("cage", cage))
|
|
}
|
|
return
|
|
}
|