model.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package model
  2. import (
  3. "gorm.io/gen"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. var db *gorm.DB
  8. type Model struct {
  9. ID int `gorm:"primary_key" json:"id"`
  10. CreatedAt time.Time `json:"created_at"`
  11. UpdatedAt time.Time `json:"updated_at"`
  12. DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at"`
  13. }
  14. func GenerateAllModel() []any {
  15. return []any{
  16. ConfigBackup{},
  17. User{},
  18. AuthToken{},
  19. Cert{},
  20. ChatGPTLog{},
  21. Site{},
  22. Stream{},
  23. DnsCredential{},
  24. Environment{},
  25. Notification{},
  26. AcmeUser{},
  27. BanIP{},
  28. Config{},
  29. Passkey{},
  30. SiteCategory{},
  31. }
  32. }
  33. func Use(tx *gorm.DB) {
  34. db = tx
  35. }
  36. func UseDB() *gorm.DB {
  37. return db
  38. }
  39. type Pagination struct {
  40. Total int64 `json:"total"`
  41. PerPage int `json:"per_page"`
  42. CurrentPage int `json:"current_page"`
  43. TotalPages int64 `json:"total_pages"`
  44. }
  45. type DataList struct {
  46. Data interface{} `json:"data"`
  47. Pagination Pagination `json:"pagination,omitempty"`
  48. }
  49. func TotalPage(total int64, pageSize int) int64 {
  50. n := total / int64(pageSize)
  51. if total%int64(pageSize) > 0 {
  52. n++
  53. }
  54. return n
  55. }
  56. type Method interface {
  57. // FirstByID Where("id=@id")
  58. FirstByID(id int) (*gen.T, error)
  59. // DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
  60. DeleteByID(id int) error
  61. }