role_service.go 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. package service
  2. import (
  3. "database/sql"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "XT_New/models"
  8. "XT_New/utils"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func GetRoles(orgID int64, appID int64, page int, count int) ([]*models.Role, int, error) {
  12. var roles []*models.Role
  13. var totalCount int
  14. err := readUserDb.Model(&models.Role{}).
  15. Where("org_id = ? AND app_id = ?", orgID, appID).
  16. Count(&totalCount).
  17. Order("ctime asc").Limit(count).Offset((page - 1) * count).
  18. Find(&roles).
  19. Error
  20. if err != nil {
  21. if err == gorm.ErrRecordNotFound {
  22. return make([]*models.Role, 0), 0, nil
  23. } else {
  24. return nil, 0, err
  25. }
  26. }
  27. return roles, totalCount, nil
  28. }
  29. func GetAppRole(orgID int64, appID int64, adminUserID int64) (*models.App_Role, error) {
  30. var appRole models.App_Role
  31. err := readUserDb.Model(models.App_Role{}).Preload("AdminUser", "status = 1").Where("org_id = ? and app_id = ? and admin_user_id = ?", orgID, appID, adminUserID).First(&appRole).Error
  32. if err != nil {
  33. if err == gorm.ErrRecordNotFound {
  34. return nil, nil
  35. } else {
  36. return nil, err
  37. }
  38. }
  39. if len(appRole.RoleIds) > 0 {
  40. role_ids := strings.Split(appRole.RoleIds, ",")
  41. for _, item := range role_ids {
  42. id, _ := strconv.ParseInt(item, 10, 64)
  43. if id != 0 {
  44. role, _ := GetRoleByRoleID(id)
  45. if role != nil {
  46. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  47. appRole.IsSubSuperAdmin = true
  48. }
  49. }
  50. }
  51. }
  52. }
  53. return &appRole, nil
  54. }
  55. func CreateRole(adminUserID int64, orgID int64, appID int64, name string, intro string) (*models.Role, error) {
  56. role := models.Role{
  57. RoleName: name,
  58. RoleIntro: intro,
  59. Creator: adminUserID,
  60. OrgId: orgID,
  61. AppId: appID,
  62. IsSuperAdmin: false,
  63. Status: 1,
  64. CreateTime: time.Now().Unix(),
  65. ModifyTime: time.Now().Unix(),
  66. }
  67. tx := writeUserDb.Begin()
  68. if err := tx.Create(&role).Error; err != nil {
  69. tx.Rollback()
  70. return nil, err
  71. }
  72. tx.Commit()
  73. return &role, nil
  74. }
  75. func GetRoleByRoleID(roleID int64) (*models.Role, error) {
  76. var role models.Role
  77. err := readUserDb.Model(&models.Role{}).Where("id = ?", roleID).First(&role).Error
  78. if err != nil {
  79. if err == gorm.ErrRecordNotFound {
  80. return nil, nil
  81. }
  82. return nil, err
  83. } else {
  84. return &role, nil
  85. }
  86. }
  87. func ModifyRole(role *models.Role) error {
  88. tx := writeUserDb.Begin()
  89. if err := tx.Save(role).Error; err != nil {
  90. tx.Rollback()
  91. return err
  92. }
  93. return tx.Commit().Error
  94. }
  95. // 拥有xx角色的管理员的数量
  96. func RoleAdminUserCount(orgID int64, appID int64, roleID int64) (int, error) {
  97. var count int
  98. err := readUserDb.Model(models.App_Role{}).
  99. Where("org_id = ? AND app_id = ? AND role_id = ? AND status = 1", orgID, appID, roleID).
  100. Count(&count).
  101. Error
  102. if err != nil {
  103. return 0, err
  104. } else {
  105. return count, nil
  106. }
  107. }
  108. type AdminUserManageViewModel struct {
  109. AdminUserId int `gorm:"admin_user_id" json:"user_id"`
  110. UserName string `gorm:"user_name" json:"user_name"`
  111. RoleName string `gorm:"role_name" json:"role_name"`
  112. UserTitle int `gorm:"user_title" json:"user_title"`
  113. Ip string `gorm:"ip" json:"ip"`
  114. Ctime int64 `gorm:"ctime" json:"last_login_time"`
  115. Status int `gorm:"status" json:"status"`
  116. Avatar string `gorm:"avatar" json:"avatar"`
  117. RoleIds string `gorm:"role_ids" json:"role_ids"`
  118. // LastLoginTimeStr string `gorm:"-" json:"last_login_time_formatted"`
  119. TitleName string `gorm:"-" json:"title_name"`
  120. IsSubSuperAdmin bool `gorm:"-" json:"is_sub_super_admin"`
  121. //mobile string `gorm:"-" json:"mobile"`
  122. }
  123. func GetAdminUsersAndLoginInfo(orgID int64, appID int64, page int, count int) ([]*AdminUserManageViewModel, int, error) {
  124. if count <= 0 {
  125. return []*AdminUserManageViewModel{}, 0, nil
  126. }
  127. if page < 1 {
  128. page = 1
  129. }
  130. var viewModels []*AdminUserManageViewModel = make([]*AdminUserManageViewModel, 0)
  131. rows, err := readUserDb.Raw("SELECT u_a_r.admin_user_id, u_a_r.user_name, u_a_r.user_title, u_l.ip, u_l.ctime, u_a_r.status,u_a_r.avatar,u_a_r.role_ids FROM sgj_user_admin_role AS u_a_r LEFT JOIN (SELECT * FROM (SELECT admin_user_id, org_id, app_id, ip, ctime FROM sgj_user_admin_login_log WHERE org_id = ? AND app_id = ? ORDER BY ctime DESC) AS t GROUP BY admin_user_id) AS u_l ON u_a_r.org_id = u_l.org_id AND u_a_r.app_id = u_l.app_id AND u_a_r.admin_user_id = u_l.admin_user_id WHERE u_a_r.org_id = ? AND u_a_r.app_id = ? GROUP BY u_a_r.admin_user_id LIMIT ? OFFSET ?;", orgID, appID, orgID, appID, count, (page-1)*count).Rows()
  132. defer rows.Close()
  133. if err != nil {
  134. if err == gorm.ErrRecordNotFound {
  135. return viewModels, 0, nil
  136. } else {
  137. return nil, 0, err
  138. }
  139. }
  140. for rows.Next() {
  141. var viewModel AdminUserManageViewModel
  142. readUserDb.ScanRows(rows, &viewModel)
  143. title, _ := models.UserTitle[viewModel.UserTitle]
  144. viewModel.TitleName = title
  145. // if viewModel.Ctime == 0 {
  146. // viewModel.LastLoginTimeStr = ""
  147. // } else {
  148. // loginTime := time.Unix(viewModel.Ctime, 0)
  149. // viewModel.LastLoginTimeStr = loginTime.Format("2006-01-02 15:04")
  150. // }
  151. viewModels = append(viewModels, &viewModel)
  152. }
  153. for _, item := range viewModels {
  154. if len(item.RoleIds) > 0 {
  155. ids := strings.Split(item.RoleIds, ",")
  156. for _, id := range ids {
  157. id, _ := strconv.ParseInt(id, 10, 64)
  158. if id != 0 {
  159. role, _ := GetRoleByRoleID(id)
  160. if role != nil {
  161. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  162. item.IsSubSuperAdmin = true
  163. }
  164. if len(item.RoleName) == 0 {
  165. item.RoleName = role.RoleName
  166. } else {
  167. item.RoleName = item.RoleName + "," + role.RoleName
  168. }
  169. }
  170. }
  171. }
  172. }
  173. //admin, _ := GetAdminUserByUserID(int64(item.AdminUserId))
  174. //item.mobile = admin.Mobile
  175. }
  176. total := 0
  177. //readUserDb.Table("sgj_user_admin_role as u_a_r").Joins("join sgj_user_role as u_r on u_r.org_id = u_a_r.org_id AND u_r.app_id = u_a_r.app_id AND u_r.id = u_a_r.role_id").Where("u_a_r.org_id = ? AND u_a_r.app_id = ?", orgID, appID).Count(&total)
  178. return viewModels, total, nil
  179. }
  180. func GetValidRoleCount(orgID int64, appID int64, superAdminUserID int64) (int, error) {
  181. var count int
  182. err := readUserDb.Model(models.Role{}).
  183. Where("org_id = ? AND app_id = ? AND creator = ? AND is_super_admin = 0 AND status = 1", orgID, appID, superAdminUserID).
  184. Count(&count).
  185. Error
  186. if err != nil {
  187. return 0, err
  188. } else {
  189. return count, nil
  190. }
  191. }
  192. func GetAllValidRoles(orgID int64, appID int64) ([]*models.Role, error) {
  193. var roles []*models.Role
  194. err := readUserDb.Model(models.Role{}).
  195. Where("org_id = ? AND app_id = ? AND status = 1", orgID, appID).
  196. Order("number desc,ctime").
  197. Find(&roles).
  198. Error
  199. if err != nil {
  200. if err == gorm.ErrRecordNotFound {
  201. return make([]*models.Role, 0), nil
  202. } else {
  203. return nil, err
  204. }
  205. }
  206. return roles, nil
  207. }
  208. func IsRoleExist(orgID int64, appID int64, roleID int64) (bool, error) {
  209. var count int
  210. err := readUserDb.Model(models.Role{}).Where("org_id = ? AND app_id = ? AND id = ? AND status = 1", orgID, appID, roleID).Count(&count).Error
  211. if err != nil {
  212. return false, err
  213. } else {
  214. return count > 0, nil
  215. }
  216. }
  217. type AdminUserEditViewModel struct {
  218. AdminUserId int64 `gorm:"admin_user_id" json:"user_id"`
  219. UserName string `gorm:"user_name" json:"user_name"`
  220. Mobile string `gorm:"mobile" json:"mobile"`
  221. UserType int `gorm:"user_type" json:"user_type"`
  222. UserTitle int `gorm:"user_title" json:"user_title"`
  223. Intro string `gorm:"intro" json:"intro"`
  224. RoleId int64 `gorm:"role_id" json:"role_id"`
  225. }
  226. func GetGeneralAdminUser(orgID int64, appID int64, user_id int64) (*AdminUserEditViewModel, error) {
  227. rows, err := readUserDb.Raw("SELECT u_a.mobile, u_a_r.admin_user_id, u_a_r.user_name, u_a_r.user_type, u_a_r.user_title, u_a_r.intro, u_a_r.role_id FROM sgj_user_admin AS u_a, sgj_user_admin_role AS u_a_r WHERE u_a.id = u_a_r.admin_user_id AND u_a.id = ? AND u_a_r.status = 1 AND u_a_r.org_id = ? AND u_a_r.app_id = ?;", user_id, orgID, appID).Rows()
  228. defer rows.Close()
  229. if err != nil {
  230. if err == gorm.ErrRecordNotFound {
  231. return nil, nil
  232. } else {
  233. return nil, err
  234. }
  235. }
  236. if rows.Next() {
  237. var viewModel AdminUserEditViewModel
  238. err := readUserDb.ScanRows(rows, &viewModel)
  239. if err != nil {
  240. return nil, err
  241. } else {
  242. return &viewModel, nil
  243. }
  244. }
  245. return nil, nil
  246. }
  247. func SaveAppRole(appRole *models.App_Role) error {
  248. tx := writeUserDb.Begin()
  249. if err := tx.Model(&models.App_Role{}).Save(appRole).Error; err != nil {
  250. tx.Rollback()
  251. return err
  252. }
  253. return tx.Commit().Error
  254. }
  255. func IsMobileDidUsedAtApp(orgID int64, appID int64, mobile string) (bool, error) {
  256. var count int
  257. rows, err := readUserDb.Raw("select count(u_a.id) as count from sgj_user_admin as u_a, sgj_user_admin_role as u_a_r where u_a_r.org_id = ? and u_a_r.app_id = ? and u_a.mobile = ? and u_a.id = u_a_r.admin_user_id;", orgID, appID, mobile).Rows()
  258. defer rows.Close()
  259. if err != nil {
  260. if err == gorm.ErrRecordNotFound {
  261. return false, nil
  262. } else {
  263. return true, err
  264. }
  265. }
  266. if rows.Next() {
  267. rows.Scan(&count)
  268. return count > 0, nil
  269. }
  270. return true, nil
  271. }
  272. func IsUserSuperAdminWithMobile(mobile string) (bool, error) {
  273. var user models.AdminUser
  274. err := readUserDb.Where("mobile = ?", mobile).First(&user).Error
  275. if err != nil {
  276. if err == gorm.ErrRecordNotFound {
  277. return false, nil
  278. } else {
  279. return false, err
  280. }
  281. }
  282. return user.IsSuperAdmin, nil
  283. }
  284. func CreateGeneralAdminUser(orgID int64, appID int64, mobile string, name string, userTitle string, roleIds string, user_type int, user_title int) (*models.AdminUser, string, error) {
  285. now := time.Now().Unix()
  286. tx := writeUserDb.Begin()
  287. var adminUser models.AdminUser
  288. err := readUserDb.Where("mobile = ? AND status = 1", mobile).First(&adminUser).Error
  289. password := ""
  290. if err != nil {
  291. if err != gorm.ErrRecordNotFound {
  292. return nil, "", err
  293. } else {
  294. password = utils.RandomNumberString(6)
  295. adminUser.Mobile = mobile
  296. adminUser.Password = utils.String2md5(password)
  297. adminUser.IsSuperAdmin = false
  298. adminUser.Status = 1
  299. adminUser.CreateTime = now
  300. adminUser.ModifyTime = now
  301. if createErr := tx.Create(&adminUser).Error; createErr != nil {
  302. tx.Rollback()
  303. return nil, "", createErr
  304. }
  305. }
  306. }
  307. app_role := models.App_Role{
  308. AdminUserId: adminUser.Id,
  309. OrgId: orgID,
  310. AppId: appID,
  311. Avatar: "",
  312. UserName: name,
  313. UserTitleName: userTitle,
  314. UserTitle: int8(user_title),
  315. UserType: int8(user_type),
  316. Status: 1,
  317. CreateTime: now,
  318. ModifyTime: now,
  319. RoleIds: roleIds,
  320. }
  321. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  322. tx.Rollback()
  323. return nil, "", createApp_RoleErr
  324. }
  325. tx.Commit()
  326. return &adminUser, password, nil
  327. }
  328. type PurviewTreeViewModel struct {
  329. ID int64 `json:"id"`
  330. PID int64 `json:"pid"`
  331. Name string `json:"name"`
  332. Childs []*PurviewTreeViewModel `json:"childs"`
  333. Number int64 `json:"number"`
  334. }
  335. func GetAllGeneralPurviewVMsProcessed(module int) ([]*PurviewTreeViewModel, error) {
  336. var originPurviews []*models.Purview
  337. getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = ? AND status = 1 AND super_admin_exclusive = 0", module).Order("listorder asc").Order("id asc").Find(&originPurviews).Error
  338. if getPurviewErr != nil {
  339. return nil, getPurviewErr
  340. }
  341. // 加工这些规则:树形化
  342. purviewVMs := make([]*PurviewTreeViewModel, 0)
  343. pid_childs := make(map[int64][]*PurviewTreeViewModel)
  344. for _, purview := range originPurviews {
  345. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  346. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  347. pvm := &PurviewTreeViewModel{
  348. ID: purview.Id,
  349. PID: purview.Parentid,
  350. Name: purview.Name,
  351. Number: 2,
  352. }
  353. if purview.Parentid == 0 {
  354. purviewVMs = append(purviewVMs, pvm)
  355. } else {
  356. childs := pid_childs[purview.Parentid]
  357. if childs == nil {
  358. childs = make([]*PurviewTreeViewModel, 0)
  359. }
  360. childs = append(childs, pvm)
  361. pid_childs[purview.Parentid] = childs
  362. }
  363. }
  364. for _, vm := range purviewVMs {
  365. vm.Childs = pid_childs[vm.ID]
  366. }
  367. return purviewVMs, nil
  368. }
  369. func GetAllGeneralFuncPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
  370. var originPurviews []*models.SgjUserOperatePurview
  371. getPurviewErr := readUserDb.Model(models.SgjUserOperatePurview{}).Where(" status = 1").Order("number asc").Find(&originPurviews).Error
  372. if getPurviewErr != nil {
  373. return nil, getPurviewErr
  374. }
  375. // 加工这些规则:树形化
  376. purviewVMs := make([]*PurviewTreeViewModel, 0)
  377. for _, purview := range originPurviews {
  378. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  379. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  380. pvm := &PurviewTreeViewModel{
  381. ID: purview.ID,
  382. PID: purview.Parentid,
  383. Name: purview.Name,
  384. }
  385. purviewVMs = append(purviewVMs, pvm)
  386. }
  387. return purviewVMs, nil
  388. }
  389. func GetRolePurviewIds(roleID int64) (string, error) {
  390. var rolePurview models.RolePurview
  391. err := readUserDb.Where("role_id = ?", roleID).First(&rolePurview).Error
  392. if err != nil {
  393. if err == gorm.ErrRecordNotFound {
  394. return "", nil
  395. } else {
  396. return "", err
  397. }
  398. }
  399. return rolePurview.PurviewIds, nil
  400. }
  401. func SaveRolePurviewIds(orgID int64, appID int64, roleID int64, purviewIds string) error {
  402. var rolePurview models.RolePurview
  403. getRPErr := readUserDb.Where("org_id = ? AND app_id = ? AND role_id = ?", orgID, appID, roleID).First(&rolePurview).Error
  404. if getRPErr != nil {
  405. if getRPErr == gorm.ErrRecordNotFound {
  406. rolePurview = models.RolePurview{
  407. RoleId: roleID,
  408. OrgId: orgID,
  409. AppId: appID,
  410. Status: 1,
  411. CreateTime: time.Now().Unix(),
  412. }
  413. } else {
  414. return getRPErr
  415. }
  416. }
  417. rolePurview.PurviewIds = purviewIds
  418. rolePurview.ModifyTime = time.Now().Unix()
  419. tx := writeUserDb.Begin()
  420. if err := tx.Save(&rolePurview).Error; err != nil {
  421. tx.Rollback()
  422. return err
  423. }
  424. return tx.Commit().Error
  425. }
  426. func SaveFuncRolePurviewIds(orgID int64, appID int64, roleID int64, funcPurviewIds string) error {
  427. var rolePurview models.SgjUserRoleFuncPurview
  428. getRPErr := readUserDb.Where("org_id = ? AND app_id = ? AND role_id = ?", orgID, appID, roleID).First(&rolePurview).Error
  429. if getRPErr != nil {
  430. if getRPErr == gorm.ErrRecordNotFound {
  431. rolePurview = models.SgjUserRoleFuncPurview{
  432. RoleId: roleID,
  433. OrgId: orgID,
  434. AppId: appID,
  435. Status: 1,
  436. Ctime: time.Now().Unix(),
  437. }
  438. } else {
  439. return getRPErr
  440. }
  441. }
  442. rolePurview.PurviewIds = funcPurviewIds
  443. rolePurview.Mtime = time.Now().Unix()
  444. tx := writeUserDb.Begin()
  445. if err := tx.Save(&rolePurview).Error; err != nil {
  446. tx.Rollback()
  447. return err
  448. }
  449. return tx.Commit().Error
  450. }
  451. func ModifyAdminUserInfo(adminUserID int64, orgID int64, appID int64, name string, avatar string, newPassword string) error {
  452. tx := writeUserDb.Begin()
  453. editInfoErr := tx.Exec("update sgj_user_admin_role set user_name = ?, avatar = ?, mtime = ? where admin_user_id = ? and org_id = ?", name, avatar, time.Now().Unix(), adminUserID, orgID).Error
  454. if editInfoErr != nil {
  455. tx.Rollback()
  456. return editInfoErr
  457. }
  458. if len(newPassword) > 0 {
  459. editPwdErr := tx.Exec("update sgj_user_admin set password = ?, mtime = ? where id = ?", newPassword, time.Now().Unix(), adminUserID).Error
  460. if editPwdErr != nil {
  461. tx.Rollback()
  462. return editPwdErr
  463. }
  464. }
  465. tx.Commit()
  466. return nil
  467. }
  468. func GetOrgApp(orgID int64, app_type int) (*models.OrgApp, error) {
  469. var apps models.OrgApp
  470. err := readUserDb.Where("app_type = ? AND org_id = ? AND status = 1", app_type, orgID).First(&apps).Error
  471. if err != nil {
  472. return nil, err
  473. }
  474. return &apps, nil
  475. }
  476. func GetAllOrgValidRoles(orgID int64, isSubSuperAdmin bool) ([]*models.Role, error) {
  477. var roles []*models.Role
  478. db := readUserDb.Model(models.Org{})
  479. if isSubSuperAdmin {
  480. db = db.Where("role_name != '子管理员' AND is_system != 1")
  481. }
  482. err := db.Where("org_id = ? AND status = 1", orgID).
  483. Order("number desc,ctime").
  484. Find(&roles).
  485. Error
  486. if err != nil {
  487. if err == gorm.ErrRecordNotFound {
  488. return make([]*models.Role, 0), nil
  489. } else {
  490. return nil, err
  491. }
  492. }
  493. for _, item := range roles {
  494. var total int64
  495. readUserDb.Model(&models.App_Role{}).Where("org_id = ? AND find_in_set(?, role_ids)", orgID, item.Id).Count(&total)
  496. item.StaffNumber = total
  497. }
  498. return roles, nil
  499. }
  500. func CreateUserRole(userRole *models.App_Role) (err error) {
  501. err = writeUserDb.Create(&userRole).Error
  502. return
  503. }
  504. func CreateOrgRole(role *models.Role) (err error) {
  505. err = writeUserDb.Create(&role).Error
  506. return
  507. }
  508. func GetRolePurview(roleID int64) (models.RolePurview, error) {
  509. var rolePurviews models.RolePurview
  510. var err error
  511. err = readUserDb.Where("role_id = ?", roleID).First(&rolePurviews).Error
  512. return rolePurviews, err
  513. }
  514. func GetFuncRolePurview(roleID int64) (models.SgjUserRoleFuncPurview, error) {
  515. var rolePurviews models.SgjUserRoleFuncPurview
  516. var err error
  517. err = readUserDb.Where("role_id = ?", roleID).First(&rolePurviews).Error
  518. return rolePurviews, err
  519. }
  520. func SaveRolePurview(purview *models.RolePurview) (err error) {
  521. err = writeUserDb.Save(&purview).Error
  522. return
  523. }
  524. func CreateRolePurview(purview *models.RolePurview) (err error) {
  525. err = writeUserDb.Create(&purview).Error
  526. return
  527. }
  528. func CreateFuncRolePurview(purview *models.SgjUserRoleFuncPurview) (err error) {
  529. err = writeUserDb.Create(&purview).Error
  530. return
  531. }
  532. func GetAllPurview(module int64, pid int64) ([]*models.Purview, error) {
  533. var originPurviews []*models.Purview
  534. getPurviewErr := readUserDb.Model(models.Purview{}).Preload("Purview", "status = 1").Where("module = ? AND status = 1 AND parentid = ? AND super_admin_exclusive = 0", module, pid).Order("listorder asc").Order("id asc").Find(&originPurviews).Error
  535. return originPurviews, getPurviewErr
  536. }
  537. type NewAdminUserModel struct {
  538. ID int64 `gorm:"id" json:"id"`
  539. AdminUserId int `gorm:"admin_user_id" json:"user_id"`
  540. UserName string `gorm:"user_name" json:"user_name"`
  541. UserTitle int `gorm:"user_title_name" json:"user_title_name"`
  542. Ip string `gorm:"ip" json:"ip"`
  543. Ctime int64 `gorm:"ctime" json:"last_login_time"`
  544. Status int `gorm:"status" json:"status"`
  545. Avatar string `gorm:"avatar" json:"avatar"`
  546. RoleIds string `gorm:"role_ids" json:"role_ids"`
  547. IsSubAdmin bool `gorm:"-" json:"is_sub_admin"`
  548. }
  549. func GetAllAdminUsersAndRole(orgID int64, appID int64, page int, count int) ([]*NewAdminUserModel, int, error) {
  550. var viewModels []*NewAdminUserModel = make([]*NewAdminUserModel, 0)
  551. var rows *sql.Rows
  552. var err error
  553. rows, err = readUserDb.Raw("SELECT u_a_r.id, u_a_r.admin_user_id, u_a_r.user_name, u_a_r.user_title, u_a_r.status,u_a_r.avatar,u_a_r.role_ids FROM sgj_user_admin_role AS u_a_r WHERE org_id = ? AND app_id = ? ORDER BY ctime asc", orgID, appID).Rows()
  554. defer rows.Close()
  555. if err != nil {
  556. if err == gorm.ErrRecordNotFound {
  557. return viewModels, 0, nil
  558. } else {
  559. return nil, 0, err
  560. }
  561. }
  562. for rows.Next() {
  563. var viewModel NewAdminUserModel
  564. readUserDb.ScanRows(rows, &viewModel)
  565. viewModels = append(viewModels, &viewModel)
  566. }
  567. for _, items := range viewModels {
  568. if len(items.RoleIds) > 0 {
  569. ids := strings.Split(items.RoleIds, ",")
  570. for _, ids := range ids {
  571. id, _ := strconv.ParseInt(ids, 10, 64)
  572. if id != 0 {
  573. role, _ := GetRoleByRoleID(id)
  574. if role != nil {
  575. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  576. items.IsSubAdmin = true
  577. }
  578. }
  579. }
  580. }
  581. }
  582. }
  583. total := 0
  584. return viewModels, total, nil
  585. }
  586. func FindAdminUserByID(admin_user_id int64, org_id int64) (total int64, err error) {
  587. err = readUserDb.Model(&models.App_Role{}).Where("admin_user_id = ? AND org_id = ?", admin_user_id, org_id).Count(&total).Error
  588. return
  589. }
  590. func GetRoleAndPurviewById(role_id int64, org_id int64, app_id int64) (rolePurview models.RolePurview, err error) {
  591. err = readUserDb.Model(&models.RolePurview{}).Preload("Role", "status = 1").Where("role_id = ? AND status = 1 AND org_id = ? AND app_id = ?", role_id, org_id, app_id).First(&rolePurview).Error
  592. return
  593. }
  594. func SaveRole(role *models.Role) (err error) {
  595. err = writeUserDb.Save(&role).Error
  596. return
  597. }
  598. // 拥有xx角色的管理员的数量
  599. func RoleAdminUserCountTwo(orgID int64, appID int64, roleID int64) (int, error) {
  600. var count int
  601. err := readUserDb.Model(models.App_Role{}).
  602. Where("org_id = ? AND app_id = ? AND find_in_set(?, role_ids)", orgID, appID, roleID).
  603. Count(&count).
  604. Error
  605. if err != nil {
  606. return 0, err
  607. } else {
  608. return count, nil
  609. }
  610. }
  611. func FindAdminUserID(id int64) (role models.App_Role, err error) {
  612. err = readUserDb.Model(&models.App_Role{}).Where("id = ?", id).First(&role).Error
  613. return
  614. }
  615. func SaveAdminUser(role *models.App_Role) (err error) {
  616. err = writeUserDb.Save(&role).Error
  617. return
  618. }
  619. func GetFunctionPurview() (purview []*models.SgjUserOperatePurview, err error) {
  620. err = readUserDb.Model(&models.SgjUserOperatePurview{}).Where("status = 1 ").Order("number asc").Find(&purview).Error
  621. return
  622. }
  623. func GetRoleFuncPurviewIds(roleID int64) (string, error) {
  624. var rolePurview models.SgjUserRoleFuncPurview
  625. err := readUserDb.Where("role_id = ?", roleID).First(&rolePurview).Error
  626. if err != nil {
  627. if err == gorm.ErrRecordNotFound {
  628. return "", nil
  629. } else {
  630. return "", err
  631. }
  632. }
  633. return rolePurview.PurviewIds, nil
  634. }
  635. func GetRoleFuncPurview(roleID int64) (rolePurviews models.SgjUserRoleFuncPurview, err error) {
  636. err = readUserDb.Where("role_id = ?", roleID).First(&rolePurviews).Error
  637. return
  638. }
  639. func SaveRoleFuncPurview(purview *models.SgjUserRoleFuncPurview) (err error) {
  640. err = writeUserDb.Save(purview).Error
  641. return
  642. }
  643. func GetUserAllRole(org_id int64, admin_user_id int64) (role models.App_Role, err error) {
  644. err = readUserDb.Model(&models.App_Role{}).Where("status = 1 AND org_id = ? AND admin_user_id = ?", org_id, admin_user_id).First(&role).Error
  645. return
  646. }
  647. func GetAllFunctionPurview() (purview []models.SgjUserOperatePurview, err error) {
  648. err = readUserDb.Model(&models.SgjUserOperatePurview{}).Where("status = 1 ").Order("number asc").Find(&purview).Error
  649. return
  650. }
  651. func FindErrorMsgByStr(str string) (string, error) {
  652. var purview models.SgjUserOperatePurview
  653. var err error
  654. err = readUserDb.Model(&models.SgjUserOperatePurview{}).Where("find_in_set(?, urlfor)", str).First(&purview).Error
  655. return purview.ErrorMsg, err
  656. }
  657. func GetNewAllOrgValidRoles(orgID int64) ([]*models.Role, error) {
  658. var roles []*models.Role
  659. db := readUserDb.Model(models.Org{})
  660. err := db.Where("org_id = ? AND status = 1", orgID).
  661. Order("number desc,ctime").
  662. Find(&roles).
  663. Error
  664. if err != nil {
  665. if err == gorm.ErrRecordNotFound {
  666. return make([]*models.Role, 0), nil
  667. } else {
  668. return nil, err
  669. }
  670. }
  671. for _, item := range roles {
  672. var total int64
  673. readUserDb.Model(&models.App_Role{}).Where("org_id = ? AND find_in_set(?, role_ids)", orgID, item.Id).Count(&total)
  674. item.StaffNumber = total
  675. }
  676. return roles, nil
  677. }
  678. func FindRoleRecordByRoleName(name string, org_id int64) (total int64) {
  679. readUserDb.Model(&models.Role{}).Where("status = 1 AND role_name = ? AND org_id = ?", name, org_id).Count(&total)
  680. return
  681. }
  682. func FindAllFuncPurview(ids []string) (purview []*models.SgjUserOperatePurview, err error) {
  683. err = readUserDb.Model(&models.SgjUserOperatePurview{}).Where("status = 1 AND id in (?)", ids).Order("number asc").Find(&purview).Error
  684. return
  685. }
  686. func GetSystemPurview() (purview []*models.Purview, err error) {
  687. err = readUserDb.Model(&models.Purview{}).Where("status = 1 AND module = 3 AND super_admin_exclusive = 1").Find(&purview).Error
  688. return
  689. }