feat: 添加租户创建功能,支持设置管理员及有效期,新增订单统计接口
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
superdto "quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
jobs_args "quyun/v2/app/jobs/args"
|
||||
"quyun/v2/app/requests"
|
||||
@@ -233,6 +234,38 @@ type order struct {
|
||||
job *provider_job.Job
|
||||
}
|
||||
|
||||
// SuperStatistics 平台侧订单统计(不限定 tenant_id)。
|
||||
func (s *order) SuperStatistics(ctx context.Context) (*superdto.OrderStatisticsResponse, error) {
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
var rows []*superdto.OrderStatisticsRow
|
||||
err := query.Select(
|
||||
tbl.Status,
|
||||
tbl.ID.Count().As("count"),
|
||||
tbl.AmountPaid.Sum().As("amount_paid_sum"),
|
||||
).Group(tbl.Status).Scan(&rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var totalCount int64
|
||||
var totalAmountPaidSum int64
|
||||
for _, row := range rows {
|
||||
if row == nil {
|
||||
continue
|
||||
}
|
||||
row.StatusDescription = row.Status.Description()
|
||||
totalCount += row.Count
|
||||
totalAmountPaidSum += row.AmountPaidSum
|
||||
}
|
||||
|
||||
return &superdto.OrderStatisticsResponse{
|
||||
TotalCount: totalCount,
|
||||
TotalAmountPaidSum: totalAmountPaidSum,
|
||||
ByStatus: rows,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ProcessRefundingOrderParams struct {
|
||||
// TenantID 租户ID。
|
||||
TenantID int64
|
||||
|
||||
@@ -25,6 +25,69 @@ import (
|
||||
// @provider
|
||||
type tenant struct{}
|
||||
|
||||
// SuperCreateTenant 超级管理员创建租户,并将指定用户设为租户管理员。
|
||||
func (t *tenant) SuperCreateTenant(ctx context.Context, form *dto.TenantCreateForm) (*models.Tenant, error) {
|
||||
if form == nil {
|
||||
return nil, errors.New("form is nil")
|
||||
}
|
||||
|
||||
code := strings.ToLower(strings.TrimSpace(form.Code))
|
||||
if code == "" {
|
||||
return nil, errors.New("code is empty")
|
||||
}
|
||||
name := strings.TrimSpace(form.Name)
|
||||
if name == "" {
|
||||
return nil, errors.New("name is empty")
|
||||
}
|
||||
if form.AdminUserID <= 0 {
|
||||
return nil, errors.New("admin_user_id must be > 0")
|
||||
}
|
||||
duration, err := (&dto.TenantExpireUpdateForm{Duration: form.Duration}).ParseDuration()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 确保管理员用户存在(同时可提前暴露“用户不存在”的错误,而不是等到外键/逻辑报错)。
|
||||
if _, err := User.FindByID(ctx, form.AdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
tenant := &models.Tenant{
|
||||
UserID: form.AdminUserID,
|
||||
Code: code,
|
||||
UUID: types.NewUUIDv4(),
|
||||
Name: name,
|
||||
Status: consts.TenantStatusVerified,
|
||||
Config: types.JSON([]byte(`{}`)),
|
||||
ExpiredAt: now.Add(duration),
|
||||
}
|
||||
|
||||
db := _db.WithContext(ctx)
|
||||
err = db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(tenant).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tenantUser := &models.TenantUser{
|
||||
TenantID: tenant.ID,
|
||||
UserID: form.AdminUserID,
|
||||
Role: types.NewArray([]consts.TenantUserRole{consts.TenantUserRoleTenantAdmin}),
|
||||
Status: consts.UserStatusVerified,
|
||||
}
|
||||
if err := tx.Create(tenantUser).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.First(tenant, tenant.ID).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tenant, nil
|
||||
}
|
||||
|
||||
// AdminTenantUsersPage 租户管理员分页查询成员列表(包含用户基础信息)。
|
||||
func (t *tenant) AdminTenantUsersPage(ctx context.Context, tenantID int64, filter *tenantdto.AdminTenantUserListFilter) (*requests.Pager, error) {
|
||||
if tenantID <= 0 {
|
||||
|
||||
Reference in New Issue
Block a user