129 lines
3.5 KiB
Go
129 lines
3.5 KiB
Go
package user
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
// 签到
|
|
type ModelSign struct {
|
|
modules.MCompModel
|
|
module *User
|
|
}
|
|
|
|
func (this *ModelSign) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableSign
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.module = module.(*User)
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
func (this *ModelSign) GetUserSign(uid string) (result *pb.DBSign, err error) {
|
|
result = &pb.DBSign{}
|
|
if err = this.module.modelSign.Get(uid, result); err != nil {
|
|
if mongo.ErrNoDocuments == err { // 创建一条新的数据
|
|
_data := this.module.configure.GetSignConf(1, 1)
|
|
if _data != nil {
|
|
result = &pb.DBSign{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: uid,
|
|
Group: _data.Group,
|
|
Puzzle: make([]int32, 31),
|
|
}
|
|
this.Add(uid, result)
|
|
} else {
|
|
err = errors.New("conf err")
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
err = nil
|
|
return result, err
|
|
}
|
|
|
|
//修改用户签到数据
|
|
func (this *ModelSign) ChangeUserSign(uid string, value map[string]interface{}) (err error) {
|
|
if len(value) == 0 {
|
|
return nil
|
|
}
|
|
return this.module.modelSign.Change(uid, value)
|
|
}
|
|
|
|
// 累计登录天数
|
|
func (this *ModelSign) updateSignData(uid string, sign *pb.DBSign) (err error) {
|
|
if !utils.IsToday(sign.SignTime) {
|
|
sign.SignCount += 1
|
|
count := sign.SignCount
|
|
update := map[string]interface{}{
|
|
"signCount": count,
|
|
"signTime": configure.Now().UTC(),
|
|
}
|
|
this.ChangeUserSign(uid, update)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *ModelSign) UserSign(session comm.IUserSession) {
|
|
var (
|
|
update map[string]interface{}
|
|
lastSignTime int64 // 上次签到时间
|
|
)
|
|
update = make(map[string]interface{}, 0)
|
|
|
|
if sign, err := this.module.modelSign.GetUserSign(session.GetUserId()); err == nil {
|
|
// 获取当前是第几天
|
|
lastSignTime = sign.SignTime
|
|
sign.SignTime = configure.Now().Unix()
|
|
|
|
start, _ := utils.GetMonthStartEnd()
|
|
iDay := (configure.Now().Unix() - start) / (24 * 3600)
|
|
if sign.SignTime < start { // 重置
|
|
sign.SignCount = 1
|
|
if newGroup := this.module.configure.GetSignResetConf(sign.Group + 1); newGroup != -1 { // 获取当前的组id
|
|
sign.Group = newGroup
|
|
sign.SignCount = 1
|
|
update["group"] = newGroup
|
|
}
|
|
sign.Puzzle = make([]int32, 31)
|
|
} else {
|
|
if sign.Puzzle[iDay] != 0 {
|
|
return
|
|
}
|
|
|
|
if utils.IsYestoday(lastSignTime) {
|
|
sign.SignCount += 1
|
|
} else {
|
|
sign.SignCount = 1
|
|
}
|
|
}
|
|
sign.Day = int32(iDay)
|
|
//sign.Puzzle[iDay] = 1
|
|
update["signTime"] = sign.SignTime
|
|
update["signCount"] = sign.SignCount
|
|
update["day"] = sign.Day
|
|
//update["puzzle"] = sign.Puzzle
|
|
this.module.modelSign.Change(session.GetUserId(), update)
|
|
// 版本需求 去掉签到奖励
|
|
// _data := this.module.configure.GetSignConf(sign.Day, sign.Group)
|
|
// if _data != nil { // 发奖
|
|
// this.module.DispenseRes(session, _data.Loopgift, true) // 签到奖励
|
|
// // 额外奖励
|
|
// if conf := this.module.configure.GetSignExtarConf(sign.SignCount, sign.Group); conf != nil {
|
|
// this.module.DispenseRes(session, conf.Extra, true) // 签到额外奖励
|
|
// }
|
|
// }
|
|
}
|
|
}
|