355 lines
8.8 KiB
Go
355 lines
8.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/identity"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// PlatformStorageController 平台端附件空间管理:
|
|
// 套餐维护、租户空间用量与基础额度调整、订单查询。
|
|
type PlatformStorageController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
func (c *PlatformStorageController) fail(httpStatus, bizCode int, msg string) {
|
|
c.Ctx.Output.SetStatus(httpStatus)
|
|
c.Data["json"] = map[string]any{"code": bizCode, "msg": msg}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *PlatformStorageController) ok(data any) {
|
|
c.Data["json"] = map[string]any{"code": 200, "msg": "success", "data": data}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func (c *PlatformStorageController) auth() bool {
|
|
if _, err := requirePlatform(&c.Controller); err != nil {
|
|
code := identity.Status(err)
|
|
c.fail(code, code, err.Error())
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
type storagePackagePayload struct {
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
CapacityMB int `json:"capacity_mb"`
|
|
Price *float64 `json:"price"`
|
|
DurationDays int `json:"duration_days"`
|
|
Sort int `json:"sort"`
|
|
Status *int8 `json:"status"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
|
|
// Packages GET /platform/storage/packages 套餐列表(含下架)
|
|
func (c *PlatformStorageController) Packages() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
var rows []models.SystemTenantStoragePackage
|
|
_, err := models.Orm.QueryTable(new(models.SystemTenantStoragePackage)).
|
|
Filter("delete_time__isnull", true).
|
|
OrderBy("sort", "id").
|
|
All(&rows)
|
|
if err != nil {
|
|
c.fail(500, 500, "查询失败: "+err.Error())
|
|
return
|
|
}
|
|
list := make([]map[string]any, 0, len(rows))
|
|
for i := range rows {
|
|
r := &rows[i]
|
|
list = append(list, map[string]any{
|
|
"id": r.ID,
|
|
"name": r.Name,
|
|
"code": r.Code,
|
|
"capacity_mb": r.CapacityMB,
|
|
"price": r.Price,
|
|
"duration_days": r.DurationDays,
|
|
"sort": r.Sort,
|
|
"status": r.Status,
|
|
"remark": r.Remark,
|
|
"create_time": r.CreateTime,
|
|
})
|
|
}
|
|
c.ok(map[string]any{"list": list})
|
|
}
|
|
|
|
// CreatePackage POST /platform/storage/package/create
|
|
func (c *PlatformStorageController) CreatePackage() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
var p storagePackagePayload
|
|
if err := c.BindJSON(&p); err != nil {
|
|
c.fail(400, 400, "参数错误")
|
|
return
|
|
}
|
|
name := strings.TrimSpace(p.Name)
|
|
if name == "" {
|
|
c.fail(400, 400, "请填写套餐名称")
|
|
return
|
|
}
|
|
if p.CapacityMB <= 0 {
|
|
c.fail(400, 400, "容量必须大于 0")
|
|
return
|
|
}
|
|
status := int8(1)
|
|
if p.Status != nil {
|
|
status = *p.Status
|
|
}
|
|
duration := p.DurationDays
|
|
if duration <= 0 {
|
|
duration = 365
|
|
}
|
|
price := 0.0
|
|
if p.Price != nil && *p.Price > 0 {
|
|
price = *p.Price
|
|
}
|
|
|
|
row := &models.SystemTenantStoragePackage{
|
|
Name: name,
|
|
Code: strings.TrimSpace(p.Code),
|
|
CapacityMB: p.CapacityMB,
|
|
Price: price,
|
|
DurationDays: duration,
|
|
Sort: p.Sort,
|
|
Status: status,
|
|
Remark: strings.TrimSpace(p.Remark),
|
|
}
|
|
id, err := models.Orm.Insert(row)
|
|
if err != nil {
|
|
c.fail(500, 500, "创建失败: "+err.Error())
|
|
return
|
|
}
|
|
c.ok(map[string]any{"id": id})
|
|
}
|
|
|
|
// UpdatePackage POST /platform/storage/package/:id
|
|
func (c *PlatformStorageController) UpdatePackage() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的套餐ID")
|
|
return
|
|
}
|
|
var p storagePackagePayload
|
|
if err := c.BindJSON(&p); err != nil {
|
|
c.fail(400, 400, "参数错误")
|
|
return
|
|
}
|
|
|
|
update := map[string]any{"update_time": time.Now()}
|
|
if name := strings.TrimSpace(p.Name); name != "" {
|
|
update["name"] = name
|
|
}
|
|
if p.Code != "" {
|
|
update["code"] = strings.TrimSpace(p.Code)
|
|
}
|
|
if p.CapacityMB > 0 {
|
|
update["capacity_mb"] = p.CapacityMB
|
|
}
|
|
if p.Price != nil {
|
|
update["price"] = *p.Price
|
|
}
|
|
if p.DurationDays > 0 {
|
|
update["duration_days"] = p.DurationDays
|
|
}
|
|
update["sort"] = p.Sort
|
|
if p.Status != nil {
|
|
update["status"] = *p.Status
|
|
}
|
|
update["remark"] = strings.TrimSpace(p.Remark)
|
|
|
|
n, err := models.Orm.QueryTable(new(models.SystemTenantStoragePackage)).
|
|
Filter("id", id).
|
|
Filter("delete_time__isnull", true).
|
|
Update(update)
|
|
if err != nil {
|
|
c.fail(500, 500, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
if n == 0 {
|
|
c.fail(404, 404, "套餐不存在")
|
|
return
|
|
}
|
|
c.ok(nil)
|
|
}
|
|
|
|
// TogglePackage POST /platform/storage/package/:id/status body: {"status":0|1}
|
|
func (c *PlatformStorageController) TogglePackage() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的套餐ID")
|
|
return
|
|
}
|
|
var p struct {
|
|
Status int8 `json:"status"`
|
|
}
|
|
if err := c.BindJSON(&p); err != nil {
|
|
c.fail(400, 400, "参数错误")
|
|
return
|
|
}
|
|
if p.Status != 0 && p.Status != 1 {
|
|
c.fail(400, 400, "状态值不合法")
|
|
return
|
|
}
|
|
_, err = models.Orm.QueryTable(new(models.SystemTenantStoragePackage)).
|
|
Filter("id", id).
|
|
Filter("delete_time__isnull", true).
|
|
Update(map[string]any{"status": p.Status, "update_time": time.Now()})
|
|
if err != nil {
|
|
c.fail(500, 500, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
c.ok(nil)
|
|
}
|
|
|
|
// DeletePackage DELETE /platform/storage/package/:id
|
|
func (c *PlatformStorageController) DeletePackage() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.fail(400, 400, "无效的套餐ID")
|
|
return
|
|
}
|
|
n, err := models.Orm.QueryTable(new(models.SystemTenantStoragePackage)).
|
|
Filter("id", id).
|
|
Filter("delete_time__isnull", true).
|
|
Update(map[string]any{"delete_time": time.Now(), "update_time": time.Now()})
|
|
if err != nil {
|
|
c.fail(500, 500, "删除失败: "+err.Error())
|
|
return
|
|
}
|
|
if n == 0 {
|
|
c.fail(404, 404, "套餐不存在")
|
|
return
|
|
}
|
|
c.ok(nil)
|
|
}
|
|
|
|
// TenantInfo GET /platform/storage/tenant/:tid 租户空间用量
|
|
func (c *PlatformStorageController) TenantInfo() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
tid, err := strconv.ParseUint(c.Ctx.Input.Param(":tid"), 10, 64)
|
|
if err != nil || tid == 0 {
|
|
c.fail(400, 400, "无效的租户ID")
|
|
return
|
|
}
|
|
info, err := services.GetTenantStorageInfo(tid)
|
|
if err != nil {
|
|
c.fail(500, 500, "获取失败: "+err.Error())
|
|
return
|
|
}
|
|
c.ok(info)
|
|
}
|
|
|
|
// UpdateTenantQuota POST /platform/storage/tenant/:tid/quota body: {"storage_quota_mb": 500}
|
|
// 平台直接调整租户基础额度(赠送 / 降级),不影响已购买的套餐。
|
|
func (c *PlatformStorageController) UpdateTenantQuota() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
tid, err := strconv.ParseUint(c.Ctx.Input.Param(":tid"), 10, 64)
|
|
if err != nil || tid == 0 {
|
|
c.fail(400, 400, "无效的租户ID")
|
|
return
|
|
}
|
|
var p struct {
|
|
StorageQuotaMB int `json:"storage_quota_mb"`
|
|
}
|
|
if err := c.BindJSON(&p); err != nil {
|
|
c.fail(400, 400, "参数错误")
|
|
return
|
|
}
|
|
if p.StorageQuotaMB < 0 {
|
|
c.fail(400, 400, "额度不能为负数")
|
|
return
|
|
}
|
|
if p.StorageQuotaMB == 0 {
|
|
p.StorageQuotaMB = services.DefaultTenantStorageMB
|
|
}
|
|
|
|
_, err = models.Orm.QueryTable(new(models.SystemTenant)).
|
|
Filter("id", tid).
|
|
Update(map[string]any{"storage_quota_mb": p.StorageQuotaMB})
|
|
if err != nil {
|
|
c.fail(500, 500, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
info, _ := services.GetTenantStorageInfo(tid)
|
|
c.ok(info)
|
|
}
|
|
|
|
// Orders GET /platform/storage/orders?tid=&status=&page=&pageSize=
|
|
func (c *PlatformStorageController) Orders() {
|
|
if !c.auth() {
|
|
return
|
|
}
|
|
page, _ := strconv.Atoi(c.GetString("page", "1"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
pageSize, _ := strconv.Atoi(c.GetString("pageSize", "10"))
|
|
if pageSize <= 0 || pageSize > 100 {
|
|
pageSize = 10
|
|
}
|
|
|
|
qs := models.Orm.QueryTable(new(models.SystemTenantStorageOrder)).
|
|
Filter("delete_time__isnull", true)
|
|
if tid, err := strconv.ParseUint(strings.TrimSpace(c.GetString("tid")), 10, 64); err == nil && tid > 0 {
|
|
qs = qs.Filter("tid", tid)
|
|
}
|
|
if st := strings.TrimSpace(c.GetString("status")); st != "" {
|
|
qs = qs.Filter("status", st)
|
|
}
|
|
|
|
total, err := qs.Count()
|
|
if err != nil {
|
|
c.fail(500, 500, "查询失败: "+err.Error())
|
|
return
|
|
}
|
|
var rows []models.SystemTenantStorageOrder
|
|
if _, err := qs.OrderBy("-id").Offset((page - 1) * pageSize).Limit(pageSize).All(&rows); err != nil {
|
|
c.fail(500, 500, "查询失败: "+err.Error())
|
|
return
|
|
}
|
|
|
|
list := make([]map[string]any, 0, len(rows))
|
|
for i := range rows {
|
|
r := &rows[i]
|
|
list = append(list, map[string]any{
|
|
"order_no": r.OrderNo,
|
|
"tid": r.Tid,
|
|
"package_id": r.PackageID,
|
|
"package_name": r.PackageName,
|
|
"capacity_mb": r.CapacityMB,
|
|
"duration_days": r.DurationDays,
|
|
"amount": r.Amount,
|
|
"status": r.Status,
|
|
"pay_no": r.PayNo,
|
|
"start_time": r.StartTime,
|
|
"end_time": r.EndTime,
|
|
"operator": r.Operator,
|
|
"create_time": r.CreateTime,
|
|
})
|
|
}
|
|
c.ok(map[string]any{"list": list, "total": total, "page": page, "pageSize": pageSize})
|
|
}
|