sys_role.go 6.3 KB

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