@@ -0,0 +1,39 @@
|
||||
package videocoupon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
const table = models.VideoCoupon
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "num", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package videocoupon
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 观影券获得渠道
|
||||
type GoldVideoCouponSource string
|
||||
|
||||
const GoldVideoCouponSourceVIP GoldVideoCouponSource = "VIP" // 购买VIP卡获得赠送观影券
|
||||
const GoldVideoCouponSourceSign GoldVideoCouponSource = "SIGN" // 签到赠送观影券
|
||||
const GoldVideoCouponSourceExchange GoldVideoCouponSource = "EXCHANGE" // 兑换码获得观影券
|
||||
|
||||
// UserGoldVideoCoupon 用户购买观影券
|
||||
type UserGoldVideoCoupon struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //ID
|
||||
UID uint64 `json:"uid" bson:"uid"` //用户ID
|
||||
Num int `json:"num" bson:"num"` //赠送观影券金币数量
|
||||
Source GoldVideoCouponSource `json:"source" bson:"source"` //观影券获得渠道
|
||||
Used bool `json:"used" bson:"used"` //观影券是否已使用:false-未使用;true-已使用
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
type EditSelector struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //ID
|
||||
Used bool `json:"used" bson:"used"` //观影券是否已使用:false-未使用;true-已使用
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package videocoupon
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/stderr"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// InsertOne ...
|
||||
func InsertOne(coupon UserGoldVideoCoupon) error {
|
||||
_, err := coll(nil).InsertOne(coupon)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertMany batch insert
|
||||
func InsertMany(coupons []UserGoldVideoCoupon) error {
|
||||
_, err := coll(nil).InsertMany(coupons)
|
||||
return err
|
||||
}
|
||||
|
||||
func InsertManyTrans(t *db.MongoTool, coupons []UserGoldVideoCoupon) error {
|
||||
_, err := coll(t).InsertMany(coupons)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetUserUnusedCouponCount 获取用户未使用的观影券总数
|
||||
func GetUserUnusedCouponCount(uid uint64) (int64, error) {
|
||||
filter := bson.M{"uid": uid, "used": false}
|
||||
return coll(nil).Count(filter)
|
||||
}
|
||||
|
||||
// GetCouponsByUID 根据uid获取观影券
|
||||
func GetCouponsByUID(uid uint64) (list []UserGoldVideoCoupon, err error) {
|
||||
filter := bson.M{"uid": uid}
|
||||
err = coll(nil).Find(&list, filter)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByUIDAndCouponNum 根据uid和观影券面值查询观影券
|
||||
func GetByUIDAndCouponNum(uid uint64, num int) (list []UserGoldVideoCoupon, err error) {
|
||||
filter := bson.M{"uid": uid, "num": num, "used": false}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}})
|
||||
err = coll(nil).Find(&list, filter)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateCoupon 更新用户观影券
|
||||
func UpdateCoupon(edit EditSelector) error {
|
||||
res, err := coll(nil).UpdateOne(bson.M{"_id": edit.ID}, bson.M{"$set": edit})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.ModifiedCount+res.UpsertedCount < 1 {
|
||||
return errors.New("update failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UseOneCoupon 使用一张观影券
|
||||
func UseOneCoupon(coupons []UserGoldVideoCoupon) error {
|
||||
if len(coupons) == 0 {
|
||||
return stderr.GoldVideoCoupleNotExist
|
||||
}
|
||||
sort.Slice(coupons, func(i, j int) bool { // 优先使用最早创建的观影券
|
||||
return coupons[i].CreatedAt.After(coupons[j].CreatedAt)
|
||||
})
|
||||
edit := EditSelector{
|
||||
ID: coupons[0].ID,
|
||||
Used: true,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
return UpdateCoupon(edit)
|
||||
}
|
||||
Reference in New Issue
Block a user