diff --git a/api/proto/account/proto/account_op.go b/api/proto/account/proto/account_op.go index 63e49952..583231af 100644 --- a/api/proto/account/proto/account_op.go +++ b/api/proto/account/proto/account_op.go @@ -207,3 +207,19 @@ type OpUpdateByIdsResp struct { base.BaseResponse Data *OpUpdateByIdsData `json:"data"` } + +// 统计数量 +type OpCountReq struct { + base.BaseRequest + CtUpperBound *int64 `json:"ct_upper_bound"` + CtLowerBound *int64 `json:"ct_lower_bound"` +} + +type OpCountData struct { + Count int64 `json:"count"` +} + +type OpCountResp struct { + base.BaseResponse + Data *OpCountData `json:"data"` +} diff --git a/app/mix/controller/account_op.go b/app/mix/controller/account_op.go index a895f54c..5a9a8461 100644 --- a/app/mix/controller/account_op.go +++ b/app/mix/controller/account_op.go @@ -219,3 +219,20 @@ func OpGetAccountListForOthersByMids(ctx *gin.Context) { ReplyOk(ctx, data) } + +func OpGetAccountCount(ctx *gin.Context) { + req := ctx.MustGet("client_req").(*accountproto.OpCountReq) + + count, ec := service.DefaultService.OpGetAccountCount(ctx, req) + if ec != errcode.ErrCodeAccountSrvOk { + logger.Error("OpGetAccountCount fail, req: %v, ec: %v", util.ToJson(req), ec) + ReplyErrCodeMsg(ctx, ec) + return + } + + data := &accountproto.OpCountData{ + Count: count, + } + + ReplyOk(ctx, data) +} diff --git a/app/mix/controller/init.go b/app/mix/controller/init.go index 983c2ddb..4bc372e3 100644 --- a/app/mix/controller/init.go +++ b/app/mix/controller/init.go @@ -220,6 +220,7 @@ func Init(r *gin.Engine) { opAccountGroup.POST("list_fuzzily_by_name", middleware.JSONParamValidator(accountproto.OpListFuzzilyByNameReq{}), middleware.JwtAuthenticator(), OpGetAccountListFuzzilyByName) opAccountGroup.POST("list_others_by_mid", middleware.JSONParamValidator(accountproto.OpListOthersByMidReq{}), middleware.JwtAuthenticator(), OpGetAccountListForOthersByMid) opAccountGroup.POST("list_others_by_mids", middleware.JSONParamValidator(accountproto.OpListOthersByMidsReq{}), middleware.JwtAuthenticator(), OpGetAccountListForOthersByMids) + opAccountGroup.POST("count", middleware.JSONParamValidator(accountproto.OpCountReq{}), middleware.JwtAuthenticator(), OpGetAccountCount) // 动态 opMomentGroup := r.Group("/op/moment", PrepareOp()) diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 0a96f864..04f3194e 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -988,6 +988,37 @@ func (m *Mongo) UpdateAccountByIds(ctx *gin.Context, account *dbstruct.Account, return err } +func (m *Mongo) GetAccountCount(ctx *gin.Context, req *accountproto.OpCountReq) (int64, error) { + col := m.getColAccount() + + filterInClause := []qmgo.M{} + if req.CtLowerBound != nil { + filterInClause = append(filterInClause, qmgo.M{ + "ct": qmgo.M{ + "$gte": util.DerefInt64(req.CtLowerBound), + }, + }) + } + if req.CtUpperBound != nil { + filterInClause = append(filterInClause, qmgo.M{ + "ct": qmgo.M{ + "$lte": util.DerefInt64(req.CtUpperBound), + }, + }) + } + + if len(filterInClause) == 0 { + return 0, nil + } + + filter := qmgo.M{ + "$and": filterInClause, + } + + count, err := col.Find(ctx, filter).Count() + return count, err +} + // vericode相关 func (m *Mongo) CreateVeriCode(ctx *gin.Context, vericode *dbstruct.VeriCode) error { col := m.getColVeriCode() diff --git a/app/mix/service/logic/account.go b/app/mix/service/logic/account.go index aa25653c..3183f61e 100644 --- a/app/mix/service/logic/account.go +++ b/app/mix/service/logic/account.go @@ -154,6 +154,15 @@ func (p *Account) OpUpdateByIds(ctx *gin.Context, req *accountproto.OpUpdateById return nil } +func (p *Account) OpCount(ctx *gin.Context, req *accountproto.OpCountReq) (int64, error) { + count, err := p.store.GetAccountCount(ctx, req) + if err != nil { + logger.Error("GetAccountCount fail, err: %v", err) + return -1, err + } + return count, err +} + func (p *Account) GenerateOriginalAccount() (*dbstruct.Account, error) { key := "account_init" cfg := apollostruct.AccountInitCfg{} diff --git a/app/mix/service/opservice_business_validation.go b/app/mix/service/opservice_business_validation.go index c2a3125f..cf7133da 100644 --- a/app/mix/service/opservice_business_validation.go +++ b/app/mix/service/opservice_business_validation.go @@ -309,7 +309,7 @@ func (s *Service) OpGetAccountListForOthersByMiBusinessValidated(ctx *gin.Contex Validate(). Collect() if ec = result[0].(errcode.ErrCode); ec != errcode.ErrCodeOk { - logger.Error("OpGetAccountListFuzzilyByName business validation failed") + logger.Error("OpGetAccountListForOthersByMid business validation failed") return } @@ -327,7 +327,24 @@ func (s *Service) OpGetAccountListForOthersByMidsBusinessValidate(ctx *gin.Conte Validate(). Collect() if ec = result[0].(errcode.ErrCode); ec != errcode.ErrCodeOk { - logger.Error("OpGetAccountListFuzzilyByName business validation failed") + logger.Error("OpGetAccountListForOthersByMids business validation failed") + return + } + return +} + +func (s *Service) OpGetAccountCountBusinessValidate(ctx *gin.Context, req *accountproto.OpCountReq) (ec errcode.ErrCode) { + ec = errcode.ErrCodeAccountSrvOk + + // 1.业务校验 + result := businessvalidator.NewAuthBusinessValidator(ctx, req). + QueryAccount(_DefaultAccount.OpListByMid). + EnsureAccountExist(). + EnsureIsOpRole(). + Validate(). + Collect() + if ec = result[0].(errcode.ErrCode); ec != errcode.ErrCodeOk { + logger.Error("OpGetAccountCount business validation failed") return } return diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 196706fa..d64fa962 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -898,6 +898,23 @@ func (s *Service) OpGetAccountListForOthersByMids(ctx *gin.Context, req *account return } +func (s *Service) OpGetAccountCount(ctx *gin.Context, req *accountproto.OpCountReq) (count int64, ec errcode.ErrCode) { + ec = errcode.ErrCodeAccountSrvOk + + if ec = s.OpGetAccountCountBusinessValidate(ctx, req); ec != errcode.ErrCodeAccountSrvOk { + return + } + + count, err := _DefaultAccount.OpCount(ctx, req) + if err != nil { + logger.Error("OpCount fail, err: %v", err) + ec = errcode.ErrCodeAccountSrvFail + return + } + + return +} + // Token func (s *Service) OpVerifyToken(ctx *gin.Context, token string) (err error) { if err = _DefaultToken.OpVerify(ctx, token); err != nil {