go_dreamfactory/modules/mail/module.go

176 lines
4.5 KiB
Go

package mail
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"time"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go.mongodb.org/mongo-driver/bson/primitive"
)
/*
模块名:Mail
描述:邮件系统模块
开发:梅雄风
*/
func NewModule() core.IModule {
m := new(Mail)
return m
}
type Mail struct {
modules.ModuleBase
api *apiComp
modelMail *modelMail
configure_comp *Configure_Comp
service base.IRPCXService
}
func (this *Mail) GetType() core.M_Modules {
return comm.ModuleMail
}
func (this *Mail) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelMail = this.RegisterComp(new(modelMail)).(*modelMail)
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
}
func (this *Mail) CreateNewMail(session comm.IUserSession, mail *pb.DBMailData) bool {
t := configure.Now()
defer func() {
log.Debugf("创建邮件 耗时:%v", time.Since(t))
}()
if mail == nil {
return false
}
if db.IsCross() { // 如果是跨服 则取本服的db
tag, _, b := utils.UIdSplit(session.GetUserId())
if b {
if conn, err := db.ServerDBConn(tag); err == nil {
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
mail.ObjId = primitive.NewObjectID().Hex()
mail.Check = false
mail.Reward = true
if len(mail.GetItems()) > 0 {
mail.Reward = false
}
_, err = dbModel.DB.InsertOne(comm.TableMail, mail)
}
}
} else {
err := this.modelMail.MailInsertUserMail(mail)
if err != nil {
this.Errorf("create mail failed :%v", err)
return false
}
}
// 通知玩家
this.AddNewMailPush(session, mail)
return true
}
func (this *Mail) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
return
}
func (this *Mail) Start() (err error) {
err = this.ModuleBase.Start()
this.service.RegisterFunctionName(string(comm.Rpc_Mail), this.Rpc_Mail)
return
}
// 获得新邮件 推送给玩家
func (this *Mail) AddNewMailPush(session comm.IUserSession, mail *pb.DBMailData) {
session.SendMsg(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail})
return
}
// 给多个用户发邮件
func (this *Mail) SendNewMail(mail *pb.DBMailData, uid ...string) bool {
if db.IsCross() {
for _, id := range uid {
tag, _, b := utils.UIdSplit(id)
if b {
if conn, err := db.ServerDBConn(tag); err == nil {
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
mail.ObjId = primitive.NewObjectID().Hex()
mail.Check = false
mail.Reward = true
if len(mail.GetItems()) > 0 {
mail.Reward = false
}
_, err = dbModel.DB.InsertOne(comm.TableMail, mail)
this.SendMsgToUser(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail}, id)
}
}
}
} else {
for _, id := range uid {
mail.Uid = id
mail.ObjId = primitive.NewObjectID().Hex()
mail.Check = false
mail.Reward = true
if len(mail.GetItems()) > 0 {
mail.Reward = false
}
this.modelMail.DB.InsertOne(comm.TableMail, mail)
this.SendMsgToUser(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail}, id)
}
}
return true
}
//红点查询
func (this *Mail) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
reddot = make(map[comm.ReddotType]bool)
for _, v := range rid {
switch v {
case comm.Reddot26:
if isredot := this.modelMail.checkReddot26(session.GetUserId()); isredot {
reddot[comm.Reddot26] = true
}
break
case comm.Reddot30:
if isredot := this.modelMail.checkReddot30(session.GetUserId()); isredot {
reddot[comm.Reddot30] = true
}
break
}
}
return
}
func (this *Mail) Rpc_Mail(ctx context.Context, args *pb.DBMailData) (err error) {
this.Debug("Rpc_Mail", log.Fields{"args": args.String()})
var (
session comm.IUserSession
online bool
)
tag, _, b := utils.UIdSplit(args.Uid)
if b {
if conn, err := db.ServerDBConn(tag); err == nil {
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
_, err = dbModel.DB.InsertOne(comm.TableMail, args)
}
}
if session, online = this.GetUserSession(args.Uid); online {
session.SendMsg(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: args})
}
return
}