@@ -0,0 +1,443 @@
|
||||
package prizemod
|
||||
|
||||
import (
|
||||
"91porn-server/models/v/taskrecordmod"
|
||||
"91porn-server/models/v/videocoupon"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models/v/backpackmod"
|
||||
"91porn-server/models/v/productmod"
|
||||
"91porn-server/models/v/txnmod"
|
||||
"91porn-server/models/v/usermod"
|
||||
"91porn-server/models/v/walletmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type PrizeHanlder interface {
|
||||
Run(t *db.MongoTool) error
|
||||
GetTransactionLog() []txnmod.TransactionLog
|
||||
}
|
||||
|
||||
func Run(uid uint64, p Prize, transType txnmod.TransType) (PrizeHanlder, error) {
|
||||
switch p.Type {
|
||||
case CurrentValue:
|
||||
return newAddCurrentValue(uid, p, transType), nil
|
||||
case Gold:
|
||||
return newAddGold(uid, p, transType), nil
|
||||
case VIPCard:
|
||||
u, err := usermod.FindUserByUID(uid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 查询会员卡详情
|
||||
productDetail, err := productmod.FindProduct(p.VIPCardID, "android")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if productDetail == nil {
|
||||
return nil, fmt.Errorf("product not found. id: %v", p.VIPCardID)
|
||||
}
|
||||
return newAddVIPCard(u, p, transType, productDetail), nil
|
||||
case VIPDiscount:
|
||||
return newAddVIPDiscount(uid, p, transType), nil
|
||||
case InKind:
|
||||
return nil, nil
|
||||
case Integral:
|
||||
return newAddIntegral(uid, p, transType), nil
|
||||
case AIUndress:
|
||||
return newAddAIUndress(uid, p, transType), nil
|
||||
case AIChangeFace:
|
||||
return newAddAIChangeFace(uid, p, transType), nil
|
||||
case VideoCoupon:
|
||||
return newAddVideoCoupon(uid, p, transType), nil
|
||||
default:
|
||||
return nil, errors.New("未知类型")
|
||||
}
|
||||
}
|
||||
|
||||
// 新增活跃值
|
||||
type addCurrentValue struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
}
|
||||
|
||||
// 处理程序初始化
|
||||
func newAddCurrentValue(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addCurrentValue{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// 运行
|
||||
func (h *addCurrentValue) Run(t *db.MongoTool) error {
|
||||
return taskrecordmod.CurrentValueUpdate(t, h.uid, h.p.Value)
|
||||
}
|
||||
|
||||
// 资金流水记录
|
||||
func (h *addCurrentValue) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 新增金币
|
||||
type addGold struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
wallet *walletmod.Wallet
|
||||
}
|
||||
|
||||
// 处理程序初始化
|
||||
func newAddGold(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addGold{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// 运行
|
||||
func (h *addGold) Run(t *db.MongoTool) error {
|
||||
walletInfo, err := walletmod.CreditAmount(t, h.p.Price, h.uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.wallet = walletInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// 资金流水记录
|
||||
func (h *addGold) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.uid,
|
||||
Amount: h.p.Price,
|
||||
ActualAmount: float64(h.p.Price),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-金币[%d个]", h.transType.Key(), h.p.Price),
|
||||
RealAmount: h.wallet.RealAmount(),
|
||||
}}
|
||||
}
|
||||
|
||||
// 会员卡处理
|
||||
type addVIPCard struct {
|
||||
u *usermod.User
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
productDetail *productmod.Product
|
||||
}
|
||||
|
||||
// 处理程序初始化
|
||||
func newAddVIPCard(u *usermod.User, p Prize, transType txnmod.TransType, productDetail *productmod.Product) PrizeHanlder {
|
||||
return &addVIPCard{
|
||||
u: u,
|
||||
p: p,
|
||||
transType: transType,
|
||||
productDetail: productDetail,
|
||||
}
|
||||
}
|
||||
|
||||
// 运行
|
||||
func (h *addVIPCard) Run(t *db.MongoTool) error {
|
||||
vipExpire, vipLevel, payVidDiscount := checkVipRenew(h.u, h.productDetail)
|
||||
sel := usermod.UserSelector{VipExpireDate: &vipExpire, VipLevel: &vipLevel, PayVidDiscount: &payVidDiscount}
|
||||
if h.productDetail.GoldVideoFreeDay > 0 {
|
||||
expire := time.Time{}
|
||||
if h.u.GoldVideoFreeExpire.IsZero() || h.u.GoldVideoFreeExpire.Before(time.Now()) {
|
||||
expire = time.Now().AddDate(0, 0, h.productDetail.GoldVideoFreeDay)
|
||||
} else {
|
||||
expire = h.u.GoldVideoFreeExpire.AddDate(0, 0, h.productDetail.GoldVideoFreeDay)
|
||||
}
|
||||
sel.GoldVideoFreeExpire = &expire
|
||||
}
|
||||
|
||||
if err := usermod.UpdateVIP(t, h.u.UID, h.u.VipExpireDate, sel); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 资金流水记录
|
||||
func (h *addVIPCard) GetTransactionLog() (out []txnmod.TransactionLog) {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.u.UID,
|
||||
Amount: h.p.Price,
|
||||
ActualAmount: float64(h.p.Price),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-VIP[%d天]", h.transType.Key(), h.productDetail.Duration),
|
||||
}}
|
||||
}
|
||||
|
||||
func checkVipRenew(u *usermod.User, p *productmod.Product) (time.Time, int, int) {
|
||||
now := time.Now()
|
||||
var end time.Time
|
||||
level := u.VipLevel
|
||||
payVidDiscount := u.PayVidDiscount
|
||||
d := time.Hour * 24 * time.Duration(p.Duration)
|
||||
if u.VipExpireDate.After(now) { //renew
|
||||
end = u.VipExpireDate.Add(d)
|
||||
} else {
|
||||
end = now.Add(time.Duration(d))
|
||||
}
|
||||
if p.VipLevel > level { //当前用户的vip等级比这次购买的大,使用用户的
|
||||
level = p.VipLevel
|
||||
}
|
||||
if p.PayVidDiscount > 0 && p.PayVidDiscount < payVidDiscount {
|
||||
payVidDiscount = p.PayVidDiscount
|
||||
}
|
||||
return end, level, payVidDiscount
|
||||
}
|
||||
|
||||
// 会员折扣卷处理
|
||||
type addVIPDiscount struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
}
|
||||
|
||||
// 处理程序初始化
|
||||
func newAddVIPDiscount(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addVIPDiscount{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// 运行
|
||||
func (h *addVIPDiscount) Run(t *db.MongoTool) error {
|
||||
goodsList := make([]backpackmod.Backpack, h.p.Count)
|
||||
now := time.Now()
|
||||
for i := 0; i < int(h.p.Count); i++ {
|
||||
goodsList[i] = backpackmod.Backpack{
|
||||
UID: h.uid,
|
||||
GoodsName: h.p.Name,
|
||||
GoodsType: backpackmod.VIPDiscount,
|
||||
GoodsValue: h.p.Value,
|
||||
GoodsOrigin: h.transType.Key(),
|
||||
GoodsDesc: h.p.Desc,
|
||||
Status: backpackmod.Unused,
|
||||
ExpiredTime: now.AddDate(0, 0, int(h.p.ValidityTime)),
|
||||
CreateTime: now,
|
||||
}
|
||||
}
|
||||
return backpackmod.AddGoodsMany(t, h.uid, goodsList)
|
||||
}
|
||||
|
||||
// 资金流水记录
|
||||
func (h *addVIPDiscount) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return nil
|
||||
}
|
||||
|
||||
// addIntegral 新增积分
|
||||
type addIntegral struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
wallet *walletmod.Wallet
|
||||
}
|
||||
|
||||
// newAddIntegral 处理程序初始化
|
||||
func newAddIntegral(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addIntegral{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 运行
|
||||
func (h *addIntegral) Run(t *db.MongoTool) error {
|
||||
walletInfo, err := walletmod.CreditIntegral(t, h.p.Price, h.uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.wallet = walletInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTransactionLog 资金流水记录
|
||||
func (h *addIntegral) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.uid,
|
||||
Integral: h.p.Price,
|
||||
ActualIntegral: float64(h.p.Price),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-积分[%d个]", h.transType.Key(), h.p.Price),
|
||||
RealIntegral: h.wallet.RealIntegral(),
|
||||
}}
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
// addAIUndress 新增ai脱衣次数
|
||||
type addAIUndress struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
wallet *walletmod.Wallet
|
||||
}
|
||||
|
||||
// newAddAIUndress 处理程序初始化
|
||||
func newAddAIUndress(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addAIUndress{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 运行
|
||||
func (h *addAIUndress) Run(t *db.MongoTool) error {
|
||||
count := int64(h.p.Count)
|
||||
price := h.p.Price
|
||||
aiUndressFreeTimes := count * price
|
||||
p := walletmod.CreditPlan{}
|
||||
p.AiUndressFreeTimes = &aiUndressFreeTimes
|
||||
walletInfo, err := walletmod.Credit(t, p, h.uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.wallet = walletInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTransactionLog 资金流水记录
|
||||
func (h *addAIUndress) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.uid,
|
||||
Amount: int64(h.p.Count),
|
||||
ActualAmount: float64(h.p.Count),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-AI脱衣次数[%v次]", h.transType.Key(), h.p.Count),
|
||||
RealAmount: h.wallet.RealAmount(),
|
||||
}}
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
// addAIChangeFace 新增ai换脸次数
|
||||
type addAIChangeFace struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
wallet *walletmod.Wallet
|
||||
}
|
||||
|
||||
// newAddAIChangeFace 处理程序初始化
|
||||
func newAddAIChangeFace(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addAIChangeFace{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 运行
|
||||
func (h *addAIChangeFace) Run(t *db.MongoTool) error {
|
||||
count := int64(h.p.Count)
|
||||
price := h.p.Price
|
||||
aiChangeFaceImgFreeTimes := count * price
|
||||
p := walletmod.CreditPlan{}
|
||||
p.AiUndressFreeTimes = &aiChangeFaceImgFreeTimes
|
||||
walletInfo, err := walletmod.Credit(t, p, h.uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.wallet = walletInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTransactionLog 资金流水记录
|
||||
func (h *addAIChangeFace) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.uid,
|
||||
Amount: int64(h.p.Count),
|
||||
ActualAmount: float64(h.p.Count),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-AI图片换脸次数[%v次]", h.transType.Key(), h.p.Count),
|
||||
RealAmount: h.wallet.RealAmount(),
|
||||
}}
|
||||
}
|
||||
|
||||
// ---------------------------
|
||||
// addVideoCoupon 新增观影券
|
||||
type addVideoCoupon struct {
|
||||
uid uint64
|
||||
p Prize
|
||||
transType txnmod.TransType
|
||||
wallet *walletmod.Wallet
|
||||
}
|
||||
|
||||
// newAddVideoCoupon 处理程序初始化
|
||||
func newAddVideoCoupon(uid uint64, p Prize, transType txnmod.TransType) PrizeHanlder {
|
||||
return &addVideoCoupon{
|
||||
uid: uid,
|
||||
p: p,
|
||||
transType: transType,
|
||||
}
|
||||
}
|
||||
|
||||
// Run 运行
|
||||
func (h *addVideoCoupon) Run(t *db.MongoTool) error {
|
||||
now := time.Now()
|
||||
for i := 0; i < int(h.p.Count); i++ {
|
||||
videoCoupon := videocoupon.UserGoldVideoCoupon{
|
||||
UID: h.uid,
|
||||
Num: int(h.p.Price),
|
||||
Used: false,
|
||||
Source: videocoupon.GoldVideoCouponSourceSign,
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err := videocoupon.InsertOne(videoCoupon); err != nil {
|
||||
continue
|
||||
}
|
||||
backpack := backpackmod.Backpack{
|
||||
UID: h.uid,
|
||||
GoodsType: backpackmod.SIGNCoupon,
|
||||
GoodsValue: int64(videoCoupon.Num),
|
||||
GoodsOrigin: string(videocoupon.GoldVideoCouponSourceSign),
|
||||
Status: backpackmod.Unused,
|
||||
ExpiredTime: now.AddDate(0, 0, 7),
|
||||
CreateTime: now,
|
||||
}
|
||||
err := backpackmod.AddGoods(nil, h.uid, backpack)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
walletInfo, err := walletmod.GetWallet(h.uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.wallet = walletInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTransactionLog 资金流水记录
|
||||
func (h *addVideoCoupon) GetTransactionLog() []txnmod.TransactionLog {
|
||||
return []txnmod.TransactionLog{txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: h.uid,
|
||||
Amount: int64(h.p.Count),
|
||||
ActualAmount: float64(h.p.Count),
|
||||
TranType: h.transType.Key(),
|
||||
TranTypeInt: int64(h.transType),
|
||||
Desc: fmt.Sprintf("%s-观影券[面额%v,%d张]", h.transType.Key(), h.p.Price, h.p.Count),
|
||||
RealAmount: h.wallet.RealAmount(),
|
||||
}}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package prizemod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"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
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(models.ActivityPrize)
|
||||
}
|
||||
return t.Coll(models.ActivityPrize)
|
||||
}
|
||||
|
||||
// initIndex 索引设置
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "activityId", Value: 1}, {Key: "status", Value: 1}, {Key: "sort", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "activityId", Value: 1}, {Key: "status", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}, {Key: "type", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "status", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "level", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createTime", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", models.ActivityPrize, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 获取指定活动奖品列表
|
||||
func GetPrizeListByActivityID(id primitive.ObjectID) ([]*Prize, error) {
|
||||
out := []*Prize{}
|
||||
if err := coll(nil).Find(&out, bson.M{"activityId": id, "status": true}, options.Find().
|
||||
SetSort(bson.M{"sort": 1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPrizeListByActivityID", models.ActivityPrize, "Find", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 获取指定活动大奖列表
|
||||
func GetHighPrizeListByActivityID(id primitive.ObjectID) ([]*Prize, error) {
|
||||
out := []*Prize{}
|
||||
if err := coll(nil).Find(&out, bson.M{"activityId": id, "status": true, "level": High}, options.Find().
|
||||
SetSort(bson.M{"sort": 1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPrizeListByActivityID", models.ActivityPrize, "Find", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 获取优惠卷列表
|
||||
func GetCouponList(couponType int) ([]*Prize, error) {
|
||||
out := []*Prize{}
|
||||
if err := coll(nil).Find(&out, bson.M{"status": true, "type": couponType}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCouponList", models.ActivityPrize, "Find", err),
|
||||
log.Any("couponType", couponType),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 通过ID获取奖品列表
|
||||
func GetPrizeListByIDs(ids []primitive.ObjectID) ([]*Prize, error) {
|
||||
out := make([]*Prize, 0)
|
||||
if len(ids) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
if err := coll(nil).Find(&out, bson.M{"_id": bson.M{"$in": ids}, "status": true}, options.Find().SetSort(bson.M{"type": 1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByUID", models.ActivityPrize, "Find", err),
|
||||
log.Any("ids", ids),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 获取所有开启的奖品
|
||||
func GetPrizeList() (map[primitive.ObjectID]Prize, error) {
|
||||
out := make([]Prize, 0)
|
||||
if err := coll(nil).Find(&out, bson.M{"status": true}, options.Find()); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetPrizeList", models.ActivityPrize, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
var m map[primitive.ObjectID]Prize = make(map[primitive.ObjectID]Prize, len(out))
|
||||
for _, v := range out {
|
||||
m[v.ID] = v
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// 新增奖品
|
||||
func AddPrize(in AddPrizeCond) (interface{}, error) {
|
||||
result, err := coll(nil).InsertOne(&Prize{
|
||||
ActivityID: in.ActivityID,
|
||||
VIPCardID: in.VIPCardID,
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
Price: in.Price,
|
||||
Count: in.Count,
|
||||
Value: in.Value,
|
||||
Desc: in.Desc,
|
||||
Weights: in.Weights,
|
||||
Level: in.Level,
|
||||
Status: false,
|
||||
ValidityTime: in.ValidityTime,
|
||||
Image: in.Image,
|
||||
Sort: in.Sort,
|
||||
UpdateTime: time.Now(),
|
||||
CreateTime: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddPrize", models.ActivityPrize, "InsertOne", err),
|
||||
log.Any("in", in),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 修改活动奖品
|
||||
func ModifyPrize(filter, update primitive.M) error {
|
||||
result, err := coll(nil).UpdateOne(filter, update)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ModifyPrize", models.ActivityPrize, "UpdateOne", err),
|
||||
log.Any("filter", filter),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount == 0 {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail", "ModifyPrize", models.ActivityPrize, "result.ModifiedCount == 0"),
|
||||
log.Any("filter", filter),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return errors.New("activity UpdateOne ModifiedCount err")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 查询活动奖品列表
|
||||
func QueryAllPrize(filter primitive.M, opts ...*options.FindOptions) ([]*Prize, error) {
|
||||
out := []*Prize{}
|
||||
if err := coll(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllPrize", models.ActivityPrize, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// CountPrize 查询奖品总条数
|
||||
func CountPrize(filter primitive.M) (int64, error) {
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CountPrize", models.ActivityPrize, "Count", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// QueryDropDownBox 奖品下拉框
|
||||
func QueryDropDownBox() ([]*Prize, error) {
|
||||
out := []*Prize{}
|
||||
if err := coll(nil).Find(&out, bson.M{"status": true}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryDropDownBox", models.ActivityPrize, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package prizemod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type PrizeType int
|
||||
|
||||
const (
|
||||
CurrentValue PrizeType = iota + 1 // 活跃值
|
||||
Gold // 金币
|
||||
_ // -
|
||||
_ // -
|
||||
VIPCard // 会员卡
|
||||
VIPDiscount // 会员折扣卷
|
||||
InKind // 实物
|
||||
Integral // 积分
|
||||
AIUndress // ai 脱衣
|
||||
AIChangeFace // ai 换脸
|
||||
VideoCoupon // 观影券
|
||||
)
|
||||
|
||||
type PrizeLevel int
|
||||
|
||||
const (
|
||||
Low PrizeLevel = iota + 1 // 初级 --- 免费可获取
|
||||
Middle // 中级 --- 放分可获取
|
||||
High // 高级 --- 奖池流水达到一定条件可获取
|
||||
)
|
||||
|
||||
type Prize struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 文档ID
|
||||
ActivityID primitive.ObjectID `json:"activityId" bson:"activityId"` // 活动ID
|
||||
VIPCardID primitive.ObjectID `json:"vipCardId" bson:"vipCardId"` // 会员卡ID
|
||||
Name string `json:"name" bson:"name"` // 奖品名称
|
||||
Type PrizeType `json:"type" bson:"type"` // 奖品类型
|
||||
Count int32 `json:"count" bson:"count"` // 奖品数量
|
||||
Price int64 `json:"price" bson:"price"` // 奖品价值
|
||||
Sort int32 `json:"sort" bson:"sort"` // 奖品排序
|
||||
Desc string `json:"desc" bson:"desc"` // 奖品描述
|
||||
Value int64 `json:"value" bson:"value"` // 折扣率/活跃值
|
||||
Weights decimal.Decimal `json:"weights" bson:"weights"` // 中奖权重
|
||||
Level PrizeLevel `json:"level" bson:"level"` // 奖品等级
|
||||
Status bool `json:"status" bson:"status"` // 启用/禁用
|
||||
ValidityTime int64 `json:"validityTime" bson:"validityTime"` // 奖品有效期 单位: 天
|
||||
Image string `json:"image" bson:"image"` // 图片地址
|
||||
UpdateTime time.Time `json:"updateTime" bson:"updateTime"` // 修改时间
|
||||
CreateTime time.Time `json:"createTimt" bson:"createTimt"` // 创建时间
|
||||
}
|
||||
|
||||
func (p *Prize) GetPrice() int64 {
|
||||
return p.Price * int64(p.Count)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(models.ActivityPrize)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package prizemod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 新增活动奖品
|
||||
type AddPrizeCond struct {
|
||||
ActivityID primitive.ObjectID `json:"activityId"` // 活动ID
|
||||
VIPCardID primitive.ObjectID `json:"vipCardId"` // 会员卡ID
|
||||
Name string `json:"name" binding:"required"` // 奖品名称
|
||||
Type PrizeType `json:"type" binding:"required"` // 奖品类型
|
||||
Count int32 `json:"count"` // 奖品数量
|
||||
Price int64 `json:"price"` // 奖品价值
|
||||
Sort int32 `json:"sort"` // 奖品排序
|
||||
Desc string `json:"desc"` // 奖品描述
|
||||
Value int64 `json:"value"` // 折扣率/活跃值
|
||||
Weights decimal.Decimal `json:"weights"` // 中奖权重
|
||||
Level PrizeLevel `json:"level"` // 奖品等级
|
||||
Status bool `json:"status"` // 启用/禁用
|
||||
ValidityTime int64 `json:"validityTime"` // 奖品有效期 单位: 天
|
||||
Image string `json:"image"` // 图片地址
|
||||
}
|
||||
|
||||
// 修改活动奖品
|
||||
type ModifyPrizeCond struct {
|
||||
ID primitive.ObjectID `json:"id" binding:"required"` // 文档ID
|
||||
ActivityID *primitive.ObjectID `json:"activityId"` // 活动ID
|
||||
VIPCardID *primitive.ObjectID `json:"vipCardId"` // 会员卡ID
|
||||
Name *string `json:"name"` // 奖品名称
|
||||
Type *PrizeType `json:"type"` // 奖品类型
|
||||
Count *int32 `json:"count"` // 奖品数量
|
||||
Price *int64 `json:"price"` // 奖品价值
|
||||
Sort *int32 `json:"sort"` // 奖品排序
|
||||
Desc *string `json:"desc"` // 奖品描述
|
||||
Value *int64 `json:"value"` // 折扣率/活跃值
|
||||
Weights *decimal.Decimal `json:"weights"` // 中奖权重
|
||||
Level *PrizeLevel `json:"level"` // 奖品等级
|
||||
Status *bool `json:"status"` // 启用/禁用
|
||||
ValidityTime *int64 `json:"validityTime"` // 奖品有效期 单位: 天
|
||||
Image *string `json:"image"` // 图片地址
|
||||
}
|
||||
|
||||
func (m *ModifyPrizeCond) Cond() primitive.M {
|
||||
return bson.M{"_id": m.ID}
|
||||
}
|
||||
|
||||
func (m *ModifyPrizeCond) Bson() primitive.M {
|
||||
set := bson.M{"updatedAt": time.Now()}
|
||||
if m.ActivityID != nil {
|
||||
set["activityId"] = m.ActivityID
|
||||
}
|
||||
if m.VIPCardID != nil {
|
||||
set["vipCardId"] = m.VIPCardID
|
||||
}
|
||||
if m.Name != nil {
|
||||
set["name"] = m.Name
|
||||
}
|
||||
if m.Type != nil {
|
||||
set["type"] = m.Type
|
||||
}
|
||||
if m.Price != nil {
|
||||
set["price"] = m.Price
|
||||
}
|
||||
if m.Value != nil {
|
||||
set["value"] = m.Value
|
||||
}
|
||||
if m.Count != nil {
|
||||
set["count"] = m.Count
|
||||
}
|
||||
if m.Sort != nil {
|
||||
set["sort"] = m.Sort
|
||||
}
|
||||
if m.Desc != nil {
|
||||
set["desc"] = m.Desc
|
||||
}
|
||||
if m.Weights != nil {
|
||||
set["weights"] = m.Weights
|
||||
}
|
||||
if m.Level != nil {
|
||||
set["level"] = m.Level
|
||||
}
|
||||
if m.Status != nil {
|
||||
set["status"] = m.Status
|
||||
}
|
||||
if m.ValidityTime != nil {
|
||||
set["validityTime"] = m.ValidityTime
|
||||
}
|
||||
if m.Image != nil {
|
||||
set["image"] = m.Image
|
||||
}
|
||||
return bson.M{"$set": set}
|
||||
}
|
||||
|
||||
// 查询活动奖品列表
|
||||
type QueryAllPrizeCond struct {
|
||||
Status *bool `form:"status"` // 状态---启用/禁用
|
||||
Type *PrizeType `form:"type"` // 奖品类型
|
||||
ActivityID *string `form:"activityId"` // 活动ID
|
||||
commod.Page
|
||||
}
|
||||
|
||||
func (q *QueryAllPrizeCond) Options() *options.FindOptions {
|
||||
return options.Find().SetLimit(int64(q.PageSize)).SetSkip(int64((q.PageNumber - 1) * q.PageSize)).SetSort(bson.M{"createdAt": -1})
|
||||
}
|
||||
|
||||
func (q *QueryAllPrizeCond) Query() primitive.M {
|
||||
fliter := bson.M{}
|
||||
if q.Status != nil {
|
||||
fliter["status"] = q.Status
|
||||
}
|
||||
if q.Type != nil {
|
||||
fliter["type"] = q.Type
|
||||
}
|
||||
if q.ActivityID != nil {
|
||||
id, _ := primitive.ObjectIDFromHex(*q.ActivityID)
|
||||
fliter["activityId"] = id
|
||||
}
|
||||
return fliter
|
||||
}
|
||||
Reference in New Issue
Block a user