package mixrepoimpl

import (
	"context"
	"fmt"
	"github.com/bwmarrin/snowflake"
	"os"
	"voucher/internal/biz/mixrepos"
	"voucher/internal/pkg/helper"
)

type GenerateRepoImpl struct {
	node *snowflake.Node
}

func NewGenerateMixRepoImpl() (mixrepos.GenerateMixRepo, error) {
	g := &GenerateRepoImpl{}

	name, err := os.Hostname()
	if err != nil {
		return nil, err
	}

	serverId := helper.HashMod(name)

	node, err := snowflake.NewNode(int64(serverId))
	if err != nil {
		return nil, err
	}

	g.node = node

	return g, nil
}

// GeneratorString 生成字符串
func (s *GenerateRepoImpl) GeneratorString(_ context.Context, uid string) string {
	id := helper.HashMod(uid)
	return fmt.Sprintf("%s%d", s.node.Generate().String(), id)
}

// GeneratorNumber 生成 int64
func (s *GenerateRepoImpl) GeneratorNumber(_ context.Context, uid string) int64 {
	id := helper.HashMod(uid)
	return s.node.Generate().Int64() + int64(id)
}
