updateAPI.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package api_management
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "adm-management/errors"
  9. v1 "adm-management/pb/v1"
  10. jsoniter "github.com/json-iterator/go"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. func UpdateAPI(ctx context.Context, req *v1.UpdateAPIRequest) (reply *v1.UpdateAPIReply, err error) {
  18. reply = &v1.UpdateAPIReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.Api == "" || req.Data == "" || req.Params == "" {
  32. return reply, nil
  33. }
  34. // _, err, request := loop2(req.Params)
  35. // if err != nil {
  36. // return nil, err
  37. // }
  38. _, err, response := loop2(req.Data)
  39. if err != nil {
  40. return nil, err
  41. }
  42. t := time.Now().Unix()
  43. 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
  44. if err != nil {
  45. if err == gorm.ErrRecordNotFound {
  46. return reply, nil
  47. }
  48. return reply, errors.SystemError
  49. }
  50. return reply, nil
  51. }
  52. func loop2(aaa string) (m map[string]interface{}, err error, s string) {
  53. var in interface{}
  54. aaaMap := make(map[string]interface{})
  55. err = jsoniter.UnmarshalFromString(aaa, &in)
  56. if err != nil {
  57. return nil, err, ""
  58. }
  59. data := in.(map[string]interface{})
  60. for k, v := range data {
  61. switch t := v.(type) {
  62. case []interface{}:
  63. var slice []interface{}
  64. switch t[0].(type) {
  65. case string:
  66. slice = append(slice, "")
  67. case int64:
  68. slice = append(slice, 0)
  69. case float64:
  70. slice = append(slice, 0.00)
  71. case bool:
  72. slice = append(slice, false)
  73. case interface{}:
  74. b, _ := json.Marshal(t[0])
  75. subMap, err, _ := loop2(string(b))
  76. if err != nil {
  77. return nil, err, ""
  78. }
  79. slice = append(slice, subMap)
  80. }
  81. aaaMap[k] = slice
  82. case string, int64, float64, bool:
  83. value := ""
  84. err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", k).Find(&value).Error
  85. aaaMap[k] = value
  86. case interface{}:
  87. b, _ := json.Marshal(t)
  88. subMap, err, _ := loop2(string(b))
  89. if err != nil {
  90. return nil, err, ""
  91. }
  92. aaaMap[k] = subMap
  93. }
  94. }
  95. b, _ := json.Marshal(aaaMap)
  96. return aaaMap, nil, string(b)
  97. }