Files
huangguo_server/models/v/videodiscountmod/videodiscount.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

143 lines
3.3 KiB
Go

package videodiscountmod
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"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/skd/skdg"
"91porn-server/web/webg"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const table = models.VideoDiscount
func coll(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdb.Coll(table)
}
return t.Coll(table)
}
// InitIndex 设置index
func initIndex() {
coll := coll(nil)
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "uid", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "expiration", Value: 1}},
},
}
if _, err := coll.CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
func RefreshDiscountCache(uid uint64) {
redisCachDel(uid)
if _, err := GetByUID(uid); err != nil {
log.Error("RefreshDiscountCache", log.Any("uid", uid), log.E(err))
}
}
func redisCachDel(uid uint64) {
redisKey := redisconst.DataCachKey(table, strconv.FormatUint(uid, 10))
if _, err := getRedis().Del(redisKey); err != nil {
log.Error("redis del user, ", log.Any("err:", err.Error()), log.Any("uid:", uid))
}
}
func getRedis() *redis.Client {
if appg.Redis != nil {
return appg.Redis
}
if webg.Redis != nil {
return webg.Redis
}
if skdg.Redis != nil {
return skdg.Redis
}
return nil
}
// GetByUID 获取用户购买视频折扣卡记录(设置了缓存!)
func GetByUID(uid uint64) (discount VideoDiscountLog, err error) {
redisKey := redisconst.DataCachKey(table, strconv.FormatUint(uid, 10))
redisc := appg.Redis
if redisc == nil || !redisc.Exists(redisKey) {
f := bson.M{"uid": uid}
if err = coll(nil).FindOne(&discount, f); err != nil {
log.Error("GetByUID", log.Any("uid", uid), log.E(err))
return
}
if discount.UID == 0 {
return
}
jsonBytes, err := json.Marshal(discount)
if err != nil {
return discount, err
}
_ = redisc.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire)
return discount, err
}
str, err := redisc.Get(redisKey)
if err != nil {
return discount, err
}
if str == nil {
return discount, errors.New("redis key is null")
}
err = json.Unmarshal([]byte(*str), &discount)
return
}
// GetByUIDAndWeb 获取用户购买视频折扣卡记录(web后台使用, 不用redis)
func GetByUIDAndWeb(uid uint64) (discount VideoDiscountLog, err error) {
f := bson.M{"uid": uid}
if err = coll(nil).FindOne(&discount, f); err != nil {
log.Error("GetByUIDAndWeb", log.Any("uid", uid), log.E(err))
return
}
if discount.UID == 0 {
return
}
return discount, err
}
func Upsert(t *db.MongoTool, set *EditSelector) (err error) {
defer RefreshDiscountCache(*set.UID)
set.UpdatedAt = time.Now()
res, err := coll(t).UpsertOne(
bson.M{"uid": set.UID},
bson.M{
"$set": set,
"$setOnInsert": bson.M{
"createdAt": set.UpdatedAt,
},
},
)
if err != nil {
log.Error("Upsert", log.Any("set", set), log.E(err))
return
}
if res.ModifiedCount+res.UpsertedCount < 1 {
err = errors.New("upsert fail")
log.Warn("Upsert", log.Any("set", set), log.Any("res", res))
}
return
}