role.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package dao
  2. // Role 角色
  3. type Role struct {
  4. ID int64 `gorm:"primary_key" json:"id"` //编号
  5. TenantId string `gorm:"type:varchar(12);default '000000'" json:"tenantId"` //租户id
  6. ParentId int64 `gorm:"type:bigint" json:"parentId"` //父主键
  7. RoleName string `gorm:"type:varchar(255)" json:"roleName"` //角色别名
  8. Sort int `gorm:"type:int" json:"sort"` //排序
  9. RoleAlias string `gorm:"type:varchar(255)" json:"roleAlias"` //角色别名
  10. IsDeleted int `gorm:"type:int" json:"isDeleted"` //是否删除
  11. }
  12. func (Role) TableName() string {
  13. return "role"
  14. }
  15. func (c *Role) Get() error {
  16. return Db.Debug().Model(&c).Where("id = ?", c.ID).Find(&c).Error
  17. }
  18. func (c *Role) GetRole() error {
  19. return Db.Debug().Model(&c).Where("id = ? and is_deleted = 0", c.ID).Find(&c).Error
  20. }
  21. func (c Role) GetRoles(offset, limit int) ([]Role, int, error) {
  22. var Roles []Role
  23. var counts int64
  24. db := Db.Debug().Model(&c)
  25. db.Count(&counts)
  26. if c.RoleName != "" {
  27. db = db.Where("role_name like ?", "%"+c.RoleName+"%")
  28. }
  29. if c.RoleAlias != "" {
  30. db = db.Where("role_alias like ?", "%"+c.RoleAlias+"%")
  31. }
  32. if c.TenantId != "" {
  33. db = db.Where("tenant_id like ?", "%"+c.TenantId+"%")
  34. }
  35. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&Roles).Error
  36. return Roles, int(counts), err
  37. }
  38. func (c *Role) Save() error {
  39. return Db.Debug().Model(&c).Save(&c).Error
  40. }
  41. func (c *Role) Update() error {
  42. return Db.Debug().Model(&c).Updates(&c).Error
  43. }
  44. func (c *Role) Remove() error {
  45. return Db.Debug().Model(&c).Updates(map[string]interface{}{"is_deleted": c.IsDeleted}).Error
  46. }
  47. func (c *Role) IsExistChild() bool {
  48. var count int64
  49. _ = Db.Debug().Model(&c).Where("parent_id = ? and is_deleted = 0", c.ParentId).Count(&count).Error
  50. return count > 0
  51. }
  52. func (c *Role) UpdatePwd(pwd string) error {
  53. return Db.Debug().Model(&c).Updates(map[string]interface{}{"password": pwd}).Error
  54. }
  55. func (c *Role) GetAll() ([]Role, error) {
  56. var Roles []Role
  57. err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&Roles).Error
  58. return Roles, err
  59. }
  60. func (c *Role) UpdateRoles(RoleIds []string, roleIds string) error {
  61. err := Db.Debug().Model(&c).Where("id in ?", RoleIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
  62. return err
  63. }
  64. func (c *Role) GetByParentId() ([]Role, error) {
  65. var Roles []Role
  66. err := Db.Debug().Model(&c).Where("is_deleted = 0 AND parent_id = ?", c.ParentId).Find(&Roles).Error
  67. return Roles, err
  68. }