@@ -0,0 +1,32 @@
|
||||
package videodiscountmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
type VideoDiscountLog struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
UID uint64 `json:"uid" bson:"uid"` //用户id
|
||||
Expiration time.Time `json:"expiration" bson:"expiration"` //过期时间
|
||||
VideoDiscount int `json:"videoDiscount" bson:"videoDiscount"` //视频折扣率
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //修改时间
|
||||
}
|
||||
|
||||
type EditSelector struct {
|
||||
UID *uint64 `json:"uid" bson:"uid,omitempty"` //用户id
|
||||
Expiration *time.Time `json:"expiration" bson:"expiration,omitempty"` //过期时间
|
||||
VideoDiscount *int `json:"videoDiscount" bson:"videoDiscount,omitempty"` //楼凤折扣率
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //修改时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user