200 lines
4.5 KiB
Go
200 lines
4.5 KiB
Go
package mail
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"main/common"
|
|
"main/constant"
|
|
"sync"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type userGroup struct {
|
|
groupId int64
|
|
userHash sync.Map
|
|
}
|
|
|
|
type mailMgr struct {
|
|
idHash sync.Map
|
|
wholeMails sync.Map
|
|
groupMails sync.Map
|
|
personalMails sync.Map
|
|
groupHash sync.Map
|
|
}
|
|
|
|
func (this *mailMgr) Init() {
|
|
this.loadMails()
|
|
this.loadGroups()
|
|
this.loadGroupMembers()
|
|
}
|
|
|
|
func (this *mailMgr) UnInit() {
|
|
}
|
|
|
|
func (this *mailMgr) loadMails() {
|
|
f5.GetSysLog().Info("mailMgr.loadMails begin")
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.MAIL_DB,
|
|
"SELECT * FROM t_mail WHERE idx > %d AND deleted = 0 AND expiretime > " + q5.ToString(nowTime),
|
|
func (ds *f5.DataSet) {
|
|
p := newMail()
|
|
p.loadFromDb(ds)
|
|
this.addMail(p)
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("mailMgr.loadMails dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("mailMgr.loadMails end lastIdx:%d mailNum:%d",
|
|
lastIdx,
|
|
q5.GetSyncMapSize(this.idHash))
|
|
}
|
|
|
|
func (this *mailMgr) loadGroups() {
|
|
f5.GetSysLog().Info("mailMgr.loadGroups begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.MAIL_DB,
|
|
"SELECT * FROM t_group WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
groupId := q5.ToInt64(ds.GetByName("group_id"))
|
|
if this.getGroup(groupId) != nil {
|
|
panic(fmt.Sprintf("mailMgr.loadGroups group_id error"))
|
|
}
|
|
p := new(userGroup)
|
|
p.groupId = groupId
|
|
this.addGroup(p)
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("mailMgr.loadGroups dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("mailMgr.loadGroups end lastIdx:%d mailNum:%d",
|
|
lastIdx,
|
|
q5.GetSyncMapSize(this.idHash))
|
|
}
|
|
|
|
func (this *mailMgr) loadGroupMembers() {
|
|
f5.GetSysLog().Info("mailMgr.loadGroupMembers begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.MAIL_DB,
|
|
"SELECT * FROM t_member WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
groupId := q5.ToInt64(ds.GetByName("group_id"))
|
|
memberId := ds.GetByName("member_id")
|
|
p := this.getGroup(groupId)
|
|
if p != nil {
|
|
p.userHash.Store(memberId, q5.ToInt64(ds.GetByName("idx")))
|
|
}
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("mailMgr.loadGroupMembers dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("mailMgr.loadGroupMembers end lastIdx:%d mailNum:%d",
|
|
lastIdx,
|
|
q5.GetSyncMapSize(this.idHash))
|
|
}
|
|
|
|
func (this *mailMgr) caGetMailList(hum common.Player, c *gin.Context) {
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
MailList []*common.MailDto `json:"maillis"`
|
|
}{}
|
|
this.traversePlayerMail(
|
|
hum,
|
|
func (m *mail) bool {
|
|
if m.isValid(hum) {
|
|
mailDto := new(common.MailDto)
|
|
if m.fillMailDto(mailDto) {
|
|
q5.AppendSlice(&rspObj.MailList, mailDto)
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
c.JSON(200, rspObj)
|
|
}
|
|
|
|
func (this *mailMgr) caMarkMail(hum common.Player, c *gin.Context) {
|
|
mailIds := q5.StrSplit(c.DefaultQuery("mail_ids", ""), ",")
|
|
for _, str := range(mailIds) {
|
|
m := this.getMail(str)
|
|
if m != nil {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *mailMgr) caGetUnreadMailCnt(hum common.Player, c *gin.Context) {
|
|
|
|
}
|
|
|
|
func (this *mailMgr) caGetAttachment(hum common.Player, c *gin.Context) {
|
|
|
|
}
|
|
|
|
func (this *mailMgr) caDeleteMails(hum common.Player, c *gin.Context) {
|
|
|
|
}
|
|
|
|
func (this *mailMgr) traversePlayerMail(hum common.Player, cb func(*mail) bool) {
|
|
stop := false
|
|
traversFunc := func (k, v interface{}) bool {
|
|
m := v.(*mail)
|
|
if m.isValid(hum) {
|
|
if !(cb(m)) {
|
|
stop = true
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
this.wholeMails.Range(traversFunc)
|
|
if stop {
|
|
return
|
|
}
|
|
this.groupMails.Range(traversFunc)
|
|
if stop {
|
|
return
|
|
}
|
|
if p, ok := this.personalMails.Load(hum.GetAccountId()); ok {
|
|
(p.(*sync.Map)).Range(traversFunc)
|
|
}
|
|
}
|
|
|
|
func (this *mailMgr) addMail(m *mail) {
|
|
this.idHash.Store(m.mailId, m)
|
|
if m.isType(constant.MAIL_TYPE_ALL) {
|
|
this.wholeMails.Store(m.mailId, m)
|
|
} else if m.isType(constant.MAIL_TYPE_GROUP) {
|
|
this.groupMails.Store(m.mailId, m)
|
|
} else if m.isType(constant.MAIL_TYPE_PERSONAL) {
|
|
if p, ok := this.personalMails.Load(m.reciver); ok {
|
|
(p.(*sync.Map)).Store(m.mailId, m)
|
|
} else {
|
|
p := new(sync.Map)
|
|
p.Store(m.mailId, m)
|
|
this.personalMails.Store(m.reciver, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *mailMgr) addGroup(g *userGroup) {
|
|
this.groupHash.Store(g.groupId, g)
|
|
}
|
|
|
|
func (this *mailMgr) getGroup(groupId int64) *userGroup {
|
|
if p, ok := this.groupHash.Load(groupId); ok {
|
|
return p.(*userGroup)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *mailMgr) getMail(mailId string) *mail {
|
|
if p, ok := this.idHash.Load(mailId); ok {
|
|
return p.(*mail)
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|