feat: 添加订单详情和退款功能,更新用户角色管理,增强超级管理员鉴权

This commit is contained in:
2025-12-24 09:48:31 +08:00
parent fcbc6bd394
commit 1e1132718c
17 changed files with 586 additions and 6 deletions

View File

@@ -417,6 +417,66 @@ func (s *order) SuperOrderPage(ctx context.Context, filter *superdto.OrderPageFi
}, nil
}
// SuperOrderDetail 平台侧订单详情(跨租户)。
func (s *order) SuperOrderDetail(ctx context.Context, orderID int64) (*superdto.SuperOrderDetail, error) {
if orderID <= 0 {
return nil, errorx.ErrInvalidParameter.WithMsg("order_id must be > 0")
}
tbl, query := models.OrderQuery.QueryContext(ctx)
orderModel, err := query.Preload(tbl.Items).Where(tbl.ID.Eq(orderID)).First()
if err != nil {
return nil, err
}
var tenantLite *superdto.OrderTenantLite
if orderModel.TenantID > 0 {
tTbl, tQuery := models.TenantQuery.QueryContext(ctx)
tenantModel, err := tQuery.Where(tTbl.ID.Eq(orderModel.TenantID)).First()
if err != nil {
return nil, err
}
tenantLite = &superdto.OrderTenantLite{ID: tenantModel.ID, Code: tenantModel.Code, Name: tenantModel.Name}
}
var buyerLite *superdto.OrderBuyerLite
if orderModel.UserID > 0 {
uTbl, uQuery := models.UserQuery.QueryContext(ctx)
userModel, err := uQuery.Where(uTbl.ID.Eq(orderModel.UserID)).First()
if err != nil {
return nil, err
}
buyerLite = &superdto.OrderBuyerLite{ID: userModel.ID, Username: userModel.Username}
}
return &superdto.SuperOrderDetail{
Order: orderModel,
Tenant: tenantLite,
Buyer: buyerLite,
}, nil
}
// SuperRefundOrder 平台侧发起退款(跨租户)。
func (s *order) SuperRefundOrder(
ctx context.Context,
operatorUserID, orderID int64,
force bool,
reason, idempotencyKey string,
now time.Time,
) (*models.Order, error) {
if operatorUserID <= 0 || orderID <= 0 {
return nil, errorx.ErrInvalidParameter.WithMsg("operator_user_id/order_id must be > 0")
}
tbl, query := models.OrderQuery.QueryContext(ctx)
orderModel, err := query.Where(tbl.ID.Eq(orderID)).First()
if err != nil {
return nil, err
}
return s.AdminRefundOrder(ctx, orderModel.TenantID, operatorUserID, orderID, force, reason, idempotencyKey, now)
}
// PurchaseContentParams 定义“租户内使用余额购买内容”的入参。
type PurchaseContentParams struct {
// TenantID 租户 ID多租户隔离范围

View File

@@ -276,6 +276,35 @@ func (t *user) UpdateStatus(ctx context.Context, userID int64, status consts.Use
return nil
}
// UpdateRoles 更新用户角色(超级管理员侧)。
func (t *user) UpdateRoles(ctx context.Context, userID int64, roles []consts.Role) error {
if userID <= 0 {
return errors.New("user_id must be > 0")
}
roles = lo.Uniq(lo.Filter(roles, func(r consts.Role, _ int) bool {
return r != ""
}))
if len(roles) == 0 {
return errors.New("roles is empty")
}
// 约定:系统用户至少包含 user 角色。
if !lo.Contains(roles, consts.RoleUser) {
roles = append(roles, consts.RoleUser)
}
roles = lo.Uniq(roles)
m, err := t.FindByID(ctx, userID)
if err != nil {
return err
}
m.Roles = types.NewArray(roles)
_, err = m.Update(ctx)
return err
}
// Statistics 按状态统计用户数量(超级管理员侧)。
func (t *user) Statistics(ctx context.Context) ([]*dto.UserStatistics, error) {
tbl, query := models.UserQuery.QueryContext(ctx)