Files
pay-bridge/backend/pkg/sequence/sequence.go
2026-03-13 15:51:59 +08:00

51 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}