@@ -0,0 +1,176 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package goldcfgmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 充值优惠配置模型
|
||||
type DiscountConfig struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
|
||||
Amount int64 `json:"amount" bson:"amount"` //基础货币额度
|
||||
IncrAmount int64 `json:"incrAmount" bson:"incrAmount"` //增加的优惠额度
|
||||
IncTax float64 `json:"incTax" bson:"incTax"` //按比率增加额外优惠额 0-1之间 ,如果 incrAmount 与 incrTax 同时存在 以 incrAmount 为准
|
||||
Type string `json:"type" bson:"type"` //充值方式
|
||||
TypeName string `json:"typeName" bson:"typeName"` //类型名称:支付宝,微信,代充
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //刷新时间
|
||||
}
|
||||
|
||||
type EditDoc struct {
|
||||
Amount *int64 `json:"amount,omitempty" bson:"amount,omitempty"` //基础货币额度
|
||||
IncrAmount *int64 `json:"incrAmount,omitempty" bson:"incrAmount,omitempty"` //增加的优惠额度
|
||||
IncTax *float64 `json:"incTax,omitempty" bson:"incTax,omitempty"` //按比率增加额外优惠额 0-1之间 ,如果 incrAmount 与 incrTax 同时存在 以 incrAmount 为准
|
||||
Type *string `json:"type,omitempty" bson:"type,omitempty"` //充值方式
|
||||
TypeName *string `json:"typeName,omitempty" bson:"typeName,omitempty"` //类型名称:支付宝,微信,代充
|
||||
UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
// 插入结构体
|
||||
type Incr struct {
|
||||
Amount int64 `form:"amount" json:"amount" bson:"amount" binding:"required"`
|
||||
IncrAmount int64 `form:"incrAmount" json:"incrAmount" bson:"incrAmount" binding:"required_without=IncTax"`
|
||||
IncTax float64 `form:"incTax" json:"incTax" bson:"incTax" binding:"required_without=IncrAmount"`
|
||||
Type string `form:"type" json:"type" bson:"type" binding:"required"`
|
||||
TypeName string `form:"typeName" json:"typeName" bson:"typeName" binding:"required"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //刷新时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package goldcfgmod
|
||||
|
||||
import "91porn-server/models/commod"
|
||||
|
||||
// web 返回模型
|
||||
type DisConfRes struct {
|
||||
Total int64 `json:"total" bson:"total"`
|
||||
List []DiscountConfig `json:"list" bson:"list"`
|
||||
}
|
||||
|
||||
type ListParam struct {
|
||||
ListFilter
|
||||
commod.Page
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
Type string `json:"type,omitempty" bson:"type,omitempty"` //充值方式
|
||||
Amount int64 `json:"amount,omitempty" bson:"amount,omitempty"` //基础货币额度
|
||||
TypeName string `json:"typeName,omitempty" bson:"typeName,omitempty"` //类型名称:支付宝,微信,代充
|
||||
}
|
||||
Reference in New Issue
Block a user