Files
go-utils/id/order_id.go
2025-06-04 15:52:46 +08:00

85 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package id
import (
"fmt"
"math/rand"
"strings"
"sync/atomic"
"time"
"github.com/tx7do/go-utils/trans"
)
type idCounter uint32
func (c *idCounter) Increase() uint32 {
cur := *c
atomic.AddUint32((*uint32)(c), 1)
atomic.CompareAndSwapUint32((*uint32)(c), 1000, 0)
return uint32(cur)
}
var orderIdIndex idCounter
// GenerateOrderIdWithRandom 生成20位订单号前缀 + 时间戳 + 随机数
func GenerateOrderIdWithRandom(prefix string, tm *time.Time) string {
// 前缀 + 时间戳14位 + 随机数4位
if tm == nil {
tm = trans.Time(time.Now())
}
timestamp := tm.Format("20060102150405")
randNum := rand.Intn(10000) // 生成0-9999之间的随机数
return fmt.Sprintf("%s%s%d", prefix, timestamp, randNum)
}
// GenerateOrderIdWithIncreaseIndex 生成20位订单号前缀+时间+自增长索引
func GenerateOrderIdWithIncreaseIndex(prefix string, tm *time.Time) string {
if tm == nil {
tm = trans.Time(time.Now())
}
timestamp := tm.Format("20060102150405")
index := orderIdIndex.Increase()
return fmt.Sprintf("%s%s%d", prefix, timestamp, index)
}
// GenerateOrderIdWithTenantId 带商户ID的订单ID生成器202506041234567890123
func GenerateOrderIdWithTenantId(tenantID string) string {
// 时间戳14位 + 商户ID固定 5 位) + 随机数4位
// 时间戳部分(精确到毫秒)
now := time.Now()
timestamp := now.Format("20060102150405")
// 商户ID部分截取或补零到5位
tenantPart := tenantID
if len(tenantPart) > 5 {
tenantPart = tenantPart[:5]
} else {
tenantPart = fmt.Sprintf("%-5s", tenantPart)
tenantPart = strings.ReplaceAll(tenantPart, " ", "0")
}
// 随机数部分4位
n := rand.Int31n(10000)
randomPart := fmt.Sprintf("%04d", n)
return timestamp + tenantPart + randomPart
}
func GenerateOrderIdWithPrefixSonyflake(prefix string) string {
id, _ := NewSonyflakeID()
return fmt.Sprintf("%s%d", prefix, id)
}
func GenerateOrderIdWithPrefixSnowflake(workerId int64, prefix string) string {
id, _ := NewSnowflakeID(workerId)
return fmt.Sprintf("%s%d", prefix, id)
}