增加租户套餐
This commit is contained in:
@@ -2,12 +2,14 @@ package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/services"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
@@ -27,10 +29,43 @@ type tenantDTO struct {
|
||||
Address string `json:"address"`
|
||||
Worktime string `json:"worktime"`
|
||||
Status int8 `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreateTime *time.Time `json:"create_time,omitempty"`
|
||||
UpdateTime *time.Time `json:"update_time,omitempty"`
|
||||
DeleteTime *time.Time `json:"delete_time,omitempty"`
|
||||
// 套餐与用户数配额
|
||||
PackageID uint64 `json:"package_id"`
|
||||
PackageName string `json:"package_name"`
|
||||
UserQuota int `json:"user_quota"`
|
||||
UserUsed int `json:"user_used"`
|
||||
Remark string `json:"remark"`
|
||||
CreateTime *time.Time `json:"create_time,omitempty"`
|
||||
UpdateTime *time.Time `json:"update_time,omitempty"`
|
||||
DeleteTime *time.Time `json:"delete_time,omitempty"`
|
||||
}
|
||||
|
||||
// tenantPackageNameMap 套餐ID -> 套餐名称
|
||||
func tenantPackageNameMap() map[uint64]string {
|
||||
result := map[uint64]string{}
|
||||
var rows []models.SystemTenantPackage
|
||||
if _, err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
|
||||
Filter("delete_time__isnull", true).
|
||||
All(&rows, "ID", "Name"); err == nil {
|
||||
for _, r := range rows {
|
||||
result[r.ID] = r.Name
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// tenantUserCountMap 租户ID -> 有效用户数
|
||||
func tenantUserCountMap() map[uint64]int {
|
||||
result := map[uint64]int{}
|
||||
var rows []models.SystemTenantUser
|
||||
if _, err := models.Orm.QueryTable(new(models.SystemTenantUser)).
|
||||
Filter("delete_time__isnull", true).
|
||||
All(&rows, "ID", "Tid"); err == nil {
|
||||
for _, r := range rows {
|
||||
result[r.Tid]++
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func stringValue(s *string) string {
|
||||
@@ -57,6 +92,8 @@ func toTenantDTO(t models.SystemTenant) tenantDTO {
|
||||
Address: stringValue(t.Address),
|
||||
Worktime: stringValue(t.Worktime),
|
||||
Status: t.Status,
|
||||
PackageID: t.PackageID,
|
||||
UserQuota: t.UserQuota,
|
||||
Remark: stringValue(t.Remark),
|
||||
CreateTime: &ct,
|
||||
UpdateTime: &ut,
|
||||
@@ -110,9 +147,14 @@ func (c *PlatformTenantController) GetTenant() {
|
||||
return
|
||||
}
|
||||
|
||||
packageNames := tenantPackageNameMap()
|
||||
userCounts := tenantUserCountMap()
|
||||
list := make([]tenantDTO, 0, len(rows))
|
||||
for _, t := range rows {
|
||||
list = append(list, toTenantDTO(t))
|
||||
dto := toTenantDTO(t)
|
||||
dto.PackageName = packageNames[t.PackageID]
|
||||
dto.UserUsed = userCounts[t.ID]
|
||||
list = append(list, dto)
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
@@ -144,10 +186,19 @@ func (c *PlatformTenantController) GetTenantDetail() {
|
||||
return
|
||||
}
|
||||
|
||||
dto := toTenantDTO(t)
|
||||
if pkg, err := services.GetTenantPackage(t.ID); err == nil && pkg != nil {
|
||||
dto.PackageID = pkg.ID
|
||||
dto.PackageName = pkg.Name
|
||||
}
|
||||
if used, err := services.CountTenantUsers(t.ID); err == nil {
|
||||
dto.UserUsed = used
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": toTenantDTO(t),
|
||||
"data": dto,
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
@@ -161,7 +212,10 @@ type tenantPayload struct {
|
||||
Address string `json:"address"`
|
||||
Worktime string `json:"worktime"`
|
||||
Status *int8 `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
// 套餐与用户数上限(可选)
|
||||
PackageID *uint64 `json:"package_id"`
|
||||
UserQuota *int `json:"user_quota"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
func (c *PlatformTenantController) parseTenantPayload() (tenantPayload, error) {
|
||||
@@ -182,6 +236,16 @@ func (c *PlatformTenantController) parseTenantPayload() (tenantPayload, error) {
|
||||
p.Status = &tmp
|
||||
}
|
||||
}
|
||||
if s := strings.TrimSpace(c.GetString("package_id")); s != "" {
|
||||
if v, err := strconv.ParseUint(s, 10, 64); err == nil {
|
||||
p.PackageID = &v
|
||||
}
|
||||
}
|
||||
if s := strings.TrimSpace(c.GetString("user_quota")); s != "" {
|
||||
if v, err := strconv.Atoi(s); err == nil {
|
||||
p.UserQuota = &v
|
||||
}
|
||||
}
|
||||
|
||||
// ĺŚćĺ
łéŽĺ掾为犺ďźĺ°čŻäť JSON body č§ŁćďźeditTenant éťčޤ JSONďź?
|
||||
if p.TenantName == "" && p.TenantCode == "" {
|
||||
@@ -226,6 +290,36 @@ func (c *PlatformTenantController) CreateTenant() {
|
||||
status = *p.Status
|
||||
}
|
||||
|
||||
// 套餐与用户数上限:未指定套餐时使用默认套餐,用户数取其包含用户数(兜底 20)
|
||||
packageID := uint64(0)
|
||||
userQuota := services.DefaultTenantUserQuota
|
||||
if p.PackageID != nil && *p.PackageID > 0 {
|
||||
packageID = *p.PackageID
|
||||
}
|
||||
if packageID == 0 {
|
||||
if pkg, err := services.GetDefaultTenantPackage(); err == nil && pkg != nil {
|
||||
packageID = pkg.ID
|
||||
if pkg.UserQuota > 0 {
|
||||
userQuota = pkg.UserQuota
|
||||
}
|
||||
}
|
||||
}
|
||||
if packageID > 0 {
|
||||
var pkg models.SystemTenantPackage
|
||||
if err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
|
||||
Filter("id", packageID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&pkg); err == nil && pkg.UserQuota > 0 {
|
||||
userQuota = pkg.UserQuota
|
||||
}
|
||||
}
|
||||
if p.UserQuota != nil && *p.UserQuota > 0 {
|
||||
userQuota = *p.UserQuota
|
||||
}
|
||||
if userQuota <= 0 {
|
||||
userQuota = services.DefaultTenantUserQuota
|
||||
}
|
||||
|
||||
t := models.SystemTenant{
|
||||
TenantCode: p.TenantCode,
|
||||
TenantName: p.TenantName,
|
||||
@@ -235,6 +329,8 @@ func (c *PlatformTenantController) CreateTenant() {
|
||||
Address: stringPtr(p.Address),
|
||||
Worktime: stringPtr(p.Worktime),
|
||||
Status: status,
|
||||
PackageID: packageID,
|
||||
UserQuota: userQuota,
|
||||
Remark: stringPtr(p.Remark),
|
||||
}
|
||||
|
||||
@@ -282,6 +378,37 @@ func (c *PlatformTenantController) EditTenant() {
|
||||
if p.Status != nil {
|
||||
update["status"] = *p.Status
|
||||
}
|
||||
if p.PackageID != nil {
|
||||
pkgID := *p.PackageID
|
||||
if pkgID > 0 {
|
||||
var pkg models.SystemTenantPackage
|
||||
if err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
|
||||
Filter("id", pkgID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&pkg); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "套餐不存在"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
}
|
||||
update["package_id"] = pkgID
|
||||
}
|
||||
if p.UserQuota != nil {
|
||||
// 用户数上限只增不减:不允许调低到当前已开用户数以下
|
||||
used, _ := services.CountTenantUsers(id)
|
||||
quota := *p.UserQuota
|
||||
if quota > 0 && quota < used {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 400,
|
||||
"msg": fmt.Sprintf("用户数上限不能小于该租户已开用户数(%d)", used),
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
if quota > 0 {
|
||||
update["user_quota"] = quota
|
||||
}
|
||||
}
|
||||
|
||||
_, err = models.Orm.QueryTable(new(models.SystemTenant)).Filter("id", id).Update(update)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user