utils.go 4.0 KB

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