105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
package oncetaskmod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"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.OnceTask
|
|
|
|
// 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 OnceTaskTypeEnum int64
|
|
|
|
const (
|
|
OnceTaskTypeUserBuyVip OnceTaskTypeEnum = 0 // 购买vip
|
|
OnceTaskTypeBindMobile OnceTaskTypeEnum = 1 // 绑定手机号
|
|
Download OnceTaskTypeEnum = 2 // 下载APP
|
|
OnceTaskTypeUserBuyCoin OnceTaskTypeEnum = 3 // 购买金币
|
|
)
|
|
|
|
type OnceTask struct {
|
|
ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
|
|
Title string `json:"title" bson:"title"` // 任务标题
|
|
Desc string `json:"desc" bson:"desc"` // 任务描述
|
|
Img string `json:"img" bson:"img"` // 任务图片
|
|
Prizes []primitive.ObjectID `json:"prizes" bson:"prizes"` // 任务奖励
|
|
Type OnceTaskTypeEnum `json:"type" bson:"type"` // 任务类型. 1 邀请用户; 2 绑定手机号
|
|
FinishCondition uint64 `json:"finishCondition" bson:"finishCondition"` // 达成条件
|
|
Link string `json:"link" bson:"link"` // 跳转链接
|
|
Status bool `json:"status" bson:"status"` // 是否启用
|
|
SortNum int `json:"sortNum" bson:"sortNum"` // 排序
|
|
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
|
}
|
|
|
|
func GetOnceTaskValid(t *db.MongoTool) ([]OnceTask, error) {
|
|
var ot []OnceTask
|
|
return ot, coll(t).Find(&ot, bson.M{"status": true})
|
|
}
|
|
|
|
func GetOnceTaskAll(t *db.MongoTool) ([]OnceTask, error) {
|
|
var ot []OnceTask
|
|
op := options.Find().SetSort(bson.D{{"sortNum", 1}})
|
|
return ot, coll(t).Find(&ot, bson.M{}, op)
|
|
}
|
|
|
|
func GetOnceTaskValidByType(t *db.MongoTool, tType OnceTaskTypeEnum) (*OnceTask, error) {
|
|
var ot OnceTask
|
|
if err := coll(t).FindOne(&ot, bson.M{"status": true, "type": tType}); err != nil {
|
|
return nil, err
|
|
}
|
|
if ot.ID.IsZero() {
|
|
return nil, errors.New("not found")
|
|
}
|
|
return &ot, nil
|
|
}
|
|
|
|
func GetTaskByID(t *db.MongoTool, id primitive.ObjectID) (*OnceTask, error) {
|
|
var dt OnceTask
|
|
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
|
|
}
|