feat: update
This commit is contained in:
@@ -3,10 +3,15 @@ package services
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
// @provider
|
||||
@@ -65,3 +70,98 @@ func (t *tenant) SetUserRole(ctx context.Context, tenantID, userID int64, role .
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pager
|
||||
func (t *tenant) Pager(ctx context.Context, filter *dto.TenantFilter) (*requests.Pager, error) {
|
||||
tbl, query := models.TenantQuery.QueryContext(ctx)
|
||||
|
||||
conds := []gen.Condition{}
|
||||
if filter.Name != nil {
|
||||
conds = append(conds, tbl.Name.Like(database.WrapLike(*filter.Name)))
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
mm, total, err := query.Where(conds...).Order(tbl.ID.Desc()).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tenantIds := lo.Map(mm, func(item *models.Tenant, _ int) int64 { return item.ID })
|
||||
|
||||
userCountMapping, err := t.TenantUserCountMapping(ctx, tenantIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userBalanceMapping, err := t.TenantUserBalanceMapping(ctx, tenantIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := lo.Map(mm, func(model *models.Tenant, _ int) *dto.TenantItem {
|
||||
return &dto.TenantItem{
|
||||
Tenant: model,
|
||||
UserCount: lo.ValueOr(userCountMapping, model.ID, 0),
|
||||
UserBalance: lo.ValueOr(userBalanceMapping, model.ID, 0),
|
||||
}
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *tenant) TenantUserCountMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
|
||||
tbl, query := models.TenantUserQuery.QueryContext(ctx)
|
||||
|
||||
var items []struct {
|
||||
TenantID int64
|
||||
Count int64
|
||||
}
|
||||
err := query.
|
||||
Select(
|
||||
tbl.TenantID,
|
||||
tbl.UserID.Count().As("count"),
|
||||
).
|
||||
Where(tbl.TenantID.In(tenantIds...)).
|
||||
Group(tbl.TenantID).
|
||||
Scan(&items)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int64)
|
||||
for _, item := range items {
|
||||
result[item.TenantID] = item.Count
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// TenantUserBalanceMapping
|
||||
func (t *tenant) TenantUserBalanceMapping(ctx context.Context, tenantIds []int64) (map[int64]int64, error) {
|
||||
tbl, query := models.TenantUserQuery.QueryContext(ctx)
|
||||
|
||||
var items []struct {
|
||||
TenantID int64
|
||||
Balance int64
|
||||
}
|
||||
err := query.
|
||||
Select(
|
||||
tbl.TenantID,
|
||||
tbl.Balance.Sum().As("balance"),
|
||||
).
|
||||
Where(tbl.TenantID.In(tenantIds...)).
|
||||
Group(tbl.TenantID).
|
||||
Scan(&items)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64]int64)
|
||||
for _, item := range items {
|
||||
result[item.TenantID] = item.Balance
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
50
backend/app/services/tenant_test.go
Normal file
50
backend/app/services/tenant_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/utils"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type TenantTestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *sql.DB
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type TenantTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
TenantTestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_Tenant(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p TenantTestSuiteInjectParams) {
|
||||
suite.Run(t, &TenantTestSuite{TenantTestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TenantTestSuite) Test_TenantUserCount() {
|
||||
Convey("test get tenants user count", t.T(), func() {
|
||||
database.Truncate(t.T().Context(), t.DB, models.TableNameTenant)
|
||||
|
||||
result, err := Tenant.TenantUserCountMapping(t.T().Context(), []int64{1, 2})
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldHaveLength, 2)
|
||||
t.T().Logf("%s", utils.MustJsonString(result))
|
||||
})
|
||||
}
|
||||
@@ -61,7 +61,7 @@ type UserPageFilter struct {
|
||||
}
|
||||
|
||||
// Page
|
||||
func (t *user) Page(ctx context.Context, filter *UserPageFilter) (*requests.Pager[*models.User], error) {
|
||||
func (t *user) Page(ctx context.Context, filter *UserPageFilter) (*requests.Pager, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
conds := []gen.Condition{}
|
||||
@@ -81,8 +81,8 @@ func (t *user) Page(ctx context.Context, filter *UserPageFilter) (*requests.Page
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &requests.Pager[*models.User]{
|
||||
Pagination: requests.Pagination{},
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
|
||||
Reference in New Issue
Block a user