1 Star 0 Fork 0

liuxuezhan / mylib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DB_Mail.go 4.43 KB
一键复制 编辑 原始数据 按行查看 历史
liuxuezhan 提交于 2020-09-04 12:57 . Initial commit
package GameDB
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
Mail *MailObject
EveryOneMail *MailObject
)
//邮件id
type MailIdDocument struct {
Id uint64 "_id"
NextId uint32 "next_id"
}
//邮件文档
type MailDocument struct {
Id uint64 "_id"
Type uint32 "type"
TimeStamp uint64 "timestamp"
SenderId uint64 "sender_id"
ReceiverId uint64 "receiver_id"
SenderName string "sender_name"
Title []byte "title"
Content []byte "content"
TitleTips []byte "title_tips"
ContentTips []byte "content_tips"
TargetId int32 "target_id"
OtherId uint64 "other_id"
OtherName string "other_name"
OtherAvatar string "other_avatar"
NPCId uint64 "npc_id"
Reward []byte "reward"
Detail []byte "detail"
Battle []byte "battle"
Read bool "read"
Kept bool "kept"
Locked bool "locked"
Removed bool "removed"
ServerId uint32 "server_id"
GiftPackId int32 "gift_pack_id"
MailSource int32 "mail_source"
IsMiracleWarGift bool "miracle_war_gift"
TitleID int32 "title_id"
RewardSource int32 "reward_source"
}
//邮件
type MailObject struct {
DBObject
mailIdCollectionName string //邮件id集合名
mailsCollectionName string //邮件集合名
}
//初始化
func (object *MailObject) Init(dbName, configCollectionName, mailIdCollectionName, mailsCollectionName string) {
object.dbName = dbName
object.mailIdCollectionName = mailIdCollectionName
object.mailsCollectionName = mailsCollectionName
object.getById(configCollectionName, 0, &object.Config)
indexes := make([]mgo.Index, 0)
indexes = append(indexes, // 已读
mgo.Index{
Key: []string{"_id", "read"},
Background: true,
})
indexes = append(indexes, // 已读/已移除
mgo.Index{
Key: []string{"_id", "read", "remove"},
Background: true,
})
indexes = append(indexes, // 以发送者分类
mgo.Index{
Key: []string{"sender_id"},
Background: true,
})
indexes = append(indexes, // 发送者,保护邮件
mgo.Index{
Key: []string{"sender_id", "kept"},
Background: true,
})
indexes = append(indexes, // 发送者,邮件分类
mgo.Index{
Key: []string{"sender_id", "type"},
Background: true,
})
indexes = append(indexes, // 发送者,已读
mgo.Index{
Key: []string{"sender_id", "read"},
Background: true,
})
indexes = append(indexes, // 发信时间
mgo.Index{
Key: []string{"-timestamp"},
Background: true,
})
object.ensureIndex(mailsCollectionName, indexes)
}
//得到一个新的全局邮件id(直接写进数据库,然后拿出来用)
func (object *MailObject) GetGlobalMailId() (error, uint64) {
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"next_id": 1}},
ReturnNew: true,
}
doc := &MailIdDocument{}
if err := object.findAndModify(object.mailIdCollectionName, 0, change, doc); nil != err {
return err, 0
}
return nil, uint64(doc.NextId)
}
//获取邮件详情
func (object *MailObject) GetMailById(mailId uint64) (error, *MailDocument) {
doc := MailDocument{}
if err := object.getById(object.mailsCollectionName, mailId, &doc); nil != err {
return err, nil
}
return nil, &doc
}
// 全局邮件表迭代
func (object *MailObject) ForEach(callback func(document *MailDocument)) {
doc := MailDocument{}
query := bson.M{"removed": false}
object.foreach(object.mailsCollectionName, query, "", &doc, func(err error, succeeded bool, value interface{}) {
if nil == err && succeeded {
callback(&doc)
}
})
}
//更新
func (object *MailObject) UpsertMailWithId(id uint64, update interface{}) error {
return object.upsertById(object.mailsCollectionName, id, update)
}
func (object *MailObject) DeleteById(id uint64) error {
return object.removeById(object.mailsCollectionName, id)
}
////>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//初始化
func initializeMailSingleton(dbName, configCollectionName, mailIdCollectionName, mailsCollectionName, everyoneMailCollectionName string) {
Mail = &MailObject{}
Mail.Init(dbName, configCollectionName, mailIdCollectionName, mailsCollectionName)
EveryOneMail = &MailObject{}
EveryOneMail.Init(dbName, configCollectionName, mailIdCollectionName, everyoneMailCollectionName)
}
1
https://gitee.com/liuxuezhan/mylib.git
git@gitee.com:liuxuezhan/mylib.git
liuxuezhan
mylib
mylib
v1.1.3

搜索帮助