insert_table.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package query
  2. import (
  3. "adm-data/errors"
  4. "encoding/json"
  5. "reflect"
  6. "time"
  7. "git.getensh.com/common/gopkgsv2/database"
  8. )
  9. func insertTable(apiNo, desc, relatedTableIds string, req, resp interface{}) (err error) {
  10. c := 0
  11. err = database.DB().Raw("select count(*) from db_adm_management.t_adm_api_management where api_no = ?", apiNo).Find(&c).Error
  12. if c > 0 {
  13. return nil
  14. }
  15. reqData, err := loop(req)
  16. if err != nil {
  17. return err
  18. }
  19. respData, err := loop(resp)
  20. if err != nil {
  21. return err
  22. }
  23. res, _ := json.Marshal(respData)
  24. request, _ := json.Marshal(reqData)
  25. path := "/api/v1.0/query"
  26. time := time.Now().Unix()
  27. err = database.DB().Exec("insert into db_adm_management.t_adm_api_management(api_no, `desc`, path, request, response, related_table_ids, created_at, updated_at) value(?,?,?,?,?,?,?,?)", apiNo, desc, path, string(request), string(res), relatedTableIds, time, time).Error
  28. return nil
  29. }
  30. func loop(aaa interface{}) (m map[string]interface{}, err error) {
  31. typeInfo := reflect.TypeOf(aaa)
  32. valInfo := reflect.ValueOf(&aaa)
  33. field := typeInfo.NumField()
  34. aaaMap := make(map[string]interface{})
  35. for i := 0; i < field; i++ {
  36. key0 := typeInfo.Field(i).Tag.Get("json")
  37. k := typeInfo.Field(i).Type.Kind()
  38. if k == reflect.Slice {
  39. var slice []interface{}
  40. eee := typeInfo.Field(i).Type.Elem()
  41. eKind := eee.Kind()
  42. if eKind == reflect.Struct {
  43. subMap, err := loop(valInfo.Elem().Elem().Field(i).Index(0).Interface())
  44. if err != nil {
  45. return nil, err
  46. }
  47. slice = append(slice, subMap)
  48. } else if eKind == reflect.String {
  49. subMap := make(map[string]interface{})
  50. key := eee.Field(i).Tag.Get("json")
  51. subMap[key] = ""
  52. slice = append(slice, subMap)
  53. } else if eKind == reflect.Int64 || eKind == reflect.Int32 || eKind == reflect.Int {
  54. subMap := make(map[string]interface{})
  55. key := eee.Field(i).Tag.Get("json")
  56. subMap[key] = 0
  57. slice = append(slice, subMap)
  58. } else if eKind == reflect.Float64 || eKind == reflect.Float32 {
  59. subMap := make(map[string]interface{})
  60. key := eee.Field(i).Tag.Get("json")
  61. subMap[key] = 0.00
  62. slice = append(slice, subMap)
  63. } else if eKind == reflect.Bool {
  64. subMap := make(map[string]interface{})
  65. key := eee.Field(i).Tag.Get("json")
  66. subMap[key] = false
  67. slice = append(slice, subMap)
  68. }
  69. aaaMap[key0] = slice
  70. } else if k == reflect.Struct {
  71. subMap, err := loop(valInfo.Elem().Elem().Field(i).Interface())
  72. if err != nil {
  73. return nil, err
  74. }
  75. aaaMap[key0] = subMap
  76. } else if k == reflect.String {
  77. value := ""
  78. err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
  79. if value == "" {
  80. return nil, errors.FieldNotExistError
  81. }
  82. aaaMap[key0] = value
  83. } else if k == reflect.Int64 || k == reflect.Int32 || k == reflect.Int {
  84. value := ""
  85. err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
  86. aaaMap[key0] = value
  87. if value == "" {
  88. return nil, errors.FieldNotExistError
  89. }
  90. } else if k == reflect.Float64 || k == reflect.Float32 {
  91. value := ""
  92. err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
  93. aaaMap[key0] = value
  94. if value == "" {
  95. return nil, errors.FieldNotExistError
  96. }
  97. } else if k == reflect.Bool {
  98. value := ""
  99. err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
  100. aaaMap[key0] = value
  101. if value == "" {
  102. return nil, errors.FieldNotExistError
  103. }
  104. }
  105. }
  106. return aaaMap, nil
  107. }