aozhiwei df0226e113 1
2024-05-26 10:29:08 +08:00

178 lines
3.8 KiB
Go

package mail
import (
"q5"
"f5"
"sync"
"main/common"
"main/constant"
)
type attachment struct {
itemId int32 `json:"itemid"`
itemNum int32 `json:"itemnum"`
}
type mail struct {
mailId int64
mailType int32
subject string
content string
sendTime int32
expireTime int32
userRegStartTime int32
userRegEndTime int32
attachments []*attachment
recipients *sync.Map //account_id, int
userGroups *sync.Map //group_id, *userGroup
}
func (this *mail) init() {
this.attachments = []*attachment{}
}
func (this *mail) loadFromDb(ds *f5.DataSet) {
this.mailId = q5.ToInt64(ds.GetByName("mail_id"))
this.mailType = q5.ToInt32(ds.GetByName("mail_type"))
this.subject = ds.GetByName("subject")
this.content = ds.GetByName("content")
this.sendTime = q5.ToInt32(ds.GetByName("sendtime"))
this.expireTime = q5.ToInt32(ds.GetByName("expiretime"))
this.userRegStartTime = q5.ToInt32(ds.GetByName("user_reg_start_time"))
this.userRegEndTime = q5.ToInt32(ds.GetByName("user_reg_end_time"))
{
attachmentsStr := ds.GetByName("attachments")
if attachmentsStr != "" {
if err := q5.DecodeJson(attachmentsStr, &this.attachments); err != nil {
panic("mail.loadFromDb parse attachments error " + q5.ToString(this.mailId))
}
}
}
{
recipientsStr := ds.GetByName("recipients")
if recipientsStr != "" {
recipientsList := []string{}
if err := q5.DecodeJson(recipientsStr, recipientsList); err != nil {
panic("mail.loadFromDb parse recipients error " + q5.ToString(this.mailId))
}
/*
for _, recipient := range(recipientsList) {
//this.recipients[recipient] = 1
}*/
}
}
}
func (this *mail) isType(mailType int32) bool {
return this.mailType == mailType
}
func (this *mail) IsValid(hum common.Player) bool {
if f5.GetApp().GetRealSeconds() < int64(this.expireTime) &&
hum.GetRegisterTime() >= this.userRegStartTime &&
hum.GetRegisterTime() <= this.userRegEndTime {
if this.mailType == constant.MAIL_TYPE_GROUP {
return this.isMailUser(hum.GetAccountId())
} else if this.mailType == constant.MAIL_TYPE_ALL {
return true
}
}
return false
}
func (this *mail) GetMailId() int64 {
return this.mailId
}
func (this *mail) GetExpireTime() int32 {
return this.expireTime
}
func (this *mail) fillMailDto(hum common.Player, p *common.MailDto) bool {
p.MailId = q5.ToString(this.mailId)
p.From = ""
p.To = ""
p.Subject = this.subject
p.Content = this.content
//p.Flags
if !hum.IsUnread(this) {
p.Flags = 1 << 0
} else {
p.Flags = 0
}
p.SendTime = this.sendTime
p.ExpireTime = this.expireTime
p.MailType = this.mailType
p.MailSubType = 0
p.Ext = ""
p.Attachments = make([]*common.AttachmentDto, 0)
for _, ele := range(this.attachments) {
attachment := new(common.AttachmentDto)
attachment.ItemId = ele.itemId
attachment.ItemNum = ele.itemNum
}
return true
}
func (this* mail) isMailUser(accountId string) bool {
{
p := this.recipients
if p != nil {
if _, ok := p.Load(accountId); ok {
return true
}
}
}
{
found := false
this.traverseUserGroup(
func (groupId int64, group *userGroup) bool {
if group != nil {
if _, ok := group.userHash.Load(accountId); ok {
found = true
return false
}
}
return true
})
if found {
return true
}
}
return false
}
func (this *mail) traverseRecipients(cb func(string) bool) {
p := this.recipients
if p != nil {
p.Range(func (k, v interface{}) bool {
return cb(k.(string))
})
}
}
func (this *mail) traverseUserGroup(cb func(int64, *userGroup) bool) {
p := this.userGroups
if p != nil {
p.Range(func (k, v interface{}) bool {
if v != nil {
return cb(k.(int64), v.(*userGroup))
} else {
g := _mailMgr.getGroup(k.(int64))
if g != nil {
p.Store(k, g)
return cb(k.(int64), g)
} else {
return cb(k.(int64), nil)
}
}
})
}
}
func newMail() *mail {
p := new(mail)
p.init()
return p
}