diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index 5643d0a1..7fbd37f0 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -191,10 +191,11 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeZoneSessionSrvFail: "空间对话表服务错误", ErrCodeZoneSessionNotExist: "空间对话表不存在", - ErrCodeZoneThirdPartnerSrvFail: "空间代运营表服务错误", - ErrCodeZoneThirdPartnerNotExist: "空间代运营表不存在", - ErrCodeZoneThirdPartnerWrongThirdPartner: "空间代运营不能设置成自己", - ErrCodeZoneThirdPartnerDuplicateKey: "空间代运营已设置,请勿重复设置代运营!", + ErrCodeZoneThirdPartnerSrvFail: "空间代运营表服务错误", + ErrCodeZoneThirdPartnerNotExist: "空间代运营表不存在", + ErrCodeZoneThirdPartnerWrongThirdPartner: "空间代运营不能设置成自己", + ErrCodeZoneThirdPartnerDuplicateKey: "空间代运营已设置,请勿重复设置代运营!", + ErrCodeZoneThirdPartnerSharingRatioNotEnough: "空间代运营的分成比例不足以支持协作者分成比例!", ErrCodeZoneCollaboratorSrvFail: "空间协作者表服务错误", ErrCodeZoneCollaboratorNotExist: "空间协作者表不存在", @@ -474,11 +475,12 @@ const ( ErrCodeZoneSessionNotExist ErrCode = -36002 // 空间对话表不存在 // ZoneThirdPartner: 37xxx - ErrCodeZoneThirdPartnerSrvOk ErrCode = ErrCodeOk - ErrCodeZoneThirdPartnerSrvFail ErrCode = -37001 // 空间代运营表服务错误 - ErrCodeZoneThirdPartnerNotExist ErrCode = -37002 // 空间代运营表不存在 - ErrCodeZoneThirdPartnerWrongThirdPartner ErrCode = -37003 // 空间代运营不能设置成自己 - ErrCodeZoneThirdPartnerDuplicateKey ErrCode = -37004 // 空间代运营重复创建 + ErrCodeZoneThirdPartnerSrvOk ErrCode = ErrCodeOk + ErrCodeZoneThirdPartnerSrvFail ErrCode = -37001 // 空间代运营表服务错误 + ErrCodeZoneThirdPartnerNotExist ErrCode = -37002 // 空间代运营表不存在 + ErrCodeZoneThirdPartnerWrongThirdPartner ErrCode = -37003 // 空间代运营不能设置成自己 + ErrCodeZoneThirdPartnerDuplicateKey ErrCode = -37004 // 空间代运营重复创建 + ErrCodeZoneThirdPartnerSharingRatioNotEnough ErrCode = -37005 // 空间代运营的分成比例不足以支持协作者分成比例 // ZoneCollaborator: 38xxx ErrCodeZoneCollaboratorSrvOk ErrCode = ErrCodeOk diff --git a/app/mix/service/apiservice.go b/app/mix/service/apiservice.go index daad24bd..006e2454 100644 --- a/app/mix/service/apiservice.go +++ b/app/mix/service/apiservice.go @@ -3115,6 +3115,11 @@ func (s *Service) ApiCreateZoneCollaborator(ctx *gin.Context, req *zone_collabor func (s *Service) ApiUpdateZoneCollaborator(ctx *gin.Context, req *zone_collaborator_proto.ApiUpdateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneCollaboratorSrvOk + + if ec = s.ApiUpdateZoneCollaboratorBusinessValidate(ctx, req); ec != errcode.ErrCodeZoneCollaboratorSrvOk { + return + } + err := _DefaultZoneCollaborator.OpUpdate(ctx, &zone_collaborator_proto.OpUpdateReq{ BaseRequest: req.BaseRequest, ZoneCollaborator: req.ZoneCollaborator, diff --git a/app/mix/service/apiservice_business_validation.go b/app/mix/service/apiservice_business_validation.go index 200c13a6..cbec6887 100644 --- a/app/mix/service/apiservice_business_validation.go +++ b/app/mix/service/apiservice_business_validation.go @@ -629,6 +629,39 @@ func (s *Service) ApiCreateZoneCollaboratorBusinessValidate(ctx *gin.Context, re EnsureZoneCollaboratorCreaterIsZoneThirdPartner(req.GetBaseRequest().Mid). EnsureIsNotOperatingHisOwn(util.DerefInt64(req.CollaboratorMid)). EnsureZoneCollaboratorIsNotZoneOwner(_DefaultZone.GetByMid, util.DerefInt64(req.CollaboratorMid)). + EnsureZtpSharingRatioIsEnoughAfterAddingACollab(_DefaultZoneThirdPartner.GetZoneThirdPartnerByZid, func(ctx *gin.Context, zid int64) ([]*dbstruct.ZoneCollaborator, error) { + return _DefaultZoneCollaborator.OpList(ctx, &zone_collaborator_proto.OpListReq{ + Zid: goproto.Int64(zid), + }) + }, req.ZoneCollaborator.GetZid(), req.ZoneCollaborator.GetSharingRatio()). + Validate(). + Collect() + ec = result[0].(errcode.ErrCode) + if ec == errcode.ErrCodeOperationToSelfIsNotPermitted { + ec = errcode.ErrCodeZoneCollaboratorWrongCollaborator + } + if ec != errcode.ErrCodeOk { + logger.Error("ApiCreateZoneCollaborator business validation failed") + return + } + + return +} + +func (s *Service) ApiUpdateZoneCollaboratorBusinessValidate(ctx *gin.Context, req *zone_collaborator_proto.ApiUpdateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeZoneCollaboratorSrvOk + + result := businessvalidator.NewAuthBusinessValidator(ctx, req). + QueryZoneThirdPartnerByZid(_DefaultZoneThirdPartner.GetZoneThirdPartnerByZid, util.DerefInt64(req.Zid)). + EnsureZoneThirdPartnerExist(). + EnsureZoneCollaboratorCreaterIsZoneThirdPartner(req.GetBaseRequest().Mid). + EnsureIsNotOperatingHisOwn(util.DerefInt64(req.CollaboratorMid)). + EnsureZoneCollaboratorIsNotZoneOwner(_DefaultZone.GetByMid, util.DerefInt64(req.CollaboratorMid)). + EnsureZtpSharingRatioIsEnoughAfterUpdatingACollab(_DefaultZoneThirdPartner.GetZoneThirdPartnerByZid, func(ctx *gin.Context, zid int64) ([]*dbstruct.ZoneCollaborator, error) { + return _DefaultZoneCollaborator.OpList(ctx, &zone_collaborator_proto.OpListReq{ + Zid: goproto.Int64(zid), + }) + }, req.ZoneCollaborator.GetZid(), req.ZoneCollaborator.GetId(), req.ZoneCollaborator.GetSharingRatio()). Validate(). Collect() ec = result[0].(errcode.ErrCode) diff --git a/app/mix/service/business_validator/auth.go b/app/mix/service/business_validator/auth.go index 46c17504..8ec84db9 100644 --- a/app/mix/service/business_validator/auth.go +++ b/app/mix/service/business_validator/auth.go @@ -562,6 +562,100 @@ func (l *AuthBusinessValidator) EnsureZoneCollaboratorIsNotZoneOwner(fun func(ct return l } +func (l *AuthBusinessValidator) EnsureZoneThirdPartnerSharingRatioIsEnough(fun func(ctx *gin.Context, zid int64) ([]*dbstruct.ZoneCollaborator, error), zid int64, ztpSharingRatio float64) *AuthBusinessValidator { + l.oplist = append(l.oplist, func() { + // 保证更新后代运营分成比例可满足协作者的分成比例 + zclist, err := fun(l.ctx, zid) + if err != nil { + logger.Error("_DefaultZoneCollaborator OpList fail, err: %v", err) + l.ec = errcode.ErrCodeZoneCollaboratorSrvFail + return + } + totalZcSharingRatio := float64(0) + for _, zc := range zclist { + totalZcSharingRatio += zc.GetSharingRatio() + } + if ztpSharingRatio < totalZcSharingRatio { + logger.Error("Sharing ratio of zone_third_partner is not enough to afford its collaborators, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerSharingRatioNotEnough + return + } + }) + return l +} + +func (l *AuthBusinessValidator) EnsureZtpSharingRatioIsEnoughAfterAddingACollab(GetZtp func(*gin.Context, int64) (*dbstruct.ZoneThirdPartner, error), GetZc func(*gin.Context, int64) ([]*dbstruct.ZoneCollaborator, error), + zid int64, addedSharingRatio float64) *AuthBusinessValidator { + + l.oplist = append(l.oplist, func() { + // 保证更新后代运营分成比例可满足协作者的分成比例 + ztp, err := GetZtp(l.ctx, zid) + if err != nil { + logger.Error("_DefaultZoneThirdPartner OpList fail, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerSrvFail + return + } + if ztp == nil { + logger.Error("No zone_third_partner entity was found, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerNotExist + return + } + zclist, err := GetZc(l.ctx, zid) + if err != nil { + logger.Error("_DefaultZoneCollaborator OpList fail, err: %v", err) + l.ec = errcode.ErrCodeZoneCollaboratorSrvFail + return + } + totalZcSharingRatio := addedSharingRatio + for _, zc := range zclist { + totalZcSharingRatio += zc.GetSharingRatio() + } + if ztp.GetSharingRatio() < totalZcSharingRatio { + logger.Error("Sharing ratio of zone_third_partner is not enough to afford its collaborators, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerSharingRatioNotEnough + return + } + }) + return l +} + +func (l *AuthBusinessValidator) EnsureZtpSharingRatioIsEnoughAfterUpdatingACollab(GetZtp func(*gin.Context, int64) (*dbstruct.ZoneThirdPartner, error), GetZc func(*gin.Context, int64) ([]*dbstruct.ZoneCollaborator, error), + zid int64, updatedCollabId int64, updatedSharingRatio float64) *AuthBusinessValidator { + + l.oplist = append(l.oplist, func() { + // 保证更新后代运营分成比例可满足协作者的分成比例 + ztp, err := GetZtp(l.ctx, zid) + if err != nil { + logger.Error("_DefaultZoneThirdPartner OpList fail, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerSrvFail + return + } + if ztp == nil { + logger.Error("No zone_third_partner entity was found, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerNotExist + return + } + zclist, err := GetZc(l.ctx, zid) + if err != nil { + logger.Error("_DefaultZoneCollaborator OpList fail, err: %v", err) + l.ec = errcode.ErrCodeZoneCollaboratorSrvFail + return + } + totalZcSharingRatio := updatedSharingRatio + for _, zc := range zclist { + if zc.GetId() != updatedCollabId { + totalZcSharingRatio += zc.GetSharingRatio() + } + } + if ztp.GetSharingRatio() < totalZcSharingRatio { + logger.Error("Sharing ratio of zone_third_partner is not enough to afford its collaborators, err: %v", err) + l.ec = errcode.ErrCodeZoneThirdPartnerSharingRatioNotEnough + return + } + }) + return l +} + // 执行校验 func (a *AuthBusinessValidator) Validate() *AuthBusinessValidator { a.BusinessValidateStream.Validate() diff --git a/app/mix/service/opservice_business_validation.go b/app/mix/service/opservice_business_validation.go index dfeb5045..13307b72 100644 --- a/app/mix/service/opservice_business_validation.go +++ b/app/mix/service/opservice_business_validation.go @@ -22,6 +22,8 @@ import ( thumbsupproto "service/api/proto/thumbsup/proto" userwxaddcheckproto "service/api/proto/userwxaddcheck/proto" vericodeproto "service/api/proto/vericode/proto" + zone_collaborator_proto "service/api/proto/zone_collaborator/proto" + zone_third_partner_proto "service/api/proto/zone_third_partner/proto" zonemomentproto "service/api/proto/zonemoment/proto" businessvalidator "service/app/mix/service/business_validator" "service/bizcommon/util" @@ -30,6 +32,7 @@ import ( "service/library/mycrypto" "github.com/gin-gonic/gin" + goproto "google.golang.org/protobuf/proto" ) // 发送验证码 @@ -1423,3 +1426,39 @@ func (s *Service) OpHeadZoneMomentBusinessValidate(ctx *gin.Context, req *zonemo } return } + +func (s *Service) OpUpdateZoneThirdPartnerBusinessValidate(ctx *gin.Context, req *zone_third_partner_proto.OpUpdateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeZoneThirdPartnerSrvOk + // 1.业务校验 + result := businessvalidator.NewAuthBusinessValidator(ctx, req). + EnsureZoneThirdPartnerSharingRatioIsEnough(func(ctx *gin.Context, zid int64) ([]*dbstruct.ZoneCollaborator, error) { + return _DefaultZoneCollaborator.OpList(ctx, &zone_collaborator_proto.OpListReq{ + Zid: goproto.Int64(zid), + }) + }, req.ZoneThirdPartner.GetZid(), req.ZoneThirdPartner.GetSharingRatio()). + Validate(). + Collect() + if ec = result[0].(errcode.ErrCode); ec != errcode.ErrCodeOk { + logger.Error("OpHeadMoment business validation failed") + return + } + return +} + +func (s *Service) OpDeleteZoneThirdPartnerBusinessValidate(ctx *gin.Context, zid int64) (ec errcode.ErrCode) { + ec = errcode.ErrCodeZoneThirdPartnerSrvOk + // 1.业务校验 + result := businessvalidator.NewAuthBusinessValidator(ctx, nil). + EnsureZoneThirdPartnerSharingRatioIsEnough(func(ctx *gin.Context, zid int64) ([]*dbstruct.ZoneCollaborator, error) { + return _DefaultZoneCollaborator.OpList(ctx, &zone_collaborator_proto.OpListReq{ + Zid: goproto.Int64(zid), + }) + }, zid, float64(0)). + Validate(). + Collect() + if ec = result[0].(errcode.ErrCode); ec != errcode.ErrCodeOk { + logger.Error("OpHeadMoment business validation failed") + return + } + return +} diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 8fdfb893..403e1dbd 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -3788,7 +3788,11 @@ func (s *Service) OpCreateZoneThirdPartner(ctx *gin.Context, req *zone_third_par func (s *Service) OpUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.OpUpdateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk - // 若要更新代运营,则查询原代运营信息 + if ec = s.OpUpdateZoneThirdPartnerBusinessValidate(ctx, req); ec != errcode.ErrCodeZoneThirdPartnerSrvOk { + return + } + + // 如果需要修改代运营,则查询原代运营信息 var oriZtp *dbstruct.ZoneThirdPartner if req.ZoneThirdPartner.ThirdPartnerMid != nil { ztp, err := _DefaultZoneThirdPartner.GetZoneThirdPartnerByZid(ctx, req.ZoneThirdPartner.GetZid()) @@ -3812,6 +3816,7 @@ func (s *Service) OpUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_par return } + // 若要修改代运营,则原代运营需要移出空间 if oriZtp != nil { err = _DefaultVas.ZoneExit(ctx, oriZtp.GetThirdPartnerMid(), oriZtp.GetZid()) if err != nil { @@ -3827,6 +3832,10 @@ func (s *Service) OpUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_par func (s *Service) OpDeleteZoneThirdPartner(ctx *gin.Context, zid int64) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk + if ec = s.OpDeleteZoneThirdPartnerBusinessValidate(ctx, zid); ec != errcode.ErrCodeZoneThirdPartnerSrvOk { + return + } + oriZtp, err := _DefaultZoneThirdPartner.OpDeleteByZid(ctx, zid) if err != nil { logger.Error("OpDelete fail, id: %v, err: %v", zid, err) diff --git a/dbstruct/zone_collaborator.go b/dbstruct/zone_collaborator.go index 2c769183..950b7789 100644 --- a/dbstruct/zone_collaborator.go +++ b/dbstruct/zone_collaborator.go @@ -17,6 +17,13 @@ func (p *ZoneCollaborator) GetZid() int64 { return 0 } +func (p *ZoneCollaborator) GetId() int64 { + if p != nil && p.Id != nil { + return *p.Id + } + return 0 +} + func (p *ZoneCollaborator) GetCollaboratorMid() int64 { if p != nil && p.CollaboratorMid != nil { return *p.CollaboratorMid