package servicemanagement import ( "context" "fmt" "git.x2erp.com/qdy/go-base/ctx" "git.x2erp.com/qdy/go-base/logger" "git.x2erp.com/qdy/go-base/model/response" "git.x2erp.com/qdy/go-base/util" "git.x2erp.com/qdy/go-db/factory/database" ) // ServiceConfig 微服务配置项 type ServiceConfig struct { ID string `json:"id"` ServiceName string `json:"service_name"` ConfigName string `json:"config_name"` YamlName string `json:"yaml_name"` YamlValue string `json:"yaml_value"` Creator string `json:"creator,omitempty"` CreatedAt string `json:"created_at,omitempty"` } // GetServiceConfigs 获取微服务配置项 func GetServiceConfigs(ctx context.Context, dbFactory *database.DBFactory, serviceName string, reqCtx *ctx.RequestContext) *response.QueryResult[[]ServiceConfig] { logger.Debug(fmt.Sprintf("GetServiceConfigs-开始获取微服务配置项: %s", serviceName)) // 获取数据库连接 db := dbFactory.GetDB() // 查询指定微服务的所有配置 query := ` SELECT id, service_name, config_name, yaml_name, yaml_value, creator, created_at FROM config_startup_svc WHERE service_name = ? ORDER BY config_name, yaml_name ` rows, err := db.QueryxContext(ctx, query, serviceName) if err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("查询微服务配置失败: %v", err)) return util.CreateErrorResult[[]ServiceConfig](fmt.Sprintf("查询微服务配置失败: %v", err), reqCtx) } defer rows.Close() var configs []ServiceConfig for rows.Next() { var config ServiceConfig var creator, createdAt interface{} err := rows.Scan(&config.ID, &config.ServiceName, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt) if err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("扫描微服务配置数据失败: %v", err)) continue } // 处理可能为nil的字段 if creator != nil { config.Creator = fmt.Sprintf("%v", creator) } if createdAt != nil { config.CreatedAt = fmt.Sprintf("%v", createdAt) } configs = append(configs, config) } if err := rows.Err(); err != nil { logger.ErrorC(reqCtx, fmt.Sprintf("遍历微服务配置数据失败: %v", err)) return util.CreateErrorResult[[]ServiceConfig](fmt.Sprintf("遍历微服务配置数据失败: %v", err), reqCtx) } logger.Debug(fmt.Sprintf("获取到微服务 %s 的 %d 个配置项", serviceName, len(configs))) return util.CreateSuccessResultData[[]ServiceConfig](configs, reqCtx) }