同步支持动态更新取件码长度,调整存量取件码以适配新长度并优化相关逻辑和文档。
This commit is contained in:
@@ -6,8 +6,12 @@ import (
|
||||
"FileRelay/internal/storage"
|
||||
"context"
|
||||
"errors"
|
||||
"math/big"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"crypto/rand"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -84,3 +88,85 @@ func (s *BatchService) IncrementDownloadCount(batchID string) error {
|
||||
return s.db.Model(&model.FileBatch{}).Where("id = ?", batchID).
|
||||
UpdateColumn("download_count", gorm.Expr("download_count + ?", 1)).Error
|
||||
}
|
||||
|
||||
func (s *BatchService) GeneratePickupCode(length int) (string, error) {
|
||||
const charset = "0123456789"
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b[i] = charset[num.Int64()]
|
||||
}
|
||||
// 检查是否冲突 (排除已删除的,但包括活跃的和已过期的)
|
||||
var count int64
|
||||
s.db.Model(&model.FileBatch{}).Where("pickup_code = ?", string(b)).Count(&count)
|
||||
if count > 0 {
|
||||
return s.GeneratePickupCode(length) // 递归生成
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func (s *BatchService) UpdateAllPickupCodes(newLength int) error {
|
||||
var batches []model.FileBatch
|
||||
// 只更新未删除的记录,包括 active 和 expired
|
||||
if err := s.db.Find(&batches).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
for _, batch := range batches {
|
||||
oldCode := batch.PickupCode
|
||||
if len(oldCode) == newLength {
|
||||
continue
|
||||
}
|
||||
|
||||
var newCode string
|
||||
if len(oldCode) < newLength {
|
||||
// 右侧补零,方便用户输入原码后通过补 0 完成输入
|
||||
newCode = oldCode + strings.Repeat("0", newLength-len(oldCode))
|
||||
} else {
|
||||
// 截取前 newLength 位,保留原码头部
|
||||
newCode = oldCode[:newLength]
|
||||
}
|
||||
|
||||
// 检查冲突 (在事务中检查)
|
||||
var count int64
|
||||
tx.Model(&model.FileBatch{}).Where("pickup_code = ? AND id != ?", newCode, batch.ID).Count(&count)
|
||||
if count > 0 {
|
||||
// 如果冲突,生成一个新的随机码
|
||||
var err error
|
||||
newCode, err = s.generateUniquePickupCodeInTx(tx, newLength)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Model(&batch).Update("pickup_code", newCode).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *BatchService) generateUniquePickupCodeInTx(tx *gorm.DB, length int) (string, error) {
|
||||
const charset = "0123456789"
|
||||
for {
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b[i] = charset[num.Int64()]
|
||||
}
|
||||
|
||||
var count int64
|
||||
tx.Model(&model.FileBatch{}).Where("pickup_code = ?", string(b)).Count(&count)
|
||||
if count == 0 {
|
||||
return string(b), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user