aozhiwei aadbf4f8fe 1
2024-04-22 11:10:48 +08:00

120 lines
2.1 KiB
Go

package player
import (
"q5"
"f5"
"sync"
"main/common"
"main/constant"
"main/model"
)
type inbox struct {
mailId int64
state int32
expireTime int32
}
type player struct {
lock sync.Mutex
accountId string
sessionId string
registerTime int32
loaded bool
inboxHash map[int64]*inbox
}
func (this *player) init() {
this.inboxHash = make(map[int64]*inbox)
}
func (this *player) Lock() {
this.lock.Lock()
if !this.loaded {
this.load()
}
}
func (this *player) UnLock() {
this.lock.Unlock()
}
func (this *player) GetAccountId() string {
return this.accountId
}
func (this *player) MarkMails(mails []common.Mail) error {
this.checkLock()
var resultErr error
var nowTime int64
for _, m := range(mails) {
if m.IsValid(this) {
mi := this.getInbox(m.GetMailId())
if mi == nil {
err := model.Inbox.Mark(this.GetAccountId(), m.GetMailId(), nowTime)
if err != nil {
resultErr = err
break
}
mi = new(inbox)
}
}
}
return resultErr
}
func (this *player) GetAttachment(mails []common.Mail) error {
this.checkLock()
var resultErr error
return resultErr
}
func (this *player) DeleteMails(mails []common.Mail) error {
this.checkLock()
var resultErr error
return resultErr
}
func (this *player) checkLock() {
if !this.lock.TryLock() {
panic("player checkLock error")
}
}
func (this *player) load() {
f5.GetGoStyleDb().RawQuery(
constant.MAIL_DB,
"SELECT * FROM t_inbox WHERE account_id=? AND expiretime>?",
[]string{
this.GetAccountId(),
q5.ToString(f5.GetApp().GetNowSeconds() - 3600 * 24 * 1),
},
func (err error, ds *f5.DataSet) {
if err != nil {
return
}
for ds.Next() {
p := new(inbox)
p.mailId = q5.ToInt64(ds.GetByName("mail_id"))
p.state = q5.ToInt32(ds.GetByName("state"))
p.expireTime = q5.ToInt32(ds.GetByName("expiretime"))
this.inboxHash[p.mailId] = p
}
this.loaded = true
})
}
func (this *player) getInbox(mailId int64) *inbox {
if m, ok := this.inboxHash[mailId]; ok {
return m
} else {
return nil
}
}
func newPlayer() *player {
p := new(player)
p.init()
return p
}