role_service.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package service
  2. import (
  3. "time"
  4. "XT_New/models"
  5. "XT_New/utils"
  6. "github.com/jinzhu/gorm"
  7. )
  8. func GetRoles(orgID int64, appID int64, page int, count int) ([]*models.Role, int, error) {
  9. var roles []*models.Role
  10. var totalCount int
  11. err := readUserDb.Model(&models.Role{}).
  12. Where("org_id = ? AND app_id = ?", orgID, appID).
  13. Count(&totalCount).
  14. Order("ctime asc").Limit(count).Offset((page - 1) * count).
  15. Find(&roles).
  16. Error
  17. if err != nil {
  18. if err == gorm.ErrRecordNotFound {
  19. return make([]*models.Role, 0), 0, nil
  20. } else {
  21. return nil, 0, err
  22. }
  23. }
  24. return roles, totalCount, nil
  25. }
  26. func GetAppRole(orgID int64, appID int64, adminUserID int64) (*models.App_Role, error) {
  27. var appRole models.App_Role
  28. err := readUserDb.Model(models.App_Role{}).Where("org_id = ? and app_id = ? and admin_user_id = ?", orgID, appID, adminUserID).First(&appRole).Error
  29. if err != nil {
  30. if err == gorm.ErrRecordNotFound {
  31. return nil, nil
  32. } else {
  33. return nil, err
  34. }
  35. }
  36. return &appRole, nil
  37. }
  38. func CreateRole(adminUserID int64, orgID int64, appID int64, name string, intro string) (*models.Role, error) {
  39. role := models.Role{
  40. RoleName: name,
  41. RoleIntro: intro,
  42. Creator: adminUserID,
  43. OrgId: orgID,
  44. AppId: appID,
  45. IsSuperAdmin: false,
  46. Status: 1,
  47. CreateTime: time.Now().Unix(),
  48. ModifyTime: time.Now().Unix(),
  49. }
  50. tx := writeUserDb.Begin()
  51. if err := tx.Create(&role).Error; err != nil {
  52. tx.Rollback()
  53. return nil, err
  54. }
  55. tx.Commit()
  56. return &role, nil
  57. }
  58. func GetRoleByRoleID(roleID int64) (*models.Role, error) {
  59. var role models.Role
  60. err := readUserDb.Model(&models.Role{}).Where("id = ?", roleID).First(&role).Error
  61. if err != nil {
  62. if err == gorm.ErrRecordNotFound {
  63. return nil, nil
  64. }
  65. return nil, err
  66. } else {
  67. return &role, nil
  68. }
  69. }
  70. func ModifyRole(role *models.Role) error {
  71. tx := writeUserDb.Begin()
  72. if err := tx.Save(role).Error; err != nil {
  73. tx.Rollback()
  74. return err
  75. }
  76. return tx.Commit().Error
  77. }
  78. // 拥有xx角色的管理员的数量
  79. func RoleAdminUserCount(orgID int64, appID int64, roleID int64) (int, error) {
  80. var count int
  81. err := readUserDb.Model(models.App_Role{}).
  82. Where("org_id = ? AND app_id = ? AND role_id = ? AND status = 1", orgID, appID, roleID).
  83. Count(&count).
  84. Error
  85. if err != nil {
  86. return 0, err
  87. } else {
  88. return count, nil
  89. }
  90. }
  91. type AdminUserManageViewModel struct {
  92. AdminUserId int `gorm:"admin_user_id" json:"user_id"`
  93. UserName string `gorm:"user_name" json:"user_name"`
  94. RoleName string `gorm:"role_name" json:"role_name"`
  95. UserTitle int `gorm:"user_title" json:"user_title"`
  96. Ip string `gorm:"ip" json:"ip"`
  97. Ctime int64 `gorm:"ctime" json:"last_login_time"`
  98. Status int `gorm:"status" json:"status"`
  99. // LastLoginTimeStr string `gorm:"-" json:"last_login_time_formatted"`
  100. TitleName string `gorm:"-" json:"title_name"`
  101. }
  102. func GetAdminUsersAndLoginInfo(orgID int64, appID int64, page int, count int) ([]*AdminUserManageViewModel, int, error) {
  103. if count <= 0 {
  104. return []*AdminUserManageViewModel{}, 0, nil
  105. }
  106. if page < 1 {
  107. page = 1
  108. }
  109. var viewModels []*AdminUserManageViewModel = make([]*AdminUserManageViewModel, 0)
  110. rows, err := readUserDb.Raw("SELECT u_a_r.admin_user_id, u_a_r.user_name, u_r.role_name, u_a_r.user_title, u_l.ip, u_l.ctime, u_a_r.status FROM sgj_user_admin_role AS u_a_r INNER JOIN sgj_user_role AS u_r ON u_a_r.org_id = u_r.org_id AND u_a_r.app_id = u_r.app_id AND u_r.id = u_a_r.role_id 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()
  111. defer rows.Close()
  112. if err != nil {
  113. if err == gorm.ErrRecordNotFound {
  114. return viewModels, 0, nil
  115. } else {
  116. return nil, 0, err
  117. }
  118. }
  119. for rows.Next() {
  120. var viewModel AdminUserManageViewModel
  121. readUserDb.ScanRows(rows, &viewModel)
  122. title, _ := models.UserTitle[viewModel.UserTitle]
  123. viewModel.TitleName = title
  124. // if viewModel.Ctime == 0 {
  125. // viewModel.LastLoginTimeStr = ""
  126. // } else {
  127. // loginTime := time.Unix(viewModel.Ctime, 0)
  128. // viewModel.LastLoginTimeStr = loginTime.Format("2006-01-02 15:04")
  129. // }
  130. viewModels = append(viewModels, &viewModel)
  131. }
  132. total := 0
  133. 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)
  134. return viewModels, total, nil
  135. }
  136. func GetValidRoleCount(orgID int64, appID int64, superAdminUserID int64) (int, error) {
  137. var count int
  138. err := readUserDb.Model(models.Role{}).
  139. Where("org_id = ? AND app_id = ? AND creator = ? AND is_super_admin = 0 AND status = 1", orgID, appID, superAdminUserID).
  140. Count(&count).
  141. Error
  142. if err != nil {
  143. return 0, err
  144. } else {
  145. return count, nil
  146. }
  147. }
  148. func GetAllValidRoles(orgID int64, appID int64) ([]*models.Role, error) {
  149. var roles []*models.Role
  150. err := readUserDb.Model(models.Role{}).
  151. Where("org_id = ? AND app_id = ? AND status = 1", orgID, appID).
  152. Order("ctime asc").
  153. Find(&roles).
  154. Error
  155. if err != nil {
  156. if err == gorm.ErrRecordNotFound {
  157. return make([]*models.Role, 0), nil
  158. } else {
  159. return nil, err
  160. }
  161. }
  162. return roles, nil
  163. }
  164. func IsRoleExist(orgID int64, appID int64, roleID int64) (bool, error) {
  165. var count int
  166. err := readUserDb.Model(models.Role{}).Where("org_id = ? AND app_id = ? AND id = ? AND status = 1", orgID, appID, roleID).Count(&count).Error
  167. if err != nil {
  168. return false, err
  169. } else {
  170. return count > 0, nil
  171. }
  172. }
  173. type AdminUserEditViewModel struct {
  174. AdminUserId int64 `gorm:"admin_user_id" json:"user_id"`
  175. UserName string `gorm:"user_name" json:"user_name"`
  176. Mobile string `gorm:"mobile" json:"mobile"`
  177. UserType int `gorm:"user_type" json:"user_type"`
  178. UserTitle int `gorm:"user_title" json:"user_title"`
  179. Intro string `gorm:"intro" json:"intro"`
  180. RoleId int64 `gorm:"role_id" json:"role_id"`
  181. }
  182. func GetGeneralAdminUser(orgID int64, appID int64, user_id int64) (*AdminUserEditViewModel, error) {
  183. 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()
  184. defer rows.Close()
  185. if err != nil {
  186. if err == gorm.ErrRecordNotFound {
  187. return nil, nil
  188. } else {
  189. return nil, err
  190. }
  191. }
  192. if rows.Next() {
  193. var viewModel AdminUserEditViewModel
  194. err := readUserDb.ScanRows(rows, &viewModel)
  195. if err != nil {
  196. return nil, err
  197. } else {
  198. return &viewModel, nil
  199. }
  200. }
  201. return nil, nil
  202. }
  203. func SaveAppRole(appRole *models.App_Role) error {
  204. tx := writeUserDb.Begin()
  205. if err := tx.Model(&models.App_Role{}).Save(appRole).Error; err != nil {
  206. tx.Rollback()
  207. return err
  208. }
  209. return tx.Commit().Error
  210. }
  211. func IsMobileDidUsedAtApp(orgID int64, appID int64, mobile string) (bool, error) {
  212. var count int
  213. 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()
  214. defer rows.Close()
  215. if err != nil {
  216. if err == gorm.ErrRecordNotFound {
  217. return false, nil
  218. } else {
  219. return true, err
  220. }
  221. }
  222. if rows.Next() {
  223. rows.Scan(&count)
  224. return count > 0, nil
  225. }
  226. return true, nil
  227. }
  228. func IsUserSuperAdminWithMobile(mobile string) (bool, error) {
  229. var user models.AdminUser
  230. err := readUserDb.Where("mobile = ?", mobile).First(&user).Error
  231. if err != nil {
  232. if err == gorm.ErrRecordNotFound {
  233. return false, nil
  234. } else {
  235. return false, err
  236. }
  237. }
  238. return user.IsSuperAdmin, nil
  239. }
  240. func CreateGeneralAdminUser(orgID int64, appID int64, mobile string, name string, userType int, userTitle int, intro string, roleID int64) (*models.AdminUser, string, error) {
  241. now := time.Now().Unix()
  242. tx := writeUserDb.Begin()
  243. var adminUser models.AdminUser
  244. err := readUserDb.Where("mobile = ? AND status = 1", mobile).First(&adminUser).Error
  245. password := ""
  246. if err != nil {
  247. if err != gorm.ErrRecordNotFound {
  248. return nil, "", err
  249. } else {
  250. password = utils.RandomNumberString(6)
  251. adminUser.Mobile = mobile
  252. adminUser.Password = utils.String2md5(password)
  253. adminUser.IsSuperAdmin = false
  254. adminUser.Status = 1
  255. adminUser.CreateTime = now
  256. adminUser.ModifyTime = now
  257. if createErr := tx.Create(&adminUser).Error; createErr != nil {
  258. tx.Rollback()
  259. return nil, "", createErr
  260. }
  261. }
  262. }
  263. app_role := models.App_Role{
  264. AdminUserId: adminUser.Id,
  265. OrgId: orgID,
  266. AppId: appID,
  267. RoleId: roleID,
  268. Avatar: "",
  269. UserName: name,
  270. Intro: intro,
  271. UserType: int8(userType),
  272. UserTitle: int8(userTitle),
  273. Status: 1,
  274. CreateTime: now,
  275. ModifyTime: now,
  276. }
  277. if createApp_RoleErr := tx.Create(&app_role).Error; createApp_RoleErr != nil {
  278. tx.Rollback()
  279. return nil, "", createApp_RoleErr
  280. }
  281. tx.Commit()
  282. return &adminUser, password, nil
  283. }
  284. type PurviewTreeViewModel struct {
  285. ID int64 `json:"id"`
  286. PID int64 `json:"pid"`
  287. Name string `json:"name"`
  288. Childs []*PurviewTreeViewModel `json:"childs"`
  289. }
  290. func GetAllGeneralPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
  291. var originPurviews []*models.Purview
  292. getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = 3 AND status = 1 AND super_admin_exclusive = 0").Order("listorder asc").Order("id asc").Find(&originPurviews).Error
  293. if getPurviewErr != nil {
  294. return nil, getPurviewErr
  295. }
  296. // 加工这些规则:树形化
  297. purviewVMs := make([]*PurviewTreeViewModel, 0)
  298. pid_childs := make(map[int64][]*PurviewTreeViewModel)
  299. for _, purview := range originPurviews {
  300. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  301. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  302. pvm := &PurviewTreeViewModel{
  303. ID: purview.Id,
  304. PID: purview.Parentid,
  305. Name: purview.Name,
  306. }
  307. if purview.Parentid == 0 {
  308. purviewVMs = append(purviewVMs, pvm)
  309. } else {
  310. childs := pid_childs[purview.Parentid]
  311. if childs == nil {
  312. childs = make([]*PurviewTreeViewModel, 0)
  313. }
  314. childs = append(childs, pvm)
  315. pid_childs[purview.Parentid] = childs
  316. }
  317. }
  318. for _, vm := range purviewVMs {
  319. vm.Childs = pid_childs[vm.ID]
  320. }
  321. return purviewVMs, nil
  322. }
  323. func GetRolePurviewIds(roleID int64) (string, error) {
  324. var rolePurview models.RolePurview
  325. err := readUserDb.Where("role_id = ?", roleID).First(&rolePurview).Error
  326. if err != nil {
  327. if err == gorm.ErrRecordNotFound {
  328. return "", nil
  329. } else {
  330. return "", err
  331. }
  332. }
  333. return rolePurview.PurviewIds, nil
  334. }
  335. func SaveRolePurviewIds(orgID int64, appID int64, roleID int64, purviewIds string) error {
  336. var rolePurview models.RolePurview
  337. getRPErr := readUserDb.Where("org_id = ? AND app_id = ? AND role_id = ?", orgID, appID, roleID).First(&rolePurview).Error
  338. if getRPErr != nil {
  339. if getRPErr == gorm.ErrRecordNotFound {
  340. rolePurview = models.RolePurview{
  341. RoleId: roleID,
  342. OrgId: orgID,
  343. AppId: appID,
  344. Status: 1,
  345. CreateTime: time.Now().Unix(),
  346. }
  347. } else {
  348. return getRPErr
  349. }
  350. }
  351. rolePurview.PurviewIds = purviewIds
  352. rolePurview.ModifyTime = time.Now().Unix()
  353. tx := writeUserDb.Begin()
  354. if err := tx.Save(&rolePurview).Error; err != nil {
  355. tx.Rollback()
  356. return err
  357. }
  358. return tx.Commit().Error
  359. }
  360. func ModifyAdminUserInfo(adminUserID int64, orgID int64, appID int64, name string, avatar string, newPassword string) error {
  361. tx := writeUserDb.Begin()
  362. 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
  363. if editInfoErr != nil {
  364. tx.Rollback()
  365. return editInfoErr
  366. }
  367. if len(newPassword) > 0 {
  368. editPwdErr := tx.Exec("update sgj_user_admin set password = ?, mtime = ? where id = ?", newPassword, time.Now().Unix(), adminUserID).Error
  369. if editPwdErr != nil {
  370. tx.Rollback()
  371. return editPwdErr
  372. }
  373. }
  374. tx.Commit()
  375. return nil
  376. }