sys_role.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package system
  2. import (
  3. "context"
  4. "hongze/hrms_api/global"
  5. "hongze/hrms_api/models/base"
  6. )
  7. // 角色表
  8. type SysRole struct {
  9. RoleId int64 `gorm:"primaryKey;column:role_id" json:"role_id"` //序号
  10. RoleName string `gorm:"column:role_name" json:"role_name"` //角色名称
  11. base.TimeBase
  12. }
  13. // TableName get sql table name.获取数据库表名
  14. func (r *SysRole) TableName() string {
  15. return "sys_role"
  16. }
  17. // 新增
  18. func (r *SysRole) Add() (err error) {
  19. err = global.DEFAULT_MYSQL.Create(r).Error
  20. return
  21. }
  22. // 修改
  23. func (r *SysRole) Update(updateCols []string) (err error) {
  24. err = global.DEFAULT_MYSQL.Model(r).Select(updateCols).Updates(r).Error
  25. return
  26. }
  27. type SysRoleAddReq struct {
  28. RoleName string `json:"role_name" binding:"required"` //角色名称
  29. }
  30. type SysRoleEditReq struct {
  31. RoleId int64 `json:"role_id" binding:"required"` //序号
  32. RoleName string `json:"role_name" binding:"required"` //角色名称
  33. }
  34. type SysRoleDelReq struct {
  35. RoleId int64 `json:"role_id" binding:"required"` //序号
  36. }
  37. type SysRoleMenuEditReq struct {
  38. RoleId int64 `json:"role_id" binding:"required,gte=1"` //序号
  39. MenuIdList []int64 `json:"menu_id_list" ` //绑定的菜单ID
  40. }
  41. func (r *SysRole) GetRoleByRoleId(roleId int64) (item *SysRole, err error) {
  42. err = global.DEFAULT_MYSQL.Model(r).Where("role_id = ?", roleId).First(&item).Error
  43. return
  44. }
  45. func (r *SysRole) GetRoleByRoleName(roleName string) (item *SysRole, err error) {
  46. err = global.DEFAULT_MYSQL.Model(r).Where("role_name = ?", roleName).First(&item).Error
  47. return
  48. }
  49. type SysRoleListItem struct {
  50. RoleId int64 `json:"role_id"` //序号
  51. RoleName string `json:"role_name"` //角色名称
  52. CreateTime string `json:"create_time"` //创建时间
  53. ModifyTime string `json:"modify_time"` //最后更新时间
  54. }
  55. // SelectPage 分页查询
  56. func (r *SysRole) SelectPage(page base.IPage, condition string, pars []interface{}) (count int64, results []SysRole, err error) {
  57. query := global.DEFAULT_MYSQL.WithContext(context.TODO()).Model(r).Where(condition, pars...)
  58. query.Count(&count)
  59. if len(page.GetOrderItemsString()) > 0 {
  60. query = query.Order(page.GetOrderItemsString())
  61. }
  62. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  63. return
  64. }
  65. func (r *SysRole) GetRoleNameList() (list []*SysRole, err error) {
  66. err = global.DEFAULT_MYSQL.Model(r).Find(&list).Error
  67. return
  68. }
  69. type SysRoleListReq struct {
  70. RoleName string `json:"role_name" form:"role_name"` //序号
  71. base.PageReq
  72. }
  73. type SysRoleMenuListReq struct {
  74. RoleId int64 `json:"role_id" form:"role_id"` //序号
  75. }
  76. type SysRoleMenuResp struct {
  77. List []*MenuListItemResp `json:"list"`
  78. ChoiceList []int64 `json:"choice_list"`
  79. }
  80. // 删除
  81. func (r *SysRole) Delete() (err error) {
  82. err = global.DEFAULT_MYSQL.Delete(r).Error
  83. return
  84. }