@@ -0,0 +1,93 @@
|
||||
package activitymod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/v/prizemod"
|
||||
|
||||
"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 userRecordDB *db.MongoDB
|
||||
|
||||
func collUserRecord(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return userRecordDB.Coll(models.ActivityUserRecord)
|
||||
}
|
||||
return t.Coll(models.ActivityUserRecord)
|
||||
}
|
||||
|
||||
// initUserFreeIndex 索引设置
|
||||
func initUserRecordIndex() {
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "activityId", Value: 1}, {Key: "uid", Value: 1}, {Key: "createTime", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createTime", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := collUserRecord(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", models.ActivityUserRecord, err))
|
||||
}
|
||||
}
|
||||
|
||||
func AddMany(records []UserRecord) error {
|
||||
result, err := collUserRecord(nil).InsertMany(&records)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddMany", models.ActivityUserRecord, "InsertMany", err),
|
||||
log.Any("records", records),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if len(result.InsertedIDs) != len(records) {
|
||||
err = errors.New("activity InsertMany InsertedIDs err")
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddMany", models.ActivityUserRecord, "len(result.InsertedIDs) != len(records)", err),
|
||||
log.Any("records", records),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func QueryAllRecord(filter primitive.M, opts ...*options.FindOptions) ([]*UserRecord, error) {
|
||||
var out []*UserRecord = []*UserRecord{}
|
||||
if err := collUserRecord(nil).Find(&out, filter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllRecord", models.ActivityUserRecord, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// 查询奖品总条数
|
||||
func CountRecord(filter primitive.M) (int64, error) {
|
||||
count, err := collUserRecord(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "CountRecord", models.ActivityUserRecord, "Count", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func GetMarquee(activityId primitive.ObjectID) ([]*UserRecord, error) {
|
||||
filter := bson.M{"activityId": activityId, "level": prizemod.High}
|
||||
var out []*UserRecord = []*UserRecord{}
|
||||
if err := collUserRecord(nil).Find(&out, filter, options.Find().SetLimit(10).SetSort(bson.M{"createTime": -1})); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllRecord", models.ActivityUserRecord, "Find", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user