package dto import ( "fmt" "git.zkcoi.com/zkcoi/meshray/internal/model" ) // NetworkResponse 网络响应 DTO(ID 为字符串格式,避免 JavaScript 精度丢失) type NetworkResponse struct { ID string `json:"id"` // ❄️ 雪花算法 ID(字符串格式) Name string `json:"name"` // 组网名称 SubnetIPv4 string `json:"subnet_ipv4"` // IPv4 子网(snake_case) MeshMode string `json:"mesh_mode"` // 组网模式(enhanced/native) WGMode string `json:"wg_mode"` // WG 运行模式(kernel/userspace) PolicyID string `json:"policy_id"` // ❄️ 关联策略 ID(字符串格式) Status string `json:"status"` // 运行状态(running/stopped) DeviceCount int64 `json:"device_count"` // 设备数量(统计字段) Description string `json:"description,omitempty"` // 描述 CreatedAt string `json:"created_at"` // 创建时间 UpdatedAt string `json:"updated_at"` // 更新时间 } // DeviceResponse 设备响应 DTO(ID 为字符串格式,避免 JavaScript 精度丢失) type DeviceResponse struct { ID string `json:"id"` // ❄️ 雪花算法 ID(字符串格式) NetworkID string `json:"networkId"` // ❄️ 所属网络 ID(字符串格式) Name string `json:"name"` // 设备名称 VirtualIP string `json:"virtualIP"` // 虚拟 IP PublicKey string `json:"publicKey"` // WireGuard 公钥 Endpoint string `json:"endpoint,omitempty"` // 公网 Endpoint IsRelayCapable bool `json:"isRelayCapable"` // 是否中继节点 Status string `json:"status"` // 在线状态(online/offline) Description string `json:"description,omitempty"` // 描述 CreatedAt string `json:"createdAt"` // 创建时间 UpdatedAt string `json:"updatedAt"` // 更新时间 } // PolicyResponse 策略响应 DTO(ID 为字符串格式) type PolicyResponse struct { ID string `json:"id"` // ❄️ 雪花算法 ID(字符串格式) Name string `json:"name"` // 策略名称 Type string `json:"type"` // 类型(system/custom) LayerConfig string `json:"layerConfig"` // 链路配置 JSON GlobalParams string `json:"globalParams"` // 全局参数 JSON Enabled bool `json:"enabled"` // 是否启用 IsDefault bool `json:"isDefault"` // 是否默认策略 Description string `json:"description,omitempty"` // 描述 CreatedAt string `json:"createdAt"` // 创建时间 UpdatedAt string `json:"updatedAt"` // 更新时间 } // ToNetworkResponse Network 转 NetworkResponse func ToNetworkResponse(network *model.Network) NetworkResponse { // 计算设备数量 var deviceCount int64 = 0 if len(network.Devices) > 0 { deviceCount = int64(len(network.Devices)) } return NetworkResponse{ ID: fmt.Sprintf("%d", network.ID), Name: network.Name, SubnetIPv4: network.SubnetIPv4, MeshMode: network.Mode, // Mode → MeshMode WGMode: network.WGMode, PolicyID: fmt.Sprintf("%d", network.PolicyID), Status: network.Status, DeviceCount: deviceCount, // 新增:设备数量 CreatedAt: network.CreatedAt.Format("2006-01-02T15:04:05Z"), UpdatedAt: network.UpdatedAt.Format("2006-01-02T15:04:05Z"), } } // ToDeviceResponse Device 转 DeviceResponse func ToDeviceResponse(device *model.Device) DeviceResponse { return DeviceResponse{ ID: fmt.Sprintf("%d", device.ID), NetworkID: fmt.Sprintf("%d", device.NetworkID), Name: device.Name, VirtualIP: device.VirtualIP, PublicKey: device.PublicKey, Endpoint: device.Endpoint, IsRelayCapable: device.IsRelayCapable, Status: device.Status, CreatedAt: device.CreatedAt.Format("2006-01-02T15:04:05Z"), } } // ToPolicyResponse Policy 转 PolicyResponse func ToPolicyResponse(policy *model.Policy) PolicyResponse { return PolicyResponse{ ID: fmt.Sprintf("%d", policy.ID), Name: policy.Name, Type: policy.Type, LayerConfig: policy.LayerConfig, GlobalParams: policy.GlobalParams, Enabled: policy.Enabled, IsDefault: policy.IsDefault, Description: policy.Description, CreatedAt: policy.CreatedAt.Format("2006-01-02T15:04:05Z"), UpdatedAt: policy.UpdatedAt.Format("2006-01-02T15:04:05Z"), } } // ToNetworkResponseList 批量转换 Network func ToNetworkResponseList(networks []model.Network) []NetworkResponse { result := make([]NetworkResponse, 0, len(networks)) for _, n := range networks { result = append(result, ToNetworkResponse(&n)) } return result } // ToDeviceResponseList 批量转换 Device func ToDeviceResponseList(devices []model.Device) []DeviceResponse { result := make([]DeviceResponse, 0, len(devices)) for _, d := range devices { result = append(result, ToDeviceResponse(&d)) } return result } // ToPolicyResponseList 批量转换 Policy func ToPolicyResponseList(policies []model.Policy) []PolicyResponse { result := make([]PolicyResponse, 0, len(policies)) for _, p := range policies { result = append(result, ToPolicyResponse(&p)) } return result }