rbac_node.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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-organization-management/errors"
  6. "fmt"
  7. "time"
  8. "github.com/jaryhe/gopkgs/logger"
  9. "github.com/jaryhe/gopkgs/util"
  10. "github.com/jinzhu/gorm"
  11. "go.uber.org/zap"
  12. )
  13. type RbacNode struct {
  14. Id int64 `json:"id" gorm:"primary_key"`
  15. Name string `json:"name" gorm:"name;comment:'节点名';not null;default:''"`
  16. Router string `json:"router" gorm:"router;comment:'节点路由';not null;default:''"`
  17. Method string `json:"method" gorm:"method;comment:'节点方法';not null;default:''"`
  18. Resource string `json:"resource" gorm:"resource;comment:'节点资源';not null;default:''"`
  19. Object string `json:"object" gorm:"object;comment:'节点对象';not null;default:''"`
  20. Pid int64 `json:"pid" gorm:"pid;comment:'父节点';not null;default:0"`
  21. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  22. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  23. IsSuper bool `json:"is_super" gorm:"is_super"`
  24. table string `json:"table"`
  25. }
  26. func (p *RbacNode) TableName() string {
  27. return p.table
  28. }
  29. func NewRbacNode(database string) *RbacNode {
  30. return &RbacNode{table: fmt.Sprintf("%s.%s", database, "t_rbac_node")}
  31. }
  32. func (p *RbacNode) CreateTable(db *gorm.DB) error {
  33. err := db.CreateTable(p).Error
  34. if err != nil {
  35. logger.Error("mysql",
  36. zap.String("sql", "create table "+p.TableName()),
  37. zap.String("fields", ""),
  38. zap.String("error", err.Error()))
  39. }
  40. return nil
  41. }
  42. func (p *RbacNode) Find(db *gorm.DB, where map[string]interface{}) error {
  43. err := db.Table(p.TableName()).Where(where).Find(p).Error
  44. if err != nil {
  45. fields, _ := util.MarshalToString(where)
  46. logger.Error("mysql",
  47. zap.String("sql", "select from "+p.TableName()),
  48. zap.String("fields", fields),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. func (p *RbacNode) Last(db *gorm.DB) error {
  54. err := db.Table(p.TableName()).Last(p).Error
  55. if err != nil {
  56. logger.Error("mysql",
  57. zap.String("sql", "select last from "+p.TableName()),
  58. zap.String("fields", ""),
  59. zap.String("error", err.Error()))
  60. }
  61. return err
  62. }
  63. // Insert 插入一条记录
  64. func (p *RbacNode) Insert(db *gorm.DB) error {
  65. timeNow := time.Now()
  66. p.CreatedAt = timeNow
  67. p.UpdatedAt = timeNow
  68. err := db.Create(p).Error
  69. if err != nil {
  70. fields, _ := util.MarshalToString(*p)
  71. logger.Error("mysql",
  72. zap.String("sql", "insert into "+p.TableName()),
  73. zap.String("fields", fields),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. func (p *RbacNode) Delete(db *gorm.DB, filter map[string]interface{}) error {
  79. err := db.Where(filter).Delete(p).Error
  80. if err != nil {
  81. fields, _ := util.MarshalToString(filter)
  82. logger.Error("mysql",
  83. zap.String("sql", "delete from "+p.TableName()),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. func (p *RbacNode) Save(db *gorm.DB) error {
  90. p.UpdatedAt = time.Now()
  91. err := db.Save(p).Error
  92. if err != nil {
  93. fields, _ := util.MarshalToString(*p)
  94. logger.Error("mysql",
  95. zap.String("sql", "save "+p.TableName()),
  96. zap.String("fields", fields),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. func (p *RbacNode) Update(db *gorm.DB) error {
  102. p.UpdatedAt = time.Now()
  103. err := db.Model(p).Updates(p).Error
  104. if err != nil {
  105. fields, _ := util.MarshalToString(*p)
  106. logger.Error("mysql",
  107. zap.String("sql", "update "+p.TableName()),
  108. zap.String("fields", fields),
  109. zap.String("error", err.Error()))
  110. }
  111. return err
  112. }
  113. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  114. func (p *RbacNode) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  115. if filed == nil {
  116. return errors.ParamsError
  117. }
  118. filed["updated_at"] = time.Now()
  119. err := db.Model(p).Updates(filed).Error
  120. if err != nil {
  121. fields, _ := util.MarshalToString(filed)
  122. logger.Error("mysql",
  123. zap.String("sql", "update "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. func (p *RbacNode) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  130. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  131. if err != nil {
  132. fields, _ := util.MarshalToString(filter)
  133. logger.Error("mysql",
  134. zap.String("sql", "select from "+p.TableName()),
  135. zap.String("fields", fields),
  136. zap.String("error", err.Error()))
  137. }
  138. return err
  139. }
  140. func (p *RbacNode) List(db *gorm.DB, limit, offset int, out interface{}) error {
  141. err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
  142. if err != nil {
  143. logger.Error("mysql",
  144. zap.String("sql", "select from "+p.TableName()),
  145. zap.String("fields", ""),
  146. zap.String("error", err.Error()))
  147. }
  148. return err
  149. }
  150. func (p *RbacNode) Count(db *gorm.DB) (int64, error) {
  151. var count int64
  152. err := db.Table(p.TableName()).Count(&count).Error
  153. if err != nil {
  154. logger.Error("mysql",
  155. zap.String("sql", "select count from "+p.TableName()),
  156. zap.String("fields", ""),
  157. zap.String("error", err.Error()))
  158. }
  159. return count, err
  160. }
  161. func (p *RbacNode) ListAll(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (list []RbacNode, err error) {
  162. if len(where) > 0 || len(or) > 0 {
  163. cond, val, err := whereBuildAndOr(where, or)
  164. if err != nil {
  165. return list, err
  166. }
  167. err = db.Table(p.TableName()).Where(cond, val...).Find(&list).Error
  168. if err != nil {
  169. logger.Error("mysql",
  170. zap.String("sql", "select * from "+p.TableName()),
  171. zap.String("error", err.Error()))
  172. return list, errors.DataBaseError
  173. }
  174. return list, err
  175. }
  176. err = db.Table(p.TableName()).Find(&list).Error
  177. if err != nil {
  178. logger.Error("mysql",
  179. zap.String("sql", "select * from "+p.TableName()),
  180. zap.String("error", err.Error()))
  181. return list, errors.DataBaseError
  182. }
  183. return list, nil
  184. }