51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package sequence
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"pay-bridge/internal/model"
|
||
"pay-bridge/internal/repository"
|
||
)
|
||
|
||
// Service 订单编码服务
|
||
type Service struct {
|
||
repo *repository.SequenceRepository
|
||
}
|
||
|
||
func NewService(repo *repository.SequenceRepository) *Service {
|
||
return &Service{repo: repo}
|
||
}
|
||
|
||
// NextTradeNo 生成下一个交易号,格式:PAY{yyMMdd}{8位序号}
|
||
func (s *Service) NextTradeNo(ctx context.Context, appID string) (string, error) {
|
||
return s.next(ctx, appID, model.SeqTypeTrade)
|
||
}
|
||
|
||
// NextRefundNo 生成下一个退款单号,格式:REF{yyMMdd}{8位序号}
|
||
func (s *Service) NextRefundNo(ctx context.Context, appID string) (string, error) {
|
||
return s.next(ctx, appID, model.SeqTypeRefund)
|
||
}
|
||
|
||
// NextSharingNo 生成下一个分润单号,格式:SHA{yyMMdd}{8位序号}
|
||
func (s *Service) NextSharingNo(ctx context.Context, appID string) (string, error) {
|
||
return s.next(ctx, appID, model.SeqTypeSharing)
|
||
}
|
||
|
||
func (s *Service) next(ctx context.Context, appID string, seqType model.SeqType) (string, error) {
|
||
val, err := s.repo.IncrAndGet(ctx, appID, seqType)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
prefix, err := s.repo.GetPrefix(ctx, appID, seqType)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
date := time.Now().Format("060102") // yyMMdd
|
||
seq := fmt.Sprintf("%08d", val%100_000_000) // 8 位,溢出归零(每日最多1亿笔)
|
||
return prefix + date + seq, nil
|
||
}
|