| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package bootconfig
-
- import (
- "context"
- "fmt"
- "strings"
-
- "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"
- )
-
- // BootConfig boot配置项
- type BootConfig struct {
- ID string `json:"id"`
- 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"`
- }
-
- // GetBootConfigs 获取boot配置项
- func GetBootConfigs(ctx context.Context, dbFactory *database.DBFactory, reqCtx *ctx.RequestContext) *response.QueryResult[[]BootConfig] {
- logger.Debug("GetBootConfigs-开始获取boot配置项")
-
- // 获取数据库连接
- db := dbFactory.GetDB()
-
- // 查询boot相关配置(config_name或yaml_name包含boot)
- query := `
- SELECT
- id,
- config_name,
- yaml_name,
- yaml_value,
- creator,
- created_at
- FROM config_startup
- WHERE LOWER(config_name) LIKE '%boot%' OR LOWER(yaml_name) LIKE '%boot%'
- ORDER BY config_name, yaml_name
- `
-
- rows, err := db.QueryxContext(ctx, query)
- if err != nil {
- logger.ErrorC(reqCtx, fmt.Sprintf("查询boot配置失败: %v", err))
- return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("查询boot配置失败: %v", err), reqCtx)
- }
- defer rows.Close()
-
- var configs []BootConfig
- for rows.Next() {
- var config BootConfig
- var creator, createdAt interface{}
-
- err := rows.Scan(&config.ID, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt)
- if err != nil {
- logger.ErrorC(reqCtx, fmt.Sprintf("扫描boot配置数据失败: %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("遍历boot配置数据失败: %v", err))
- return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("遍历boot配置数据失败: %v", err), reqCtx)
- }
-
- logger.Debug(fmt.Sprintf("获取到 %d 个boot配置项", len(configs)))
- return util.CreateSuccessResultData[[]BootConfig](configs, reqCtx)
- }
-
- // SearchBootConfigs 搜索boot配置项
- func SearchBootConfigs(ctx context.Context, dbFactory *database.DBFactory, keyword string, reqCtx *ctx.RequestContext) *response.QueryResult[[]BootConfig] {
- logger.Debug(fmt.Sprintf("SearchBootConfigs-开始搜索boot配置项: %s", keyword))
-
- // 获取数据库连接
- db := dbFactory.GetDB()
-
- // 构建查询条件
- query := `
- SELECT
- id,
- config_name,
- yaml_name,
- yaml_value,
- creator,
- created_at
- FROM config_startup
- WHERE (LOWER(config_name) LIKE '%boot%' OR LOWER(yaml_name) LIKE '%boot%')
- `
- var args []interface{}
-
- if keyword != "" {
- query += " AND (LOWER(config_name) LIKE ? OR LOWER(yaml_name) LIKE ? OR LOWER(yaml_value) LIKE ?)"
- searchTerm := "%" + strings.ToLower(keyword) + "%"
- args = append(args, searchTerm, searchTerm, searchTerm)
- }
-
- query += " ORDER BY config_name, yaml_name"
-
- rows, err := db.QueryxContext(ctx, query, args...)
- if err != nil {
- logger.ErrorC(reqCtx, fmt.Sprintf("搜索boot配置失败: %v", err))
- return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("搜索boot配置失败: %v", err), reqCtx)
- }
- defer rows.Close()
-
- var configs []BootConfig
- for rows.Next() {
- var config BootConfig
- var creator, createdAt interface{}
-
- err := rows.Scan(&config.ID, &config.ConfigName, &config.YamlName, &config.YamlValue, &creator, &createdAt)
- if err != nil {
- logger.ErrorC(reqCtx, fmt.Sprintf("扫描boot配置数据失败: %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("遍历boot配置数据失败: %v", err))
- return util.CreateErrorResult[[]BootConfig](fmt.Sprintf("遍历boot配置数据失败: %v", err), reqCtx)
- }
-
- logger.Debug(fmt.Sprintf("搜索到 %d 个boot配置项", len(configs)))
- return util.CreateSuccessResultData[[]BootConfig](configs, reqCtx)
- }
|