123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package mail
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
|
|
"go_dreamfactory/lego/core"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
const (
|
|
DB_MailTable core.SqlTable = "mail"
|
|
)
|
|
|
|
type modelMail struct {
|
|
modules.MCompModel
|
|
}
|
|
|
|
func (this *modelMail) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.MCompModel.Init(service, module, comp, options)
|
|
this.TableName = "mail"
|
|
//创建uid索引
|
|
this.DB.CreateIndex(core.SqlTable(DB_MailTable), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
func (this *modelMail) Mail_QueryUserMail(uId string) (mail []*pb.DB_MailData, err error) {
|
|
|
|
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 {
|
|
mail = append(mail, temp)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 插入一封新的邮件
|
|
func (this *modelMail) Mail_InsertUserMail(mail *pb.DB_MailData) (err error) {
|
|
|
|
mail.ObjId = primitive.NewObjectID().Hex()
|
|
mail.Check = false
|
|
mail.Reward = false
|
|
|
|
if len(mail.GetItems()) > 0 {
|
|
mail.Reward = true
|
|
}
|
|
_, err = this.DB.InsertOne(DB_MailTable, mail)
|
|
|
|
return err
|
|
}
|
|
|
|
func (this *modelMail) Mail_ReadOneMail(objId string) (mail *pb.DB_MailData, err error) {
|
|
|
|
err = this.DB.FindOneAndUpdate(
|
|
DB_MailTable,
|
|
bson.M{"_id": objId},
|
|
bson.M{"$set": bson.M{
|
|
"check": false,
|
|
}},
|
|
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
|
|
).Decode(mail)
|
|
|
|
return mail, err
|
|
}
|
|
|
|
// 查询附件信息
|
|
func (this *modelMail) Mail_GetMailAttachment(objId string) (itmes []*pb.MailAttachment, err error) {
|
|
|
|
obj := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId})
|
|
var nd *pb.DB_MailData
|
|
err = obj.Decode(&nd)
|
|
itmes = nd.GetItems()
|
|
|
|
return itmes, err
|
|
}
|
|
|
|
// 查看领取附件状态
|
|
func (this *modelMail) Mail_GetMailAttachmentState(objId string) bool {
|
|
var nd *pb.DB_MailData
|
|
err := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(nd)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return nd.Reward && len(nd.GetItems()) > 0
|
|
}
|
|
|
|
// 更新领取附件状态
|
|
func (this *modelMail) Mail_UpdateMailAttachmentState(objId string) bool {
|
|
this.DB.FindOneAndUpdate(
|
|
DB_MailTable,
|
|
bson.M{"_id": objId},
|
|
bson.M{"$set": bson.M{
|
|
"reward": true,
|
|
}},
|
|
options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After),
|
|
)
|
|
|
|
return true
|
|
}
|
|
|
|
// 删除一封邮件
|
|
func (this *modelMail) Mail_DelUserMail(objId string) bool {
|
|
var obj *pb.DB_MailData
|
|
err := this.DB.FindOne(DB_MailTable, bson.M{"_id": objId}).Decode(obj)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
this.DB.DeleteOne(DB_MailTable, bson.M{"_id": objId})
|
|
|
|
return true
|
|
}
|