package api_management import ( "context" "encoding/json" "fmt" "strings" "time" "adm-management/errors" v1 "adm-management/pb/v1" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" ) func UpdateAPI(ctx context.Context, req *v1.UpdateAPIRequest) (reply *v1.UpdateAPIReply, err error) { reply = &v1.UpdateAPIReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.Api == "" || req.Data == "" || req.Params == "" { return reply, nil } // _, err, request := loop2(req.Params) // if err != nil { // return nil, err // } _, err, response := loop2(req.Data) if err != nil { return nil, err } t := time.Now().Unix() err = database.DB().Exec("UPDATE db_adm_management.t_adm_api_management SET response = ?, updated_at = ? WHERE api_no = ?", response, t, strings.ToUpper(req.Api)).Error if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } return reply, errors.SystemError } return reply, nil } func loop2(aaa string) (m map[string]interface{}, err error, s string) { var in interface{} aaaMap := make(map[string]interface{}) err = jsoniter.UnmarshalFromString(aaa, &in) if err != nil { return nil, err, "" } data := in.(map[string]interface{}) for k, v := range data { switch t := v.(type) { case []interface{}: var slice []interface{} switch t[0].(type) { case string: slice = append(slice, "") case int64: slice = append(slice, 0) case float64: slice = append(slice, 0.00) case bool: slice = append(slice, false) case interface{}: b, _ := json.Marshal(t[0]) subMap, err, _ := loop2(string(b)) if err != nil { return nil, err, "" } slice = append(slice, subMap) } aaaMap[k] = slice case string, int64, float64, bool: value := "" err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", k).Find(&value).Error aaaMap[k] = value case interface{}: b, _ := json.Marshal(t) subMap, err, _ := loop2(string(b)) if err != nil { return nil, err, "" } aaaMap[k] = subMap } } b, _ := json.Marshal(aaaMap) return aaaMap, nil, string(b) }