diff --git a/api/errcode/errcode.go b/api/errcode/errcode.go index 5643d0a1..7215a397 100644 --- a/api/errcode/errcode.go +++ b/api/errcode/errcode.go @@ -191,10 +191,12 @@ var ErrCodeMsgMap = map[ErrCode]string{ ErrCodeZoneSessionSrvFail: "空间对话表服务错误", ErrCodeZoneSessionNotExist: "空间对话表不存在", - ErrCodeZoneThirdPartnerSrvFail: "空间代运营表服务错误", - ErrCodeZoneThirdPartnerNotExist: "空间代运营表不存在", - ErrCodeZoneThirdPartnerWrongThirdPartner: "空间代运营不能设置成自己", - ErrCodeZoneThirdPartnerDuplicateKey: "空间代运营已设置,请勿重复设置代运营!", + ErrCodeZoneThirdPartnerSrvFail: "空间代运营表服务错误", + ErrCodeZoneThirdPartnerNotExist: "空间代运营表不存在", + ErrCodeZoneThirdPartnerWrongThirdPartner: "空间代运营不能设置成自己", + ErrCodeZoneThirdPartnerDuplicateKey: "空间代运营已设置,请勿重复设置代运营!", + ErrCodeZoneThirdPartnerSharingRatioNotEnough: "分成比例必须大于合伙人比例之和!", + ErrCodeZoneThirdPartnerSharingRatioTooLarge: "分成比例不可超过50%!", ErrCodeZoneCollaboratorSrvFail: "空间协作者表服务错误", ErrCodeZoneCollaboratorNotExist: "空间协作者表不存在", @@ -474,11 +476,13 @@ 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 // 空间代运营的分成比例不足以支持协作者分成比例 + ErrCodeZoneThirdPartnerSharingRatioTooLarge ErrCode = -37006 // 分成比例不可超过50% // ZoneCollaborator: 38xxx ErrCodeZoneCollaboratorSrvOk ErrCode = ErrCodeOk diff --git a/api/proto/zone_third_partner/proto/not_null_def_op.go b/api/proto/zone_third_partner/proto/not_null_def_op.go index 50cb14f4..e796a143 100644 --- a/api/proto/zone_third_partner/proto/not_null_def_op.go +++ b/api/proto/zone_third_partner/proto/not_null_def_op.go @@ -4,10 +4,24 @@ import ( "service/library/validator" ) -func (p *OpSetIsHidedReq) ProvideNotNullValue() (params []*validator.JsonParam) { +func (p *OpCreateReq) ProvideNotNullValue() (params []*validator.JsonParam) { + params = make([]*validator.JsonParam, 0) + params = append(params, validator.NewInt64PtrParam("请填写空间id!", p.ZoneThirdPartner.Zid)) + params = append(params, validator.NewInt64PtrParam("请填写代运营用户id!", p.ZoneThirdPartner.ThirdPartnerMid)) + params = append(params, validator.NewFloat64PtrParam("请填写分成比例!", p.ZoneThirdPartner.SharingRatio)) + return +} + +func (p *OpUpdateReq) ProvideNotNullValue() (params []*validator.JsonParam) { + params = make([]*validator.JsonParam, 0) + + params = append(params, validator.NewInt64PtrParam("请填写空间id!", p.Zid)) + return +} + +func (p *OpDeleteReq) ProvideNotNullValue() (params []*validator.JsonParam) { params = make([]*validator.JsonParam, 0) params = append(params, validator.NewInt64PtrParam("请填写空间id!", p.Zid)) - params = append(params, validator.NewInt64PtrParam("请填写是否隐藏!", p.IsHided)) return } diff --git a/api/proto/zone_third_partner/proto/zone_third_partner_op.go b/api/proto/zone_third_partner/proto/zone_third_partner_op.go index 1fd1e763..736e6020 100644 --- a/api/proto/zone_third_partner/proto/zone_third_partner_op.go +++ b/api/proto/zone_third_partner/proto/zone_third_partner_op.go @@ -22,7 +22,7 @@ type OpCreateResp struct { // op 删除 type OpDeleteReq struct { base.BaseRequest - Id int64 `json:"id"` + Zid *int64 `json:"zid"` } type OpDeleteData struct { @@ -61,18 +61,3 @@ type OpListResp struct { base.BaseResponse Data *OpListData `json:"data"` } - -// op 更新 -type OpSetIsHidedReq struct { - base.BaseRequest - Zid *int64 `json:"zid"` - IsHided *int64 `json:"is_hided"` -} - -type OpSetIsHidedData struct { -} - -type OpSetIsHidedResp struct { - base.BaseResponse - Data *OpSetIsHidedData `json:"data"` -} diff --git a/api/proto/zone_third_partner/proto/zone_third_partner_vo_api.go b/api/proto/zone_third_partner/proto/zone_third_partner_vo_api.go index 120e5c02..c7c6af74 100644 --- a/api/proto/zone_third_partner/proto/zone_third_partner_vo_api.go +++ b/api/proto/zone_third_partner/proto/zone_third_partner_vo_api.go @@ -8,4 +8,5 @@ import ( type ZoneThirdPartnerApiVO struct { *dbstruct.ZoneThirdPartner Account *accountproto.ApiListOthersVO `json:"third_partner_account"` + IsHided int64 `json:"is_hided"` } diff --git a/app/mix/controller/init.go b/app/mix/controller/init.go index 6033048c..25d51aab 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -517,8 +517,10 @@ func Init(r *gin.Engine) { // 空间代运营表 opZoneThirdPartnerGroup := r.Group("/op/zone_third_partner", PrepareOp()) - opZoneThirdPartnerGroup.POST("set_is_hided", middleware.JSONParamValidator(zone_third_partner_proto.OpSetIsHidedReq{}), middleware.JwtAuthenticator(), OpSetIsHidedZoneThirdPartner) - opZoneThirdPartnerGroup.POST("list", middleware.JSONParamValidator(zone_third_partner_proto.ApiListReq{}), middleware.JwtAuthenticator(), OpGetZoneThirdPartnerList) + opZoneThirdPartnerGroup.POST("create", middleware.JSONParamValidator(zone_third_partner_proto.OpCreateReq{}), middleware.JwtAuthenticator(), OpCreateZoneThirdPartner) + opZoneThirdPartnerGroup.POST("update", middleware.JSONParamValidator(zone_third_partner_proto.OpUpdateReq{}), middleware.JwtAuthenticator(), OpUpdateZoneThirdPartner) + opZoneThirdPartnerGroup.POST("delete", middleware.JSONParamValidator(zone_third_partner_proto.OpDeleteReq{}), middleware.JwtAuthenticator(), OpDeleteZoneThirdPartner) + opZoneThirdPartnerGroup.POST("list", middleware.JSONParamValidator(zone_third_partner_proto.OpListReq{}), middleware.JwtAuthenticator(), OpGetZoneThirdPartnerList) // 空间相关每日报表 opDailyStatementZoneInfoGroup := r.Group("/op/daily_statement_zone_info", PrepareOp()) diff --git a/app/mix/controller/zone_third_partner_op.go b/app/mix/controller/zone_third_partner_op.go index ffdb6c13..7947b0b8 100644 --- a/app/mix/controller/zone_third_partner_op.go +++ b/app/mix/controller/zone_third_partner_op.go @@ -37,7 +37,7 @@ func OpUpdateZoneThirdPartner(ctx *gin.Context) { func OpDeleteZoneThirdPartner(ctx *gin.Context) { req := ctx.MustGet("client_req").(*zone_third_partnerproto.OpDeleteReq) - ec := service.DefaultService.OpDeleteZoneThirdPartner(ctx, req.Id) + ec := service.DefaultService.OpDeleteZoneThirdPartner(ctx, util.DerefInt64(req.Zid)) if ec != errcode.ErrCodeZoneThirdPartnerSrvOk { logger.Error("OpDeleteZoneThirdPartner fail, req: %v, ec: %v", util.ToJson(req), ec) ReplyErrCodeMsg(ctx, ec) @@ -67,15 +67,3 @@ func OpGetZoneThirdPartnerList(ctx *gin.Context) { } ReplyOk(ctx, data) } - -func OpSetIsHidedZoneThirdPartner(ctx *gin.Context) { - req := ctx.MustGet("client_req").(*zone_third_partnerproto.OpSetIsHidedReq) - ec := service.DefaultService.OpSetIsHidedZoneThirdPartner(ctx, req) - if ec != errcode.ErrCodeZoneThirdPartnerSrvOk { - logger.Error("OpSetIsHidedZoneThirdPartner fail, req: %v, ec: %v", util.ToJson(req), ec) - ReplyErrCodeMsg(ctx, ec) - return - } - - ReplyOk(ctx, nil) -} diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 8142bf49..0e763716 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -4639,6 +4639,21 @@ func (m *Mongo) UpdateZoneThirdPartner(ctx *gin.Context, zone_third_partner *dbs return err } +func (m *Mongo) UpdateZoneThirdPartnerByZid(ctx *gin.Context, zone_third_partner *dbstruct.ZoneThirdPartner) error { + col := m.getColZoneThirdPartner() + set := util.EntityToM(zone_third_partner) + delete(set, "zid") + set["ut"] = time.Now().Unix() + up := qmgo.M{ + "$set": set, + } + filter := qmgo.M{ + "zid": zone_third_partner.GetZid(), + } + err := col.UpdateOne(ctx, filter, up) + return err +} + func (m *Mongo) DeleteZoneThirdPartner(ctx *gin.Context, id int64) error { col := m.getColZoneThirdPartner() err := col.RemoveId(ctx, id) @@ -4660,6 +4675,14 @@ func (m *Mongo) GetZoneThirdPartnerList(ctx *gin.Context, req *zone_third_partne return zoneThirdPartner, err } +func (m *Mongo) DeleteZoneThirdPartnerByZid(ctx *gin.Context, zid int64) error { + col := m.getColZoneThirdPartner() + err := col.Remove(ctx, qmgo.M{ + "zid": zid, + }) + return err +} + func (m *Mongo) GetZoneThirdPartnerListByTpMid(ctx *gin.Context, tp_mid int64) ([]*dbstruct.ZoneThirdPartner, error) { list := make([]*dbstruct.ZoneThirdPartner, 0) col := m.getColZoneThirdPartner() @@ -4720,21 +4743,6 @@ func (m *Mongo) GetZoneThirdPartnerById(ctx *gin.Context, id int64) (*dbstruct.Z return &one, err } -func (m *Mongo) SetIsHidedForZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.OpSetIsHidedReq) error { - col := m.getColZoneThirdPartner() - up := qmgo.M{ - "$set": qmgo.M{ - "is_hided": util.DerefInt64(req.IsHided), - "ut": time.Now().Unix(), - }, - } - filter := qmgo.M{ - "zid": util.DerefInt64(req.Zid), - } - err := col.UpdateOne(ctx, filter, up) - return err -} - func (m *Mongo) CreateZoneThirdPartnerHis(ctx *gin.Context, zone_third_partner *dbstruct.ZoneThirdPartner) error { col := m.getColZoneThirdPartnerHis() _, err := col.InsertOne(ctx, zone_third_partner) diff --git a/app/mix/service/apiservice.go b/app/mix/service/apiservice.go index 4952e13e..b291a00c 100644 --- a/app/mix/service/apiservice.go +++ b/app/mix/service/apiservice.go @@ -2116,6 +2116,7 @@ func (s *Service) ApiCreateZone(ctx *gin.Context, req *zoneproto.ApiCreateReq) ( req.Zone.ZoneMomentCount = goproto.Int64(0) req.Zone.ImageCount = goproto.Int64(0) req.Zone.VideoCount = goproto.Int64(0) + req.Zone.IsZoneThirdPartnerHided = goproto.Int64(consts.IsHided_No) err, zid := _DefaultZone.OpCreate(ctx, &zoneproto.OpCreateReq{ Zone: req.Zone, }) @@ -3034,38 +3035,32 @@ func (s *Service) ApiCreateZoneThirdPartner(ctx *gin.Context, req *zone_third_pa return } -func (s *Service) ApiUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.ApiUpdateReq) (ec errcode.ErrCode) { - ec = errcode.ErrCodeZoneThirdPartnerSrvOk - err := _DefaultZoneThirdPartner.OpUpdate(ctx, &zone_third_partner_proto.OpUpdateReq{ - BaseRequest: req.BaseRequest, - ZoneThirdPartner: req.ZoneThirdPartner, - }) - if err == qmgo.ErrNoSuchDocuments { - ec = errcode.ErrCodeZoneThirdPartnerNotExist - err = nil - return - } - if err != nil { - logger.Error("OpUpdate fail, req: %v, err: %v", util.ToJson(req), err) - ec = errcode.ErrCodeZoneThirdPartnerSrvFail - return - } - return -} - -func (s *Service) ApiDeleteZoneThirdPartner(ctx *gin.Context, id int64) (ec errcode.ErrCode) { - ec = errcode.ErrCodeZoneThirdPartnerSrvOk - err := _DefaultZoneThirdPartner.OpDelete(ctx, id) - if err != nil { - logger.Error("OpDelete fail, id: %v, err: %v", id, err) - ec = errcode.ErrCodeZoneThirdPartnerSrvFail - return - } - return -} - func (s *Service) ApiGetZoneThirdPartnerList(ctx *gin.Context, req *zone_third_partner_proto.ApiListReq) (vo *zone_third_partner_proto.ZoneThirdPartnerApiVO, ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk + + zone, err := _DefaultZone.GetById(ctx, util.DerefInt64(req.Zid)) + if err != nil { + logger.Error("_DefaultZone GetById fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeZoneSrvFail + return + } + if zone == nil { + vo = nil + return + } + + if zone.GetIsZoneThirdPartnerHided() == consts.IsHided_Yes { + vo = &zone_third_partner_proto.ZoneThirdPartnerApiVO{ + ZoneThirdPartner: &dbstruct.ZoneThirdPartner{ + Zid: req.Zid, + ThirdPartnerMid: nil, + SharingRatio: nil, + }, + IsHided: consts.IsHided_Yes, + } + return + } + zoneThirdPartner, err := _DefaultZoneThirdPartner.OpList(ctx, &zone_third_partner_proto.OpListReq{ BaseRequest: req.BaseRequest, Zid: req.Zid, @@ -3079,14 +3074,6 @@ func (s *Service) ApiGetZoneThirdPartnerList(ctx *gin.Context, req *zone_third_p vo = nil return } - if zoneThirdPartner.GetIsHided() == consts.IsHided_Yes { - zoneThirdPartner.ThirdPartnerMid = nil - zoneThirdPartner.SharingRatio = nil - vo = &zone_third_partner_proto.ZoneThirdPartnerApiVO{ - ZoneThirdPartner: zoneThirdPartner, - } - return - } acct, err := _DefaultAccount.OpListByMid(ctx, &accountproto.OpListByMidReq{ Mid: zoneThirdPartner.ThirdPartnerMid, @@ -3107,6 +3094,7 @@ func (s *Service) ApiGetZoneThirdPartnerList(ctx *gin.Context, req *zone_third_p vo = &zone_third_partner_proto.ZoneThirdPartnerApiVO{ ZoneThirdPartner: zoneThirdPartner, Account: acctVO, + IsHided: consts.IsHided_No, } return @@ -3145,6 +3133,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..bf5a43cc 100644 --- a/app/mix/service/business_validator/auth.go +++ b/app/mix/service/business_validator/auth.go @@ -562,6 +562,111 @@ func (l *AuthBusinessValidator) EnsureZoneCollaboratorIsNotZoneOwner(fun func(ct return l } +func (l *AuthBusinessValidator) EnsureZoneThirdPartnerSharingRatioIsNotTooLarge(sharingRatio float64) *AuthBusinessValidator { + l.oplist = append(l.oplist, func() { + if sharingRatio > 0.5 { + l.ec = errcode.ErrCodeZoneThirdPartnerSharingRatioTooLarge + return + } + }) + 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/logic/zone_third_partner.go b/app/mix/service/logic/zone_third_partner.go index 27df2cca..dd04baa1 100644 --- a/app/mix/service/logic/zone_third_partner.go +++ b/app/mix/service/logic/zone_third_partner.go @@ -31,7 +31,6 @@ func (p *ZoneThirdPartner) OpCreate(ctx *gin.Context, req *zone_third_partner_pr req.ZoneThirdPartner.Ct = goproto.Int64(time.Now().Unix()) req.ZoneThirdPartner.Ut = goproto.Int64(time.Now().Unix()) req.ZoneThirdPartner.DelFlag = goproto.Int64(consts.Exist) - req.ZoneThirdPartner.IsHided = goproto.Int64(consts.IsHided_No) err := p.store.CreateZoneThirdPartner(ctx, req.ZoneThirdPartner) if err != nil { logger.Error("CreateZoneThirdPartner fail, err: %v", err) @@ -40,43 +39,44 @@ func (p *ZoneThirdPartner) OpCreate(ctx *gin.Context, req *zone_third_partner_pr return nil } -func (p *ZoneThirdPartner) OpUpdate(ctx *gin.Context, req *zone_third_partner_proto.OpUpdateReq) error { - err := p.store.UpdateZoneThirdPartner(ctx, req.ZoneThirdPartner) +func (p *ZoneThirdPartner) OpUpdateByZid(ctx *gin.Context, req *zone_third_partner_proto.OpUpdateReq) error { + err := p.store.UpdateZoneThirdPartnerByZid(ctx, req.ZoneThirdPartner) if err != nil { - logger.Error("UpdateZoneThirdPartner fail, err: %v", err) + logger.Error("UpdateZoneThirdPartnerByZid fail, err: %v", err) return err } return nil } -func (p *ZoneThirdPartner) OpDelete(ctx *gin.Context, id int64) error { +func (p *ZoneThirdPartner) OpDeleteByZid(ctx *gin.Context, zid int64) (zoneThirdPartner *dbstruct.ZoneThirdPartner, err error) { - //1.查出原协作者信息 - zoneThirdPartner, err := p.store.GetZoneThirdPartnerById(ctx, id) + //1.查出原代运营信息 + zoneThirdPartner, err = p.store.GetZoneThirdPartnerByZid(ctx, zid) if zoneThirdPartner == nil { logger.Error("GetZoneThirdPartnerById fail, err: %v", qmgo.ErrNoSuchDocuments) - return qmgo.ErrNoSuchDocuments + err = qmgo.ErrNoSuchDocuments + return } if err != nil { logger.Error("GetZoneThirdPartnerById fail, err: %v", err) - return err + return } - //2.原协作者记录标记删除后转存到历史表 + //2.原代运营记录标记删除后转存到历史表 zoneThirdPartner.DelFlag = goproto.Int64(consts.Deleted) err = p.store.CreateZoneThirdPartnerHis(ctx, zoneThirdPartner) if err != nil { logger.Error("CreateZoneThirdPartnerHis fail, err: %v", err) - return err + return } - //3.真删除原有协作者记录 - err = p.store.DeleteZoneThirdPartner(ctx, id) + //3.真删除原有代运营记录 + err = p.store.DeleteZoneThirdPartnerByZid(ctx, zid) if err != nil { - logger.Error("DeleteZoneThirdPartner fail, err: %v", err) - return err + logger.Error("DeleteZoneThirdPartnerByZid fail, err: %v", err) + return } - return nil + return } func (p *ZoneThirdPartner) OpList(ctx *gin.Context, req *zone_third_partner_proto.OpListReq) (*dbstruct.ZoneThirdPartner, error) { @@ -138,12 +138,3 @@ func (p *ZoneThirdPartner) GetZoneThirdPartnerMapByZids(ctx *gin.Context, zids [ return mp, nil } - -func (p *ZoneThirdPartner) OpSetIsHided(ctx *gin.Context, req *zone_third_partner_proto.OpSetIsHidedReq) error { - err := p.store.SetIsHidedForZoneThirdPartner(ctx, req) - if err != nil { - logger.Error("SetIsHidedZoneThirdPartner fail, err: %v", err) - return err - } - return nil -} diff --git a/app/mix/service/opservice_business_validation.go b/app/mix/service/opservice_business_validation.go index dfeb5045..3ea1a119 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,22 @@ 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). + EnsureZoneThirdPartnerSharingRatioIsNotTooLarge(req.ZoneThirdPartner.GetSharingRatio()). + 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("OpUpdateZoneThirdPartner business validation failed") + return + } + return +} diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 433f0157..3b2733a8 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -3215,6 +3215,7 @@ func (s *Service) OpGetAccountCancellationList(ctx *gin.Context, req *account_ca // Zone func (s *Service) OpCreateZone(ctx *gin.Context, req *zoneproto.OpCreateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneSrvOk + req.Zone.IsZoneThirdPartnerHided = goproto.Int64(consts.IsHided_No) err, _ := _DefaultZone.OpCreate(ctx, req) if err != nil { logger.Error("OpCreate fail, req: %v, err: %v", util.ToJson(req), err) @@ -3759,18 +3760,40 @@ func (s *Service) OpSetPrivateZoneMoment(ctx *gin.Context, req *zonemomentproto. // ZoneThirdPartner func (s *Service) OpCreateZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.OpCreateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk + err := _DefaultZoneThirdPartner.OpCreate(ctx, req) if err != nil { logger.Error("OpCreate fail, req: %v, err: %v", util.ToJson(req), err) ec = errcode.ErrCodeZoneThirdPartnerSrvFail return } + if mongo.IsDuplicateKeyError(err) { + logger.Error("OpCreate duplicate key found, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeZoneThirdPartnerDuplicateKey + return + } + if err != nil { + logger.Error("OpCreate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeZoneThirdPartnerSrvFail + return + } + + _err := _DefaultVas.ZoneFreeJoinThirdPartner(ctx, req.ZoneThirdPartner.GetThirdPartnerMid(), req.ZoneThirdPartner.GetZid()) + if _err != nil { + logger.Error("ZoneFreeJoinThirdPartner fail, mid: %v, zid: %v, err: %v", req.ZoneThirdPartner.GetThirdPartnerMid(), req.ZoneThirdPartner.GetZid(), _err) + } + return } func (s *Service) OpUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.OpUpdateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk - err := _DefaultZoneThirdPartner.OpUpdate(ctx, req) + + if ec = s.OpUpdateZoneThirdPartnerBusinessValidate(ctx, req); ec != errcode.ErrCodeZoneThirdPartnerSrvOk { + return + } + + err := _DefaultZoneThirdPartner.OpUpdateByZid(ctx, req) if err == qmgo.ErrNoSuchDocuments { ec = errcode.ErrCodeZoneThirdPartnerNotExist err = nil @@ -3781,17 +3804,38 @@ func (s *Service) OpUpdateZoneThirdPartner(ctx *gin.Context, req *zone_third_par ec = errcode.ErrCodeZoneThirdPartnerSrvFail return } + return } -func (s *Service) OpDeleteZoneThirdPartner(ctx *gin.Context, id int64) (ec errcode.ErrCode) { +func (s *Service) OpDeleteZoneThirdPartner(ctx *gin.Context, zid int64) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneThirdPartnerSrvOk - err := _DefaultZoneThirdPartner.OpDelete(ctx, id) + + _, err := _DefaultZoneThirdPartner.OpDeleteByZid(ctx, zid) if err != nil { - logger.Error("OpDelete fail, id: %v, err: %v", id, err) + logger.Error("OpDelete fail, id: %v, err: %v", zid, err) ec = errcode.ErrCodeZoneThirdPartnerSrvFail return } + + zclist, err := _DefaultZoneCollaborator.OpList(ctx, &zone_collaborator_proto.OpListReq{ + Zid: goproto.Int64(zid), + }) + if err != nil { + logger.Error("OpList fail, id: %v, err: %v", zid, err) + ec = errcode.ErrCodeZoneCollaboratorSrvFail + return + } + + for _, zc := range zclist { + err := _DefaultZoneCollaborator.OpDelete(ctx, zc.GetId()) + if err != nil { + logger.Error("OpDelete fail, id: %v, err: %v", zid, err) + ec = errcode.ErrCodeZoneCollaboratorSrvFail + return + } + } + return } @@ -3832,22 +3876,6 @@ func (s *Service) OpGetZoneThirdPartnerList(ctx *gin.Context, req *zone_third_pa return } -func (s *Service) OpSetIsHidedZoneThirdPartner(ctx *gin.Context, req *zone_third_partner_proto.OpSetIsHidedReq) (ec errcode.ErrCode) { - ec = errcode.ErrCodeZoneThirdPartnerSrvOk - err := _DefaultZoneThirdPartner.OpSetIsHided(ctx, req) - if err == qmgo.ErrNoSuchDocuments { - ec = errcode.ErrCodeZoneThirdPartnerNotExist - err = nil - return - } - if err != nil { - logger.Error("OpSetIsHided fail, req: %v, err: %v", util.ToJson(req), err) - ec = errcode.ErrCodeZoneThirdPartnerSrvFail - return - } - return -} - // ZoneCollaborator func (s *Service) OpCreateZoneCollaborator(ctx *gin.Context, req *zone_collaborator_proto.OpCreateReq) (ec errcode.ErrCode) { ec = errcode.ErrCodeZoneCollaboratorSrvOk diff --git a/app/mix/service/utilservice.go b/app/mix/service/utilservice.go index 87ebb751..ed4a7918 100644 --- a/app/mix/service/utilservice.go +++ b/app/mix/service/utilservice.go @@ -1984,6 +1984,7 @@ func (s *Service) utilFillZonesWithOpVOInfo(ctx *gin.Context, list []*dbstruct.Z ZoneThirdPartner: ztp, Account: ztpAcctVO, } + vo.ZoneThirdPartner.IsHided = vo.Zone.GetIsZoneThirdPartnerHided() // 填充协作者 zclist := zcMp[zid] diff --git a/dbstruct/zone.go b/dbstruct/zone.go index bcd64ca7..1b89ec0e 100644 --- a/dbstruct/zone.go +++ b/dbstruct/zone.go @@ -1,16 +1,17 @@ package dbstruct type Zone struct { - Id *int64 `json:"id" bson:"_id"` // 空间表id - Mid *int64 `json:"mid" bson:"mid"` // 用户表id - Profile *string `json:"profile" bson:"profile"` // 空间简介 - LastZoneMomentCt *int64 `json:"last_zone_moment_ct" bson:"last_zone_moment_ct"` // 最后空间动态创建时间 - ZoneMomentCount *int64 `json:"zone_moment_count" bson:"zone_moment_count"` // 空间内动态总数 - ImageCount *int64 `json:"image_count" bson:"image_count"` // 空间内图片总数 - VideoCount *int64 `json:"video_count" bson:"video_count"` // 空间内视频总数 - Ct *int64 `json:"ct" bson:"ct"` // 创建时间 - Ut *int64 `json:"ut" bson:"ut"` // 更新时间 - DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记 + Id *int64 `json:"id" bson:"_id"` // 空间表id + Mid *int64 `json:"mid" bson:"mid"` // 用户表id + Profile *string `json:"profile" bson:"profile"` // 空间简介 + LastZoneMomentCt *int64 `json:"last_zone_moment_ct" bson:"last_zone_moment_ct"` // 最后空间动态创建时间 + ZoneMomentCount *int64 `json:"zone_moment_count" bson:"zone_moment_count"` // 空间内动态总数 + ImageCount *int64 `json:"image_count" bson:"image_count"` // 空间内图片总数 + VideoCount *int64 `json:"video_count" bson:"video_count"` // 空间内视频总数 + IsZoneThirdPartnerHided *int64 `json:"is_zone_third_partner_hided" bson:"is_zone_third_partner_hided"` // 是否隐藏空间代运营 + Ct *int64 `json:"ct" bson:"ct"` // 创建时间 + Ut *int64 `json:"ut" bson:"ut"` // 更新时间 + DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记 } func (p *Zone) GetId() int64 { @@ -33,3 +34,10 @@ func (p *Zone) GetLastZoneMomentCt() int64 { } return 0 } + +func (p *Zone) GetIsZoneThirdPartnerHided() int64 { + if p != nil && p.IsZoneThirdPartnerHided != nil { + return *p.IsZoneThirdPartnerHided + } + return 0 +} 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 diff --git a/dbstruct/zone_third_partner.go b/dbstruct/zone_third_partner.go index ce3467ca..3e5cebcb 100644 --- a/dbstruct/zone_third_partner.go +++ b/dbstruct/zone_third_partner.go @@ -5,7 +5,6 @@ type ZoneThirdPartner struct { Zid *int64 `json:"zid" bson:"zid"` // 空间id ThirdPartnerMid *int64 `json:"third_partner_mid" bson:"third_partner_mid"` // 代运营用户id SharingRatio *float64 `json:"sharing_ratio" bson:"sharing_ratio"` // 分成比例 - IsHided *int64 `json:"is_hided" bson:"is_hided"` // 是否隐藏 Ct *int64 `json:"ct" bson:"ct"` // 创建时间 Ut *int64 `json:"ut" bson:"ut"` // 更新时间 DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记 @@ -32,10 +31,3 @@ func (p *ZoneThirdPartner) GetSharingRatio() float64 { } return 0 } - -func (p *ZoneThirdPartner) GetIsHided() int64 { - if p != nil && p.IsHided != nil { - return *p.IsHided - } - return 0 -}