119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package goldextramod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"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
|
|
|
|
const table = models.GoldExtra // 金币加赠券表
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
// ActInitIndex 索引设置
|
|
func initIndex() {
|
|
coll := coll(nil)
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func InsertGoldExtra(t *db.MongoTool, ge *GoldExtra) error {
|
|
_, err := coll(t).InsertOne(ge)
|
|
return err
|
|
}
|
|
|
|
func InsertGoldExtras(t *db.MongoTool, ges *[]GoldExtra) error {
|
|
_, err := coll(t).InsertMany(ges)
|
|
return err
|
|
}
|
|
|
|
// 返回所有用户金币加购券
|
|
func GetUserGoldExtra(t *db.MongoTool, uid uint64, skip, limit uint64) ([]GoldExtra, error) {
|
|
var ges []GoldExtra
|
|
opt := &options.FindOptions{
|
|
Sort: bson.D{{Key: "used", Value: 1}, {Key: "expired", Value: 1}}, // 未使用的排在前列
|
|
}
|
|
opt.SetSkip(int64(skip))
|
|
opt.SetLimit(int64(limit))
|
|
if err := coll(t).Find(&ges, bson.M{"uid": uid}, opt); err != nil {
|
|
return nil, err
|
|
}
|
|
return ges, nil
|
|
}
|
|
|
|
// 返回所有可用的金币加购券
|
|
func GetUserGoldExtraValid(t *db.MongoTool, uid uint64, skip, limit uint64) ([]GoldExtra, error) {
|
|
var ges []GoldExtra
|
|
opt := &options.FindOptions{
|
|
Sort: bson.D{{Key: "expired", Value: 1}},
|
|
}
|
|
opt.SetSkip(int64(skip))
|
|
opt.SetLimit(int64(limit))
|
|
return ges, coll(t).Find(&ges, bson.M{"uid": uid, "used": false, "expired": bson.M{"$gt": time.Now()}}, opt)
|
|
}
|
|
|
|
// 返回所有已过期金币加购券
|
|
func GetUserGoldExtraExpired(t *db.MongoTool, uid uint64, skip, limit uint64) ([]GoldExtra, error) {
|
|
var ges []GoldExtra
|
|
opt := &options.FindOptions{
|
|
Sort: bson.D{{Key: "expired", Value: -1}},
|
|
}
|
|
opt.SetSkip(int64(skip))
|
|
opt.SetLimit(int64(limit))
|
|
return ges, coll(t).Find(&ges, bson.M{"uid": uid, "expired": bson.M{"$lte": time.Now()}}, opt)
|
|
}
|
|
|
|
// 返回所有已使用金币加购券
|
|
func GetUserGoldExtraUsed(t *db.MongoTool, uid uint64, skip, limit uint64) ([]GoldExtra, error) {
|
|
var ges []GoldExtra
|
|
opt := &options.FindOptions{
|
|
// Sort: bson.D{{Key: "expired", Value: -1}},
|
|
}
|
|
opt.SetSkip(int64(skip))
|
|
opt.SetLimit(int64(limit))
|
|
return ges, coll(t).Find(&ges, bson.M{"uid": uid, "used": true}, opt)
|
|
}
|
|
|
|
func GetUserGoldExtraByID(t *db.MongoTool, id primitive.ObjectID) (*GoldExtra, error) {
|
|
var ge *GoldExtra
|
|
return ge, coll(t).FindOne(&ge, bson.M{"_id": id})
|
|
}
|
|
|
|
func UseGoldExtra(t *db.MongoTool, uid uint64, id primitive.ObjectID) error {
|
|
now := time.Now()
|
|
result, err := coll(t).UpdateOne(bson.M{"_id": id, "uid": uid, "used": false, "expired": bson.M{"$gte": now}}, bson.M{"$set": bson.M{"used": true, "updatedAt": now}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.ModifiedCount != 1 {
|
|
return errors.New("无效的金币加购券")
|
|
}
|
|
return nil
|
|
}
|