You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.7 KiB

  1. package sysModel
  2. import (
  3. "gin-vue-admin/controller/servers"
  4. "gin-vue-admin/init/qmsql"
  5. "gin-vue-admin/model/modelInterface"
  6. "github.com/jinzhu/gorm"
  7. "github.com/pkg/errors"
  8. )
  9. type SysAuthority struct {
  10. gorm.Model
  11. AuthorityId string `json:"authorityId" gorm:"not null;unique"`
  12. AuthorityName string `json:"authorityName"`
  13. ParentId string `json:"parentId"`
  14. DataAuthorityId []SysAuthority `json:"dataAuthorityId" gorm:"many2many:sys_data_authority_id;association_jointable_foreignkey:data_id"`
  15. Children []SysAuthority `json:"children"`
  16. }
  17. // 创建角色
  18. func (a *SysAuthority) CreateAuthority() (err error, authority *SysAuthority) {
  19. err = qmsql.DEFAULTDB.Create(a).Error
  20. return err, a
  21. }
  22. // 删除角色
  23. func (a *SysAuthority) DeleteAuthority() (err error) {
  24. err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error
  25. if err != nil {
  26. err = qmsql.DEFAULTDB.Where("parent_id = ?", a.AuthorityId).Find(&SysAuthority{}).Error
  27. if err != nil {
  28. err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error
  29. new(CasbinModel).clearCasbin(0, a.AuthorityId)
  30. } else {
  31. err = errors.New("此角色存在子角色不允许删除")
  32. }
  33. } else {
  34. err = errors.New("此角色有用户正在使用禁止删除")
  35. }
  36. return err
  37. }
  38. // 分页获取数据 需要分页实现这个接口即可
  39. func (a *SysAuthority) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  40. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  41. err, db, total := servers.PagingServer(a, info)
  42. if err != nil {
  43. return
  44. } else {
  45. var authority []SysAuthority
  46. err = db.Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  47. if len(authority) > 0 {
  48. for k, _ := range authority {
  49. err = findChildrenAuthority(&authority[k])
  50. }
  51. }
  52. return err, authority, total
  53. }
  54. }
  55. func findChildrenAuthority(authority *SysAuthority) (err error) {
  56. err = qmsql.DEFAULTDB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  57. if len(authority.Children) > 0 {
  58. for k, _ := range authority.Children {
  59. err = findChildrenAuthority(&authority.Children[k])
  60. }
  61. }
  62. return err
  63. }
  64. func (a *SysAuthority) SetDataAuthority() error {
  65. var s SysAuthority
  66. qmsql.DEFAULTDB.Preload("DataAuthorityId").First(&s, "id = ?", a.ID)
  67. err := qmsql.DEFAULTDB.Model(&s).Association("DataAuthorityId").Replace(&a.DataAuthorityId).Error
  68. return err
  69. }
  70. func (a *SysAuthority) GetAuthorityInfo() (err error, sa SysAuthority) {
  71. err = qmsql.DEFAULTDB.Preload("DataAuthorityId").Where("authority_id = ?", a.AuthorityId).First(&sa).Error
  72. return err, sa
  73. }