订单计算相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.3 KiB

3 years ago
  1. package ordercalc
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. /**
  9. * 订单号规则
  10. * id后4位随机4位+当前时间10
  11. * 2020/08/10
  12. */
  13. func NewOrderSn(user_id string) string {
  14. var prefix string
  15. if user_id != "" {
  16. if len(user_id) > 4 {
  17. prefix = user_id[len(user_id)-4:] //截取后4位
  18. } else {
  19. prefix = user_id
  20. }
  21. } else {
  22. //随机4位
  23. str := "123456789"
  24. bytes := []byte(str)
  25. result := []byte{}
  26. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  27. for i := 0; i < 4; i++ {
  28. result = append(result, bytes[r.Intn(len(bytes))])
  29. }
  30. prefix = string(result)
  31. }
  32. //拼接
  33. var build strings.Builder
  34. build.WriteString(prefix)
  35. build.WriteString(strconv.FormatInt(time.Now().Unix(), 10)) //当前时间
  36. return build.String()
  37. }
  38. /**
  39. * 核销码规则
  40. * 随机2位+当前时间6
  41. * 2020/08/10
  42. */
  43. func VerifyNumber() string {
  44. var prefix string
  45. //随机4位
  46. str := "123456789"
  47. bytes := []byte(str)
  48. result := []byte{}
  49. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  50. for i := 0; i < 1; i++ {
  51. result = append(result, bytes[r.Intn(len(bytes))])
  52. }
  53. prefix = string(result)
  54. //拼接
  55. var build strings.Builder
  56. build.WriteString(prefix)
  57. build.WriteString(strconv.FormatInt(time.Now().UnixNano(), 10)[10:17]) //当前时间
  58. return build.String()
  59. }