2023-02-26 23:25:20 +08:00
|
|
|
package sonyflake
|
|
|
|
|
|
|
|
import (
|
2023-02-28 00:03:02 +08:00
|
|
|
"fmt"
|
2023-02-26 23:25:20 +08:00
|
|
|
"github.com/sony/sonyflake"
|
2023-02-26 23:48:03 +08:00
|
|
|
sf "matrix-middle-service/pkg/config/sonyflake"
|
|
|
|
"matrix-middle-service/pkg/logger"
|
2023-02-26 23:25:20 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var generator *sonyflake.Sonyflake
|
|
|
|
|
2023-02-26 23:48:03 +08:00
|
|
|
func Init(config *sf.Config) {
|
|
|
|
logger.Log().Debug("[sonyFlake] 初始化")
|
2023-02-26 23:25:20 +08:00
|
|
|
var s sonyflake.Settings
|
2023-02-26 23:48:03 +08:00
|
|
|
s.StartTime, _ = time.Parse("2006-01-02", config.StartTime)
|
2023-02-26 23:25:20 +08:00
|
|
|
generator = sonyflake.NewSonyflake(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SonyFlake() *sonyflake.Sonyflake {
|
|
|
|
return generator
|
|
|
|
}
|
2023-02-28 00:03:02 +08:00
|
|
|
|
|
|
|
func NextId() uint64 {
|
|
|
|
id, _ := generator.NextID()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func NextStrId() string {
|
|
|
|
return fmt.Sprintf("%d", NextId())
|
|
|
|
}
|