76 lines
1.9 KiB
Go
76 lines
1.9 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,
|
|
}
|
|
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
|
|
}
|