Files
hfb_sys/backend/internal/modules/order/utils.go
T
2026-06-09 19:56:32 +08:00

56 lines
1.2 KiB
Go

package order
import (
"crypto/rand"
"errors"
"strconv"
"gorm.io/gorm"
"hfb_sys/backend/internal/auditlog"
"hfb_sys/backend/internal/timeutil"
)
func firstNonEmpty(values ...string) string {
for _, value := range values {
if value != "" {
return value
}
}
return ""
}
func newOrderNo() (string, error) {
// 生成格式:RO + YYYYMMDDHHMMSS + 3位随机数
// 例如:RO20260603123456789
now := timeutil.ShanghaiNow()
// 时间部分:年月日时分秒 (14位)
timeStr := now.Format("20060102150405")
// 随机部分:3位数字 (000-999)
buf := make([]byte, 2)
if _, err := rand.Read(buf); err != nil {
return "", err
}
// 将2字节转为0-999的数字
randomNum := (int(buf[0])<<8 | int(buf[1])) % 1000
return "RO" + timeStr + strconv.Itoa(1000 + randomNum)[1:], nil
}
func appendAuditLog(tx *gorm.DB, actorID uint64, action string, bizType string, bizID uint64, meta AuditMeta, detail map[string]any) error {
return auditlog.Append(tx, auditlog.Entry{
ActorType: "admin",
ActorID: actorID,
Action: action,
BizType: bizType,
BizID: &bizID,
Meta: meta,
Detail: detail,
})
}
func IsNotFound(err error) bool {
return errors.Is(err, gorm.ErrRecordNotFound)
}