order_no.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package domainservice
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "context"
  5. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/database/myredis"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/idutil"
  9. "time"
  10. )
  11. const OrderRedisKey = "bzd_clod_logistic_order_no"
  12. func GetOrderNo(ctx context.Context) (string, error) {
  13. client := global.CommonConnectRepoInst.Redis
  14. if client == nil {
  15. return "", errors.WithStackOnce(errors.New("未初始化Redis,无法使用"))
  16. }
  17. orderNo, err := client.BRPop(ctx, 3*time.Second, OrderRedisKey).Result()
  18. if err != nil {
  19. return "", errors.WithStackOnce(err)
  20. }
  21. if len(orderNo) < 2 {
  22. return "", errors.WithStackOnce(errors.New("订单号获取失败"))
  23. }
  24. // 异步增加订单号
  25. go func() {
  26. if err = GenOrderNo(ctx); err != nil {
  27. log.FromContext(ctx).Errorf("%#+v", err)
  28. }
  29. }()
  30. return orderNo[1], nil
  31. }
  32. func GenOrderNo(ctx context.Context) (err error) {
  33. client := global.CommonConnectRepoInst.Redis
  34. if client == nil {
  35. return errors.WithStackOnce(errors.New("未初始化Redis,无法使用"))
  36. }
  37. count, err := client.LLen(ctx, OrderRedisKey).Result()
  38. if err != nil {
  39. return errors.WithStackOnce(err)
  40. }
  41. if count > 1000 {
  42. return nil
  43. }
  44. mutex := myredis.NewRedisMutexLock(OrderRedisKey, client)
  45. if err = mutex.LockContext(ctx); err != nil {
  46. return errors.WithStackOnce(errors.New("获取分布式锁失败"))
  47. }
  48. defer func() {
  49. // 如果释放失败了,就重试一次
  50. var ok bool
  51. if ok, err = mutex.UnlockContext(ctx); !ok || err != nil {
  52. _, err = mutex.UnlockContext(ctx)
  53. }
  54. }()
  55. // 每次生成订单号:n*i
  56. for n := 0; n < 10; n++ {
  57. orderNoList := make([]string, 0, 500)
  58. for i := 0; i < 500; i++ {
  59. orderNoList = append(orderNoList, idutil.GetSonyFlakeStringID())
  60. time.Sleep(500 * time.Millisecond)
  61. }
  62. err = client.LPush(ctx, OrderRedisKey, orderNoList).Err()
  63. if err != nil {
  64. return errors.WithStackOnce(err)
  65. }
  66. }
  67. return nil
  68. }
  69. // GetOrderSubNo 订单揽件码
  70. func GetOrderSubNo(ctx context.Context, orderNo string) (string, error) {
  71. client := global.CommonConnectRepoInst.Redis
  72. code, err := client.Get(ctx, orderNo).Result()
  73. if err != nil {
  74. return "", errors.WithStackOnce(err)
  75. }
  76. if code == "" {
  77. code = orderNo[len(orderNo)-6:]
  78. err = client.Set(ctx, code, orderNo, 7*24*time.Hour).Err()
  79. if err != nil {
  80. return "", errors.WithStackOnce(err)
  81. }
  82. }
  83. return code, nil
  84. }
  85. // DelOrderSubNo 删除订单揽件码
  86. func DelOrderSubNo(ctx context.Context, keys ...string) error {
  87. client := global.CommonConnectRepoInst.Redis
  88. _, err := client.Del(ctx, keys...).Result()
  89. if err != nil {
  90. return errors.WithStackOnce(err)
  91. }
  92. return nil
  93. }