Files
huangguo_server/app/service/integral_config_ser/integral_config.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

525 lines
15 KiB
Go

package integral_config_ser
import (
"91porn-server/app/appg"
"91porn-server/common"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/common/stderr"
"91porn-server/models/v/backpackmod"
"91porn-server/models/v/coupon_record_mod"
"91porn-server/models/v/integralconfigmod"
"91porn-server/models/v/integralexcangemod"
"91porn-server/models/v/prdcthsomod"
"91porn-server/models/v/txnmod"
"91porn-server/models/v/usermod"
"91porn-server/models/v/videocoupon"
"91porn-server/models/v/walletmod"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func GetAllConfig() []integralconfigmod.APPIntegralConfig {
var data []integralconfigmod.APPIntegralConfig
op := options.Find().SetSort(bson.M{"duration": 1})
filter := bson.M{"status": true}
list, err := integralconfigmod.QueryAllList(filter, op)
if err != nil {
return data
}
if len(list) <= 0 {
return data
}
for _, i := range list {
appdata := integralconfigmod.APPIntegralConfig{
ID: i.ID,
Name: i.Name,
Type: i.Type,
Desc: i.Desc,
Price: i.Price,
Img: i.Img,
Duration: i.Duration,
}
data = append(data, appdata)
}
return data
}
func ExchangeIntegral(uid uint64, in *integralconfigmod.ExchangeIntegralReq) stderr.Code {
user, err := usermod.FindUserByUID(uid)
if err != nil {
return stderr.ErrDbQueryError
}
if user == nil || user.ID.IsZero() {
return stderr.UserIsNotExists
}
if user.HasBanned || user.HasLocked {
return stderr.ErrAccountHasBinded
}
wallet, err := walletmod.GetWallet(uid)
if err != nil {
return stderr.ErrDbQueryError
}
if wallet.ID.IsZero() || wallet.Integral <= 0 {
return stderr.InsufficientIntegral
}
id, err := primitive.ObjectIDFromHex(in.ID)
if err != nil {
return stderr.Failure
}
// 获取积分配置
config, err := integralconfigmod.FindOneById(id)
if err != nil {
return stderr.ErrDbQueryError
}
if config.ID.IsZero() {
return stderr.CodeEmptyData
}
if config.Price > wallet.Integral {
return stderr.InsufficientIntegral
}
var code stderr.Code
switch config.Type {
case integralconfigmod.VIPDate:
// 1 VIP兑换
code = HandeExchangeVip(config, user)
case integralconfigmod.AICount:
// 2 AI黑科技卷
code = HandeExchangeAIUndressCount(config, user)
//code = HandeExchangeAICount(config, user)
case integralconfigmod.GoldCoinBonus:
// 3 金币加赠券
code = HandeExchangeGoldCoinBonus(config, user)
case integralconfigmod.GoldWatch:
// 4 金币观影券
code = HandeExchangeGoldWatch(config, user)
case integralconfigmod.InKind:
// 5 实物奖励
code = HandeExchangeInKind(config, user, in)
case integralconfigmod.FreeCount:
// 6 幸运抽奖次数
code = HandeExchangeFreeCount(config, user)
default:
code = stderr.Success
}
return code
}
// HandeExchangeAIUndressCount 兑换ai脱衣次数
func HandeExchangeAIUndressCount(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
uid := user.UID
integral := -config.Price
var creditPlan = walletmod.CreditPlan{Integral: &integral}
undressTimes := config.EquivalentPrice
creditPlan.AiUndressFreeTimes = &undressTimes
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.Credit(t, creditPlan, uid)
if err != nil { //扣钱
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeAICount.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeAICount),
Desc: "积分兑换AI黑科技券-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeAIUndressCount Transaction Log err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeAIUndressCount Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeAICount 积分兑换AI黑科技劵
func HandeExchangeAICount(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
var (
goodsList []backpackmod.Backpack
now = time.Now()
uid = user.UID
)
goodsList = append(goodsList, backpackmod.Backpack{
UID: user.UID,
GoodsName: config.Name,
GoodsType: backpackmod.AiChangeFaceDiscount,
GoodsValue: config.EquivalentPrice,
GoodsOrigin: "积分兑换AI黑科技劵",
GoodsDesc: config.Desc,
Status: backpackmod.Unused,
ExpiredTime: now.AddDate(0, 0, int(config.Duration)),
CreateTime: now,
})
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.DebitIntegral(t, config.Price, uid)
if err != nil { //扣钱
return err
}
if err := backpackmod.AddGoodsMany(t, uid, goodsList); err != nil {
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeAICount.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeAICount),
Desc: "积分兑换AI黑科技券-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeAICount Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeAICount Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeGoldCoinBonus 积分兑换金币加赠券
func HandeExchangeGoldCoinBonus(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
uid := user.UID
// 保存用户优惠券
now := time.Now()
cId := HandleCId(config.ID)
// 获取观影券是否存在
c, err := coupon_record_mod.FindOneByCId(cId)
if err != nil {
return stderr.Failure
}
if !c.ID.IsZero() {
return stderr.Failure
}
newCo := coupon_record_mod.CouponRecord{
CID: cId,
UID: user.UID,
UserName: user.Name,
Name: config.Name,
Count: 1,
Price: config.EquivalentPrice,
Type: coupon_record_mod.PrizeTypeGoldCoinBonus,
Used: false,
IsDelete: false,
ExpireTime: now.Add(time.Hour * 24 * time.Duration(config.Duration)),
}
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.DebitIntegral(t, config.Price, uid)
if err != nil { //扣钱
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeGoldCoinBonus.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeGoldCoinBonus),
Desc: "积分兑换金币加赠券-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
err = coupon_record_mod.InsertOne(newCo)
if err != nil {
return err
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeGoldCoinBonus Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeGoldCoinBonus Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeGoldWatch 积分兑换金币观影券
func HandeExchangeGoldWatch(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
uid := user.UID
var coupons []videocoupon.UserGoldVideoCoupon
coupon := videocoupon.UserGoldVideoCoupon{
UID: uid,
Num: int(config.EquivalentPrice),
Used: false,
Source: "积分兑换金币观影券",
}
coupons = append(coupons, coupon)
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.DebitIntegral(t, config.Price, uid)
if err != nil { //扣钱
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeGoldWatch.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeGoldWatch),
Desc: "积分兑换金币观影券-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
// 赠送观影券
if len(coupons) > 0 {
if err := videocoupon.InsertMany(coupons); err != nil {
return err
}
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeGoldWatch Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeGoldWatch Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeFreeCount 积分兑换幸运抽奖次数
func HandeExchangeFreeCount(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
uid := user.UID
integral := -config.Price
var creditPlan = walletmod.CreditPlan{Integral: &integral}
lotteryTimes := config.EquivalentPrice
creditPlan.LotteryTimes = &lotteryTimes
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.Credit(t, creditPlan, uid)
if err != nil { //扣钱
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeFreeCount.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeFreeCount),
Desc: "积分兑换幸运抽奖次数-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeFreeCount Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeFreeCount Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeVip 积分兑换VIP
func HandeExchangeVip(config integralconfigmod.IntegralConfig, user *usermod.User) (code stderr.Code) {
var vipExpire time.Time
uid := user.UID
now := time.Now()
d := time.Hour * 24 * time.Duration(config.EquivalentPrice)
if user.VipExpireDate.After(now) { //renew
vipExpire = user.VipExpireDate.Add(d)
} else {
vipExpire = now.Add(d)
}
vipLevel := usermod.RenewVIPLevel(user.VipExpireDate, now, user.VipLevel, 1)
sel := usermod.UserSelector{VipExpireDate: &vipExpire, VipLevel: &vipLevel}
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.DebitIntegral(t, config.Price, uid)
if err != nil { //扣钱
return err
}
history := prdcthsomod.ProductHistory{ //插入商品购买记录
ID: primitive.NewObjectID(),
UID: uid,
ProductID: config.ID,
Name: config.Name,
Amount: config.Price,
Income: 0,
ProductType: prdcthsomod.VIP,
SysType: user.SysType,
}
if err = prdcthsomod.InsertProductHistory(t, &history); err != nil {
return err
}
// 修改会员
if err = usermod.UpdateVIP(t, uid, user.VipExpireDate, sel); err != nil {
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeVip.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeVip),
TransNo: history.ID,
Desc: "积分兑换VIP-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeVip Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeVip Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
// HandeExchangeInKind 积分兑换实体
func HandeExchangeInKind(config integralconfigmod.IntegralConfig, user *usermod.User, in *integralconfigmod.ExchangeIntegralReq) (code stderr.Code) {
//if in.Name == "" || in.Tel == "" || in.Address == "" {
// return stderr.ErrParamError
//}
uid := user.UID
now := time.Now()
ie := integralexcangemod.IntegralExchange{
EID: config.ID,
UID: user.UID,
Portrait: user.Portrait,
Name: in.Name,
Tel: in.Tel,
Address: in.Address,
Status: integralexcangemod.PENDING,
CreateTime: now,
}
if err := appg.VideoDB.Trans(func(t *db.MongoTool) error {
var err error
wallet, err := walletmod.DebitIntegral(t, config.Price, uid)
if err != nil { //扣钱
return err
}
err = integralexcangemod.InsertOne(t, ie)
if err != nil {
return err
}
txnLogs := []txnmod.TransactionLog{
{
UID: uid,
Integral: -config.Price,
ActualIntegral: float64(-config.Price),
TranType: txnmod.IntegralExchangeInKind.Key(),
TranTypeInt: int64(txnmod.IntegralExchangeInKind),
Desc: "积分兑换实物-" + config.Name,
SysType: user.SysType,
RealIntegral: wallet.RealIntegral(),
},
}
if err = txnmod.InsertManyTransactionLog(t, txnLogs); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeInKind Transaction err %s", err.Error()))
return err
}
return nil
}); err != nil {
log.Warn(fmt.Sprintf("HandeExchangeInKind Transaction err %s", err.Error()))
return stderr.BuyFailed //通知消息
}
return stderr.Success
}
func HandleCId(id primitive.ObjectID) string {
return id.Hex() + common.UUID()
}
func GetRecordConfig(uid uint64) []integralconfigmod.AppIntegralRecord {
var data []integralconfigmod.AppIntegralRecord
filter := bson.M{"tranTypeInt": bson.M{"$in": []txnmod.TransType{
txnmod.IntegralExchangeVip,
txnmod.IntegralExchangeAICount,
txnmod.IntegralExchangeGoldCoinBonus,
txnmod.IntegralExchangeGoldWatch,
txnmod.IntegralExchangeInKind,
txnmod.IntegralExchangeFreeCount,
}}, "uid": uid}
op := options.Find().SetSort(bson.D{{"createdAt", -1}})
all, err := txnmod.QueryAll(filter, op)
if err != nil {
return data
}
for _, i := range all {
appdata := integralconfigmod.AppIntegralRecord{
ID: i.ID,
Integral: i.Integral,
Name: i.TranType,
Desc: i.Desc,
Type: int(i.TranTypeInt),
CreatedAt: i.CreatedAt,
}
data = append(data, appdata)
}
return data
}