util.go 466 B

12345678910111213141516171819202122
  1. package util
  2. import (
  3. "gorm.io/gorm"
  4. "strconv"
  5. "strings"
  6. )
  7. type result struct {
  8. ID string
  9. }
  10. func GetNextId(db *gorm.DB, tableName string) string {
  11. var res result
  12. db.Table(tableName).Order("create_time desc").Select("id").Last(&res)
  13. strSlice := strings.Split(res.ID, "_")
  14. num, _ := strconv.Atoi(strSlice[len(strSlice)-1:][0])
  15. num += 1
  16. strSlice = append(strSlice[:len(strSlice)-1], strconv.Itoa(num))
  17. resId := strings.Join(strSlice, "_")
  18. return resId
  19. }