This commit is contained in:
李子铭 2025-03-20 16:17:13 +08:00
parent f469b36833
commit e511aae2d4
7 changed files with 35 additions and 64 deletions

View File

@ -36,6 +36,7 @@ type OrderCreateReqBo struct {
AppID string
Type vo.OrderType
AccountType vo.OrderAccountType
NotifyUrl string
Attach string
}

View File

@ -4,6 +4,6 @@ import "context"
// GenerateMixRepo interface
type GenerateMixRepo interface {
GeneratorString(ctx context.Context) string
GeneratorNumber(ctx context.Context) int64
GeneratorString(ctx context.Context, uid string) string
GeneratorNumber(ctx context.Context, uid string) int64
}

View File

@ -64,7 +64,7 @@ func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error {
func (v *VoucherBiz) create(ctx context.Context, req *bo.OrderCreateReqBo, product *bo.ProductBo) (*bo.OrderBo, error) {
return v.OrderRepo.Create(ctx, &bo.OrderBo{
OrderNo: v.GenerateMixRepo.GeneratorString(ctx),
OrderNo: v.GenerateMixRepo.GeneratorString(ctx, fmt.Sprintf("%d%s", req.Type, req.OutBizNo)),
OutBizNo: req.OutBizNo,
ProductNo: req.ProductNo,
Account: req.Account,
@ -72,9 +72,9 @@ func (v *VoucherBiz) create(ctx context.Context, req *bo.OrderCreateReqBo, produ
MerchantNo: product.MchId,
Channel: product.Channel,
BatchNo: product.BatchNo,
NotifyUrl: v.bc.Cmb.NotifyUrl,
NotifyUrl: req.NotifyUrl,
AccountType: vo.OrderAccountTypeOpenId,
Type: vo.OrderTypeCmb,
Type: req.Type,
Status: vo.OrderStatusIng, // 同步发放,状态至为发放中
Attach: req.Attach,
})

View File

@ -2,12 +2,12 @@ package mixrepoimpl
import (
"context"
"fmt"
"github.com/bwmarrin/snowflake"
"hash/fnv"
"math"
"os"
"voucher/internal/biz/mixrepos"
"voucher/internal/data"
"voucher/internal/pkg/helper"
)
type GenerateRepoImpl struct {
@ -25,7 +25,7 @@ func NewGenerateMixRepoImpl(rdb *data.Rdb) (mixrepos.GenerateMixRepo, error) {
return nil, err
}
serverId := g.hashMod(name)
serverId := helper.HashMod(name)
node, err := snowflake.NewNode(int64(serverId))
if err != nil {
@ -37,21 +37,14 @@ func NewGenerateMixRepoImpl(rdb *data.Rdb) (mixrepos.GenerateMixRepo, error) {
return g, nil
}
// hashMod hash mod
func (s *GenerateRepoImpl) hashMod(hashStr string) int {
hash := fnv.New32a()
_, _ = hash.Write([]byte(hashStr))
hashValue := hash.Sum32()
return int(math.Mod(float64(hashValue), 32))
}
// GeneratorString 生成字符串
func (s *GenerateRepoImpl) GeneratorString(_ context.Context) string {
return s.node.Generate().String()
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) int64 {
return s.node.Generate().Int64()
func (s *GenerateRepoImpl) GeneratorNumber(_ context.Context, uid string) int64 {
id := helper.HashMod(uid)
return s.node.Generate().Int64() + int64(id)
}

View File

@ -1,8 +1,9 @@
package helper
import (
"hash/fnv"
"math"
"os"
"reflect"
)
func FileExists(filePath string) bool {
@ -10,46 +11,10 @@ func FileExists(filePath string) bool {
return err == nil || os.IsExist(err)
}
// DeepCopy 深拷贝函数
func DeepCopy(src interface{}) interface{} {
if src == nil {
return nil
}
t := reflect.TypeOf(src)
v := reflect.ValueOf(src)
// 创建新的对象
dst := reflect.New(t.Elem()).Elem()
// 遍历结构体的所有字段
for i := 0; i < t.Elem().NumField(); i++ {
field := t.Elem().Field(i)
srcField := v.Elem().Field(i)
dstField := dst.Field(i)
// 处理不同类型的字段
switch field.Type.Kind() {
case reflect.Map:
// 处理映射类型
newMap := reflect.MakeMap(field.Type)
for _, key := range srcField.MapKeys() {
value := srcField.MapIndex(key)
newMap.SetMapIndex(key, value)
}
dstField.Set(newMap)
case reflect.Slice:
// 处理切片类型
newSlice := reflect.MakeSlice(field.Type, srcField.Len(), srcField.Cap())
reflect.Copy(newSlice, srcField)
dstField.Set(newSlice)
case reflect.Ptr:
// 处理指针类型
if !srcField.IsNil() {
newPtr := reflect.New(field.Type.Elem())
newPtr.Elem().Set(DeepCopy(srcField.Elem()).(reflect.Value))
dstField.Set(newPtr)
}
default:
// 处理其他类型
dstField.Set(srcField)
}
}
return dst.Interface()
func HashMod(hashStr string) int {
hash := fnv.New32a()
_, _ = hash.Write([]byte(hashStr))
hashValue := hash.Sum32()
return int(math.Mod(float64(hashValue), 32))
}

View File

@ -0,0 +1,11 @@
package helper
import (
"fmt"
"testing"
)
func TestHashMod(t *testing.T) {
serverId := HashMod("1dfsfdsfsddf12dddd5451212iodewnsanf2")
fmt.Println(serverId)
}

View File

@ -101,6 +101,7 @@ func (v *VoucherService) cmbOrder(ctx http.Context) (string, error) {
Attach: bizContent.Attach,
AccountType: vo.OrderAccountTypeOpenId,
Type: vo.OrderTypeCmb,
NotifyUrl: v.bc.Cmb.NotifyUrl,
}
orderNo, err := v.VoucherBiz.CmbOrder(ctx, boReq)