RoleController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.xintong.visualinspection.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import com.github.pagehelper.PageHelper;
  11. import com.github.pagehelper.PageInfo;
  12. import com.xintong.system.err.BusinessException;
  13. import com.xintong.visualinspection.bean.Role;
  14. import com.xintong.visualinspection.service.RoleService;
  15. /**
  16. * 文件名:TestController
  17. * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  18. */
  19. /**
  20. * @author wenhongquan
  21. *
  22. */
  23. @RestController
  24. @RequestMapping("/role")
  25. public class RoleController extends BaseController {
  26. @Autowired
  27. private RoleService roleService;
  28. /**
  29. * 添加角色
  30. *
  31. * @return String
  32. * @exception @since
  33. * 1.0.0
  34. */
  35. @RequestMapping(value = "/addRole", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  36. public String addRole(@RequestBody Role role) throws Exception {
  37. roleService.insert(role);
  38. return returnResult(0, "添加成功", null);
  39. }
  40. /**
  41. * 修改角色
  42. *
  43. * @return String
  44. * @exception @since
  45. * 1.0.0
  46. */
  47. @RequestMapping(value = "/updateRole/{roleid}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
  48. public String updateRole(@RequestBody Role role, @PathVariable int roleid) {
  49. try {
  50. role.setId(roleid);
  51. roleService.update(role);
  52. return super.returnResult(0, "修改成功", null);
  53. } catch (Exception e) {
  54. throw new BusinessException(20003);
  55. }
  56. }
  57. /**
  58. * 删除角色(软删除)
  59. *
  60. * @return String
  61. * @exception @since
  62. * 1.0.0
  63. */
  64. @RequestMapping(value = "/deleteRole/{roleid}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
  65. public String deleteRole(@PathVariable Integer roleid) {
  66. try {
  67. roleService.delete((roleid));
  68. return returnResult(0, "删除成功", null);
  69. } catch (Exception e) {
  70. throw new BusinessException(20002);
  71. }
  72. }
  73. /**
  74. * 获取所有角色
  75. *
  76. * @param page
  77. * @param size
  78. * @return
  79. */
  80. @RequestMapping(value = "/get/all/{page}/{size}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  81. public String getAllRole(@PathVariable Integer page, @PathVariable Integer size) {
  82. try {
  83. PageHelper.startPage(page, size);
  84. List<Role> roles = roleService.getRoles(new Role());
  85. return returnResult(0, "获取成功", new PageInfo(roles));
  86. } catch (Exception e) {
  87. throw new BusinessException(20001);
  88. }
  89. }
  90. @RequestMapping(value = "/roleBindPermission/{roleId}/{permissions}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
  91. public String roleBindPermission(@PathVariable Integer roleId, @PathVariable String permissions) {
  92. String[] permissionids = permissions.split(",");
  93. List<Integer> pids = new ArrayList<>();
  94. for (String id : permissionids) {
  95. if (!id.isEmpty()) {
  96. try {
  97. Integer idt = Integer.parseInt(id);
  98. pids.add(idt);
  99. } catch (Exception e) {
  100. throw new BusinessException(20001);
  101. }
  102. }
  103. }
  104. for (Integer id : pids) {
  105. roleService.roleBindPermission(roleId, id);
  106. }
  107. return returnResult(0, "绑定成功", null);
  108. }
  109. @RequestMapping(value = "/roleBindUser/{roleId}/{users}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
  110. public String roleBindUser(@PathVariable Integer roleId, @PathVariable String users) {
  111. String[] userids = users.split(",");
  112. List<Integer> uids = new ArrayList<>();
  113. for (String id : userids) {
  114. if (!id.isEmpty()) {
  115. try {
  116. Integer idt = Integer.parseInt(id);
  117. uids.add(idt);
  118. } catch (Exception e) {
  119. throw new BusinessException(20001);
  120. }
  121. }
  122. }
  123. for (Integer id : uids) {
  124. roleService.roleBindUser(roleId, id);
  125. }
  126. return returnResult(0, "绑定成功", null);
  127. }
  128. @RequestMapping(value = "/getRoleByDept/{deptId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  129. public String getRoleByDept(@PathVariable Integer deptId) {
  130. List<Role> roles = roleService.getRoleByDept(deptId);
  131. return returnResult(0, "获取成功", roles);
  132. }
  133. @RequestMapping(value = "/getRoleByUser/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  134. public String getRoleByUser(@PathVariable Integer userId) {
  135. List<Role> roles = roleService.getRoleByUser(userId);
  136. return returnResult(0, "获取成功", roles);
  137. }
  138. @RequestMapping(value = "/getRoleByPosition/{positionId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  139. public String getRoleByPosition(@PathVariable Integer positionId) {
  140. List<Role> roles = roleService.getRoleByPosition(positionId);
  141. return returnResult(0, "获取成功", roles);
  142. }
  143. }