Explorar o código

fix(internal): 增加删除全球限制时检查实例是否过期的功能

- 在 globalLimitService 的 DeleteGlobalLimit 方法中添加了检查实例是否过期的逻辑
- 在 hostRepository 中新增了 GetExpireTime 方法用于获取实例的到期时间
- 在 hostService 中实现了 CheckExpired 方法用于检查实例是否过期
fusu hai 1 mes
pai
achega
56dbc36f55
Modificáronse 3 ficheiros con 52 adicións e 0 borrados
  1. 18 0
      internal/repository/host.go
  2. 9 0
      internal/service/globallimit.go
  3. 25 0
      internal/service/host.go

+ 18 - 0
internal/repository/host.go

@@ -12,6 +12,8 @@ type HostRepository interface {
 	GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error)
 	GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error)
 	GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error)
 	GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error)
 	GetDomainById(ctx context.Context, id int) (string, error)
 	GetDomainById(ctx context.Context, id int) (string, error)
+	// 获取到期时间
+	GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error)
 }
 }
 
 
 func NewHostRepository(
 func NewHostRepository(
@@ -64,4 +66,20 @@ func (r *hostRepository) GetDomainById(ctx context.Context, id int) (string, err
 		return "", err
 		return "", err
 	}
 	}
 	return res, nil
 	return res, nil
+}
+
+// 获取到期时间
+func (r *hostRepository) GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error) {
+	var nextDueDate string
+	err := r.DB(ctx).Table("shd_host").
+		Select("nextduedate").
+		Where("id = ?", hostId).
+		Where("uid = ?", uid).
+		Scan(&nextDueDate).Error
+
+	if err != nil {
+		return "", err
+	}
+
+	return nextDueDate, nil
 }
 }

+ 9 - 0
internal/service/globallimit.go

@@ -395,6 +395,15 @@ func (s *globalLimitService) EditGlobalLimit(ctx context.Context, req v1.GlobalL
 
 
 
 
 func (s *globalLimitService) DeleteGlobalLimit(ctx context.Context, req v1.GlobalLimitRequest) error {
 func (s *globalLimitService) DeleteGlobalLimit(ctx context.Context, req v1.GlobalLimitRequest) error {
+	// 检查是否过期
+	isExpired, err := s.host.CheckExpired(ctx, int64(req.Uid), int64(req.HostId))
+	if err != nil {
+		return err
+	}
+	if isExpired {
+		return fmt.Errorf("实例未过期,无法删除")
+	}
+
 	oldData, err := s.globalLimitRepository.GetGlobalLimitByHostId(ctx, int64(req.HostId))
 	oldData, err := s.globalLimitRepository.GetGlobalLimitByHostId(ctx, int64(req.HostId))
 	if err != nil {
 	if err != nil {
 		return err
 		return err

+ 25 - 0
internal/service/host.go

@@ -8,6 +8,7 @@ import (
 	"github.com/spf13/cast"
 	"github.com/spf13/cast"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"time"
 )
 )
 
 
 // HostService 接口定义
 // HostService 接口定义
@@ -18,6 +19,8 @@ type HostService interface {
 	GetTcpLimitConfig(ctx context.Context, hostId int) (v1.TcpLimitRequest, error)
 	GetTcpLimitConfig(ctx context.Context, hostId int) (v1.TcpLimitRequest, error)
 	GetUdpLimitConfig(ctx context.Context, hostId int) (v1.UdpLimitRequest, error)
 	GetUdpLimitConfig(ctx context.Context, hostId int) (v1.UdpLimitRequest, error)
 	GetWebLimitConfig(ctx context.Context, hostId int) (v1.WebLimitRequest, error)
 	GetWebLimitConfig(ctx context.Context, hostId int) (v1.WebLimitRequest, error)
+	// 检查是否过期 到期false 未到期true
+	CheckExpired(ctx context.Context, uid int64, hostId int64) (bool, error)
 }
 }
 
 
 func NewHostService(
 func NewHostService(
@@ -299,3 +302,25 @@ func (s *hostService) GetWebLimitConfig(ctx context.Context, hostId int) (v1.Web
 
 
 	return data, nil
 	return data, nil
 }
 }
+
+// 检查是否过期 到期false 未到期true
+func (s *hostService) CheckExpired(ctx context.Context, uid int64, hostId int64) (bool, error) {
+	var expireTime string
+	expireTime, err := s.hostRepository.GetExpireTime(ctx, uid, hostId)
+	if err != nil {
+		return false, err
+	}
+	if expireTime == "" {
+		return true, nil
+	}
+	unixTime, err := strconv.ParseInt(expireTime, 10, 64)
+	if err != nil {
+		return false, err
+	}
+	now := time.Now().Unix() // 当前时间戳(秒级)
+	if now > unixTime {
+		return false, nil
+	}
+
+	return true, nil
+}