fix version 1
This commit is contained in:
@@ -12,53 +12,86 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 申请加群处理
|
||||
func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var identity string
|
||||
switch strings.ToUpper(in.Direction) {
|
||||
case "PASS":
|
||||
var apply models.GroupApply
|
||||
if err = impl.DBService.Where("identity=?", in.Identity).First(&apply).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 直接加入群成员
|
||||
identity = utils.UUID()
|
||||
member := &models.GroupMember{
|
||||
GroupID: apply.GroupID,
|
||||
GroupIdentity: apply.GroupIdentity,
|
||||
Role: 0,
|
||||
}
|
||||
member.Identity = utils.UUID()
|
||||
member.PassportID = apply.FromID
|
||||
member.PassportIdentity = apply.FromIdentity
|
||||
|
||||
if err = impl.DBService.Create(&member).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
//更新申请表处理结果
|
||||
impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", 1)
|
||||
|
||||
//更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(apply.GroupIdentity, "+")
|
||||
case "REJECT":
|
||||
//更新申请表处理结果
|
||||
impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", -1)
|
||||
// 未识别的操作方向直接报错,不再默认返回成功
|
||||
direction := strings.ToUpper(in.Direction)
|
||||
if direction != "PASS" && direction != "REJECT" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// 查询申请记录
|
||||
var apply models.GroupApply
|
||||
if err = impl.DBService.Where("identity=?", in.Identity).First(&apply).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// 幂等:申请已处理则直接返回成功
|
||||
if apply.Status != 0 {
|
||||
return &pb.DataStatusReply{Data: vars.OK, Timeseq: time.Now().UnixMilli()}, nil
|
||||
}
|
||||
|
||||
// 角色校验:仅该群管理员/创建者可处理申请
|
||||
var opRole int32
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", apply.GroupIdentity, auth.ID).
|
||||
Pluck("role", &opRole).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if opRole <= ROLE_MEMBER {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var identity string
|
||||
switch direction {
|
||||
case "PASS":
|
||||
// 幂等:申请人已是成员则不再重复创建
|
||||
var memberCnt int64
|
||||
if err = impl.DBService.Model(&models.GroupMember{}).
|
||||
Where("group_identity=? and passport_id=?", apply.GroupIdentity, apply.FromID).Count(&memberCnt).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if memberCnt == 0 {
|
||||
member := &models.GroupMember{
|
||||
GroupID: apply.GroupID,
|
||||
GroupIdentity: apply.GroupIdentity,
|
||||
Role: ROLE_MEMBER,
|
||||
}
|
||||
member.Identity = utils.UUID()
|
||||
member.PassportID = apply.FromID
|
||||
member.PassportIdentity = apply.FromIdentity
|
||||
if err = impl.DBService.Create(member).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
identity = member.Identity
|
||||
// 更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(apply.GroupIdentity, "+")
|
||||
}
|
||||
|
||||
// 更新申请表处理结果
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", 1).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
case "REJECT":
|
||||
// 更新申请表处理结果
|
||||
if err = impl.DBService.Model(&models.GroupApply{}).Where("identity=?", in.Identity).UpdateColumn("status", -1).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.DataStatusReply{
|
||||
Data: identity,
|
||||
|
||||
Reference in New Issue
Block a user