rbac_user.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. "encoding/json"
  13. )
  14. type RbacUser struct {
  15. Id int64 `json:"id" gorm:"primary_key"`
  16. Username string `json:"username" gorm:"username;comment:'用户名';not null;default:''"`
  17. Passowrd string `json:"passowrd" gorm:"passowrd;comment:'密码';not null;default:''"`
  18. Phone string `json:"phone" gorm:"phone;commnet:'手机号';not null;default:''"`
  19. Email string `json:"email" gorm:"email;commnet:'电子邮件';not null;default:''"`
  20. IsSuperUser bool `json:"is_super_user" gorm:"is_super_user;commnet:'是否是超级账号,超级账号不能删除';not null;default:0"`
  21. GroupId int64 `json:"group_id" gorm:"group_id;commnet:'权限组id';not null;default:0"`
  22. //Zones string `json:"zones" gorm:"zones;commnet:'管辖区域列表';not null;default:''"`
  23. CreatedAt time.Time `json:"created_at" gorm:"created_at"`
  24. UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
  25. Name string `json:"name" gorm:"name"`
  26. Pids string `json:"pids" gorm:"pids"`
  27. table string `json:"table"`
  28. }
  29. func (p *RbacUser) TableName() string {
  30. return p.table
  31. }
  32. func NewRbacUser(database string) *RbacUser {
  33. return &RbacUser{table: fmt.Sprintf("%s.%s", database, "t_rbac_user")}
  34. }
  35. func (p *RbacUser) CreateTable(db *gorm.DB) error {
  36. err := db.CreateTable(p).Error
  37. if err != nil {
  38. logger.Error("mysql",
  39. zap.String("sql", "create table "+p.TableName()),
  40. zap.String("fields", ""),
  41. zap.String("error", err.Error()))
  42. }
  43. return nil
  44. }
  45. func (p *RbacUser) Find(db *gorm.DB, where map[string]interface{}) error {
  46. err := db.Table(p.TableName()).Where(where).Find(p).Error
  47. if err != nil {
  48. fields, _ := util.MarshalToString(where)
  49. logger.Error("mysql",
  50. zap.String("sql", "select from "+p.TableName()),
  51. zap.String("fields", fields),
  52. zap.String("error", err.Error()))
  53. }
  54. return err
  55. }
  56. func (p *RbacUser) Last(db *gorm.DB) error {
  57. err := db.Table(p.TableName()).Last(p).Error
  58. if err != nil {
  59. logger.Error("mysql",
  60. zap.String("sql", "select last from "+p.TableName()),
  61. zap.String("fields", ""),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. // Insert 插入一条记录
  67. func (p *RbacUser) Insert(db *gorm.DB) error {
  68. timeNow := time.Now()
  69. p.CreatedAt = timeNow
  70. p.UpdatedAt = timeNow
  71. err := db.Create(p).Error
  72. if err != nil {
  73. fields, _ := util.MarshalToString(*p)
  74. logger.Error("mysql",
  75. zap.String("sql", "insert into "+p.TableName()),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *RbacUser) Delete(db *gorm.DB, filter map[string]interface{}) error {
  82. err := db.Where(filter).Delete(p).Error
  83. if err != nil {
  84. fields, _ := util.MarshalToString(filter)
  85. logger.Error("mysql",
  86. zap.String("sql", "delete from "+p.TableName()),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return err
  91. }
  92. func (p *RbacUser) Save(db *gorm.DB) error {
  93. p.UpdatedAt = time.Now()
  94. err := db.Save(p).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(*p)
  97. logger.Error("mysql",
  98. zap.String("sql", "save "+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return err
  103. }
  104. func (p *RbacUser) Update(db *gorm.DB) error {
  105. p.UpdatedAt = time.Now()
  106. err := db.Model(p).Updates(p).Error
  107. if err != nil {
  108. fields, _ := util.MarshalToString(*p)
  109. logger.Error("mysql",
  110. zap.String("sql", "update "+p.TableName()),
  111. zap.String("fields", fields),
  112. zap.String("error", err.Error()))
  113. }
  114. return err
  115. }
  116. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  117. func (p *RbacUser) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  118. if filed == nil {
  119. return errors.ParamsError
  120. }
  121. filed["updated_at"] = time.Now()
  122. err := db.Model(p).Updates(filed).Error
  123. if err != nil {
  124. fields, _ := util.MarshalToString(filed)
  125. logger.Error("mysql",
  126. zap.String("sql", "update "+p.TableName()),
  127. zap.String("fields", fields),
  128. zap.String("error", err.Error()))
  129. }
  130. return err
  131. }
  132. func (p *RbacUser) UpdateWithAffectRows(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
  133. cond, val, err := whereBuild(where)
  134. if err != nil {
  135. return 0, err
  136. }
  137. r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
  138. if r.Error != nil {
  139. fields, _ := json.Marshal(where)
  140. updateValues, _ := json.Marshal(values)
  141. logger.Error("mysql",
  142. zap.String("sql", "update "+p.TableName()),
  143. zap.String("fields", string(fields)),
  144. zap.String("values", string(updateValues)),
  145. zap.String("error", err.Error()))
  146. return 0, errors.DataBaseError
  147. }
  148. return r.RowsAffected, err
  149. }
  150. func (p *RbacUser) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  151. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  152. if err != nil {
  153. fields, _ := util.MarshalToString(filter)
  154. logger.Error("mysql",
  155. zap.String("sql", "select from "+p.TableName()),
  156. zap.String("fields", fields),
  157. zap.String("error", err.Error()))
  158. }
  159. return err
  160. }
  161. func (p *RbacUser) List(db *gorm.DB, limit, offset int, out interface{}) error {
  162. err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error
  163. if err != nil {
  164. logger.Error("mysql",
  165. zap.String("sql", "select from "+p.TableName()),
  166. zap.String("fields", ""),
  167. zap.String("error", err.Error()))
  168. }
  169. return err
  170. }
  171. func (p *RbacUser) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  172. if len(where) > 0 {
  173. cond, val, err := whereBuild(where)
  174. if err != nil {
  175. return 0, err
  176. }
  177. ret := int64(0)
  178. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  179. if err != nil {
  180. fields, _ := json.Marshal(where)
  181. logger.Error("mysql",
  182. zap.String("sql", "select count from "+p.TableName()),
  183. zap.String("fields", string(fields)),
  184. zap.String("error", err.Error()))
  185. return ret, errors.DataBaseError
  186. }
  187. return ret, err
  188. }
  189. ret := int64(0)
  190. err := db.Table(p.TableName()).Count(&ret).Error
  191. if err != nil {
  192. fields, _ := json.Marshal(where)
  193. logger.Error("mysql",
  194. zap.String("sql", "select count from "+p.TableName()),
  195. zap.String("fields", string(fields)),
  196. zap.String("error", err.Error()))
  197. return ret, errors.DataBaseError
  198. }
  199. return ret, err
  200. }
  201. func (p *RbacUser) ListNew(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, limit, offset int64) (list []RbacUser, err error) {
  202. if len(where) > 0 || len(or) > 0 {
  203. cond, val, err := whereBuildAndOr(where, or)
  204. if err != nil {
  205. return list, err
  206. }
  207. err = db.Table(p.TableName()).Where(cond, val...).Limit(limit).Offset(offset).Find(&list).Error
  208. if err != nil {
  209. logger.Error("mysql",
  210. zap.String("sql", "select * from "+p.TableName()),
  211. zap.String("error", err.Error()))
  212. return list, errors.DataBaseError
  213. }
  214. return list, err
  215. }
  216. err = db.Table(p.TableName()).Find(&list).Error
  217. if err != nil {
  218. logger.Error("mysql",
  219. zap.String("sql", "select * from "+p.TableName()),
  220. zap.String("error", err.Error()))
  221. return list, errors.DataBaseError
  222. }
  223. return list, nil
  224. }