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 } // GenerateSerialNumber 生成任务流水号 func (c *snowClient) GenerateSerialNumber() (string, error) { return c.generateCodeWithPrefix("SN") } // GenerateCouponTemplateId 生成券模板 ID func (c *snowClient) GenerateCouponTemplateId() (string, error) { return c.generateCodeWithPrefix("Q") }