role_service.go 13KB

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