role_service.go 23KB

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