sys_role.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package models
  2. import (
  3. "context"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type SysRole struct {
  8. SysRoleId int `orm:"pk" description:"角色id"`
  9. SysRoleName string `description:"姓名"`
  10. CreateTime time.Time `description:"创建时间"`
  11. ModifyTime time.Time `description:"更新时间"`
  12. }
  13. func (s *SysRole) Add() (err error) {
  14. o := orm.NewOrm()
  15. _, err = o.Insert(s)
  16. return
  17. }
  18. func DeleteSysRoleById(roleId int) (err error) {
  19. sql := `DELETE FROM sys_role WHERE sys_role_id=? `
  20. o := orm.NewOrm()
  21. _, err = o.Raw(sql, roleId).Exec()
  22. return
  23. }
  24. func GetSysRoleByRoleName(roleName string) (item *SysRole, err error) {
  25. sql := `SELECT * FROM sys_role WHERE sys_role_name=? `
  26. o := orm.NewOrm()
  27. err = o.Raw(sql, roleName).QueryRow(&item)
  28. return
  29. }
  30. func GetSysRoleCountByRoleName(roleName string) (count int, err error) {
  31. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE sys_role_name=? `
  32. o := orm.NewOrm()
  33. err = o.Raw(sql, roleName).QueryRow(&count)
  34. return
  35. }
  36. func UpdateSysUserRoleByRoleId(roleId int, roleName string) (err error) {
  37. o := orm.NewOrm()
  38. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  39. role := &SysRole{}
  40. role.SysRoleId = roleId
  41. role.SysRoleName = roleName
  42. role.ModifyTime = time.Now()
  43. _, e := txOrm.Update(role, "sys_role_id", "sys_role_name", "modify_time")
  44. if e != nil {
  45. return e
  46. }
  47. sql := `UPDATE sys_user SET sys_role_name=? WHERE sys_role_id=?`
  48. _, e = txOrm.Raw(sql, roleName, roleId).Exec()
  49. return e
  50. })
  51. return
  52. }
  53. func GetSysRoleById(id int) (item *SysRole, err error) {
  54. o := orm.NewOrm()
  55. sql := `SELECT * FROM sys_role WHERE sys_role_id=?`
  56. err = o.Raw(sql, id).QueryRow(&item)
  57. return
  58. }
  59. func GetSysRoleListCount() (count int, err error) {
  60. o := orm.NewOrm()
  61. sql := `SELECT COUNT(1) AS count FROM sys_role `
  62. err = o.Raw(sql).QueryRow(&count)
  63. return
  64. }
  65. func GetSysRoleList(startSize, pageSize int) (items []*SysRole, err error) {
  66. o := orm.NewOrm()
  67. sql := `SELECT * FROM sys_role ORDER BY modify_time DESC LIMIT ?,?`
  68. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  69. return
  70. }
  71. func GetSysRoleListNoPage() (items []*SysRole, err error) {
  72. o := orm.NewOrm()
  73. sql := `SELECT * FROM sys_role ORDER BY modify_time DESC `
  74. _, err = o.Raw(sql).QueryRows(&items)
  75. return
  76. }
  77. // CreateMultiSysRoleMenu 删除并新增角色权限
  78. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenuMapping) (err error) {
  79. if roleId == 0 {
  80. return
  81. }
  82. o := orm.NewOrm()
  83. err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  84. sql := `DELETE FROM sys_role_menu_mapping WHERE sys_role_id = ?`
  85. _, e := txOrm.Raw(sql, roleId).Exec()
  86. if e != nil {
  87. return e
  88. }
  89. if len(items) > 0 {
  90. _, e = txOrm.InsertMulti(len(items), items)
  91. if e != nil {
  92. return e
  93. }
  94. }
  95. return nil
  96. })
  97. return
  98. }
  99. // GetSysRoleMenuByRoleId 获取角色关联菜单
  100. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenuMapping, err error) {
  101. o := orm.NewOrm()
  102. sql := `SELECT * FROM sys_role_menu_mapping WHERE sys_role_id = ?`
  103. _, err = o.Raw(sql, roleId).QueryRows(&items)
  104. return
  105. }