rbac_base_node.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "cp-system-management/errors"
  6. "time"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jinzhu/gorm"
  9. "go.uber.org/zap"
  10. )
  11. type RbacBaseNode struct {
  12. Id int64 `json:"id" gorm:"primary_key"`
  13. Name string `json:"name" gorm:"name;comment:'节点名';not null;default:''"`
  14. Router string `json:"router" gorm:"router;comment:'节点路由';not null;default:''"`
  15. Method string `json:"method" gorm:"method;comment:'节点方法';not null;default:''"`
  16. Resource string `json:"resource" gorm:"resource;comment:'节点资源';not null;default:''"`
  17. Object string `json:"object" gorm:"object;comment:'节点对象';not null;default:''"`
  18. Pid int64 `json:"pid" gorm:"pid;comment:'父节点';not null;default:0"`
  19. IsSuper bool `json:"is_super" gorm:"is_super"`
  20. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  21. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  22. }
  23. func (p *RbacBaseNode) TableName() string {
  24. return "t_rbac_base_node"
  25. }
  26. func (p *RbacBaseNode) Find(db *gorm.DB, where map[string]interface{}) error {
  27. err := db.Where(where).Find(p).Error
  28. if err != nil {
  29. fields, _ := json.MarshalToString(where)
  30. logger.Error("mysql",
  31. zap.String("sql", "select from "+p.TableName()),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. // Insert 插入一条记录
  38. func (p *RbacBaseNode) Insert(db *gorm.DB) error {
  39. timeNow := time.Now()
  40. p.CreatedAt = timeNow
  41. p.UpdatedAt = timeNow
  42. err := db.Create(p).Error
  43. if err != nil {
  44. fields, _ := json.MarshalToString(*p)
  45. logger.Error("mysql",
  46. zap.String("sql", "insert into "+p.TableName()),
  47. zap.String("fields", fields),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. func (p *RbacBaseNode) Delete(db *gorm.DB, filter map[string]interface{}) error {
  53. err := db.Where(filter).Delete(p).Error
  54. if err != nil {
  55. fields, _ := json.MarshalToString(filter)
  56. logger.Error("mysql",
  57. zap.String("sql", "delete from "+p.TableName()),
  58. zap.String("fields", fields),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. func (p *RbacBaseNode) Save(db *gorm.DB) error {
  64. p.UpdatedAt = time.Now()
  65. err := db.Save(p).Error
  66. if err != nil {
  67. fields, _ := json.MarshalToString(*p)
  68. logger.Error("mysql",
  69. zap.String("sql", "save "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. func (p *RbacBaseNode) Update(db *gorm.DB) error {
  76. p.UpdatedAt = time.Now()
  77. err := db.Model(p).Updates(p).Error
  78. if err != nil {
  79. fields, _ := json.MarshalToString(*p)
  80. logger.Error("mysql",
  81. zap.String("sql", "update "+p.TableName()),
  82. zap.String("fields", fields),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  88. func (p *RbacBaseNode) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  89. if filed == nil {
  90. return errors.ParamsError
  91. }
  92. filed["updated_at"] = time.Now()
  93. err := db.Model(p).Updates(filed).Error
  94. if err != nil {
  95. fields, _ := json.MarshalToString(filed)
  96. logger.Error("mysql",
  97. zap.String("sql", "update "+p.TableName()),
  98. zap.String("fields", fields),
  99. zap.String("error", err.Error()))
  100. }
  101. return err
  102. }
  103. func (p *RbacBaseNode) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  104. err := db.Where(filter).Find(out).Error
  105. if err != nil {
  106. fields, _ := json.MarshalToString(filter)
  107. logger.Error("mysql",
  108. zap.String("sql", "select from "+p.TableName()),
  109. zap.String("fields", fields),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }