sys_role.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type SysRoleAddReq struct {
  9. RoleName string `description:"角色名称"`
  10. RoleType string `description:"角色类型"`
  11. }
  12. type SysRole struct {
  13. RoleId int `gorm:"primaryKey"`
  14. RoleName string `description:"角色名称"`
  15. RoleType string `description:"角色类型"`
  16. RoleTypeCode string `description:"角色类型编码"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time
  19. }
  20. type SysRoleItem struct {
  21. RoleId int `orm:"column(role_id);pk" description:"角色ID"`
  22. RoleName string `description:"角色名称"`
  23. RoleType string `description:"角色类型"`
  24. RoleTypeCode string `description:"角色类型编码"`
  25. CreateTime time.Time `description:"创建时间"`
  26. RoleLevel int `description:"角色等级:0-表示一级角色,每个账号只能绑定一个一级角色 ,1-表示二级角色,每个账号可以绑定多个二级 角色"`
  27. }
  28. func GetSysRoleCount(roleName string) (count int, err error) {
  29. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE role_name=? `
  30. err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&count).Error
  31. return
  32. }
  33. func AddSysRole(item *SysRole) (lastId int64, err error) {
  34. err = global.DEFAULT_DmSQL.Create(item).Error
  35. return
  36. }
  37. type SysRoleEditReq struct {
  38. RoleId int `description:"角色ID"`
  39. RoleName string `description:"角色名称"`
  40. RoleType string `description:"角色类型"`
  41. }
  42. func GetSysRoleById(roleId int) (item *SysRoleItem, err error) {
  43. sql := `SELECT * FROM sys_role WHERE role_id=? `
  44. err = global.DEFAULT_DmSQL.Raw(sql, roleId).First(&item).Error
  45. return
  46. }
  47. func GetSysRoleByName(roleName string) (item *SysRoleItem, err error) {
  48. sql := `SELECT * FROM sys_role WHERE role_name=? `
  49. err = global.DEFAULT_DmSQL.Raw(sql, roleName).First(&item).Error
  50. return
  51. }
  52. func GetSysRoleByRoleTypeCode(roleTypeCode string) (item *SysRoleItem, err error) {
  53. sql := `SELECT * FROM sys_role WHERE role_type_code=? `
  54. err = global.DEFAULT_DmSQL.Raw(sql, roleTypeCode).First(&item).Error
  55. return
  56. }
  57. func ModifySysRole(roleName, roleType, roleTypeCode string, groupId int) (err error) {
  58. sql := `UPDATE sys_role SET role_name=?,role_type=?,role_type_code=?,modify_time=NOW() WHERE role_id=? `
  59. err = global.DEFAULT_DmSQL.Exec(sql, roleName, roleType, roleTypeCode, groupId).Error
  60. return
  61. }
  62. type SysRoleDeleteReq struct {
  63. RoleId int `description:"角色ID"`
  64. }
  65. func DeleteSysRole(roleId int) (err error) {
  66. sql := `DELETE FROM sys_role WHERE role_id=? `
  67. err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
  68. return
  69. }
  70. func GetSysRoleListCount(condition string, pars []interface{}) (count int, err error) {
  71. sql := `SELECT COUNT(1) AS count FROM sys_role WHERE 1=1 `
  72. if condition != "" {
  73. sql += condition
  74. }
  75. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  76. return
  77. }
  78. func GetSysRoleList(condition string, pars []interface{}, startSize, pageSize int) (items []*SysRoleItem, err error) {
  79. sql := `SELECT * FROM sys_role WHERE 1=1 `
  80. if condition != "" {
  81. sql += condition
  82. }
  83. sql += `ORDER BY modify_time DESC LIMIT ?,?`
  84. pars = append(pars, startSize)
  85. pars = append(pars, pageSize)
  86. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  87. return
  88. }
  89. type SysRoleListResp struct {
  90. List []*SysRoleItem
  91. Paging *paging.PagingItem `description:"分页数据"`
  92. }
  93. type RoleMenu struct {
  94. MenuId int `description:"导航唯一标识"`
  95. Name string `description:"导航名称"`
  96. NameEn string `description:"导航名称(英文)"`
  97. }
  98. type RoleMenuList struct {
  99. MenuId int `description:"导航唯一标识"`
  100. Name string `description:"导航名称"`
  101. NameEn string `description:"导航名称(英文)"`
  102. Child []*RoleMenu `gorm:"-"`
  103. CheckList []int
  104. }
  105. type RoleMenuListResp struct {
  106. List []*RoleMenuList
  107. }
  108. func GetRoleMenuList() (items []*RoleMenuList, err error) {
  109. sql := `SELECT * FROM sys_menu WHERE parent_id=0 ORDER BY sort ASC `
  110. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  111. return
  112. }
  113. func GetRoleMenuByParentId(parentId int) (items []*RoleMenu, err error) {
  114. sql := `SELECT * FROM sys_menu WHERE parent_id=? ORDER BY sort ASC `
  115. err = global.DEFAULT_DmSQL.Raw(sql, parentId).Find(&items).Error
  116. return
  117. }
  118. func GetCheckListRoleMenu(roleId, parentId int) (items []*RoleMenuList, err error) {
  119. sql := ` SELECT b.menu_id FROM sys_role_menu AS a
  120. INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
  121. WHERE a.role_id=? AND a.type = 0
  122. AND b.parent_id=? `
  123. err = global.DEFAULT_DmSQL.Raw(sql, roleId, parentId).Find(&items).Error
  124. return
  125. }
  126. type RoleMenusAddReq struct {
  127. RoleId int `description:"角色ID"`
  128. MenuIdStr string `description:"MenuId,多个用英文逗号隔开"`
  129. MenuButtonIdStr string `description:"menu_button_id,多个用英文逗号隔开"`
  130. }
  131. func DeleteRoleMenuByRoleId(roleId int) (err error) {
  132. sql := `DELETE FROM sys_role_menu WHERE role_id=? `
  133. err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
  134. return
  135. }
  136. type SysRoleMenu struct {
  137. RoleMenuId int `gorm:"primaryKey"`
  138. RoleId int
  139. MenuId int
  140. Type int `description:"类型:0-全选;1-半选(此字段仅用于前端的回显)"`
  141. }
  142. func AddSysRoleMenu(item *SysRoleMenu) (lastId int64, err error) {
  143. err = global.DEFAULT_DmSQL.Create(item).Error
  144. return
  145. }
  146. func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenu) (err error) {
  147. if roleId == 0 {
  148. return
  149. }
  150. tx := global.DEFAULT_DmSQL.Begin()
  151. defer func() {
  152. if err != nil {
  153. _ = tx.Rollback()
  154. return
  155. }
  156. _ = tx.Commit()
  157. }()
  158. sql := `DELETE FROM sys_role_menu WHERE role_id = ?`
  159. err = tx.Exec(sql, roleId).Error
  160. if err != nil {
  161. return
  162. }
  163. if len(items) > 0 {
  164. err = tx.CreateInBatches(items, utils.MultiAddNum).Error
  165. }
  166. return
  167. }
  168. func GetSysRoleAll(levelFlag bool) (items []*SysRoleItem, err error) {
  169. sql := `SELECT * FROM sys_role WHERE 1=1`
  170. if levelFlag {
  171. sql += ` and role_level = 0`
  172. }
  173. sql += ` ORDER BY create_time DESC `
  174. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  175. return
  176. }
  177. type SysRoleAllResp struct {
  178. List []*SysRoleItem
  179. }
  180. func GetEnglishAuthRoleIds(roleCodes []string) (roleIds []int, err error) {
  181. if len(roleCodes) == 0 {
  182. return
  183. }
  184. sql := `SELECT role_id FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  185. err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Scan(&roleIds).Error
  186. return
  187. }
  188. func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenu, err error) {
  189. sql := `SELECT
  190. *
  191. FROM
  192. sys_role_menu
  193. WHERE
  194. role_id = ?`
  195. err = global.DEFAULT_DmSQL.Raw(sql, roleId).Find(&items).Error
  196. return
  197. }
  198. func GetEnglishAuthRoleItems(roleCodes []string) (items []*SysRole, err error) {
  199. if len(roleCodes) == 0 {
  200. return
  201. }
  202. sql := `SELECT * FROM sys_role WHERE role_type_code IN (` + utils.GetOrmInReplace(len(roleCodes)) + `)`
  203. err = global.DEFAULT_DmSQL.Raw(sql, roleCodes).Find(&items).Error
  204. return
  205. }