@@ -0,0 +1,128 @@
|
||||
package dailytaskmod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.DailyTask
|
||||
|
||||
// InitIndex 设置index
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).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 DailyTaskTypeEnum int64
|
||||
|
||||
const (
|
||||
DailyTaskTypeAdsClick DailyTaskTypeEnum = 0 // 每日广告点击
|
||||
DailyTaskTypeUserInvite DailyTaskTypeEnum = 1 // 每日邀请
|
||||
DailyUserLogin DailyTaskTypeEnum = 2 // 每日登录
|
||||
DailyComment DailyTaskTypeEnum = 3 // 每日评论
|
||||
DailyPublish DailyTaskTypeEnum = 4 // 每日发布
|
||||
CommentSetGod DailyTaskTypeEnum = 5 // 评论被设置成神评
|
||||
DailyDoCollect DailyTaskTypeEnum = 6 // 每日收藏
|
||||
DailyUnlockByGold DailyTaskTypeEnum = 7 // 每日解锁(video/acg)
|
||||
)
|
||||
|
||||
type DailyTask struct {
|
||||
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
|
||||
Title string `json:"title" bson:"title"` // 任务标题
|
||||
Img string `json:"img" bson:"img"` // 任务图片
|
||||
Type DailyTaskTypeEnum `json:"type" bson:"type"` // 0 每日广告点击; 1 每日邀请
|
||||
Desc string `json:"desc" bson:"desc"` // 任务说明
|
||||
Detail []TaskDetail `json:"detail" bson:"detail"` // 任务详情
|
||||
Status bool `json:"status" bson:"status"` // 启用/禁用
|
||||
Link string `json:"link" bson:"link"` // 跳转链接
|
||||
SortNum int `json:"sortNum" bson:"sortNum"` // 排序
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type TaskDetail struct {
|
||||
Prizes []primitive.ObjectID `json:"prizes" bson:"prizes"` // 奖品列表
|
||||
FinishCondition uint64 `json:"finishCondition" bson:"finishCondition"` // 达成条件
|
||||
}
|
||||
|
||||
func GetUserDailyTasks(t *db.MongoTool) ([]DailyTask, error) {
|
||||
var ds []DailyTask
|
||||
op := options.Find().SetSort(bson.D{{"sortNum", 1}})
|
||||
return ds, coll(t).Find(&ds, bson.M{"status": true}, op)
|
||||
}
|
||||
func GetTasks(filter bson.M, opt *options.FindOptions) ([]DailyTask, bool, error) {
|
||||
var ds []DailyTask
|
||||
if opt == nil {
|
||||
opt = options.Find()
|
||||
}
|
||||
if opt.Limit == nil || *opt.Limit == 0 {
|
||||
opt.SetLimit(101)
|
||||
}
|
||||
|
||||
err := coll(nil).Find(&ds, filter, opt)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
next := len(ds) == int(*opt.Limit)
|
||||
if next {
|
||||
ds = ds[:*opt.Limit-1]
|
||||
}
|
||||
return ds, true, nil
|
||||
}
|
||||
|
||||
func GetUserDailyTaskByType(t *db.MongoTool, taskType DailyTaskTypeEnum) (*DailyTask, error) {
|
||||
var dt DailyTask
|
||||
if err := coll(t).FindOne(&dt, bson.M{"type": taskType, "status": true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dt.ID.IsZero() {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
if !dt.Status {
|
||||
return nil, errors.New("task not active")
|
||||
}
|
||||
return &dt, nil
|
||||
}
|
||||
|
||||
func GetUserDailyTaskByID(t *db.MongoTool, id primitive.ObjectID) (*DailyTask, error) {
|
||||
var dt DailyTask
|
||||
if err := coll(t).FindOne(&dt, bson.M{"_id": id}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dt.ID.IsZero() {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
if !dt.Status {
|
||||
return nil, errors.New("task not active")
|
||||
}
|
||||
return &dt, nil
|
||||
}
|
||||
Reference in New Issue
Block a user