utils.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package utils
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "git.i2edu.net/i2/i2-bill-erp/transform"
  6. "hash/crc32"
  7. "reflect"
  8. "strconv"
  9. "time"
  10. uuid "github.com/satori/go.uuid"
  11. )
  12. func GetUUID() string {
  13. return uuid.NewV4().String()
  14. }
  15. //the result likes 1423361979
  16. func GetTimestamp() int64 {
  17. return time.Now().Unix()
  18. }
  19. func Int2String(val int64) string {
  20. return strconv.Itoa(int(val))
  21. }
  22. func ValidTime(t time.Time) bool {
  23. t1970, _ := time.Parse("2006-01-02", "1970-01-01")
  24. return t.After(t1970)
  25. }
  26. func String2Int64(str string) int64 {
  27. number, _ := strconv.ParseInt(str, 10, 64)
  28. return number
  29. }
  30. func HashCode(s string) int {
  31. v := int(crc32.ChecksumIEEE([]byte(s)))
  32. if v >= 0 {
  33. return v
  34. }
  35. if -v >= 0 {
  36. return -v
  37. }
  38. return 0
  39. }
  40. func GetTimeYmd() time.Time {
  41. timeNow := time.Now()
  42. return time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  43. }
  44. //剔除sqlx.NullTime等model数据结构
  45. func UnmarshalMysqlRows(row interface{}) []map[string]interface{} {
  46. tyR := reflect.TypeOf(row)
  47. if tyR.Kind() != reflect.Slice {
  48. panic("row must slice")
  49. }
  50. vR := reflect.ValueOf(row)
  51. var rows []map[string]interface{}
  52. for i := 0; i < vR.Len(); i++ {
  53. value := vR.Index(i).Interface()
  54. r := UnmarshalMysqlRow(value)
  55. rows = append(rows, r)
  56. }
  57. return rows
  58. }
  59. func UnmarshalMysqlRow(row interface{}) map[string]interface{} {
  60. tyR := reflect.TypeOf(row)
  61. if tyR.Kind() != reflect.Struct {
  62. panic("row must struct")
  63. }
  64. tyV := reflect.ValueOf(row)
  65. var Row = make(map[string]interface{})
  66. for i := 0; i < tyR.NumField(); i++ {
  67. tag := tyR.Field(i).Tag.Get("db")
  68. if tag == "" {
  69. tag = tyR.Field(i).Name
  70. }
  71. tyF := reflect.TypeOf(tyV.Field(i).Interface())
  72. switch tyF.Kind() {
  73. case reflect.Struct:
  74. switch tyV.Field(i).Type().String() {
  75. case "sql.NullTime":
  76. reStruct := tyV.Field(i).Interface().(sql.NullTime)
  77. if !reStruct.Time.Equal(time.Time{}) && reStruct.Time.Unix() != 0 {
  78. Row[tag] = reStruct.Time
  79. } else {
  80. Row[tag] = ""
  81. }
  82. case "sql.NullBool":
  83. reStruct := tyV.Field(i).Interface().(sql.NullBool)
  84. Row[tag] = reStruct.Bool
  85. case "sql.NullFloat64":
  86. reStruct := tyV.Field(i).Interface().(sql.NullFloat64)
  87. Row[tag] = reStruct.Float64
  88. case "sql.NullInt32":
  89. reStruct := tyV.Field(i).Interface().(sql.NullInt32)
  90. Row[tag] = reStruct.Int32
  91. case "sql.NullInt64":
  92. reStruct := tyV.Field(i).Interface().(sql.NullInt64)
  93. Row[tag] = reStruct.Int64
  94. case "sql.NullString":
  95. reStruct := tyV.Field(i).Interface().(sql.NullString)
  96. Row[tag] = reStruct.String
  97. case "time.Time":
  98. reStruct := tyV.Field(i).Interface().(time.Time)
  99. if !reStruct.Equal(time.Time{}) && reStruct.Unix() != 0 {
  100. Row[tag] = tyV.Field(i).Interface().(time.Time)
  101. } else {
  102. Row[tag] = ""
  103. }
  104. }
  105. case reflect.Bool, reflect.Int,
  106. reflect.Int8, reflect.Int16,
  107. reflect.Int32, reflect.Int64,
  108. reflect.Complex128, reflect.Uint,
  109. reflect.Uint8, reflect.Uint16,
  110. reflect.Uint32, reflect.Uint64,
  111. reflect.Uintptr, reflect.Float32,
  112. reflect.String, reflect.Float64, reflect.Complex64:
  113. Row[tag] = tyV.Field(i).Interface()
  114. }
  115. }
  116. return Row
  117. }
  118. //返回对应tree的路径id,name
  119. func TreePath(nodes []*transform.TreeNode, tagId int64, idPath, namePath *string) bool {
  120. for _, node := range nodes {
  121. if node.Id == tagId {
  122. *idPath = *idPath + "," + fmt.Sprintf("%d", node.Id)
  123. *namePath = *namePath + "," + node.Text
  124. return true
  125. }
  126. if node.Nodes != nil {
  127. if ok := TreePath(node.Nodes, tagId, idPath, namePath); ok {
  128. *idPath = fmt.Sprintf("%d", node.Id) + "," + *idPath
  129. *namePath = node.Text + "," + *namePath
  130. return ok
  131. }
  132. }
  133. }
  134. return false
  135. }
  136. //活动id对应的name
  137. func GetErpActiveName(activeId int64, actives []*transform.Active) string {
  138. for _, a := range actives {
  139. if a.ActiveId == activeId {
  140. return a.MaName
  141. }
  142. }
  143. return ""
  144. }
  145. //校区对应的id
  146. func GetErpSchoolName(schId int64, schools []*transform.OrganSchool) string {
  147. for _, sch := range schools {
  148. if sch.Id == schId {
  149. return sch.Name
  150. }
  151. }
  152. return ""
  153. }