role_controller.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package controllers
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "XT_New/enums"
  7. "XT_New/models"
  8. "XT_New/service"
  9. "github.com/astaxie/beego"
  10. )
  11. func RoleAPIControllerRegistRouters() {
  12. beego.Router("/api/roles", &RoleAPIController{}, "get:GetRoles")
  13. beego.Router("/api/role/create", &RoleAPIController{}, "post:CreateRole")
  14. beego.Router("/api/role/modify", &RoleAPIController{}, "post:ModifyRole")
  15. beego.Router("/api/role/setstatus", &RoleAPIController{}, "post:ModifyRoleStatus")
  16. beego.Router("/role/purview/editinit", &RoleAPIController{}, "get:EditPurviewInitData")
  17. beego.Router("/role/purview/edit", &RoleAPIController{}, "post:EditPurview")
  18. beego.Router("/api/adminmain", &RoleAPIController{}, "get:AdminMainView")
  19. beego.Router("/api/admins", &RoleAPIController{}, "get:Admins")
  20. beego.Router("/api/admin/addinit", &RoleAPIController{}, "get:AddAdminInitData")
  21. beego.Router("/api/admin/add", &RoleAPIController{}, "post:AddAdmin")
  22. beego.Router("/api/admin/editinit", &RoleAPIController{}, "get:EditAdminInitData")
  23. beego.Router("/api/admin/edit", &RoleAPIController{}, "post:EditAdmin")
  24. beego.Router("/api/admin/setstatus", &RoleAPIController{}, "post:AdminSetStatus")
  25. beego.Router("/api/admin/specialpermission/initdata", &RoleAPIController{}, "get:SpecialPermissionInitData")
  26. beego.Router("/api/admin/specialpermission/dialysisrecord/submit", &RoleAPIController{}, "post:SubmitDialysisRecordPermission")
  27. }
  28. type RoleAPIController struct {
  29. BaseAuthAPIController
  30. }
  31. // /api/roles [get]
  32. // @param page?:int
  33. func (this *RoleAPIController) GetRoles() {
  34. page, _ := this.GetInt("page")
  35. adminUserInfo := this.GetAdminUserInfo()
  36. //beego.Alert(adminUserInfo.AdminUser)
  37. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  38. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  39. return
  40. }
  41. if page <= 0 {
  42. page = 1
  43. }
  44. roles, total, getRoleErr := service.GetRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
  45. if getRoleErr != nil {
  46. //beego.Error("获取角色列表失败:", getRoleErr)
  47. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  48. } else {
  49. this.ServeSuccessJSON(map[string]interface{}{
  50. "roles": roles,
  51. "total_count": total,
  52. })
  53. }
  54. }
  55. // /api/role/create [post]
  56. // @param name:string
  57. // @param intro:string
  58. func (this *RoleAPIController) CreateRole() {
  59. name := this.GetString("name")
  60. intro := this.GetString("intro")
  61. if len(name) == 0 || len(intro) == 0 {
  62. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  63. return
  64. }
  65. adminUserInfo := this.GetAdminUserInfo()
  66. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  67. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  68. return
  69. }
  70. role, createErr := service.CreateRole(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, intro)
  71. if createErr != nil {
  72. //beego.Error("创建角色失败:", createErr)
  73. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  74. } else {
  75. this.ServeSuccessJSON(map[string]interface{}{
  76. "id": role.Id,
  77. "name": role.RoleName,
  78. "intro": role.RoleIntro,
  79. "status": role.Status,
  80. })
  81. }
  82. }
  83. // /api/role/modify
  84. // @param role_id:int
  85. // @param name:string
  86. // @param intro:string
  87. func (this *RoleAPIController) ModifyRole() {
  88. roleID, _ := this.GetInt64("role_id")
  89. name := this.GetString("name")
  90. intro := this.GetString("intro")
  91. if roleID <= 0 || len(name) == 0 || len(intro) == 0 {
  92. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  93. return
  94. }
  95. adminUserInfo := this.GetAdminUserInfo()
  96. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  97. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  98. return
  99. }
  100. role, getRoleErr := service.GetRoleByRoleID(roleID)
  101. if getRoleErr != nil {
  102. //beego.Error("获取角色失败:", getRoleErr)
  103. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  104. return
  105. } else if role == nil {
  106. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  107. return
  108. }
  109. role.RoleName = name
  110. role.RoleIntro = intro
  111. role.ModifyTime = time.Now().Unix()
  112. saveErr := service.ModifyRole(role)
  113. if saveErr != nil {
  114. //beego.Error("修改角色失败:", role.Id, saveErr)
  115. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  116. } else {
  117. this.ServeSuccessJSON(nil)
  118. }
  119. }
  120. // /api/role/setstatus
  121. // @param role_id:int
  122. // @param enable:bool
  123. func (this *RoleAPIController) ModifyRoleStatus() {
  124. roleID, _ := this.GetInt64("role_id")
  125. enable, _ := this.GetBool("enable")
  126. if roleID <= 0 {
  127. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  128. return
  129. }
  130. adminUserInfo := this.GetAdminUserInfo()
  131. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  132. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  133. return
  134. }
  135. role, getRoleErr := service.GetRoleByRoleID(roleID)
  136. if getRoleErr != nil {
  137. //beego.Error("获取角色失败:", getRoleErr)
  138. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  139. return
  140. } else if role == nil {
  141. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  142. return
  143. }
  144. if enable == false {
  145. if count, _ := service.RoleAdminUserCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
  146. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
  147. return
  148. }
  149. }
  150. if enable {
  151. role.Status = 1
  152. } else {
  153. role.Status = 2
  154. }
  155. role.ModifyTime = time.Now().Unix()
  156. saveErr := service.ModifyRole(role)
  157. if saveErr != nil {
  158. //beego.Error("修改角色失败:", role.Id, saveErr)
  159. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  160. } else {
  161. this.ServeSuccessJSON(nil)
  162. }
  163. }
  164. // /role/purview/editinit [get]
  165. // @param role_id:int
  166. func (this *RoleAPIController) EditPurviewInitData() {
  167. adminUserInfo := this.GetAdminUserInfo()
  168. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  169. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  170. return
  171. }
  172. roleId, _ := this.GetInt64("role_id")
  173. if roleId <= 0 {
  174. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  175. return
  176. }
  177. purviews, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed()
  178. if getPurviewsErr != nil {
  179. //beego.Error("获取所有权限时出错:", getPurviewsErr)
  180. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  181. return
  182. }
  183. rolePurviewIdStr, getRPIdsErr := service.GetRolePurviewIds(roleId)
  184. if getRPIdsErr != nil {
  185. //beego.Error("获取角色的权限时出错:", getRPIdsErr)
  186. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  187. return
  188. }
  189. this.ServeSuccessJSON(map[string]interface{}{
  190. "purviews": purviews,
  191. "role_purview_ids": rolePurviewIdStr,
  192. })
  193. }
  194. // /role/purview/edit [post]
  195. // @param role_id:int
  196. // @param purview_ids:string
  197. func (this *RoleAPIController) EditPurview() {
  198. adminUserInfo := this.GetAdminUserInfo()
  199. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  200. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  201. return
  202. }
  203. roleId, _ := this.GetInt64("role_id")
  204. purviewIds := this.GetString("purview_ids")
  205. if roleId <= 0 {
  206. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  207. return
  208. }
  209. err := service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
  210. if err != nil {
  211. //beego.Error("设置角色的权限时出错:", err)
  212. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  213. } else {
  214. this.ServeSuccessJSON(nil)
  215. }
  216. }
  217. // func (this *RoleAPIController) doesUserHaveAccess(userID int64) bool {
  218. // adminUser, getAdminUserErr := service.GetAdminUserByUserID(userID)
  219. // if getAdminUserErr != nil {
  220. // beego.Error("获取用户信息失败:%v", getAdminUserErr)
  221. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  222. // return false
  223. // } else if adminUser == nil {
  224. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  225. // return false
  226. // } else if adminUser.Status == 2 {
  227. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeUserWasForbidden)
  228. // return false
  229. // } else if adminUser.IsSuperAdmin == false {
  230. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  231. // return false
  232. // }
  233. // return true
  234. // }
  235. // func (this *RoleAPIController) isAppRoleExist(orgID int64, appID int64, userID int64) bool {
  236. // appRole, getAppRoleErr := service.GetAppRole(orgID, appID, userID)
  237. // if getAppRoleErr != nil {
  238. // beego.Error("检查用户和机构应用对应关系时失败:%v", getAppRoleErr)
  239. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  240. // return false
  241. // } else if appRole == nil {
  242. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  243. // return false
  244. // }
  245. // return true
  246. // }
  247. // /api/adminmain [get]
  248. func (this *RoleAPIController) AdminMainView() {
  249. adminUserInfo := this.GetAdminUserInfo()
  250. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  251. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  252. return
  253. }
  254. viewModels, total, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, 1, 10)
  255. if getAdminsErr != nil {
  256. //beego.Error("获取管理员列表失败:", getAdminsErr)
  257. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  258. return
  259. }
  260. existRoleCount, _ := service.GetValidRoleCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserInfo.AdminUser.Id)
  261. this.ServeSuccessJSON(map[string]interface{}{
  262. "admins": viewModels,
  263. "total_count": total,
  264. "is_exist_role": existRoleCount > 0,
  265. })
  266. }
  267. // /api/admins [get]
  268. // @param page?:int
  269. func (this *RoleAPIController) Admins() {
  270. adminUserInfo := this.GetAdminUserInfo()
  271. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  272. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  273. return
  274. }
  275. page, _ := this.GetInt("page")
  276. viewModels, total, getAdminsErr := service.GetAdminUsersAndLoginInfo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, page, 10)
  277. if getAdminsErr != nil {
  278. //beego.Error("获取管理员列表失败:", getAdminsErr)
  279. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  280. } else {
  281. this.ServeSuccessJSON(map[string]interface{}{
  282. "admins": viewModels,
  283. "total_count": total,
  284. })
  285. }
  286. }
  287. // /api/admin/addinit [get]
  288. func (this *RoleAPIController) AddAdminInitData() {
  289. adminUserInfo := this.GetAdminUserInfo()
  290. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  291. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  292. return
  293. }
  294. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  295. if getRoleErr != nil {
  296. //beego.Error("获取所有角色失败:", getRoleErr)
  297. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  298. return
  299. }
  300. redisClient := service.RedisClient()
  301. defer redisClient.Close()
  302. qntoken, _ := redisClient.Get("qn_token").Result()
  303. this.ServeSuccessJSON(map[string]interface{}{
  304. "roles": roles,
  305. "qntoken": qntoken,
  306. })
  307. }
  308. // /api/admin/add [post]
  309. // @param mobile:string
  310. // @param name:string
  311. // @param type:int 管理员类型:2.医生 3.护士 4.运营
  312. // @param title:int 用户职称(1.医士;2.医师;3.住院医师;4.主治医师;5.副主任医师;6.主任医师;7.护士;8.护师;9.主管护师;10.副主任护师;11.主任护师;12.运营专员;13.运营主管)
  313. // @param role:int
  314. // @param intro?:string
  315. func (this *RoleAPIController) AddAdmin() {
  316. adminUserInfo := this.GetAdminUserInfo()
  317. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  318. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  319. return
  320. }
  321. mobile := this.GetString("mobile")
  322. name := this.GetString("name")
  323. userType, _ := this.GetInt("type")
  324. userTitle, _ := this.GetInt("title")
  325. roleId, _ := this.GetInt64("role")
  326. //intro := this.GetString("intro")
  327. _, titleExist := models.UserTitle[userTitle]
  328. if len(mobile) == 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  329. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  330. return
  331. }
  332. isRoleExist, getRoleErr := service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  333. if getRoleErr != nil {
  334. //beego.Error("查询角色是否存在时失败:", getRoleErr)
  335. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  336. return
  337. }
  338. if !isRoleExist {
  339. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  340. return
  341. }
  342. // 判断该应用是否已存在该手机号
  343. if isMobileDidUsed, err := service.IsMobileDidUsedAtApp(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile); err != nil {
  344. //beego.Error("查询用户是否已被添加为管理员时失败:", err)
  345. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  346. return
  347. } else {
  348. if isMobileDidUsed {
  349. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileDidUsedInApp)
  350. return
  351. }
  352. }
  353. if isSuperAdmin, err := service.IsUserSuperAdminWithMobile(mobile); err != nil {
  354. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileNotExit)
  355. return
  356. } else {
  357. if isSuperAdmin {
  358. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleMobileIsSuperAdmin)
  359. return
  360. }
  361. }
  362. //TODO
  363. //_, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId)
  364. //if createErr != nil {
  365. // //beego.Error("创建管理员失败:", createErr)
  366. // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  367. // return
  368. //
  369. //} else {
  370. // //beego.Trace("用户密码:", password)
  371. // // 发送短信通知这个手机号
  372. // sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  373. // if sendSMSErr != nil {
  374. // //beego.Error("发送邀请短信失败:%v", sendSMSErr)
  375. // }
  376. //
  377. // this.ServeSuccessJSON(nil)
  378. // return
  379. //}
  380. }
  381. // /api/admin/editinit [get]
  382. // @param uid:int
  383. func (this *RoleAPIController) EditAdminInitData() {
  384. adminUserInfo := this.GetAdminUserInfo()
  385. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  386. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  387. return
  388. }
  389. admin_user_id, _ := this.GetInt64("uid")
  390. if admin_user_id <= 0 {
  391. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  392. return
  393. }
  394. adminUserViewModel, getInfoErr := service.GetGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
  395. if getInfoErr != nil {
  396. //beego.Error("获取管理员信息失败:", getInfoErr)
  397. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  398. return
  399. }
  400. if adminUserViewModel == nil {
  401. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  402. return
  403. }
  404. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  405. if getRoleErr != nil {
  406. //beego.Error("获取所有角色失败:", getRoleErr)
  407. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  408. return
  409. }
  410. redisClient := service.RedisClient()
  411. defer redisClient.Close()
  412. qntoken, _ := redisClient.Get("qn_token").Result()
  413. this.ServeSuccessJSON(map[string]interface{}{
  414. "admin": adminUserViewModel,
  415. "roles": roles,
  416. "qntoken": qntoken,
  417. })
  418. }
  419. // /api/admin/edit [post]
  420. // @param uid:int
  421. // @param name:string
  422. // @param type:int
  423. // @param title:int
  424. // @param role:int
  425. // @param intro?:string
  426. func (this *RoleAPIController) EditAdmin() {
  427. adminUserInfo := this.GetAdminUserInfo()
  428. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  429. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  430. return
  431. }
  432. adminUserId, _ := this.GetInt64("uid")
  433. name := this.GetString("name")
  434. userType, _ := this.GetInt("type")
  435. userTitle, _ := this.GetInt("title")
  436. roleId, _ := this.GetInt64("role")
  437. intro := this.GetString("intro")
  438. _, titleExist := models.UserTitle[userTitle]
  439. if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  440. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  441. return
  442. }
  443. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
  444. if getAppRoleErr != nil {
  445. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  446. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  447. return
  448. }
  449. if appRole == nil {
  450. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  451. return
  452. }
  453. isRoleExist, getRoleErr := service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  454. if getRoleErr != nil {
  455. //beego.Error("查询角色是否存在时失败:", getRoleErr)
  456. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  457. return
  458. }
  459. if !isRoleExist {
  460. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  461. return
  462. }
  463. appRole.UserName = name
  464. appRole.UserType = int8(userType)
  465. appRole.UserTitle = int8(userTitle)
  466. appRole.RoleId = roleId
  467. appRole.Intro = intro
  468. appRole.ModifyTime = time.Now().Unix()
  469. saveErr := service.SaveAppRole(appRole)
  470. if saveErr != nil {
  471. //beego.Error("修改App_Role失败:", saveErr)
  472. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  473. } else {
  474. this.ServeSuccessJSON(nil)
  475. }
  476. }
  477. // /api/admin/setstatus [post]
  478. // @param uid:int
  479. // @param enable:bool
  480. func (this *RoleAPIController) AdminSetStatus() {
  481. adminUserInfo := this.GetAdminUserInfo()
  482. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  483. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  484. return
  485. }
  486. userID, _ := this.GetInt64("uid")
  487. if userID <= 0 {
  488. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  489. return
  490. }
  491. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
  492. if getAppRoleErr != nil {
  493. //beego.Error("查询管理员信息失败:", getAppRoleErr)
  494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  495. return
  496. } else if appRole == nil {
  497. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  498. return
  499. }
  500. enable, _ := this.GetBool("enable")
  501. if enable == true {
  502. if roleEnable, _ := service.IsRoleExist(appRole.OrgId, appRole.AppId, appRole.RoleId); roleEnable == false {
  503. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  504. return
  505. }
  506. }
  507. if enable {
  508. appRole.Status = 1
  509. } else {
  510. appRole.Status = 0
  511. }
  512. appRole.ModifyTime = time.Now().Unix()
  513. saveErr := service.SaveAppRole(appRole)
  514. if saveErr != nil {
  515. //beego.Error("保存AppRole失败:", saveErr)
  516. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  517. } else {
  518. this.ServeSuccessJSON(nil)
  519. }
  520. }
  521. // /api/admin/specialpermission/initdata [get]
  522. func (this *RoleAPIController) SpecialPermissionInitData() {
  523. adminUserInfo := this.GetAdminUserInfo()
  524. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  525. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  526. return
  527. }
  528. adminUsers, getAdminUsersErr := service.GetAllGeneralAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  529. if getAdminUsersErr != nil {
  530. this.ErrorLog("获取所有普通用户失败:%v", getAdminUsersErr)
  531. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  532. return
  533. }
  534. headNurses, getAllHeadNursesErr := service.GetAllValidAdminUsersWithSpecialPermission(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  535. if getAllHeadNursesErr != nil {
  536. this.ErrorLog("获取所有拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  537. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  538. return
  539. }
  540. this.ServeSuccessJSON(map[string]interface{}{
  541. "users": adminUsers,
  542. "head_nurses": headNurses,
  543. })
  544. }
  545. // /api/admin/specialpermission/dialysisrecord/submit [post]
  546. // @param ids:string ("1,2,5")
  547. func (this *RoleAPIController) SubmitDialysisRecordPermission() {
  548. adminUserInfo := this.GetAdminUserInfo()
  549. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  550. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  551. return
  552. }
  553. idsString := this.GetString("ids")
  554. if len(idsString) == 0 {
  555. // 取消所有用户的护士长权限
  556. cancelErr := service.CancelAllSpecialPermissionAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  557. if cancelErr != nil {
  558. this.ErrorLog("取消所有用户的护士长权限失败:%v", cancelErr)
  559. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  560. return
  561. } else {
  562. this.ServeSuccessJSON(nil)
  563. return
  564. }
  565. } else {
  566. ids := make([]int64, 0)
  567. idStrs := strings.Split(idsString, ",")
  568. for _, idStr := range idStrs {
  569. id, parseErr := strconv.Atoi(idStr)
  570. if parseErr != nil {
  571. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  572. return
  573. }
  574. ids = append(ids, int64(id))
  575. }
  576. headNurses, getAllHeadNursesErr := service.GetAllSpecialPermissionAdminUsersWithoutStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  577. if getAllHeadNursesErr != nil {
  578. this.ErrorLog("获取所有拥有或曾拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  579. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  580. return
  581. }
  582. cancelList := make([]*models.AdminUserSpecialPermission, 0)
  583. addList := make([]*models.AdminUserSpecialPermission, 0)
  584. for _, id := range ids {
  585. exit := false
  586. for _, headNurse := range headNurses {
  587. if headNurse.AdminUserID == id {
  588. exit = true
  589. if headNurse.Status != 1 {
  590. headNurse.Status = 1
  591. headNurse.ModifyTime = time.Now().Unix()
  592. addList = append(addList, headNurse)
  593. }
  594. break
  595. }
  596. }
  597. if exit == false {
  598. newHeadNurse := &models.AdminUserSpecialPermission{
  599. OrgID: adminUserInfo.CurrentOrgId,
  600. AppID: adminUserInfo.CurrentAppId,
  601. AdminUserID: id,
  602. Permission: int64(models.SpecialPermissionTypeHeadNurse),
  603. Status: 1,
  604. CreateTime: time.Now().Unix(),
  605. ModifyTime: time.Now().Unix(),
  606. }
  607. addList = append(addList, newHeadNurse)
  608. }
  609. }
  610. for _, headNurse := range headNurses {
  611. cancel := true
  612. for _, willAdd := range addList {
  613. if willAdd.AdminUserID == headNurse.AdminUserID {
  614. cancel = false
  615. break
  616. }
  617. }
  618. if cancel {
  619. headNurse.Status = 0
  620. headNurse.ModifyTime = time.Now().Unix()
  621. cancelList = append(cancelList, headNurse)
  622. }
  623. }
  624. addErr := service.BatchSaveSpecialPermissionAdminUsers(addList)
  625. if addErr != nil {
  626. this.ErrorLog("授权失败:%v", addErr)
  627. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  628. return
  629. }
  630. cancelErr := service.BatchSaveSpecialPermissionAdminUsers(cancelList)
  631. if cancelErr != nil {
  632. this.ErrorLog("取消授权失败:%v", cancelErr)
  633. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  634. return
  635. }
  636. this.ServeSuccessJSON(nil)
  637. }
  638. }