role_controller.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. _, password, createErr := service.CreateGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, mobile, name, userType, userTitle, intro, roleId)
  363. if createErr != nil {
  364. //beego.Error("创建管理员失败:", createErr)
  365. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  366. return
  367. } else {
  368. //beego.Trace("用户密码:", password)
  369. // 发送短信通知这个手机号
  370. sendSMSErr := service.SMSSendInviteMobileToJoinOrgAdmin(name, mobile, password)
  371. if sendSMSErr != nil {
  372. //beego.Error("发送邀请短信失败:%v", sendSMSErr)
  373. }
  374. this.ServeSuccessJSON(nil)
  375. return
  376. }
  377. }
  378. // /api/admin/editinit [get]
  379. // @param uid:int
  380. func (this *RoleAPIController) EditAdminInitData() {
  381. adminUserInfo := this.GetAdminUserInfo()
  382. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  383. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  384. return
  385. }
  386. admin_user_id, _ := this.GetInt64("uid")
  387. if admin_user_id <= 0 {
  388. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  389. return
  390. }
  391. adminUserViewModel, getInfoErr := service.GetGeneralAdminUser(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, admin_user_id)
  392. if getInfoErr != nil {
  393. //beego.Error("获取管理员信息失败:", getInfoErr)
  394. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  395. return
  396. }
  397. if adminUserViewModel == nil {
  398. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  399. return
  400. }
  401. roles, getRoleErr := service.GetAllValidRoles(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  402. if getRoleErr != nil {
  403. //beego.Error("获取所有角色失败:", getRoleErr)
  404. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  405. return
  406. }
  407. redisClient := service.RedisClient()
  408. defer redisClient.Close()
  409. qntoken, _ := redisClient.Get("qn_token").Result()
  410. this.ServeSuccessJSON(map[string]interface{}{
  411. "admin": adminUserViewModel,
  412. "roles": roles,
  413. "qntoken": qntoken,
  414. })
  415. }
  416. // /api/admin/edit [post]
  417. // @param uid:int
  418. // @param name:string
  419. // @param type:int
  420. // @param title:int
  421. // @param role:int
  422. // @param intro?:string
  423. func (this *RoleAPIController) EditAdmin() {
  424. adminUserInfo := this.GetAdminUserInfo()
  425. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  426. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  427. return
  428. }
  429. adminUserId, _ := this.GetInt64("uid")
  430. name := this.GetString("name")
  431. userType, _ := this.GetInt("type")
  432. userTitle, _ := this.GetInt("title")
  433. roleId, _ := this.GetInt64("role")
  434. intro := this.GetString("intro")
  435. _, titleExist := models.UserTitle[userTitle]
  436. if adminUserId <= 0 || len(name) == 0 || (userType != 2 && userType != 3 && userType != 4) || !titleExist || roleId <= 0 {
  437. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  438. return
  439. }
  440. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, adminUserId)
  441. if getAppRoleErr != nil {
  442. //beego.Error("查询管理员信息时失败:", getAppRoleErr)
  443. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  444. return
  445. }
  446. if appRole == nil {
  447. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  448. return
  449. }
  450. isRoleExist, getRoleErr := service.IsRoleExist(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId)
  451. if getRoleErr != nil {
  452. //beego.Error("查询角色是否存在时失败:", getRoleErr)
  453. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  454. return
  455. }
  456. if !isRoleExist {
  457. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  458. return
  459. }
  460. appRole.UserName = name
  461. appRole.UserType = int8(userType)
  462. appRole.UserTitle = int8(userTitle)
  463. appRole.RoleId = roleId
  464. appRole.Intro = intro
  465. appRole.ModifyTime = time.Now().Unix()
  466. saveErr := service.SaveAppRole(appRole)
  467. if saveErr != nil {
  468. //beego.Error("修改App_Role失败:", saveErr)
  469. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  470. } else {
  471. this.ServeSuccessJSON(nil)
  472. }
  473. }
  474. // /api/admin/setstatus [post]
  475. // @param uid:int
  476. // @param enable:bool
  477. func (this *RoleAPIController) AdminSetStatus() {
  478. adminUserInfo := this.GetAdminUserInfo()
  479. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  480. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  481. return
  482. }
  483. userID, _ := this.GetInt64("uid")
  484. if userID <= 0 {
  485. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  486. return
  487. }
  488. appRole, getAppRoleErr := service.GetAppRole(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, userID)
  489. if getAppRoleErr != nil {
  490. //beego.Error("查询管理员信息失败:", getAppRoleErr)
  491. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  492. return
  493. } else if appRole == nil {
  494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserNotExist)
  495. return
  496. }
  497. enable, _ := this.GetBool("enable")
  498. if enable == true {
  499. if roleEnable, _ := service.IsRoleExist(appRole.OrgId, appRole.AppId, appRole.RoleId); roleEnable == false {
  500. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRoleNotExist)
  501. return
  502. }
  503. }
  504. if enable {
  505. appRole.Status = 1
  506. } else {
  507. appRole.Status = 0
  508. }
  509. appRole.ModifyTime = time.Now().Unix()
  510. saveErr := service.SaveAppRole(appRole)
  511. if saveErr != nil {
  512. //beego.Error("保存AppRole失败:", saveErr)
  513. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  514. } else {
  515. this.ServeSuccessJSON(nil)
  516. }
  517. }
  518. // /api/admin/specialpermission/initdata [get]
  519. func (this *RoleAPIController) SpecialPermissionInitData() {
  520. adminUserInfo := this.GetAdminUserInfo()
  521. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  522. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  523. return
  524. }
  525. adminUsers, getAdminUsersErr := service.GetAllGeneralAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId)
  526. if getAdminUsersErr != nil {
  527. this.ErrorLog("获取所有普通用户失败:%v", getAdminUsersErr)
  528. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  529. return
  530. }
  531. headNurses, getAllHeadNursesErr := service.GetAllValidAdminUsersWithSpecialPermission(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  532. if getAllHeadNursesErr != nil {
  533. this.ErrorLog("获取所有拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  534. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  535. return
  536. }
  537. this.ServeSuccessJSON(map[string]interface{}{
  538. "users": adminUsers,
  539. "head_nurses": headNurses,
  540. })
  541. }
  542. // /api/admin/specialpermission/dialysisrecord/submit [post]
  543. // @param ids:string ("1,2,5")
  544. func (this *RoleAPIController) SubmitDialysisRecordPermission() {
  545. adminUserInfo := this.GetAdminUserInfo()
  546. if adminUserInfo.AdminUser.IsSuperAdmin == false {
  547. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  548. return
  549. }
  550. idsString := this.GetString("ids")
  551. if len(idsString) == 0 {
  552. // 取消所有用户的护士长权限
  553. cancelErr := service.CancelAllSpecialPermissionAdminUsers(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  554. if cancelErr != nil {
  555. this.ErrorLog("取消所有用户的护士长权限失败:%v", cancelErr)
  556. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  557. return
  558. } else {
  559. this.ServeSuccessJSON(nil)
  560. return
  561. }
  562. } else {
  563. ids := make([]int64, 0)
  564. idStrs := strings.Split(idsString, ",")
  565. for _, idStr := range idStrs {
  566. id, parseErr := strconv.Atoi(idStr)
  567. if parseErr != nil {
  568. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  569. return
  570. }
  571. ids = append(ids, int64(id))
  572. }
  573. headNurses, getAllHeadNursesErr := service.GetAllSpecialPermissionAdminUsersWithoutStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, models.SpecialPermissionTypeHeadNurse)
  574. if getAllHeadNursesErr != nil {
  575. this.ErrorLog("获取所有拥有或曾拥有护士长特殊权限的用户失败:%v", getAllHeadNursesErr)
  576. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  577. return
  578. }
  579. cancelList := make([]*models.AdminUserSpecialPermission, 0)
  580. addList := make([]*models.AdminUserSpecialPermission, 0)
  581. for _, id := range ids {
  582. exit := false
  583. for _, headNurse := range headNurses {
  584. if headNurse.AdminUserID == id {
  585. exit = true
  586. if headNurse.Status != 1 {
  587. headNurse.Status = 1
  588. headNurse.ModifyTime = time.Now().Unix()
  589. addList = append(addList, headNurse)
  590. }
  591. break
  592. }
  593. }
  594. if exit == false {
  595. newHeadNurse := &models.AdminUserSpecialPermission{
  596. OrgID: adminUserInfo.CurrentOrgId,
  597. AppID: adminUserInfo.CurrentAppId,
  598. AdminUserID: id,
  599. Permission: int64(models.SpecialPermissionTypeHeadNurse),
  600. Status: 1,
  601. CreateTime: time.Now().Unix(),
  602. ModifyTime: time.Now().Unix(),
  603. }
  604. addList = append(addList, newHeadNurse)
  605. }
  606. }
  607. for _, headNurse := range headNurses {
  608. cancel := true
  609. for _, willAdd := range addList {
  610. if willAdd.AdminUserID == headNurse.AdminUserID {
  611. cancel = false
  612. break
  613. }
  614. }
  615. if cancel {
  616. headNurse.Status = 0
  617. headNurse.ModifyTime = time.Now().Unix()
  618. cancelList = append(cancelList, headNurse)
  619. }
  620. }
  621. addErr := service.BatchSaveSpecialPermissionAdminUsers(addList)
  622. if addErr != nil {
  623. this.ErrorLog("授权失败:%v", addErr)
  624. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  625. return
  626. }
  627. cancelErr := service.BatchSaveSpecialPermissionAdminUsers(cancelList)
  628. if cancelErr != nil {
  629. this.ErrorLog("取消授权失败:%v", cancelErr)
  630. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  631. return
  632. }
  633. this.ServeSuccessJSON(nil)
  634. }
  635. }