feat: 添加租户创建功能,支持设置管理员及有效期,新增订单统计接口
This commit is contained in:
17
backend/app/http/super/dto/order.go
Normal file
17
backend/app/http/super/dto/order.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package dto
|
||||
|
||||
import "quyun/v2/pkg/consts"
|
||||
|
||||
type OrderStatisticsRow struct {
|
||||
Status consts.OrderStatus `json:"status"`
|
||||
StatusDescription string `json:"status_description"`
|
||||
|
||||
Count int64 `json:"count"`
|
||||
AmountPaidSum int64 `json:"amount_paid_sum"`
|
||||
}
|
||||
|
||||
type OrderStatisticsResponse struct {
|
||||
TotalCount int64 `json:"total_count"`
|
||||
TotalAmountPaidSum int64 `json:"total_amount_paid_sum"`
|
||||
ByStatus []*OrderStatisticsRow `json:"by_status"`
|
||||
}
|
||||
@@ -25,6 +25,14 @@ type TenantItem struct {
|
||||
StatusDescription string `json:"status_description"`
|
||||
}
|
||||
|
||||
type TenantCreateForm struct {
|
||||
Code string `json:"code" validate:"required,max=64"`
|
||||
Name string `json:"name" validate:"required,max=128"`
|
||||
AdminUserID int64 `json:"admin_user_id" validate:"required,gt=0"`
|
||||
// Duration 租户有效期(天),从“创建时刻”起算;与续期接口保持一致。
|
||||
Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
|
||||
}
|
||||
|
||||
type TenantExpireUpdateForm struct {
|
||||
Duration int `json:"duration" validate:"required,oneof=7 30 90 180 365"`
|
||||
}
|
||||
|
||||
25
backend/app/http/super/order.go
Normal file
25
backend/app/http/super/order.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package super
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/services"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type order struct{}
|
||||
|
||||
// statistics
|
||||
//
|
||||
// @Summary 订单统计信息
|
||||
// @Tags Super
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} dto.OrderStatisticsResponse
|
||||
//
|
||||
// @Router /super/v1/orders/statistics [get]
|
||||
func (*order) statistics(ctx fiber.Ctx) (*dto.OrderStatisticsResponse, error) {
|
||||
return services.Order.SuperStatistics(ctx)
|
||||
}
|
||||
|
||||
@@ -28,12 +28,14 @@ func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
auth *auth,
|
||||
middlewares *middlewares.Middlewares,
|
||||
order *order,
|
||||
tenant *tenant,
|
||||
user *user,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
auth: auth,
|
||||
middlewares: middlewares,
|
||||
order: order,
|
||||
tenant: tenant,
|
||||
user: user,
|
||||
}
|
||||
@@ -52,6 +54,13 @@ func Provide(opts ...opt.Option) error {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*order, error) {
|
||||
obj := &order{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*tenant, error) {
|
||||
obj := &tenant{}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ type Routes struct {
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
auth *auth
|
||||
order *order
|
||||
tenant *tenant
|
||||
user *user
|
||||
}
|
||||
@@ -53,12 +54,22 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
r.auth.login,
|
||||
Body[dto.LoginForm]("form"),
|
||||
))
|
||||
// Register routes for controller: order
|
||||
r.log.Debugf("Registering route: Get /super/v1/orders/statistics -> order.statistics")
|
||||
router.Get("/super/v1/orders/statistics"[len(r.Path()):], DataFunc0(
|
||||
r.order.statistics,
|
||||
))
|
||||
// Register routes for controller: tenant
|
||||
r.log.Debugf("Registering route: Get /super/v1/tenants -> tenant.list")
|
||||
router.Get("/super/v1/tenants"[len(r.Path()):], DataFunc1(
|
||||
r.tenant.list,
|
||||
Query[dto.TenantFilter]("filter"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /super/v1/tenants -> tenant.create")
|
||||
router.Post("/super/v1/tenants"[len(r.Path()):], DataFunc1(
|
||||
r.tenant.create,
|
||||
Body[dto.TenantCreateForm]("form"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /super/v1/tenants/statuses -> tenant.statusList")
|
||||
router.Get("/super/v1/tenants/statuses"[len(r.Path()):], DataFunc0(
|
||||
r.tenant.statusList,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"quyun/v2/app/http/super/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
@@ -28,6 +29,21 @@ func (*tenant) list(ctx fiber.Ctx, filter *dto.TenantFilter) (*requests.Pager, e
|
||||
return services.Tenant.Pager(ctx, filter)
|
||||
}
|
||||
|
||||
// create
|
||||
//
|
||||
// @Summary 创建租户并设置租户管理员
|
||||
// @Tags Super
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param form body dto.TenantCreateForm true "Form"
|
||||
// @Success 200 {object} models.Tenant
|
||||
//
|
||||
// @Router /super/v1/tenants [post]
|
||||
// @Bind form body
|
||||
func (*tenant) create(ctx fiber.Ctx, form *dto.TenantCreateForm) (*models.Tenant, error) {
|
||||
return services.Tenant.SuperCreateTenant(ctx, form)
|
||||
}
|
||||
|
||||
// updateExpire
|
||||
//
|
||||
// @Summary 更新过期时间
|
||||
|
||||
@@ -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