tool.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package tool
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. Layout = "2006-01-02 15:04:05"
  13. DateTime = "2006-01-02"
  14. DefaultExecTime = "0_0_0"
  15. )
  16. // StringToTimeUnix 时间字符串转换成时间戳
  17. // a.g 6_2_3 ===> 转换成 6天2小时3分钟后的时间戳
  18. // TODO 需要优化代码写得太死,需要优化后兼容秒的场景
  19. func StringToTimeUnix(execTimeStr string) int64 {
  20. var processAt = time.Now().Unix()
  21. if execTimeStr == DefaultExecTime {
  22. return processAt
  23. }
  24. execTime := strings.Split(execTimeStr, "_")
  25. if len(execTime) < 3 {
  26. return processAt
  27. }
  28. // 天数
  29. days, _ := strconv.Atoi(execTime[0])
  30. processAt += int64(days) * 24 * 60 * 60
  31. // 小时
  32. hours, _ := strconv.Atoi(execTime[1])
  33. processAt += int64(hours) * 60 * 60
  34. // 分钟
  35. minutes, _ := strconv.Atoi(execTime[2])
  36. processAt += int64(minutes) * 60
  37. return processAt
  38. }
  39. func GetLocalTime(timeStr string) time.Time {
  40. execTime, _ := time.ParseInLocation(Layout, timeStr, time.Local)
  41. return execTime
  42. }
  43. // GetTargetByValueForTag 利用反射根据cond获取对应字段数据,主要tag必须为gorm
  44. // e.g person{Name:"ping",Age:12}
  45. // e.g GetTargetByTag(person,age) ==> 12
  46. func GetTargetByValueForTag(target interface{}, cond string) interface{} {
  47. tf := reflect.Value{}
  48. // 判断是不是指针类型
  49. if reflect.TypeOf(target).Kind() == reflect.Ptr {
  50. if reflect.ValueOf(target).IsNil() { // 空指针
  51. return ""
  52. }
  53. tf = reflect.Indirect(reflect.ValueOf(target))
  54. } else {
  55. tf = reflect.ValueOf(target)
  56. }
  57. typ := tf.Type()
  58. var condValue interface{}
  59. for k := 0; k < typ.NumField(); k++ {
  60. key := typ.Field(k).Tag.Get("gorm")
  61. if key != cond {
  62. continue
  63. }
  64. field := typ.Field(k).Name
  65. switch tf.FieldByName(field).Kind() {
  66. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  67. condValue = tf.FieldByName(field).Int()
  68. case reflect.String:
  69. condValue = tf.FieldByName(field).String()
  70. }
  71. }
  72. return condValue
  73. }
  74. // GetTargetByTypeForTag 利用反射根据cond获取对应的字段类型默认值
  75. // e.g person{Name:"ping",Age:12,EventStatus: fieldPb.EventStatus_ADMISSION}
  76. // e.g GetTargetByTypeForTag(person,age) ==> person{Name:"ping",Age:0,EventStatus: fieldPb.EventStatus_ADMISSION}
  77. // e.g GetTargetByTypeForTag(person,name) ==> person{Name:"",Age:12,EventStatus: fieldPb.EventStatus_ADMISSION}
  78. // e.g GetTargetByTypeForTag(person,event_status) ==> person{Name:"ping",Age:12,EventStatus: fieldPb.EventStatus_INVALID}
  79. func GetTargetByTypeForTag(target interface{}, cond string) {
  80. tf := reflect.TypeOf(target)
  81. // 判断是不是指针类型
  82. if tf.Kind() == reflect.Ptr {
  83. tf = tf.Elem()
  84. }
  85. tv := reflect.ValueOf(target)
  86. if tv.Kind() == reflect.Ptr {
  87. tv = tv.Elem()
  88. }
  89. for i := 0; i < tf.NumField(); i++ {
  90. key := tf.Field(i).Tag.Get("gorm")
  91. if key != cond {
  92. continue
  93. }
  94. fieldName := tf.Field(i).Name
  95. tfKind := tf.Field(i).Type
  96. if tfKind.Kind() == reflect.String {
  97. tv.FieldByName(fieldName).SetString("")
  98. }
  99. if tfKind.Kind() == reflect.Int || tfKind.Kind() == reflect.Int8 || tfKind.Kind() == reflect.Int16 ||
  100. tfKind.Kind() == reflect.Int32 || tfKind.Kind() == reflect.Int64 {
  101. tv.FieldByName(fieldName).SetInt(0)
  102. }
  103. if tfKind.Kind() == reflect.Uint || tfKind.Kind() == reflect.Uint8 || tfKind.Kind() == reflect.Uint16 ||
  104. tfKind.Kind() == reflect.Uint32 || tfKind.Kind() == reflect.Uint64 {
  105. tv.FieldByName(fieldName).SetUint(0)
  106. }
  107. if tfKind.Kind() == reflect.Float32 || tfKind.Kind() == reflect.Float64 {
  108. tv.FieldByName(fieldName).SetFloat(0)
  109. }
  110. }
  111. }
  112. // MonthLastDay 获取当月最后一天
  113. //eg 2023-01 => 2023-01-31
  114. //eg 2023-02 => 2023-02-28
  115. func MonthLastDay(yearMonth string) string {
  116. item := strings.Split(yearMonth, "-")
  117. if len(item) < 2 {
  118. return ""
  119. }
  120. year, _ := strconv.Atoi(item[0])
  121. loc, _ := time.LoadLocation("Local")
  122. theTime, _ := time.ParseInLocation(Layout, fmt.Sprintf("%s-01 00:00:00", yearMonth), loc)
  123. return time.Date(year, theTime.Month()+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  124. }
  125. // TimeParseLocalUnix 获取当天零点的时间戳
  126. // eg 2023-02-22 => 1676995200
  127. func TimeParseLocalUnix(DayTime string) int64 {
  128. value := DayTime
  129. if len(DayTime) <= 11 {
  130. value = fmt.Sprintf("%s 00:00:00", DayTime)
  131. }
  132. loc, _ := time.LoadLocation("Local")
  133. theTime, _ := time.ParseInLocation(Layout, value, loc)
  134. return theTime.Unix()
  135. }
  136. // ColumNameValueJoinSqlString 获取columName对应的值拼接成sql语句
  137. // eg columName = "a,b,c" ==> ('1','2','3')
  138. func ColumNameValueJoinSqlString(columName string, target interface{}) string {
  139. columNames := strings.Split(columName, ",")
  140. insertValue := "("
  141. if target == nil {
  142. return insertValue
  143. }
  144. for _, c := range columNames {
  145. val := reflect.Value{}
  146. if reflect.TypeOf(target).Kind() == reflect.Ptr {
  147. val = reflect.Indirect(reflect.ValueOf(target))
  148. } else {
  149. val = reflect.ValueOf(target)
  150. }
  151. typ := val.Type()
  152. strValue := ""
  153. for k := 0; k < typ.NumField(); k++ {
  154. key := typ.Field(k).Tag.Get("json")
  155. key = strings.ReplaceAll(key, ",omitempty", "")
  156. if key != c {
  157. continue
  158. }
  159. field := typ.Field(k).Name
  160. switch val.FieldByName(field).Kind() {
  161. case reflect.String:
  162. strValue = fmt.Sprintf("'%s'", val.FieldByName(field).String())
  163. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  164. strValue = fmt.Sprintf("%d", val.FieldByName(field).Int())
  165. case reflect.Float64, reflect.Float32:
  166. strValue = fmt.Sprintf("%f", val.FieldByName(field).Float())
  167. }
  168. }
  169. insertValue += fmt.Sprintf("%s,", strValue)
  170. }
  171. return fmt.Sprintf("%s)", strings.TrimRight(insertValue, ","))
  172. }
  173. // ColumNameValue 获取columName 对应的值
  174. // eg a => 1
  175. func ColumNameValue(columName string, target interface{}) string {
  176. strValue := ""
  177. if target == nil {
  178. return strValue
  179. }
  180. val := reflect.Value{}
  181. if reflect.TypeOf(target).Kind() == reflect.Ptr {
  182. val = reflect.Indirect(reflect.ValueOf(target))
  183. } else {
  184. val = reflect.ValueOf(target)
  185. }
  186. typ := val.Type()
  187. for k := 0; k < typ.NumField(); k++ {
  188. key := typ.Field(k).Tag.Get("json")
  189. key = strings.ReplaceAll(key, ",omitempty", "")
  190. if key != columName {
  191. continue
  192. }
  193. field := typ.Field(k).Name
  194. switch val.FieldByName(field).Kind() {
  195. case reflect.String:
  196. strValue = fmt.Sprintf("'%s'", val.FieldByName(field).String())
  197. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  198. strValue = fmt.Sprintf("%d", val.FieldByName(field).Int())
  199. case reflect.Float64, reflect.Float32:
  200. strValue = fmt.Sprintf("%f", val.FieldByName(field).Float())
  201. }
  202. }
  203. return strValue
  204. }
  205. // UnixTimeString unix时间戳转化成string eg 1673939363 => 2023-01-17
  206. func UnixTimeString(unixTime int64) string {
  207. return time.Unix(unixTime, 0).Format(DateTime)
  208. }
  209. func Md5String(input string) string {
  210. s := md5.New()
  211. digest := strings.ReplaceAll(string(input), "\n", "")
  212. s.Write([]byte(digest))
  213. return hex.EncodeToString(s.Sum(nil))
  214. }