feat: 添加租户创建功能,支持设置管理员及有效期,新增订单统计接口

This commit is contained in:
2025-12-23 23:03:49 +08:00
parent d09a9374ee
commit bcb8c822f1
15 changed files with 377 additions and 3 deletions

View 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"`
}

View File

@@ -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"`
}

View 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)
}

View File

@@ -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{}

View File

@@ -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,

View File

@@ -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 更新过期时间

View File

@@ -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

View File

@@ -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 {

View File

@@ -49,6 +49,10 @@
核心对象:`dto.TenantItem`
- 创建租户并关联租户管理员(新增)
- 表单字段:`code``name``admin_user_id``duration(7/30/90/180/365 天)`
- API`POST /super/v1/tenants``dto.TenantCreateForm`
- 行为:创建 `tenants` 记录,并创建 `tenant_users` 关系,角色为 `tenant_admin`
- 租户列表(筛选/分页/排序)
- 过滤:`name``status`
- 排序:`asc/desc`
@@ -87,4 +91,3 @@
- **swagger 不一致**`dto.TenantStatusUpdateForm.status` / `dto.UserStatusUpdateForm.status` 在 swagger 里额外出现 `normal/disabled` enum`consts.*Status` 与列表筛选 enum 为 `pending_verify/verified/banned`;前端应以 `/statuses` 接口返回为准,并推动后端修正 swagger。
- **分页 items 结构疑似不完整**:列表接口 swagger 中 `items` 被标成单个 object`$ref dto.TenantItem`/`dto.UserItem`),实际应为数组;当前前端服务层已做兼容(`normalizeItems`),但建议后端修正 swagger。
- **租户/用户详情与更多运维能力缺失**:目前没有用户详情、租户详情、角色管理、密码重置等超管常见能力;如需要可扩展接口与页面。

View File

@@ -7,7 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sakai Vue</title>
<link href="https://fonts.cdnfonts.com/css/lato" rel="stylesheet">
<script type="module" crossorigin src="./assets/index-BB3R-ZJM.js"></script>
<script type="module" crossorigin src="./assets/index-Brwtp57n.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-Ba8sjR1v.css">
</head>

View File

@@ -0,0 +1,8 @@
import { requestJson } from './apiClient';
export const OrderService = {
async getOrderStatistics() {
return requestJson('/super/v1/orders/statistics');
}
};

View File

@@ -22,6 +22,17 @@ export const TenantService = {
items: normalizeItems(data?.items)
};
},
async createTenant({ code, name, admin_user_id, duration } = {}) {
return requestJson('/super/v1/tenants', {
method: 'POST',
body: {
code,
name,
admin_user_id,
duration
}
});
},
async renewTenantExpire({ tenantID, duration }) {
return requestJson(`/super/v1/tenants/${tenantID}`, {
method: 'PATCH',

View File

@@ -1,5 +1,6 @@
<script setup>
import StatisticsStrip from '@/components/StatisticsStrip.vue';
import { OrderService } from '@/service/OrderService';
import { TenantService } from '@/service/TenantService';
import { UserService } from '@/service/UserService';
import { useToast } from 'primevue/usetoast';
@@ -13,6 +14,15 @@ const tenantLoading = ref(false);
const statistics = ref([]);
const statisticsLoading = ref(false);
const orderStats = ref(null);
const orderStatsLoading = ref(false);
function formatCny(amountInCents) {
const amount = Number(amountInCents) / 100;
if (!Number.isFinite(amount)) return '-';
return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(amount);
}
const statisticsItems = computed(() => {
const total = (statistics.value || []).reduce((sum, row) => sum + (Number(row?.count) || 0), 0);
const statusIcon = (status) => {
@@ -59,6 +69,26 @@ const tenantItems = computed(() => [
}
]);
const orderItems = computed(() => {
const byStatus = new Map((orderStats.value?.by_status || []).map((row) => [row?.status, row]));
const getCount = (status) => byStatus.get(status)?.count ?? 0;
const totalCount = orderStats.value?.total_count ?? 0;
const totalAmountPaidSum = orderStats.value?.total_amount_paid_sum ?? 0;
const refundingCount = getCount('refunding');
const refundedCount = getCount('refunded');
const refundRequestedCount = refundingCount + refundedCount;
return [
{ key: 'orders-total', label: '订单总数:', value: orderStatsLoading.value ? '-' : totalCount, icon: 'pi-shopping-cart' },
{ key: 'orders-paid', label: '已支付:', value: orderStatsLoading.value ? '-' : getCount('paid'), icon: 'pi-check-circle' },
{ key: 'orders-refunding', label: '退款中:', value: orderStatsLoading.value ? '-' : refundingCount, icon: 'pi-spin pi-spinner' },
{ key: 'orders-refund-requested', label: '退款申请:', value: orderStatsLoading.value ? '-' : refundRequestedCount, icon: 'pi-undo' },
{ key: 'orders-refunded', label: '已退款:', value: orderStatsLoading.value ? '-' : getCount('refunded'), icon: 'pi-undo' },
{ key: 'orders-amount', label: '实付总额:', value: orderStatsLoading.value ? '-' : formatCny(totalAmountPaidSum), icon: 'pi-wallet' }
];
});
async function loadTenantTotal() {
tenantLoading.value = true;
try {
@@ -82,15 +112,28 @@ async function loadStatistics() {
}
}
async function loadOrderStatistics() {
orderStatsLoading.value = true;
try {
orderStats.value = await OrderService.getOrderStatistics();
} catch (error) {
toast.add({ severity: 'error', summary: '加载失败', detail: error?.message || '无法加载订单统计信息', life: 4000 });
} finally {
orderStatsLoading.value = false;
}
}
onMounted(() => {
loadTenantTotal();
loadStatistics();
loadOrderStatistics();
});
</script>
<template>
<div>
<StatisticsStrip :items="tenantItems" containerClass="card mb-4" />
<StatisticsStrip :items="orderItems" containerClass="card mb-4" />
<StatisticsStrip :items="statisticsItems" containerClass="card mb-4" />
<div class="card">

View File

@@ -1,9 +1,10 @@
<script setup>
import { TenantService } from '@/service/TenantService';
import { UserService } from '@/service/UserService';
import SearchField from '@/components/SearchField.vue';
import SearchPanel from '@/components/SearchPanel.vue';
import { useToast } from 'primevue/usetoast';
import { onMounted, ref } from 'vue';
import { computed, onMounted, ref } from 'vue';
const toast = useToast();
@@ -19,6 +20,17 @@ const status = ref('');
const sortField = ref('id');
const sortOrder = ref(-1);
const createDialogVisible = ref(false);
const creating = ref(false);
const createCode = ref('');
const createName = ref('');
const createDuration = ref(365);
const createAdminUserID = ref(null);
const adminSearchUsername = ref('');
const adminSearchLoading = ref(false);
const adminSearchResults = ref([]);
function formatDate(value) {
if (!value) return '-';
const date = new Date(value);
@@ -123,6 +135,63 @@ const durationOptions = [
{ label: '365 天', value: 365 }
];
const canCreateTenant = computed(() => {
return Boolean(createCode.value?.trim()) && Boolean(createName.value?.trim()) && Number(createAdminUserID.value) > 0 && Number(createDuration.value) > 0;
});
function openCreateDialog() {
createCode.value = '';
createName.value = '';
createDuration.value = 365;
createAdminUserID.value = null;
adminSearchUsername.value = '';
adminSearchResults.value = [];
createDialogVisible.value = true;
}
async function searchAdminUsers() {
const keyword = String(adminSearchUsername.value || '').trim();
if (!keyword) {
adminSearchResults.value = [];
return;
}
adminSearchLoading.value = true;
try {
const result = await UserService.listUsers({ page: 1, limit: 10, username: keyword });
adminSearchResults.value = result?.items || [];
} catch (error) {
toast.add({ severity: 'error', summary: '搜索失败', detail: error?.message || '无法搜索用户', life: 4000 });
} finally {
adminSearchLoading.value = false;
}
}
function pickAdmin(user) {
createAdminUserID.value = user?.id ?? null;
}
async function confirmCreateTenant() {
if (!canCreateTenant.value) return;
creating.value = true;
try {
await TenantService.createTenant({
code: createCode.value.trim(),
name: createName.value.trim(),
admin_user_id: Number(createAdminUserID.value),
duration: Number(createDuration.value)
});
toast.add({ severity: 'success', summary: '创建成功', detail: `租户:${createName.value.trim()}`, life: 3000 });
createDialogVisible.value = false;
await loadTenants();
} catch (error) {
toast.add({ severity: 'error', summary: '创建失败', detail: error?.message || '无法创建租户', life: 4000 });
} finally {
creating.value = false;
}
}
function openRenewDialog(item) {
renewTenant.value = item;
renewDuration.value = 30;
@@ -209,6 +278,7 @@ onMounted(() => {
<div class="card">
<div class="flex items-center justify-between mb-4">
<h4 class="m-0">租户列表</h4>
<Button label="创建租户" icon="pi pi-plus" @click="openCreateDialog" />
</div>
<SearchPanel :loading="loading" @search="onSearch" @reset="onReset">
@@ -318,5 +388,61 @@ onMounted(() => {
<Button label="确认" icon="pi pi-check" @click="confirmUpdateTenantStatus" :loading="tenantStatusUpdating" :disabled="!tenantStatusValue" />
</template>
</Dialog>
<Dialog v-model:visible="createDialogVisible" :modal="true" :style="{ width: '720px' }">
<template #header>
<div class="flex items-center gap-2">
<span class="font-medium">创建租户</span>
<span class="text-muted-color">并关联租户管理员</span>
</div>
</template>
<div class="flex flex-col gap-4">
<div class="grid grid-cols-12 gap-4">
<div class="col-span-12 md:col-span-6">
<label class="block font-medium mb-2">租户 Code</label>
<InputText v-model="createCode" placeholder="例如acme" class="w-full" :disabled="creating" />
<small class="text-muted-color">用于 URL 标识建议小写字母/数字创建后不建议修改</small>
</div>
<div class="col-span-12 md:col-span-6">
<label class="block font-medium mb-2">租户名称</label>
<InputText v-model="createName" placeholder="例如ACME 工作室" class="w-full" :disabled="creating" />
</div>
<div class="col-span-12 md:col-span-6">
<label class="block font-medium mb-2">有效期</label>
<Select v-model="createDuration" :options="durationOptions" optionLabel="label" optionValue="value" placeholder="选择有效期" fluid :disabled="creating" />
</div>
<div class="col-span-12 md:col-span-6">
<label class="block font-medium mb-2">租户管理员 UserID</label>
<InputNumber v-model="createAdminUserID" :min="1" placeholder="输入用户ID" class="w-full" :disabled="creating" />
<small class="text-muted-color">下方可通过用户名搜索并一键填入</small>
</div>
</div>
<Divider />
<div class="flex items-end gap-2">
<div class="flex-1">
<label class="block font-medium mb-2">按用户名搜索管理员</label>
<InputText v-model="adminSearchUsername" placeholder="输入用户名关键字" class="w-full" :disabled="creating" @keyup.enter="searchAdminUsers" />
</div>
<Button label="搜索" icon="pi pi-search" :loading="adminSearchLoading" :disabled="creating" @click="searchAdminUsers" />
</div>
<DataTable :value="adminSearchResults" dataKey="id" size="small" :loading="adminSearchLoading" scrollable scrollHeight="220px">
<Column field="id" header="ID" style="width: 7rem" />
<Column field="username" header="用户名" />
<Column field="status_description" header="状态" style="width: 10rem" />
<Column header="操作" style="width: 8rem">
<template #body="{ data }">
<Button label="设为管理员" size="small" severity="secondary" @click="pickAdmin(data)" />
</template>
</Column>
</DataTable>
</div>
<template #footer>
<Button label="取消" icon="pi pi-times" text @click="createDialogVisible = false" :disabled="creating" />
<Button label="确认创建" icon="pi pi-check" @click="confirmCreateTenant" :loading="creating" :disabled="!canCreateTenant" />
</template>
</Dialog>
</div>
</template>

View File

@@ -13,6 +13,7 @@ export default defineConfig({
noDiscovery: true
},
server: {
host: '0.0.0.0',
proxy: {
'/super/v1': {
target: 'http://localhost:8080',