@@ -0,0 +1,71 @@
|
||||
package userdailytasklogmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/v/dailytaskmod"
|
||||
|
||||
"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.UserDailyTaskLog
|
||||
|
||||
// InitIndex 设置index
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "type", Value: 1}, {Key: "date", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
type UserDailyTask struct {
|
||||
ID primitive.ObjectID `json:"_id" bson:"_id"`
|
||||
UID uint64 `json:"uid" bson:"uid"`
|
||||
Type int `json:"type" bson:"type"` // 0 每日广告点击; 1 每日任务
|
||||
Date string `json:"date" bson:"date"` // 日期. 格式 2006-01-02
|
||||
FinishCount uint64 `json:"finishCount" bson:"finishCount"` // 当日完成次数
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
func CompleteDailyTask(uid uint64, taskType int64, completeTime uint64) error {
|
||||
_, err := coll(nil).UpsertOne(bson.M{"uid": uid, "type": taskType, "date": time.Now().Format("2006-01-02")}, bson.M{"$inc": bson.M{"finishCount": completeTime}})
|
||||
return err
|
||||
}
|
||||
|
||||
func GetUserFinishCount(uid uint64, taskType dailytaskmod.DailyTaskTypeEnum) (uint64, error) {
|
||||
var udk UserDailyTask
|
||||
if err := coll(nil).FindOne(&udk, bson.M{"uid": uid, "type": taskType, "date": time.Now().Format("2006-01-02")}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if udk.ID.IsZero() {
|
||||
return 0, nil
|
||||
}
|
||||
return udk.FinishCount, nil
|
||||
}
|
||||
Reference in New Issue
Block a user