123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package query
- import (
- "adm-data/errors"
- "encoding/json"
- "reflect"
- "time"
- "git.getensh.com/common/gopkgsv2/database"
- )
- func insertTable(apiNo, desc, relatedTableIds string, req, resp interface{}) (err error) {
- c := 0
- err = database.DB().Raw("select count(*) from db_adm_management.t_adm_api_management where api_no = ?", apiNo).Find(&c).Error
- if c > 0 {
- return nil
- }
- reqData, err := loop(req)
- if err != nil {
- return err
- }
- respData, err := loop(resp)
- if err != nil {
- return err
- }
- res, _ := json.Marshal(respData)
- request, _ := json.Marshal(reqData)
- path := "/api/v1.0/query"
- time := time.Now().Unix()
- 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
- return nil
- }
- func loop(aaa interface{}) (m map[string]interface{}, err error) {
- typeInfo := reflect.TypeOf(aaa)
- valInfo := reflect.ValueOf(&aaa)
- field := typeInfo.NumField()
- aaaMap := make(map[string]interface{})
- for i := 0; i < field; i++ {
- key0 := typeInfo.Field(i).Tag.Get("json")
- k := typeInfo.Field(i).Type.Kind()
- if k == reflect.Slice {
- var slice []interface{}
- eee := typeInfo.Field(i).Type.Elem()
- eKind := eee.Kind()
- if eKind == reflect.Struct {
- subMap, err := loop(valInfo.Elem().Elem().Field(i).Index(0).Interface())
- if err != nil {
- return nil, err
- }
- slice = append(slice, subMap)
- } else if eKind == reflect.String {
- subMap := make(map[string]interface{})
- key := eee.Field(i).Tag.Get("json")
- subMap[key] = ""
- slice = append(slice, subMap)
- } else if eKind == reflect.Int64 || eKind == reflect.Int32 || eKind == reflect.Int {
- subMap := make(map[string]interface{})
- key := eee.Field(i).Tag.Get("json")
- subMap[key] = 0
- slice = append(slice, subMap)
- } else if eKind == reflect.Float64 || eKind == reflect.Float32 {
- subMap := make(map[string]interface{})
- key := eee.Field(i).Tag.Get("json")
- subMap[key] = 0.00
- slice = append(slice, subMap)
- } else if eKind == reflect.Bool {
- subMap := make(map[string]interface{})
- key := eee.Field(i).Tag.Get("json")
- subMap[key] = false
- slice = append(slice, subMap)
- }
- aaaMap[key0] = slice
- } else if k == reflect.Struct {
- subMap, err := loop(valInfo.Elem().Elem().Field(i).Interface())
- if err != nil {
- return nil, err
- }
- aaaMap[key0] = subMap
- } else if k == reflect.String {
- value := ""
- err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
- if value == "" {
- return nil, errors.FieldNotExistError
- }
- aaaMap[key0] = value
- } else if k == reflect.Int64 || k == reflect.Int32 || k == reflect.Int {
- value := ""
- err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
- aaaMap[key0] = value
- if value == "" {
- return nil, errors.FieldNotExistError
- }
- } else if k == reflect.Float64 || k == reflect.Float32 {
- value := ""
- err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
- aaaMap[key0] = value
- if value == "" {
- return nil, errors.FieldNotExistError
- }
- } else if k == reflect.Bool {
- value := ""
- err = database.DB().Raw("select `desc` from db_adm_management.t_adm_field_management where field = ?", key0).Find(&value).Error
- aaaMap[key0] = value
- if value == "" {
- return nil, errors.FieldNotExistError
- }
- }
- }
- return aaaMap, nil
- }
|