role.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, error) {
  22. var Roles []Role
  23. db := Db.Debug().Model(&c)
  24. if c.RoleName != "" {
  25. db = db.Where("role_name like ?", "%"+c.RoleName+"%")
  26. }
  27. if c.RoleAlias != "" {
  28. db = db.Where("role_alias like ?", "%"+c.RoleAlias+"%")
  29. }
  30. if c.TenantId != "" {
  31. db = db.Where("tenant_id like ?", "%"+c.TenantId+"%")
  32. }
  33. err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&Roles).Error
  34. return Roles, err
  35. }
  36. func (c *Role) Save() error {
  37. return Db.Debug().Model(&c).Save(&c).Error
  38. }
  39. func (c *Role) Update() error {
  40. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
  41. }
  42. func (c *Role) Remove() error {
  43. return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"is_deleted": c.IsDeleted}).Error
  44. }
  45. func (c *Role) IsExistChild() bool {
  46. var count int
  47. _ = Db.Debug().Model(&c).Where("parent_id = ? and is_deleted = 0", c.ParentId).Count(&count).Error
  48. return count > 0
  49. }
  50. func (c *Role) UpdatePwd(pwd string) error {
  51. return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
  52. }
  53. func (c *Role) GetAll() ([]Role, error) {
  54. var Roles []Role
  55. err := Db.Debug().Model(&c).Where("is_deleted = 0").Find(&Roles).Error
  56. return Roles, err
  57. }
  58. func (c *Role) UpdateRoles(RoleIds []string, roleIds string) error {
  59. err := Db.Debug().Model(&c).Where("id in ?", RoleIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
  60. return err
  61. }