mapdata.go 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "go-common/library/log"
  8. )
  9. // MapData .
  10. type MapData map[string]interface{}
  11. // StrID .
  12. func (m MapData) StrID(indexID string) string {
  13. if indexID == "base" { // 需要改配置
  14. return ""
  15. }
  16. var data []interface{}
  17. arr := strings.Split(indexID, ",")
  18. for _, v := range arr[1:] {
  19. v = strings.TrimSpace(v)
  20. if item, ok := m[v].(interface{}); ok {
  21. if reflect.TypeOf(item).Kind() == reflect.Float64 {
  22. item = int64(item.(float64))
  23. }
  24. data = append(data, item)
  25. continue
  26. }
  27. log.Error("model.MapData.StrID err (%v)", v)
  28. }
  29. if len(data) == 0 {
  30. return ""
  31. }
  32. return fmt.Sprintf(arr[0], data...)
  33. }
  34. func (m MapData) NumberToInt64() (err error) {
  35. for k, v := range m {
  36. if integer, ok := v.(json.Number); ok {
  37. if m[k], err = integer.Int64(); err != nil {
  38. log.Error("service.log.numberToInt64(%v)(%v)", integer, err)
  39. }
  40. }
  41. }
  42. return
  43. }