调整表结构
This commit is contained in:
52
utility/snowid/snowid.go
Normal file
52
utility/snowid/snowid.go
Normal file
@ -0,0 +1,52 @@
|
||||
package snowid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
instance *snowClient
|
||||
)
|
||||
|
||||
type snowClient struct {
|
||||
node *snowflake.Node
|
||||
}
|
||||
|
||||
func init() {
|
||||
ctx := context.Background()
|
||||
once.Do(func() {
|
||||
instance = &snowClient{}
|
||||
instance.node, _ = snowflake.NewNode(g.Config().MustGet(ctx, "snowId.workerId").Int64())
|
||||
})
|
||||
}
|
||||
func GetSnowClient() *snowClient {
|
||||
return instance
|
||||
}
|
||||
|
||||
// GenerateMerchantCode 生成商户编号
|
||||
func (c *snowClient) GenerateMerchantCode() (string, error) {
|
||||
return c.generateCodeWithPrefix("M")
|
||||
}
|
||||
|
||||
// GenerateStoreCode 生成门店编号
|
||||
func (c *snowClient) GenerateStoreCode() (string, error) {
|
||||
return c.generateCodeWithPrefix("S")
|
||||
}
|
||||
|
||||
// GenerateUserCode 生成用户编号
|
||||
func (c *snowClient) GenerateUserCode() (string, error) {
|
||||
return c.generateCodeWithPrefix("U")
|
||||
}
|
||||
|
||||
func (c *snowClient) generateCodeWithPrefix(prefix string) (string, error) {
|
||||
if c.node == nil {
|
||||
return "", fmt.Errorf("雪花节点未初始化")
|
||||
}
|
||||
id := c.node.Generate().Int64()
|
||||
return fmt.Sprintf("%s%d", prefix, id), nil
|
||||
}
|
||||
Reference in New Issue
Block a user