node.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package dao
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. // Node 节点数据库
  9. type Node struct {
  10. Id int `json:"id" gorm:"primary_key" description:"自增主键"`
  11. Name string `json:"name" gorm:"column:name" description:"服务器名称"`
  12. Host string `json:"host" gorm:"column:host" description:"服务器IP"`
  13. Port int `json:"port" gorm:"column:port" description:"服务器端口"`
  14. Url string `json:"url" gorm:"column:url" description:"更新IP地址"`
  15. UpdatedAt time.Time `json:"update_at" gorm:"column:update_at" description:"更新时间"`
  16. CreatedAt time.Time `json:"create_at" gorm:"column:create_at" description:"创建时间"`
  17. }
  18. func (t *Node) TableName() string {
  19. return "node"
  20. }
  21. func (t *Node) GetNode(c *gin.Context, tx *gorm.DB) ([]*Node, error) {
  22. var node []*Node
  23. if err := tx.Table(t.TableName()).Find(&node).Error; err != nil {
  24. return nil, err
  25. }
  26. return node, nil
  27. }
  28. func (t *Node) IsExists(c *gin.Context, tx *gorm.DB, host string) error {
  29. var exists bool
  30. //var count int64
  31. err := tx.Table("node").WithContext(c).Select("count(*) > 0").Where("host = ?", host).Find(&exists).Error
  32. if err != nil {
  33. return err
  34. }
  35. if exists {
  36. return errors.New("数据已经存在")
  37. }
  38. return nil
  39. }
  40. func (t *Node) Save(c *gin.Context, tx *gorm.DB) error {
  41. if err := tx.WithContext(c).Save(t).Error; err != nil {
  42. return err
  43. }
  44. return nil
  45. }
  46. func (t *Node) Del(c *gin.Context, tx *gorm.DB, host string) error {
  47. if err := tx.WithContext(c).Where("host = ?", host).Delete(&Node{}).Error; err != nil {
  48. return err
  49. }
  50. return nil
  51. }