aozhiwei ff4f33b972 1
2024-08-20 13:17:40 +08:00

180 lines
3.9 KiB
Go

package mail
import (
"q5"
"f5"
"sync"
"main/common"
"main/constant"
)
type mail struct {
mailId int64
mailType int32
subject string
content string
sendTime int32
expireTime int32
userRegStartTime int32
userRegEndTime int32
attachments []*common.AttachmentDto
recipients *sync.Map //account_id, int
userGroups *sync.Map //group_id, *userGroup
}
func (this *mail) init() {
this.attachments = []*common.AttachmentDto{}
}
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))
}
if len(recipientsList) > 0 {
this.recipients = new(sync.Map)
for _, recipient := range(recipientsList) {
this.recipients.Store(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) HasAttachment() bool {
return len(this.attachments) > 0
}
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 = this.attachments
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 (this *mail) TraverseAttachment(cb func(int32, int32)) {
for _, val := range this.attachments {
cb(val.ItemId, val.ItemNum)
}
}
func newMail() *mail {
p := new(mail)
p.init()
return p
}