cmb
This commit is contained in:
parent
ad6bb8b928
commit
7c764e53b7
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"voucher/internal/biz"
|
"voucher/internal/biz"
|
||||||
"voucher/internal/conf"
|
"voucher/internal/conf"
|
||||||
"voucher/internal/data"
|
"voucher/internal/data"
|
||||||
|
"voucher/internal/data/mixrepoimpl"
|
||||||
"voucher/internal/data/repoimpl"
|
"voucher/internal/data/repoimpl"
|
||||||
"voucher/internal/data/thirdrepoimpl"
|
"voucher/internal/data/thirdrepoimpl"
|
||||||
"voucher/internal/data/wechatrepoimpl"
|
"voucher/internal/data/wechatrepoimpl"
|
||||||
|
|
@ -26,10 +27,11 @@ func wireApp(*conf.Bootstrap, log.Logger, *log2.AccessLogger) (*kratos.App, func
|
||||||
server.ProviderSetServer,
|
server.ProviderSetServer,
|
||||||
service.ProviderSetService,
|
service.ProviderSetService,
|
||||||
biz.ProviderSetBiz,
|
biz.ProviderSetBiz,
|
||||||
|
data.ProviderDataSet,
|
||||||
repoimpl.ProviderRepoImplSet,
|
repoimpl.ProviderRepoImplSet,
|
||||||
thirdrepoimpl.ProviderThirdRepositoryImplSet,
|
thirdrepoimpl.ProviderThirdRepositoryImplSet,
|
||||||
wechatrepoimpl.ProviderWechatReposImplSet,
|
wechatrepoimpl.ProviderWechatReposImplSet,
|
||||||
data.ProviderDataSet,
|
mixrepoimpl.ProviderMixRepoImplSet,
|
||||||
log2.NewLogHelper,
|
log2.NewLogHelper,
|
||||||
newApp,
|
newApp,
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"voucher/internal/biz"
|
"voucher/internal/biz"
|
||||||
"voucher/internal/conf"
|
"voucher/internal/conf"
|
||||||
"voucher/internal/data"
|
"voucher/internal/data"
|
||||||
|
"voucher/internal/data/mixrepoimpl"
|
||||||
"voucher/internal/data/repoimpl"
|
"voucher/internal/data/repoimpl"
|
||||||
"voucher/internal/data/thirdrepoimpl"
|
"voucher/internal/data/thirdrepoimpl"
|
||||||
"voucher/internal/data/wechatrepoimpl"
|
"voucher/internal/data/wechatrepoimpl"
|
||||||
|
|
@ -46,7 +47,8 @@ func wireApp(bootstrap *conf.Bootstrap, logger log.Logger, accessLogger *log2.Ac
|
||||||
cleanup()
|
cleanup()
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
voucherBiz := biz.NewVoucherBiz(rdb, orderRepo, thirdMQSend, wechatCpnRepo)
|
generateMixRepo := mixrepoimpl.NewGenerateMixRepoImpl(rdb)
|
||||||
|
voucherBiz := biz.NewVoucherBiz(rdb, orderRepo, thirdMQSend, wechatCpnRepo, generateMixRepo)
|
||||||
voucherService := service.NewVoucherService(bootstrap, voucherBiz)
|
voucherService := service.NewVoucherService(bootstrap, voucherBiz)
|
||||||
httpServer := server.NewHTTPServer(bootstrap, helper, accessLogger, voucherService)
|
httpServer := server.NewHTTPServer(bootstrap, helper, accessLogger, voucherService)
|
||||||
consumer := server.NewConsumer(helper, bootstrap, voucherService)
|
consumer := server.NewConsumer(helper, bootstrap, voucherService)
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,36 @@ package biz
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
"time"
|
"time"
|
||||||
"voucher/internal/biz/bo"
|
"voucher/internal/biz/bo"
|
||||||
"voucher/internal/pkg/lock"
|
"voucher/internal/pkg/lock"
|
||||||
|
"voucher/internal/pkg/uid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (reps *bo.OrderCreateRepBo, err error) {
|
func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (reps *bo.OrderCreateRepBo, err error) {
|
||||||
|
|
||||||
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_order_%s", req.OutBizNo), func(ctx context.Context) error {
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_order_%s", req.OutBizNo), func(ctx context.Context) error {
|
||||||
|
|
||||||
|
order, err := v.OrderRepo.GetByOutBizNo(ctx, req.OutBizNo)
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if order != nil {
|
||||||
|
// todo
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
orderNo, err := v.GenerateMixRepo.GeneratorString(ctx, uid.Order)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.OrderNo = orderNo
|
||||||
|
v.OrderRepo.Create(ctx, req)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package biz
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"voucher/internal/pkg/lock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err error) {
|
||||||
|
|
||||||
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("order_consume_%s", orderNo), func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VoucherBiz) QueryConsume(ctx context.Context, orderNo string) (err error) {
|
||||||
|
|
||||||
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("query_consume_%s", orderNo), func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo string) (err error) {
|
||||||
|
|
||||||
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error {
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package mixrepos
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
// GenerateMixRepo interface
|
||||||
|
type GenerateMixRepo interface {
|
||||||
|
// GeneratorString 生成字符串
|
||||||
|
GeneratorString(ctx context.Context, workId int) (string, error)
|
||||||
|
GeneratorNumber(ctx context.Context, workId int) (uint64, error)
|
||||||
|
}
|
||||||
|
|
@ -1,21 +1,19 @@
|
||||||
package biz
|
package biz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"voucher/internal/biz/mixrepos"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"voucher/internal/biz/repo"
|
"voucher/internal/biz/repo"
|
||||||
"voucher/internal/biz/thirdrepo"
|
"voucher/internal/biz/thirdrepo"
|
||||||
"voucher/internal/biz/wechatrepo"
|
"voucher/internal/biz/wechatrepo"
|
||||||
"voucher/internal/data"
|
"voucher/internal/data"
|
||||||
"voucher/internal/pkg/lock"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type VoucherBiz struct {
|
type VoucherBiz struct {
|
||||||
rdb *data.Rdb
|
rdb *data.Rdb
|
||||||
OrderRepo repo.OrderRepo
|
OrderRepo repo.OrderRepo
|
||||||
ThirdMQSend thirdrepo.ThirdMQSend
|
ThirdMQSend thirdrepo.ThirdMQSend
|
||||||
WechatCpnRepo wechatrepo.WechatCpnRepo
|
WechatCpnRepo wechatrepo.WechatCpnRepo
|
||||||
|
GenerateMixRepo mixrepos.GenerateMixRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVoucherBiz(
|
func NewVoucherBiz(
|
||||||
|
|
@ -23,41 +21,13 @@ func NewVoucherBiz(
|
||||||
orderRepo repo.OrderRepo,
|
orderRepo repo.OrderRepo,
|
||||||
thirdMQSend thirdrepo.ThirdMQSend,
|
thirdMQSend thirdrepo.ThirdMQSend,
|
||||||
WechatCpnRepo wechatrepo.WechatCpnRepo,
|
WechatCpnRepo wechatrepo.WechatCpnRepo,
|
||||||
|
GenerateMixRepo mixrepos.GenerateMixRepo,
|
||||||
) *VoucherBiz {
|
) *VoucherBiz {
|
||||||
return &VoucherBiz{
|
return &VoucherBiz{
|
||||||
rdb: rdb,
|
rdb: rdb,
|
||||||
OrderRepo: orderRepo,
|
OrderRepo: orderRepo,
|
||||||
ThirdMQSend: thirdMQSend,
|
ThirdMQSend: thirdMQSend,
|
||||||
WechatCpnRepo: WechatCpnRepo,
|
WechatCpnRepo: WechatCpnRepo,
|
||||||
|
GenerateMixRepo: GenerateMixRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err error) {
|
|
||||||
|
|
||||||
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("order_consume_%s", orderNo), func(ctx context.Context) error {
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *VoucherBiz) QueryConsume(ctx context.Context, orderNo string) (err error) {
|
|
||||||
|
|
||||||
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("query_consume_%s", orderNo), func(ctx context.Context) error {
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo string) (err error) {
|
|
||||||
|
|
||||||
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error {
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package mixrepoimpl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"hash/fnv"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"voucher/internal/biz/mixrepos"
|
||||||
|
"voucher/internal/data"
|
||||||
|
"voucher/internal/pkg/uid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GenerateRepoImpl struct {
|
||||||
|
rdb *data.Rdb
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGenerateMixRepoImpl(rdb *data.Rdb) mixrepos.GenerateMixRepo {
|
||||||
|
return &GenerateRepoImpl{rdb: rdb}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeneratorString 生成字符串
|
||||||
|
func (s *GenerateRepoImpl) GeneratorString(ctx context.Context, workId int) (string, error) {
|
||||||
|
serverIdStr := uid.GetAppId(ctx)
|
||||||
|
serverId := s.hashMod(serverIdStr)
|
||||||
|
number, err := uid.NewSignGenerator(s.rdb.Rdb).SetWorkerID(workId).SetServerID(serverId).GetNumber()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strconv.FormatUint(number, 10), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GeneratorNumber 生成unit64
|
||||||
|
func (s *GenerateRepoImpl) GeneratorNumber(ctx context.Context, workId int) (uint64, error) {
|
||||||
|
serverIdStr := uid.GetAppId(ctx)
|
||||||
|
serverId := s.hashMod(serverIdStr)
|
||||||
|
number, err := uid.NewSignGenerator(s.rdb.Rdb).SetWorkerID(workId).SetServerID(serverId).GetNumber()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return number, 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))
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
package mixrepoimpl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/wire"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProviderMixRepoImplSet is providers.
|
||||||
|
var ProviderMixRepoImplSet = wire.NewSet(NewGenerateMixRepoImpl)
|
||||||
|
|
@ -74,6 +74,7 @@ func (p *OrderRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.OrderBo, er
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return nil, tx.Error
|
return nil, tx.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.RowsAffected == 0 {
|
if tx.RowsAffected == 0 {
|
||||||
return nil, gorm.ErrRecordNotFound
|
return nil, gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Key = 1 // key码
|
Order = 1 // Order
|
||||||
|
OrderWechat = 2 // OrderWechat
|
||||||
BitsFull = 64 //
|
BitsFull = 64 //
|
||||||
BitsPre = 1 // 固定
|
BitsPre = 1 // 固定
|
||||||
BitsTime = 41 // 毫秒时间戳 可以最多支持69年
|
BitsTime = 41 // 毫秒时间戳 可以最多支持69年
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue