go_dreamfactory/modules/sociaty/api_cross_assign.go
2022-12-19 16:25:22 +08:00

92 lines
2.6 KiB
Go

package sociaty
import (
"errors"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
// 公会转让
func (this *apiComp) AssignCheck(session comm.IUserSession, req *pb.SociatyAssignReq) (code pb.ErrorCode) {
if req.TargetId == "" {
code = pb.ErrorCode_ReqParameterError
this.module.Error("公会转让参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})
}
return
}
func (this *apiComp) Assign(session comm.IUserSession, req *pb.SociatyAssignReq) (code pb.ErrorCode, data proto.Message) {
if code = this.AssignCheck(session, req); code != pb.ErrorCode_Success {
return
}
uid := session.GetUserId()
if uid == req.TargetId {
code = pb.ErrorCode_ReqParameterError
this.module.Error("不能转让给自己", log.Field{Key: "uid", Value: uid})
return
}
sociaty := this.module.modelSociaty.getUserSociaty(uid)
if sociaty != nil && sociaty.Id == "" {
code = pb.ErrorCode_SociatyNoFound
this.module.Error("当前玩家所在的公会未找到", log.Field{Key: "uid", Value: uid})
return
}
// 校验权限
if !this.module.modelSociaty.isRight(uid, sociaty, pb.SociatyJob_PRESIDENT) {
code = pb.ErrorCode_SociatyNoRight
return
}
// 校验目标是否公会成员
if !this.module.modelSociaty.isMember(req.TargetId, sociaty) {
code = pb.ErrorCode_SociatyNoMember
return
}
// 公会转让
if err := this.module.modelSociaty.assign(uid, req.TargetId, sociaty); err != nil {
code = pb.ErrorCode_DBError
this.module.Error("公会转让",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "sociatyId", Value: sociaty.Id},
log.Field{Key: "targetId", Value: req.TargetId},
log.Field{Key: "uid", Value: err.Error()},
)
return
}
// 添加日志(成员职位变动)
if err := this.module.modelSociatyLog.addLog(Log_Job, sociaty.Id,
uid, req.TargetId, pb.SociatyJob_PRESIDENT.String()); err != nil {
var customErr = new(comm.CustomError)
if errors.As(err, &customErr) {
code = customErr.Code
} else {
code = pb.ErrorCode_DBError
}
this.module.Error("公会转让日志",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "sociatyId", Value: sociaty.Id},
log.Field{Key: "targetId", Value: req.TargetId},
log.Field{Key: "日志模板", Value: Log_Job},
log.Field{Key: "err", Value: err.Error()},
)
}
rsp := &pb.SociatyAssignResp{
TargetId: req.TargetId,
SociatyId: sociaty.Id,
}
if err := session.SendMsg(string(this.module.GetType()), SociatySubTypeAssign, rsp); err != nil {
code = pb.ErrorCode_SystemError
}
return
}