tool.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package assist
  2. import (
  3. "fmt"
  4. "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
  5. "math"
  6. "math/rand"
  7. "regexp"
  8. "strconv"
  9. "time"
  10. "unicode"
  11. )
  12. func GetOrderNumber() string {
  13. parse, _ := time.Parse(time_parse.CSTLayout, "2021-06-01 00:00:00")
  14. hours := time.Now().Sub(parse).Hours()
  15. day := math.Floor(hours / 24)
  16. prefix := fmt.Sprintf("%05d", int64(day))
  17. format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d"
  18. n := math.Pow10(10)
  19. return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n)))
  20. }
  21. func StringInArray(str string, strArray []string) bool {
  22. for _, s := range strArray {
  23. if str == s {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. func Uint64InArray(str uint64, uint64Array []uint64) bool {
  30. for _, s := range uint64Array {
  31. if str == s {
  32. return true
  33. }
  34. }
  35. return false
  36. }
  37. func CurrencyDecimal(value float64) float64 {
  38. value, _ = strconv.ParseFloat(fmt.Sprintf("%.8f", value), 64)
  39. return value
  40. }
  41. func IsChinese(str string) bool {
  42. var count int
  43. for _, v := range str {
  44. if unicode.Is(unicode.Han, v) {
  45. count++
  46. break
  47. }
  48. }
  49. return count > 0
  50. }
  51. func RandInt(max int) int {
  52. rand.Seed(time.Now().UnixNano())
  53. <-time.After(time.Nanosecond)
  54. return rand.Intn(max)
  55. }
  56. func StringUniqueInArray(valueArray []string) []string {
  57. result := make([]string, 0, len(valueArray))
  58. temp := map[string]struct{}{}
  59. for _, item := range valueArray {
  60. if _, ok := temp[item]; !ok {
  61. temp[item] = struct{}{}
  62. result = append(result, item)
  63. }
  64. }
  65. return result
  66. }
  67. func Uint64UniqueInArray(valueArray []uint64) []uint64 {
  68. result := make([]uint64, 0, len(valueArray))
  69. temp := map[uint64]struct{}{}
  70. for _, item := range valueArray {
  71. if _, ok := temp[item]; !ok {
  72. temp[item] = struct{}{}
  73. result = append(result, item)
  74. }
  75. }
  76. return result
  77. }
  78. func FindIPv4(input string) string {
  79. partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
  80. must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp
  81. matchMe := regexp.MustCompile(must)
  82. return matchMe.FindString(input)
  83. }
  84. func RandString(n int) string {
  85. var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  86. b := make([]rune, n)
  87. for i := range b {
  88. b[i] = letterRunes[RandInt(len(letterRunes))]
  89. }
  90. return string(b)
  91. }