utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package utils
  2. import (
  3. "database/sql"
  4. "hash/crc32"
  5. "reflect"
  6. "strconv"
  7. "time"
  8. uuid "github.com/satori/go.uuid"
  9. )
  10. func GetUUID() string {
  11. return uuid.NewV4().String()
  12. }
  13. //the result likes 1423361979
  14. func GetTimestamp() int64 {
  15. return time.Now().Unix()
  16. }
  17. func Int2String(val int64) string {
  18. return strconv.Itoa(int(val))
  19. }
  20. func ValidTime(t time.Time) bool {
  21. t1970, _ := time.Parse("2006-01-02", "1970-01-01")
  22. return t.After(t1970)
  23. }
  24. func String2Int64(str string) int64 {
  25. number, _ := strconv.ParseInt(str, 10, 64)
  26. return number
  27. }
  28. func HashCode(s string) int {
  29. v := int(crc32.ChecksumIEEE([]byte(s)))
  30. if v >= 0 {
  31. return v
  32. }
  33. if -v >= 0 {
  34. return -v
  35. }
  36. return 0
  37. }
  38. func GetTimeYmd() time.Time {
  39. timeNow := time.Now()
  40. return time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  41. }
  42. //剔除sqlx.NullTime等model数据结构
  43. func UnmarshalMysqlRows(row interface{}) []map[string]interface{} {
  44. tyR := reflect.TypeOf(row)
  45. if tyR.Kind() != reflect.Slice {
  46. panic("row must slice")
  47. }
  48. vR := reflect.ValueOf(row)
  49. var rows []map[string]interface{}
  50. for i := 0; i < vR.Len(); i++ {
  51. value := vR.Index(i).Interface()
  52. r := UnmarshalMysqlRow(value)
  53. rows = append(rows, r)
  54. }
  55. return rows
  56. }
  57. func UnmarshalMysqlRow(row interface{}) map[string]interface{} {
  58. tyR := reflect.TypeOf(row)
  59. if tyR.Kind() != reflect.Struct {
  60. panic("row must struct")
  61. }
  62. tyV := reflect.ValueOf(row)
  63. var Row = make(map[string]interface{})
  64. for i := 0; i < tyR.NumField(); i++ {
  65. tag := tyR.Field(i).Tag.Get("db")
  66. if tag == "" {
  67. tag = tyR.Field(i).Name
  68. }
  69. tyF := reflect.TypeOf(tyV.Field(i).Interface())
  70. switch tyF.Kind() {
  71. case reflect.Struct:
  72. switch tyV.Field(i).Type().String() {
  73. case "sql.NullTime":
  74. reStruct := tyV.Field(i).Interface().(sql.NullTime)
  75. if !reStruct.Time.Equal(time.Time{}) && reStruct.Time.Unix() != 0 {
  76. Row[tag] = reStruct.Time
  77. } else {
  78. Row[tag] = ""
  79. }
  80. case "sql.NullBool":
  81. reStruct := tyV.Field(i).Interface().(sql.NullBool)
  82. Row[tag] = reStruct.Bool
  83. case "sql.NullFloat64":
  84. reStruct := tyV.Field(i).Interface().(sql.NullFloat64)
  85. Row[tag] = reStruct.Float64
  86. case "sql.NullInt32":
  87. reStruct := tyV.Field(i).Interface().(sql.NullInt32)
  88. Row[tag] = reStruct.Int32
  89. case "sql.NullInt64":
  90. reStruct := tyV.Field(i).Interface().(sql.NullInt64)
  91. Row[tag] = reStruct.Int64
  92. case "sql.NullString":
  93. reStruct := tyV.Field(i).Interface().(sql.NullString)
  94. Row[tag] = reStruct.String
  95. case "time.Time":
  96. reStruct := tyV.Field(i).Interface().(time.Time)
  97. if !reStruct.Equal(time.Time{}) && reStruct.Unix() != 0 {
  98. Row[tag] = tyV.Field(i).Interface().(time.Time)
  99. } else {
  100. Row[tag] = ""
  101. }
  102. }
  103. case reflect.Bool, reflect.Int,
  104. reflect.Int8, reflect.Int16,
  105. reflect.Int32, reflect.Int64,
  106. reflect.Complex128, reflect.Uint,
  107. reflect.Uint8, reflect.Uint16,
  108. reflect.Uint32, reflect.Uint64,
  109. reflect.Uintptr, reflect.Float32,
  110. reflect.String, reflect.Float64, reflect.Complex64:
  111. Row[tag] = tyV.Field(i).Interface()
  112. }
  113. }
  114. return Row
  115. }