Selaa lähdekoodia

feat: add Create and Update for system config writes

Expose mbsd create/update APIs so services like schs can persist
system_configs from business admin flows, with cache refresh on success.

Co-authored-by: Cursor <cursoragent@cursor.com>
郭铭泽 3 päivää sitten
vanhempi
commit
f097ee3fa0
2 muutettua tiedostoa jossa 119 lisäystä ja 3 poistoa
  1. 14 3
      README.md
  2. 105 0
      client.go

+ 14 - 3
README.md

@@ -36,20 +36,31 @@ func main() {
 | `sd_url` 未配置 | 不初始化客户端,`Get*` 返回调用方默认值 |
 | mbsd 不可用 | 使用未过期缓存;否则默认值 |
 | 改配置 | 默认约 60s 内通过后台刷新生效(`CacheTTL`) |
+| 写配置 | `Create` / `Update` 成功后立即更新本地缓存 |
 
 各服务在自身仓库维护 **KnownKeys** 与业务默认值封装(如 HCS 的 `application/support/system_config/keys.go`),不要写进本 SDK。
 
+写配置示例(需 mbsd 已部署 create/update 接口):
+
+```go
+err := mbsdsdk.Update(mbsdsdk.ConfigInfo{
+    ID: info.ID, ConfigKey: info.ConfigKey, ConfigValue: newValue,
+    ValueType: info.ValueType, ConfigGroup: info.ConfigGroup,
+    Service: info.Service, Description: info.Description,
+}, userID)
+```
+
 ## 版本发布(Gogs)
 
 ```bash
-git tag v0.1.0
-git push origin v0.1.0
+git tag v0.1.1
+git push origin v0.1.1
 ```
 
 业务服务:
 
 ```bash
-go get git.sxidc.com/health-checkup-system/mbsd-sdk@v0.1.0
+go get git.sxidc.com/health-checkup-system/mbsd-sdk@v0.1.1
 ```
 
 私服需配置 `GOPRIVATE=git.sxidc.com`。

+ 105 - 0
client.go

@@ -58,6 +58,27 @@ type infoResponse struct {
 	Info getByKeysResult `json:"info"`
 }
 
+type updateRequest struct {
+	ID           string `json:"id"`
+	ConfigKey    string `json:"configKey"`
+	ConfigValue  string `json:"configValue"`
+	ValueType    string `json:"valueType"`
+	ConfigGroup  string `json:"configGroup"`
+	Service      string `json:"service"`
+	Description  string `json:"description"`
+	UpdateUserID string `json:"updateUserId"`
+}
+
+type createRequest struct {
+	ConfigKey    string `json:"configKey"`
+	ConfigValue  string `json:"configValue"`
+	ValueType    string `json:"valueType"`
+	ConfigGroup  string `json:"configGroup"`
+	Service      string `json:"service"`
+	Description  string `json:"description"`
+	CreateUserID string `json:"createUserId"`
+}
+
 var (
 	invokeAPI    *invoke.API
 	urlPrefix    = DefaultURLPrefix
@@ -278,6 +299,90 @@ func GetBool(key string, defaultValue bool) bool {
 	return boolValue
 }
 
+// Update 更新已有配置项(需带 ID)。
+func Update(info ConfigInfo, updateUserID string) error {
+	if invokeAPI == nil {
+		return errors.New("mbsdsdk not initialized")
+	}
+	if info.ID == "" {
+		return errors.New("config id is required")
+	}
+
+	body, err := json.Marshal(&updateRequest{
+		ID:           info.ID,
+		ConfigKey:    info.ConfigKey,
+		ConfigValue:  info.ConfigValue,
+		ValueType:    info.ValueType,
+		ConfigGroup:  info.ConfigGroup,
+		Service:      info.Service,
+		Description:  info.Description,
+		UpdateUserID: updateUserID,
+	})
+	if err != nil {
+		return err
+	}
+
+	path, err := url.JoinPath(urlPrefix, "update")
+	if err != nil {
+		return err
+	}
+
+	respBytes, err := invokeAPI.PutJSON(path, body, nil)
+	if err != nil {
+		return err
+	}
+
+	var resp msgResponse
+	if err = json.Unmarshal(respBytes, &resp); err != nil {
+		return err
+	}
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+
+	storeCache(info.ConfigKey, info)
+	return nil
+}
+
+// Create 新建配置项。
+func Create(info ConfigInfo, createUserID string) error {
+	if invokeAPI == nil {
+		return errors.New("mbsdsdk not initialized")
+	}
+
+	body, err := json.Marshal(&createRequest{
+		ConfigKey:    info.ConfigKey,
+		ConfigValue:  info.ConfigValue,
+		ValueType:    info.ValueType,
+		ConfigGroup:  info.ConfigGroup,
+		Service:      info.Service,
+		Description:  info.Description,
+		CreateUserID: createUserID,
+	})
+	if err != nil {
+		return err
+	}
+
+	path, err := url.JoinPath(urlPrefix, "create")
+	if err != nil {
+		return err
+	}
+
+	respBytes, err := invokeAPI.PostJSON(path, body, nil)
+	if err != nil {
+		return err
+	}
+
+	var resp msgResponse
+	if err = json.Unmarshal(respBytes, &resp); err != nil {
+		return err
+	}
+	if !resp.Success {
+		return errors.New(resp.Msg)
+	}
+	return nil
+}
+
 // GetJSON 将配置值反序列化到 target;未找到 key 时返回错误。
 func GetJSON(key string, target any) error {
 	info, ok, err := Get(key)