|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+package service
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "strconv"
|
|
|
6
|
+ "strings"
|
|
|
7
|
+ "time"
|
|
|
8
|
+
|
|
|
9
|
+ "git.x2erp.com/qdy/go-base/ctx"
|
|
|
10
|
+ "git.x2erp.com/qdy/go-base/logger"
|
|
|
11
|
+ "git.x2erp.com/qdy/go-base/types"
|
|
|
12
|
+ "git.x2erp.com/qdy/go-db/factory/database"
|
|
|
13
|
+ "github.com/jmoiron/sqlx"
|
|
|
14
|
+)
|
|
|
15
|
+
|
|
|
16
|
+func QueryYamlConfigure(dbFactory *database.DBFactory, req types.QueryRequest, reqCtx *ctx.RequestContext) *types.QueryResult[map[string]interface{}] {
|
|
|
17
|
+ return query(dbFactory.GetDB(), req, reqCtx)
|
|
|
18
|
+
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+// QueryYamlConfigure 执行带位置参数的查询
|
|
|
22
|
+func query(db *sqlx.DB, req types.QueryRequest, reqCtx *ctx.RequestContext) *types.QueryResult[map[string]interface{}] {
|
|
|
23
|
+
|
|
|
24
|
+ startTime := time.Now()
|
|
|
25
|
+
|
|
|
26
|
+ // 安全检查
|
|
|
27
|
+ if db == nil {
|
|
|
28
|
+ return createErrorResult("database connection is nil", startTime, reqCtx)
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ sqlStr := `
|
|
|
32
|
+ SELECT b.config_startup_detail_id, b.config_key, b.config_value
|
|
|
33
|
+ FROM config_startup a
|
|
|
34
|
+ JOIN config_startup_detail b ON a.config_startup_id = b.config_startup_id
|
|
|
35
|
+ WHERE a.config_environment_id = $1
|
|
|
36
|
+ AND a.config_service_id = $2;`
|
|
|
37
|
+
|
|
|
38
|
+ params := req.PositionalParams
|
|
|
39
|
+ logger.DebugC(reqCtx, sqlStr)
|
|
|
40
|
+ logger.DebugC(reqCtx, fmt.Sprintf("PositionalParams: %v", params))
|
|
|
41
|
+
|
|
|
42
|
+ // 执行查询
|
|
|
43
|
+ rows, err := db.Queryx(sqlStr, params...)
|
|
|
44
|
+ if err != nil {
|
|
|
45
|
+ return createErrorResult(fmt.Sprintf("Query execution failed: %v", err), startTime, reqCtx)
|
|
|
46
|
+ }
|
|
|
47
|
+ defer rows.Close()
|
|
|
48
|
+
|
|
|
49
|
+ // 处理结果集
|
|
|
50
|
+ return processQueryResult(rows, startTime, reqCtx)
|
|
|
51
|
+}
|
|
|
52
|
+
|
|
|
53
|
+func processQueryResult(rows *sqlx.Rows, startTime time.Time, reqCtx *ctx.RequestContext) *types.QueryResult[map[string]interface{}] {
|
|
|
54
|
+ // 初始化结果结构体
|
|
|
55
|
+ result := &types.QueryResult[map[string]interface{}]{
|
|
|
56
|
+ Metadata: reqCtx,
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ // 获取列信息
|
|
|
60
|
+ columns, err := rows.Columns()
|
|
|
61
|
+ if err != nil {
|
|
|
62
|
+ return createErrorResult(fmt.Sprintf("Failed to get columns: %v", err), startTime, reqCtx)
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ // 存储最终结果:根key -> 内层配置map
|
|
|
66
|
+ results := make(map[string]interface{})
|
|
|
67
|
+ count := 0
|
|
|
68
|
+
|
|
|
69
|
+ // 记录已经出现的内层key,用于检测重复
|
|
|
70
|
+ // outerKey -> innerKey -> rowNumber
|
|
|
71
|
+ //keyTracker := make(map[string]map[string]int)
|
|
|
72
|
+
|
|
|
73
|
+ // 遍历行数据
|
|
|
74
|
+ for rows.Next() {
|
|
|
75
|
+ count++
|
|
|
76
|
+ values := make([]interface{}, len(columns))
|
|
|
77
|
+ valuePtrs := make([]interface{}, len(columns))
|
|
|
78
|
+ for i := range columns {
|
|
|
79
|
+ valuePtrs[i] = &values[i]
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ // 扫描行数据
|
|
|
83
|
+ if err := rows.Scan(valuePtrs...); err != nil {
|
|
|
84
|
+ return createErrorResult(fmt.Sprintf("Scan row %d failed: %v", count, err), startTime, reqCtx)
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ // 提取当前行的三个核心字段
|
|
|
88
|
+ var detailID, configKey string
|
|
|
89
|
+ var configValue interface{}
|
|
|
90
|
+ for i, col := range columns {
|
|
|
91
|
+ v := values[i]
|
|
|
92
|
+ if v == nil {
|
|
|
93
|
+ continue
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ switch col {
|
|
|
97
|
+ case "config_startup_detail_id":
|
|
|
98
|
+ detailID = toString(v) // detailID 需要字符串
|
|
|
99
|
+ case "config_key":
|
|
|
100
|
+ configKey = toString(v) // config_key 需要字符串
|
|
|
101
|
+ case "config_value":
|
|
|
102
|
+ configValue = convertValue(v) // config_value 需要带类型转换
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ //logger.DebugC(reqCtx, fmt.Sprintf("Row %d - detailID: %s, configKey: %s", count, detailID, configKey))
|
|
|
107
|
+
|
|
|
108
|
+ // 解析 detailID 获取根key(倒数第3段)
|
|
|
109
|
+ parts := strings.Split(detailID, ".")
|
|
|
110
|
+ if len(parts) < 3 {
|
|
|
111
|
+ return createErrorResult(fmt.Sprintf("Row %d detailID format error: %s", count, detailID), startTime, reqCtx)
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ rootKey := parts[len(parts)-3] // 取倒数第3段,例如 "database"
|
|
|
115
|
+
|
|
|
116
|
+ // 获取或创建外层key对应的map
|
|
|
117
|
+ configMap, exists := results[rootKey]
|
|
|
118
|
+ if !exists {
|
|
|
119
|
+ // 不存在则创建
|
|
|
120
|
+ configMap = make(map[string]interface{})
|
|
|
121
|
+ results[rootKey] = configMap
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ // 添加或覆盖内层配置
|
|
|
125
|
+ configMap.(map[string]interface{})[configKey] = configValue
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ // 检查行遍历错误
|
|
|
129
|
+ if err := rows.Err(); err != nil {
|
|
|
130
|
+ return createErrorResult(fmt.Sprintf("Row iteration error: %v", err), startTime, reqCtx)
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ // 构建成功结果
|
|
|
134
|
+ result.Success = true
|
|
|
135
|
+ result.Data = results
|
|
|
136
|
+ result.Count = count
|
|
|
137
|
+ result.Time = time.Since(startTime).String()
|
|
|
138
|
+ result.Message = "Query success"
|
|
|
139
|
+
|
|
|
140
|
+ return result
|
|
|
141
|
+}
|
|
|
142
|
+
|
|
|
143
|
+// createErrorResult 创建错误结果的辅助函数(复用你提供的)
|
|
|
144
|
+func createErrorResult(errorMsg string, startTime time.Time, reqCtx *ctx.RequestContext) *types.QueryResult[map[string]interface{}] {
|
|
|
145
|
+ logger.ErrorC(reqCtx, errorMsg)
|
|
|
146
|
+ return &types.QueryResult[map[string]interface{}]{
|
|
|
147
|
+ Success: false,
|
|
|
148
|
+ Error: errorMsg,
|
|
|
149
|
+ Time: time.Since(startTime).String(),
|
|
|
150
|
+ Metadata: reqCtx,
|
|
|
151
|
+ }
|
|
|
152
|
+}
|
|
|
153
|
+
|
|
|
154
|
+// 辅助函数:转换为字符串
|
|
|
155
|
+func toString(v interface{}) string {
|
|
|
156
|
+ switch val := v.(type) {
|
|
|
157
|
+ case string:
|
|
|
158
|
+ return val
|
|
|
159
|
+ case []byte:
|
|
|
160
|
+ return string(val)
|
|
|
161
|
+ case int64:
|
|
|
162
|
+ return strconv.FormatInt(val, 10)
|
|
|
163
|
+ case int32:
|
|
|
164
|
+ return strconv.FormatInt(int64(val), 10)
|
|
|
165
|
+ case int:
|
|
|
166
|
+ return strconv.Itoa(val)
|
|
|
167
|
+ case float64:
|
|
|
168
|
+ return strconv.FormatFloat(val, 'f', -1, 64)
|
|
|
169
|
+ case float32:
|
|
|
170
|
+ return strconv.FormatFloat(float64(val), 'f', -1, 32)
|
|
|
171
|
+ case bool:
|
|
|
172
|
+ return strconv.FormatBool(val)
|
|
|
173
|
+ default:
|
|
|
174
|
+ return fmt.Sprintf("%v", val)
|
|
|
175
|
+ }
|
|
|
176
|
+}
|
|
|
177
|
+
|
|
|
178
|
+// 辅助函数:转换值,保持合适的数据类型
|
|
|
179
|
+func convertValue(v interface{}) interface{} {
|
|
|
180
|
+ switch val := v.(type) {
|
|
|
181
|
+ case []byte:
|
|
|
182
|
+ // 尝试解析为合适的数据类型
|
|
|
183
|
+ strVal := string(val)
|
|
|
184
|
+ return parseStringValue(strVal)
|
|
|
185
|
+
|
|
|
186
|
+ case string:
|
|
|
187
|
+ return parseStringValue(val)
|
|
|
188
|
+
|
|
|
189
|
+ case int64, int32, int:
|
|
|
190
|
+ return val
|
|
|
191
|
+
|
|
|
192
|
+ case float64, float32:
|
|
|
193
|
+ return val
|
|
|
194
|
+
|
|
|
195
|
+ case bool:
|
|
|
196
|
+ return val
|
|
|
197
|
+
|
|
|
198
|
+ default:
|
|
|
199
|
+ // 其他类型转换为字符串
|
|
|
200
|
+ return toString(val)
|
|
|
201
|
+ }
|
|
|
202
|
+}
|
|
|
203
|
+
|
|
|
204
|
+// 辅助函数:解析字符串值为合适的数据类型
|
|
|
205
|
+func parseStringValue(strVal string) interface{} {
|
|
|
206
|
+ // 1. 尝试布尔值 (支持 true/false, yes/no, on/off, 1/0)
|
|
|
207
|
+ lowerVal := strings.ToLower(strings.TrimSpace(strVal))
|
|
|
208
|
+ if lowerVal == "true" || lowerVal == "yes" || lowerVal == "on" || lowerVal == "1" {
|
|
|
209
|
+ return true
|
|
|
210
|
+ }
|
|
|
211
|
+ if lowerVal == "false" || lowerVal == "no" || lowerVal == "off" || lowerVal == "0" {
|
|
|
212
|
+ return false
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ // 2. 尝试整数
|
|
|
216
|
+ if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
|
|
|
217
|
+ return intVal
|
|
|
218
|
+ }
|
|
|
219
|
+
|
|
|
220
|
+ // 3. 尝试浮点数
|
|
|
221
|
+ if floatVal, err := strconv.ParseFloat(strVal, 64); err == nil {
|
|
|
222
|
+ return floatVal
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ // 4. 保持为字符串
|
|
|
226
|
+ return strVal
|
|
|
227
|
+}
|