common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package common
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "time"
  8. )
  9. // TimeFormat time format
  10. var TimeFormat = "2006-01-02 15:04:05"
  11. //GrayField gray config for each business
  12. type GrayField struct {
  13. Name string
  14. Value string
  15. }
  16. // Pager .
  17. type Pager struct {
  18. Total int `json:"total" reflect:"ignore"`
  19. Pn int `form:"pn" default:"1" json:"pn" reflect:"ignore"`
  20. Ps int `form:"ps" default:"20" json:"ps" reflect:"ignore"`
  21. }
  22. // BaseOptions 公共参数
  23. type BaseOptions struct {
  24. BusinessID int64 `form:"business_id" json:"business_id"`
  25. NetID int64 `form:"net_id" json:"net_id"`
  26. FlowID int64 `form:"flow_id" json:"flow_id"`
  27. UID int64 `form:"uid" json:"uid" submit:"int"`
  28. OID string `form:"oid" json:"oid" submit:"string"`
  29. RID int64 `form:"rid" json:"rid"`
  30. Role int8 `form:"role" json:"role"`
  31. Debug int8 `form:"debug" json:"debug"`
  32. Uname string `form:"uname" json:"uname" submit:"string"`
  33. }
  34. // FormatTime .
  35. type FormatTime string
  36. // Scan .
  37. func (f *FormatTime) Scan(src interface{}) (err error) {
  38. switch sc := src.(type) {
  39. case time.Time:
  40. *f = FormatTime(sc.Format("2006-01-02 15:04:05"))
  41. case string:
  42. *f = FormatTime(sc)
  43. }
  44. return
  45. }
  46. // WaitTime 计算等待时长
  47. func WaitTime(ctime time.Time) string {
  48. wt := time.Since(ctime)
  49. h := int(wt.Hours())
  50. m := int(wt.Minutes()) % 60
  51. s := int(wt.Seconds()) % 60
  52. return fmt.Sprintf("%.2d:%.2d:%.2d", h, m, s)
  53. }
  54. //ParseWaitTime 。
  55. func ParseWaitTime(ut int64) string {
  56. h := ut / 3600
  57. m := ut % 3600 / 60
  58. s := ut % 60
  59. return fmt.Sprintf("%.2d:%.2d:%.2d", h, m, s)
  60. }
  61. // Group .
  62. type Group struct {
  63. ID int64 `json:"group_id"`
  64. Name string `json:"group_name"`
  65. Note string `json:"group_note"`
  66. Tag string `json:"group_tag"`
  67. FontColor string `json:"font_color"`
  68. BgColor string `json:"bg_color"`
  69. }
  70. // IntTime .
  71. type IntTime int64
  72. // Scan scan time.
  73. func (jt *IntTime) Scan(src interface{}) (err error) {
  74. switch sc := src.(type) {
  75. case time.Time:
  76. *jt = IntTime(sc.Unix())
  77. case string:
  78. var i int64
  79. i, err = strconv.ParseInt(sc, 10, 64)
  80. *jt = IntTime(i)
  81. }
  82. return
  83. }
  84. // Value get time value.
  85. func (jt IntTime) Value() (driver.Value, error) {
  86. return time.Unix(int64(jt), 0), nil
  87. }
  88. // Time get time.
  89. func (jt IntTime) Time() time.Time {
  90. return time.Unix(int64(jt), 0)
  91. }
  92. // UnmarshalJSON implement Unmarshaler
  93. func (jt *IntTime) UnmarshalJSON(data []byte) error {
  94. if data == nil || len(data) <= 1 {
  95. *jt = 0
  96. return nil
  97. }
  98. if data[0] != '"' {
  99. // 1.直接判断数字
  100. sti, err := strconv.Atoi(string(data))
  101. if err == nil {
  102. *jt = IntTime(sti)
  103. }
  104. return nil
  105. }
  106. str := string(data[1 : len(data)-1])
  107. // 2.标准格式判断
  108. st, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  109. if err == nil {
  110. *jt = IntTime(st.Unix())
  111. return nil
  112. }
  113. *jt = IntTime(0)
  114. return nil
  115. }
  116. // FilterName .
  117. func FilterName(s string) (res string) {
  118. exp := "[^a-zA-Z0-9_]+"
  119. reg, err := regexp.Compile(exp)
  120. if err != nil {
  121. res = s
  122. return
  123. }
  124. res = reg.ReplaceAllString(s, "")
  125. return
  126. }
  127. // FilterChname .
  128. func FilterChname(s string) (res string) {
  129. exp := "[^0-9_\u4e00-\u9fa5]+"
  130. reg, err := regexp.Compile(exp)
  131. if err != nil {
  132. res = s
  133. return
  134. }
  135. res = reg.ReplaceAllString(s, "")
  136. return
  137. }
  138. // FilterBusinessName .
  139. func FilterBusinessName(s string) (res string) {
  140. exp := "[^a-zA-Z\u4e00-\u9fa5]+"
  141. reg, err := regexp.Compile(exp)
  142. if err != nil {
  143. res = s
  144. return
  145. }
  146. res = reg.ReplaceAllString(s, "")
  147. return
  148. }
  149. //Unique remove duplicated value from slice
  150. func Unique(ids []int64, gthan0 bool) (res []int64) {
  151. res = []int64{}
  152. mm := map[int64]int64{}
  153. for _, id := range ids {
  154. if mm[id] == id || (gthan0 && id <= 0) {
  155. continue
  156. }
  157. res = append(res, id)
  158. mm[id] = id
  159. }
  160. return
  161. }
  162. //CopyMap copy src to dest
  163. func CopyMap(src, dest map[int64][]int64, gthan0 bool) (res map[int64][]int64) {
  164. if dest == nil {
  165. dest = map[int64][]int64{}
  166. }
  167. for k, v := range src {
  168. dest[k] = append(dest[k], v...)
  169. dest[k] = Unique(dest[k], gthan0)
  170. }
  171. res = dest
  172. return
  173. }