96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package friend
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
type ModelFriendQiecuo struct {
|
|
modules.MCompModel
|
|
moduleFriend *Friend
|
|
}
|
|
|
|
func (this *ModelFriendQiecuo) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableFrinedQiecuo
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.moduleFriend = module.(*Friend)
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (this *ModelFriendQiecuo) getQiecuo(uid string) *pb.QiecuoRecord {
|
|
record := &pb.QiecuoRecord{}
|
|
if err := this.Get(uid, record); err != nil {
|
|
this.moduleFriend.Errorln(err)
|
|
return nil
|
|
}
|
|
return record
|
|
}
|
|
|
|
// 切磋请求处理
|
|
func (this *ModelFriendQiecuo) qiecuoReq(uid, targetUid string) error {
|
|
qr := this.getQiecuo(uid)
|
|
if qr == nil {
|
|
//创建切磋记录
|
|
qr = &pb.QiecuoRecord{
|
|
Uid: uid,
|
|
TargetId: targetUid,
|
|
Timestamp: configure.Now().Unix(),
|
|
Status: 1, //已发起
|
|
}
|
|
if err := this.Add(uid, qr); err != nil {
|
|
this.moduleFriend.Errorln(err)
|
|
return err
|
|
}
|
|
} else {
|
|
//如果目标未接受且在超时时间内,不允许再次发送
|
|
now := configure.Now().Unix()
|
|
if qr.Status == 1 && now-qr.Timestamp < 10 {
|
|
return comm.NewCustomError(pb.ErrorCode_FriendQiecuoRequested)
|
|
} else if qr.Status == 2 || qr.MatchId != "" {
|
|
return comm.NewCustomError(pb.ErrorCode_FriendQiecuoing)
|
|
} else {
|
|
update := map[string]interface{}{
|
|
"targetId": targetUid,
|
|
"timestamp": configure.Now().Unix(),
|
|
}
|
|
if err := this.Change(uid, update); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 更新切磋记录
|
|
func (this *ModelFriendQiecuo) updateQiecuoRecord(uid, targetUid, matchId string) error {
|
|
qr := this.getQiecuo(uid)
|
|
if qr != nil {
|
|
update := map[string]interface{}{
|
|
"matchId": matchId,
|
|
"status": 2, //已接受
|
|
"timestamp": configure.Now().Unix(),
|
|
}
|
|
if err := this.Change(uid, update); err != nil {
|
|
this.moduleFriend.Errorln(err)
|
|
return err
|
|
}
|
|
this.moduleFriend.Debug("更新切磋",
|
|
log.Field{Key: "uid", Value: uid},
|
|
log.Field{Key: "matchId", Value: matchId})
|
|
}
|
|
|
|
return nil
|
|
}
|