go_dreamfactory/modules/sociaty/api_cross_apply.go
2023-06-06 09:52:44 +08:00

131 lines
3.6 KiB
Go

package sociaty
import (
"errors"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
)
// 公会申请
func (this *apiComp) ApplyCheck(session comm.IUserSession, req *pb.SociatyApplyReq) (errdata *pb.ErrorData) {
if req.SociatyId == "" {
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) Apply(session comm.IUserSession, req *pb.SociatyApplyReq) (errdata *pb.ErrorData) {
if code = this.ApplyCheck(session, req); code != pb.ErrorCode_Success {
return
}
data = &pb.ErrorData{}
uid := session.GetUserId()
sociaty := this.module.modelSociaty.getSociaty(req.SociatyId)
if sociaty == nil {
code = pb.ErrorCode_SociatyNoFound
data.Title = code.ToString()
data.Datastring = req.SociatyId
this.module.Error("公会未找到", log.Field{Key: "uid", Value: uid}, log.Field{Key: "sociatyId", Value: req.SociatyId})
return
}
// userex
userEx, err := this.module.ModuleUser.GetUserExpand(uid)
if err != nil {
this.module.Error("GetRemoteUserExpand", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err.Error()})
code = pb.ErrorCode_UserNofound
data.Title = code.ToString()
data.Datastring = uid
data.Message = err.Error()
return
}
if utils.IsInCDHour(userEx.SociatyCd) {
code = pb.ErrorCode_SociatyCDLimit
data.Title = code.ToString()
data.Message = fmt.Sprintf("CD内[%v]不允许申请", userEx.SociatyCd)
return
}
// 是否公会成员
if this.module.modelSociaty.isMember(uid, sociaty) {
code = pb.ErrorCode_SociatyBelongTo
data.Title = code.ToString()
data.Datastring = sociaty.Id
data.Message = fmt.Sprintf("[%v]已经是公会[%v]成员", uid, sociaty.Name)
return
}
user := this.module.ModuleUser.GetUser(uid)
if user == nil {
code = pb.ErrorCode_UserNofound
data.Title = code.ToString()
data.Datastring = uid
return
}
// 是否达到入会等级
if user.Lv < sociaty.ApplyLv {
code = pb.ErrorCode_SociatyAppyLvNoEnough
data.Title = code.ToString()
data.Message = fmt.Sprintf("实际等级:%v 期望等级:%v", user.Lv, sociaty.ApplyLv)
return
}
// 是否已申请
if this.module.modelSociaty.isApplied(uid, sociaty) {
code = pb.ErrorCode_SociatyApplied
data.Title = code.ToString()
data.Message = fmt.Sprintf("[%v]已申请", sociaty.Name)
return
}
//判断申请人数是否超出最大允许申请数
if len(sociaty.ApplyRecord) >= int(this.module.globalConf.GuildAcceptApplyMax) {
code = pb.ErrorCode_SociatyApplyMax
data.Title = code.ToString()
data.Message = fmt.Sprintf("实际人数:%d 期望人数:%d", len(sociaty.ApplyRecord), int(this.module.globalConf.GuildAcceptApplyMax))
return
}
// 申请
isCheck, err := this.module.modelSociaty.apply(uid, sociaty)
if err != nil {
var customError = new(comm.CustomError)
if errors.As(err, &customError) {
code = customError.Code
} else {
code = pb.ErrorCode_DBError
}
data.Title = code.ToString()
data.Message = err.Error()
this.module.Error("公会申请",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "sociatyId", Value: req.SociatyId},
log.Field{Key: "err", Value: err.Error()},
)
return
}
// 无需审核
if !isCheck {
// 触发任务条件
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype109, 1))
}
rsp := &pb.SociatyApplyResp{
Uid: uid,
ScoiatyId: req.SociatyId,
}
if err := session.SendMsg(string(this.module.GetType()), SociatySubTypeApply, rsp); err != nil {
code = pb.ErrorCode_SystemError
}
return
}