diff --git a/modules/db_comp.go b/modules/db_comp.go index 3fdbedf59..f340d0e70 100644 --- a/modules/db_comp.go +++ b/modules/db_comp.go @@ -23,6 +23,8 @@ type MComp_DBComp struct { DB mgo.ISys } +const () + //组件初始化接口 func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.ModuleCompBase.Init(service, module, comp, options) diff --git a/modules/mail/api_delmail.go b/modules/mail/api_delmail.go index 98a47a96e..eafc9113e 100644 --- a/modules/mail/api_delmail.go +++ b/modules/mail/api_delmail.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" - "go_dreamfactory/sys/db" ) // 删除邮件 @@ -20,12 +19,12 @@ func (this *Api_Comp) DelUserMailReq(ctx context.Context, session comm.IUserSess code = pb.ErrorCode_NoLogin return } - bRet := db.Defsys.Mail_DelUserMail(req.ObjID) + bRet := this.M.DB().(*DB_Comp).Mail_DelUserMail(req.ObjID) if !bRet { code = pb.ErrorCode_DBError return } - if mailinfo, err = db.Defsys.Mail_QueryUserMail(session.GetUserId()); err != nil { + if mailinfo, err = this.M.DB().(*DB_Comp).Mail_QueryUserMail(session.GetUserId()); err != nil { log.Errorf("QueryUserMailResp err:%v", err) code = pb.ErrorCode_CacheReadError return diff --git a/modules/mail/api_getAttachment.go b/modules/mail/api_getAttachment.go index 68d2ed57b..e9a73caf0 100644 --- a/modules/mail/api_getAttachment.go +++ b/modules/mail/api_getAttachment.go @@ -4,7 +4,6 @@ import ( "context" "go_dreamfactory/comm" "go_dreamfactory/pb" - "go_dreamfactory/sys/db" ) // 领取附件 @@ -21,12 +20,12 @@ func (this *Api_Comp) GetUserMailAttachmentReq(ctx context.Context, session comm code = pb.ErrorCode_NoLogin return } - _bGet := db.Defsys.Mail_GetMailAttachmentState(req.ObjID) + _bGet := this.M.DB().(*DB_Comp).Mail_GetMailAttachmentState(req.ObjID) if !_bGet { code = pb.ErrorCode_StateInvalid return } - _data, err := db.Defsys.Mail_GetMailAttachment(req.ObjID) + _data, err := this.M.DB().(*DB_Comp).Mail_GetMailAttachment(req.ObjID) if err != nil { if len(_data) > 0 { // todo 领取附件 @@ -37,7 +36,7 @@ func (this *Api_Comp) GetUserMailAttachmentReq(ctx context.Context, session comm bRet := this.pack.AddItemsToUserPack(mail.UserId, _items) if bRet != nil { // 修改状态 - db.Defsys.Mail_UpdateMailAttachmentState(req.ObjID) + this.M.DB().(*DB_Comp).Mail_UpdateMailAttachmentState(req.ObjID) mail.Reward = true return } diff --git a/modules/mail/api_getmail.go b/modules/mail/api_getmail.go index 673682c37..aedbf4e71 100644 --- a/modules/mail/api_getmail.go +++ b/modules/mail/api_getmail.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" - "go_dreamfactory/sys/db" ) // 查看所有邮件信息 @@ -20,7 +19,7 @@ func (this *Api_Comp) QueryUserMailReq(ctx context.Context, session comm.IUserSe code = pb.ErrorCode_NoLogin return } - if mailinfo, err = db.Defsys.Mail_QueryUserMail(session.GetUserId()); err != nil { + if mailinfo, err = this.M.DB().(*DB_Comp).Mail_QueryUserMail(session.GetUserId()); err != nil { log.Errorf("QueryUserMailResp err:%v", err) code = pb.ErrorCode_CacheReadError return diff --git a/modules/mail/api_readmail.go b/modules/mail/api_readmail.go index d83db2499..6bf7a98fe 100644 --- a/modules/mail/api_readmail.go +++ b/modules/mail/api_readmail.go @@ -4,7 +4,6 @@ import ( "context" "go_dreamfactory/comm" "go_dreamfactory/pb" - "go_dreamfactory/sys/db" ) // 查看某一封邮件 @@ -21,7 +20,7 @@ func (this *Api_Comp) ReadUserMailReq(ctx context.Context, session comm.IUserSes return } - mail, err = db.Defsys.Mail_ReadOneMail(req.ObjID) + mail, err = this.M.DB().(*DB_Comp).Mail_ReadOneMail(req.ObjID) if err != nil { code = pb.ErrorCode_ReqParameterError } diff --git a/sys/db/mail.go b/modules/mail/db_comp.go similarity index 62% rename from sys/db/mail.go rename to modules/mail/db_comp.go index 4b317af19..c7e2d267a 100644 --- a/sys/db/mail.go +++ b/modules/mail/db_comp.go @@ -1,7 +1,8 @@ -package db +package mail import ( "context" + "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/lego/core" @@ -15,6 +16,9 @@ const ( DB_MailTable core.SqlTable = "mail" ) +type DB_Comp struct { + modules.MComp_DBComp +} type IMail interface { Mail_QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) Mail_InsertUserMail(mail *pb.DB_MailData) (err error) @@ -25,9 +29,9 @@ type IMail interface { Mail_DelUserMail(objId string) bool } -func (this *DB) Mail_QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) { +func (this *DB_Comp) Mail_QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) { - if _data, err := this.mgo.Find(DB_MailTable, bson.M{"userid": uId}); err == nil { + if _data, err := this.DB.Find(DB_MailTable, bson.M{"userid": uId}); err == nil { for _data.Next(context.TODO()) { temp := &pb.DB_MailData{} if err = _data.Decode(temp); err == nil { @@ -39,7 +43,7 @@ func (this *DB) Mail_QueryUserMail(uId string) (mail []*pb.DB_MailData, err erro } // 插入一封新的邮件 -func (this *DB) Mail_InsertUserMail(mail *pb.DB_MailData) (err error) { +func (this *DB_Comp) Mail_InsertUserMail(mail *pb.DB_MailData) (err error) { mail.ObjId = primitive.NewObjectID().Hex() mail.Check = false @@ -48,14 +52,14 @@ func (this *DB) Mail_InsertUserMail(mail *pb.DB_MailData) (err error) { if len(mail.GetItems()) > 0 { mail.Reward = true } - _, err = this.mgo.InsertOne(DB_MailTable, mail) + _, err = this.DB.InsertOne(DB_MailTable, mail) return err } -func (this *DB) Mail_ReadOneMail(objId string) (mail *pb.DB_MailData, err error) { +func (this *DB_Comp) Mail_ReadOneMail(objId string) (mail *pb.DB_MailData, err error) { - err = this.mgo.FindOneAndUpdate( + err = this.DB.FindOneAndUpdate( DB_MailTable, bson.M{"_id": objId}, bson.M{"$set": bson.M{ @@ -68,9 +72,9 @@ func (this *DB) Mail_ReadOneMail(objId string) (mail *pb.DB_MailData, err error) } // 查询附件信息 -func (this *DB) Mail_GetMailAttachment(objId string) (itmes []*pb.MailAttachment, err error) { +func (this *DB_Comp) Mail_GetMailAttachment(objId string) (itmes []*pb.MailAttachment, err error) { - obj := this.mgo.FindOne(DB_MailTable, bson.M{"_id": objId}) + obj := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId}) var nd *pb.DB_MailData err = obj.Decode(&nd) itmes = nd.GetItems() @@ -79,9 +83,9 @@ func (this *DB) Mail_GetMailAttachment(objId string) (itmes []*pb.MailAttachment } // 查看领取附件状态 -func (this *DB) Mail_GetMailAttachmentState(objId string) bool { +func (this *DB_Comp) Mail_GetMailAttachmentState(objId string) bool { var nd *pb.DB_MailData - err := this.mgo.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(nd) + err := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(nd) if err != nil { return false } @@ -89,8 +93,8 @@ func (this *DB) Mail_GetMailAttachmentState(objId string) bool { } // 更新领取附件状态 -func (this *DB) Mail_UpdateMailAttachmentState(objId string) bool { - this.mgo.FindOneAndUpdate( +func (this *DB_Comp) Mail_UpdateMailAttachmentState(objId string) bool { + this.DB.FindOneAndUpdate( DB_MailTable, bson.M{"_id": objId}, bson.M{"$set": bson.M{ @@ -103,13 +107,13 @@ func (this *DB) Mail_UpdateMailAttachmentState(objId string) bool { } // 删除一封邮件 -func (this *DB) Mail_DelUserMail(objId string) bool { +func (this *DB_Comp) Mail_DelUserMail(objId string) bool { var obj *pb.DB_MailData - err := this.mgo.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(obj) + err := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(obj) if err != nil { return false } - this.mgo.DeleteOne(DB_MailTable, bson.M{"_id": objId}) + this.DB.DeleteOne(DB_MailTable, bson.M{"_id": objId}) return true } diff --git a/modules/mail/mail_test.go b/modules/mail/mail_test.go new file mode 100644 index 000000000..53ec56f93 --- /dev/null +++ b/modules/mail/mail_test.go @@ -0,0 +1,32 @@ +package mail + +import ( + "testing" +) + +func TestCreateEmail(t *testing.T) { + // err := db.Mail_InsertUserMail(&pb.DB_MailData{ + + // UserId: "uid123", + // Title: "系统邮件", + // Contex: "恭喜获得专属礼包一份", + // CreateTime: uint64(time.Now().Unix()), + // DueTime: uint64(time.Now().Unix()) + 30*24*3600, + // Check: false, + // Reward: false, + // }) + // _data, err := db.Mail_QueryUserMail("uid123") + + // for _, v := range _data { + // log.Printf("userid = %s", v.UserId) + // } + // require.Nil(t, err, nil) +} + +func TestReadEmail(t *testing.T) { + // data, err := db.Mail_ReadOneMail("62a078c0726ea54890c34937") + // if err != nil { + // log.Printf("%v", data.Reward) + // } + // require.Nil(t, err, data) +} diff --git a/modules/mail/module.go b/modules/mail/module.go index 8cc6f8fc7..d77e47e93 100644 --- a/modules/mail/module.go +++ b/modules/mail/module.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/cache" - "go_dreamfactory/sys/db" "time" "go_dreamfactory/lego/core" @@ -51,7 +50,7 @@ func (this *Mail) CreateNewMail(uId string) { Check: false, Reward: false, } - err := db.Defsys.Mail_InsertUserMail(mail) + err := this.DB().(*DB_Comp).Mail_InsertUserMail(mail) if err != nil { log.Error("create mail failed") } diff --git a/modules/mail_test.go b/modules/mail_test.go index 37f6b3c8a..4329db094 100644 --- a/modules/mail_test.go +++ b/modules/mail_test.go @@ -14,8 +14,9 @@ var ( func TestCreatemoudles(t *testing.T) { _mail := &pb.DB_MailData{ - UserId: "uid123", - Title: "系统邮件", + UserId: "uid123", + Title: "系统邮件", + Contex: "恭喜获得专属礼包一份", CreateTime: uint64(time.Now().Unix()), DueTime: uint64(time.Now().Unix()) + 30*24*3600, diff --git a/sys/db/core.go b/sys/db/core.go index 5937bc925..7eb3d159d 100644 --- a/sys/db/core.go +++ b/sys/db/core.go @@ -6,7 +6,6 @@ type ( ISys interface { Mgo() mgo.ISys IUser - IMail IFriend } ) diff --git a/sys/db/mail_test.go b/sys/db/mail_test.go deleted file mode 100644 index 6be8ac769..000000000 --- a/sys/db/mail_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package db - -import ( - "go_dreamfactory/pb" - "log" - "testing" - "time" - - "github.com/stretchr/testify/require" -) - -func TestCreateEmail(t *testing.T) { - err := db.Mail_InsertUserMail(&pb.DB_MailData{ - - UserId: "uid123", - Title: "系统邮件", - Contex: "恭喜获得专属礼包一份", - CreateTime: uint64(time.Now().Unix()), - DueTime: uint64(time.Now().Unix()) + 30*24*3600, - Check: false, - Reward: false, - }) - _data, err := db.Mail_QueryUserMail("uid123") - - for _, v := range _data { - log.Printf("userid = %s", v.UserId) - } - require.Nil(t, err, nil) -} - -func TestReadEmail(t *testing.T) { - data, err := db.Mail_ReadOneMail("62a078c0726ea54890c34937") - if err != nil { - log.Printf("%v", data.Reward) - } - require.Nil(t, err, data) -}