|
@@ -2,6 +2,7 @@ package repository
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
|
|
@@ -10,6 +11,7 @@ import (
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
+ "gorm.io/gorm"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -29,6 +31,7 @@ type WebForwardingRepository interface {
|
|
|
GetDomainCount(ctx context.Context, hostId int, domain string) (int, error)
|
|
|
// 获取IP数量等于1的IP
|
|
|
GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error)
|
|
|
+ GetSslCertId (ctx context.Context, sslPocyID int) ([]v1.SslCertsJSON, error)
|
|
|
}
|
|
|
|
|
|
func NewWebForwardingRepository(
|
|
@@ -259,3 +262,41 @@ func (r *webForwardingRepository) GetIpCountByIp(ctx context.Context, ips []stri
|
|
|
|
|
|
return results, nil
|
|
|
}
|
|
|
+
|
|
|
+func (r *webForwardingRepository) GetSslCertId (ctx context.Context, sslPolicyID int) ([]v1.SslCertsJSON, error) {
|
|
|
+ var certsJSON string
|
|
|
+
|
|
|
+ // 2. 查询数据库,只获取 `certs` 字段的字符串内容
|
|
|
+ // 使用 Scopes 来确保没有不必要的 ORDER BY 子句,或者直接用 Raw/Scan
|
|
|
+ // 但在这里,用 .First(&certsJSON) 通常是安全的,因为目标是简单类型 string
|
|
|
+ err := r.DBWithName(ctx, "cdn").WithContext(ctx).
|
|
|
+ Table("cloud_ssl_policies").
|
|
|
+ Select("certs").
|
|
|
+ Where("id = ?", sslPolicyID).
|
|
|
+ Row(). // 获取 sql.Row
|
|
|
+ Scan(&certsJSON) // 将结果扫描到字符串变量中
|
|
|
+
|
|
|
+ // 如果查询出错,或者没有找到记录 (sql.ErrNoRows)
|
|
|
+ if err != nil {
|
|
|
+ if err == gorm.ErrRecordNotFound {
|
|
|
+ // 如果记录不存在是正常情况,可以返回一个空切片和nil错误
|
|
|
+ return []v1.SslCertsJSON{}, nil
|
|
|
+ }
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果certs字段在数据库中可能是NULL或者空字符串,需要处理
|
|
|
+ if certsJSON == "" {
|
|
|
+ return []v1.SslCertsJSON{}, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 将JSON字符串反序列化到Go结构体切片中
|
|
|
+ var res []v1.SslCertsJSON
|
|
|
+ err = json.Unmarshal([]byte(certsJSON), &res)
|
|
|
+ if err != nil {
|
|
|
+ // 这里是真正的JSON格式转换错误
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return res, nil
|
|
|
+}
|