Przeglądaj źródła

fix(flexCdn): 修复 CC 黑名单 IP 添加和修改逻辑

- 新增 GetCcIpCount 方法,用于检查 IP 是否已存在于 CC 黑名单中
- 在添加和修改 IP 时,增加对 CC 黑名单中 IP 存在性的检查
-优化 GetIpId 方法,使用降序排序并限制结果数量,提高查询效率
fusu 2 tygodni temu
rodzic
commit
67be6093a2

+ 8 - 1
internal/repository/api/flexCdn/cciplist.go

@@ -10,6 +10,8 @@ type CcIpListRepository interface {
 	GetHttpWebId(ctx context.Context, serverId int64) (int64, error)
 	GetIpListId(ctx context.Context, serverId int64,ipListType string) (int64,error)
 	GetIpId(ctx context.Context, ipListId int64,ip string,sourceCategory string) (int64,error)
+	GetCcIpCount(ctx context.Context,ipListId int64, ip string,sourceCategory string) (int64, error)
+
 }
 
 func NewCcIpListRepository(
@@ -45,5 +47,10 @@ func (r *ccIpListRepository) GetIpListId(ctx context.Context, serverId int64,ipL
 
 func (r *ccIpListRepository) GetIpId(ctx context.Context, ipListId int64,ip string,sourceCategory string) (int64,error) {
 	var ipId int64
-	return ipId, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND value = ? AND sourceCategory = ?", ipListId,ip,sourceCategory).Select("id").Scan(&ipId).Error
+	return ipId, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND value = ? AND sourceCategory = ?", ipListId,ip,sourceCategory).Order("id desc").Limit(1).Select("id").Scan(&ipId).Error
+}
+
+func (r *ccIpListRepository) GetCcIpCount(ctx context.Context,ipListId int64, ip string,sourceCategory string) (int64, error) {
+	var count int64
+	return count, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND value = ? AND sourceCategory = ?", ipListId,ip,sourceCategory).Count(&count).Error
 }

+ 20 - 0
internal/service/api/flexCdn/cciplist.go

@@ -104,6 +104,15 @@ func (s *ccIpListService) AddCcWhiteIp(ctx context.Context, webId int64,ip strin
 		return err
 	}
 
+	count, err := s.ccIpListRepository.GetCcIpCount(ctx,ipListId, ip,"cc")
+	if err != nil {
+		return err
+	}
+	if count > 0 {
+		return fmt.Errorf("ip已存在")
+	}
+
+
 	err = s.cdn.AddIpItem(ctx,v1.AddIpItem{
 		IpListId: ipListId,
 		Value: ip,
@@ -127,6 +136,17 @@ func (s *ccIpListService) EditCcWhiteIp(ctx context.Context, webId int64,oldIp s
 	if err != nil {
 		return err
 	}
+
+
+	count, err := s.ccIpListRepository.GetCcIpCount(ctx,ipListId, newIp,"cc")
+	if err != nil {
+		return err
+	}
+	if count > 0 {
+		return fmt.Errorf("ip已存在")
+	}
+
+
 	ipItemId, err := s.ccIpListRepository.GetIpId(ctx, ipListId,oldIp,"cc")
 	if err != nil {
 		return err