完善购买和套餐功能

This commit is contained in:
2026-09-16 17:26:02 +08:00
parent 898dedd75a
commit b48688cbab
19 changed files with 554 additions and 58 deletions
+171 -28
View File
@@ -103,10 +103,12 @@ type tenantPackagePayload struct {
UserQuota *int `json:"user_quota"`
ExtraUserPrice *float64 `json:"extra_user_price"`
IsDefault *int8 `json:"is_default"`
Sort *int `json:"sort"`
Status *int8 `json:"status"`
Remark string `json:"remark"`
Modules []string `json:"modules"`
// AllowRepurchase 是否允许重复购买:1允许(默认) 0仅可购买一次(体验套餐)
AllowRepurchase *int8 `json:"allow_repurchase"`
Sort *int `json:"sort"`
Status *int8 `json:"status"`
Remark string `json:"remark"`
Modules []string `json:"modules"`
}
// normalizePackageDuration 套餐有效时长(天)规范化:<=0 时按 365(年付)
@@ -193,6 +195,7 @@ func (c *PlatformTenantPackageController) GetPackageList() {
"user_quota": r.UserQuota,
"extra_user_price": r.ExtraUserPrice,
"is_default": r.IsDefault,
"allow_repurchase": r.AllowRepurchase,
"sort": r.Sort,
"status": r.Status,
"remark": r.Remark,
@@ -249,6 +252,7 @@ func (c *PlatformTenantPackageController) GetPackageSelectList() {
"user_quota": r.UserQuota,
"extra_user_price": r.ExtraUserPrice,
"is_default": r.IsDefault,
"allow_repurchase": r.AllowRepurchase,
"modules": modules,
"module_codes": moduleCodes(modules),
})
@@ -393,22 +397,27 @@ func (c *PlatformTenantPackageController) CreatePackage() {
now := time.Now()
row := &models.SystemTenantPackage{
Name: p.Name,
Code: p.Code,
Description: strings.TrimSpace(p.Description),
Price: valueFloat(p.Price, 0),
DurationDays: normalizePackageDuration(p.DurationDays),
UserQuota: valueInt(p.UserQuota, services.DefaultTenantUserQuota),
ExtraUserPrice: valueFloat(p.ExtraUserPrice, services.DefaultTenantExtraUserPrice),
IsDefault: valueInt8(p.IsDefault, 0),
Sort: valueInt(p.Sort, 0),
Status: valueInt8(p.Status, 1),
Remark: strings.TrimSpace(p.Remark),
UpdateTime: &now,
Name: p.Name,
Code: p.Code,
Description: strings.TrimSpace(p.Description),
Price: valueFloat(p.Price, 0),
DurationDays: normalizePackageDuration(p.DurationDays),
UserQuota: valueInt(p.UserQuota, services.DefaultTenantUserQuota),
ExtraUserPrice: valueFloat(p.ExtraUserPrice, services.DefaultTenantExtraUserPrice),
IsDefault: valueInt8(p.IsDefault, 0),
AllowRepurchase: valueInt8(p.AllowRepurchase, 1),
Sort: valueInt(p.Sort, 0),
Status: valueInt8(p.Status, 1),
Remark: strings.TrimSpace(p.Remark),
UpdateTime: &now,
}
if row.UserQuota <= 0 {
row.UserQuota = services.DefaultTenantUserQuota
}
// 体验套餐(不允许重复购买)不涉及用户数增购,增购价恒为 0
if row.AllowRepurchase == 0 {
row.ExtraUserPrice = 0
}
id, err := models.Orm.Insert(row)
if err != nil {
c.jsonErr(500, 500, "创建失败:"+err.Error())
@@ -483,6 +492,26 @@ func (c *PlatformTenantPackageController) EditPackage() {
update["is_default"] = *p.IsDefault
update["status"] = 1
}
if p.AllowRepurchase != nil {
update["allow_repurchase"] = *p.AllowRepurchase
}
// 体验套餐(不允许重复购买)不涉及用户数增购,增购价恒为 0
allowRepurchase := int8(1)
if p.AllowRepurchase != nil {
allowRepurchase = *p.AllowRepurchase
} else {
var cur models.SystemTenantPackage
if err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
Filter("id", id).
Filter("delete_time__isnull", true).
One(&cur); err == nil {
allowRepurchase = cur.AllowRepurchase
}
}
if allowRepurchase == 0 {
update["extra_user_price"] = 0
}
if _, err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
Filter("id", id).
@@ -512,12 +541,21 @@ func clearOtherDefaultPackages(keepID uint64) {
}
// DeletePackage DELETE /platform/tenantPackage/delete/:id(软删)
// 注意:基础套餐(code=basic)为系统内置兜底套餐(套餐到期自动降级目标),任何情况下不允许删除。
func (c *PlatformTenantPackageController) DeletePackage() {
id := parseUint64Param(c.Ctx.Input.Param(":id"))
if id == 0 {
c.jsonErr(400, 400, "参数错误")
return
}
var pkg models.SystemTenantPackage
if err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
Filter("id", id).
Filter("delete_time__isnull", true).
One(&pkg); err == nil && strings.EqualFold(strings.TrimSpace(pkg.Code), "basic") {
c.jsonErr(400, 400, "基础套餐为系统内置套餐,不允许删除")
return
}
// 有租户正在使用该套餐时不允许删除
used, _ := models.Orm.QueryTable(new(models.SystemTenant)).Filter("package_id", id).Count()
if used > 0 {
@@ -744,23 +782,128 @@ func (c *PlatformTenantPackageController) GetTenantQuotaInfo() {
moduleList = append(moduleList, tenantPackageModuleItem{ModuleCode: m.ModuleCode, ModuleName: m.ModuleName})
}
// 已开通单品功能:单品订单不改变租户套餐,需与套餐模块并集展示,避免“买了功能看不到”
productOrders, productModules := loadTenantProductEntitlements(tid)
c.jsonOK(map[string]interface{}{
"tid": info.Tid,
"package_id": info.PackageID,
"tid": info.Tid,
"package_id": info.PackageID,
"effective_package_id": info.EffectivePackageID,
"package_name": info.PackageName,
"modules": moduleList,
"quota": info.Quota,
"used": info.Used,
"remaining": info.Remaining,
"extra_user_price": info.ExtraUserPrice,
"duration_days": info.DurationDays,
"package_expire_time": info.PackageExpireTime,
"days_remaining": info.DaysRemaining,
"quota_packages": quotaPackages,
"package_name": info.PackageName,
"modules": moduleList,
"product_modules": productModules,
"product_orders": productOrders,
"quota": info.Quota,
"used": info.Used,
"remaining": info.Remaining,
"extra_user_price": info.ExtraUserPrice,
"duration_days": info.DurationDays,
"package_expire_time": info.PackageExpireTime,
"days_remaining": info.DaysRemaining,
"quota_packages": quotaPackages,
}, "获取成功")
}
// productEntitlement 已开通单品功能明细(租户详情「套餐与用户数」展示 / 关闭用)
type productEntitlement struct {
OrderID uint64 `json:"order_id"`
OrderNo string `json:"order_no"`
ProductID uint64 `json:"product_id"`
ProductName string `json:"product_name"`
ModuleCode string `json:"module_code"`
ModuleName string `json:"module_name"`
Amount float64 `json:"amount"`
ActivateTime *time.Time `json:"activate_time"`
ExpireTime *time.Time `json:"expire_time"`
// Expired 是否已过有效期:到期后租户端权益自动失效,需续费
Expired bool `json:"expired"`
}
// loadTenantProductEntitlements 加载租户已开通的单品功能:
// - orders 订单明细(按开通时间倒序),供面板展示与「关闭功能」操作;
// - modules 去重后的模块列表,与套餐模块并集构成租户完整功能权益。
func loadTenantProductEntitlements(tid uint64) ([]productEntitlement, []tenantPackageModuleItem) {
orders := make([]productEntitlement, 0)
modules := make([]tenantPackageModuleItem, 0)
if tid == 0 {
return orders, modules
}
var rows []models.PlatformProductOrder
if _, err := models.Orm.QueryTable(new(models.PlatformProductOrder)).
Filter("tid", tid).
Filter("status", models.ProductOrderStatusActivated).
Filter("product_id__gt", 0).
Filter("delete_time__isnull", true).
OrderBy("-id").
All(&rows); err != nil || len(rows) == 0 {
return orders, modules
}
productIDs := make([]uint64, 0, len(rows))
for _, r := range rows {
productIDs = append(productIDs, r.ProductID)
}
var products []models.PlatformProduct
_, _ = models.Orm.QueryTable(new(models.PlatformProduct)).
Filter("id__in", productIDs).
All(&products)
productMap := make(map[uint64]models.PlatformProduct, len(products))
for _, p := range products {
productMap[p.ID] = p
}
// 模块 code -> 名称(大小写/空格不敏感,与租户端权益判定保持一致)
var moduleRows []models.SystemModules
_, _ = models.Orm.QueryTable(new(models.SystemModules)).
Filter("delete_time__isnull", true).
All(&moduleRows)
moduleNameMap := make(map[string]string, len(moduleRows))
for _, m := range moduleRows {
moduleNameMap[services.NormalizeModuleCode(m.Code)] = m.Name
}
now := time.Now()
seen := map[string]bool{}
for _, r := range rows {
code, name := "", ""
if p, ok := productMap[r.ProductID]; ok {
code = strings.TrimSpace(p.ModuleCode)
if code == "" {
// 产品未显式关联模块时按产品编码兜底(与 GetTenantModuleCodes 一致)
code = strings.TrimSpace(p.Code)
}
name = p.Name
}
normCode := services.NormalizeModuleCode(code)
expire := services.ProductOrderExpireAt(&r)
expired := services.ProductOrderExpired(&r, now)
// 权益模块只统计仍在有效期内的订单,与租户端 GetTenantModuleCodes 判定保持一致
if !expired && normCode != "" && !seen[normCode] {
seen[normCode] = true
moduleName := moduleNameMap[normCode]
if moduleName == "" {
moduleName = name
}
modules = append(modules, tenantPackageModuleItem{ModuleCode: code, ModuleName: moduleName})
}
// 明细保留全部已开通订单(含已过期):便于平台看到「已过期,待续费并关闭」
orders = append(orders, productEntitlement{
OrderID: r.ID,
OrderNo: r.OrderNo,
ProductID: r.ProductID,
ProductName: name,
ModuleCode: code,
ModuleName: moduleNameMap[normCode],
Amount: r.Amount,
ActivateTime: r.ActivateTime,
ExpireTime: expire,
Expired: expired,
})
}
return orders, modules
}
type rechargeQuotaPayload struct {
Tid uint64 `json:"tid"`
Type int8 `json:"type"` // 1 单个增购 2 套餐增购