Files
yunzerwebsiteallinone/go/services/payment/ids.go
T
2026-11-16 17:22:50 +08:00

64 lines
1.2 KiB
Go

package payment
import (
"crypto/rand"
"fmt"
"math/big"
"strconv"
"strings"
"sync"
"time"
)
func FenToYuan(fen int64) string {
if fen < 0 {
fen = 0
}
return strconv.FormatInt(fen/100, 10) + "." + fmt.Sprintf("%02d", fen%100)
}
func YuanToFen(yuan string) (int64, error) {
yuan = strings.TrimSpace(yuan)
if yuan == "" {
return 0, nil
}
f, err := strconv.ParseFloat(yuan, 64)
if err != nil {
return 0, fmt.Errorf("金额格式不正确: %s", yuan)
}
if f < 0 {
return 0, fmt.Errorf("金额不能为负数: %s", yuan)
}
return int64(f*100 + 0.5), nil
}
var (
noMu sync.Mutex
lastNoTime int64
lastNoSeq int64
)
func nextSerial(prefix string) string {
noMu.Lock()
defer noMu.Unlock()
now := time.Now().Unix()
seq := int64(0)
if now == lastNoTime {
seq = lastNoSeq + 1
}
lastNoTime = now
lastNoSeq = seq
n, _ := rand.Int(rand.Reader, big.NewInt(10000))
return fmt.Sprintf("%s%s%04d%04d", prefix, time.Now().Format("20060102150405"), seq, n.Int64())
}
func NextPayNo() string { return nextSerial("P") }
func NextRefundNo() string { return nextSerial("R") }
func NextCommissionNo() string { return nextSerial("CM") }
func NextBatchNo() string { return nextSerial("RC") }