BackendDutyController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.Constants;
  3. import com.fuint.common.dto.AccountDto;
  4. import com.fuint.common.dto.AccountInfo;
  5. import com.fuint.common.dto.RoleDto;
  6. import com.fuint.common.enums.AdminRoleEnum;
  7. import com.fuint.common.service.DutyService;
  8. import com.fuint.common.service.SourceService;
  9. import com.fuint.common.util.TokenUtil;
  10. import com.fuint.framework.exception.BusinessCheckException;
  11. import com.fuint.framework.exception.BusinessRuntimeException;
  12. import com.fuint.framework.pagination.PaginationRequest;
  13. import com.fuint.framework.pagination.PaginationResponse;
  14. import com.fuint.framework.web.BaseController;
  15. import com.fuint.framework.web.ResponseObject;
  16. import com.fuint.module.backendApi.request.DutyStatusRequest;
  17. import com.fuint.repository.model.TDuty;
  18. import com.fuint.repository.model.TSource;
  19. import com.fuint.utils.StringUtil;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.data.domain.Page;
  24. import org.springframework.data.domain.PageImpl;
  25. import org.springframework.data.domain.PageRequest;
  26. import org.springframework.web.bind.annotation.*;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * 后台角色管理控制类
  34. *
  35. * Created by FSQ
  36. * CopyRight https://www.fuint.cn
  37. */
  38. @Api(tags="管理端-后台角色相关接口")
  39. @RestController
  40. @RequestMapping(value = "/backendApi/duty")
  41. public class BackendDutyController extends BaseController {
  42. @Autowired
  43. private DutyService tDutyService;
  44. @Autowired
  45. private SourceService tSourceService;
  46. /**
  47. * 角色列表
  48. *
  49. * @param request HttpServletRequest对象
  50. * @return 角色信息列表
  51. */
  52. @ApiOperation(value = "获取角色列表")
  53. @RequestMapping(value = "/list")
  54. @CrossOrigin
  55. public ResponseObject list(HttpServletRequest request) {
  56. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  57. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  58. String name = request.getParameter("name") == null ? "" : request.getParameter("name");
  59. String status = request.getParameter("status") == null ? "" : request.getParameter("status");
  60. PaginationRequest paginationRequest = new PaginationRequest();
  61. paginationRequest.setCurrentPage(page);
  62. paginationRequest.setPageSize(pageSize);
  63. Map<String, Object> searchParams = new HashMap<>();
  64. if (StringUtil.isNotEmpty(name)) {
  65. searchParams.put("name", name);
  66. }
  67. if (StringUtil.isNotEmpty(status)) {
  68. searchParams.put("status", status);
  69. }
  70. paginationRequest.setSearchParams(searchParams);
  71. PaginationResponse<TDuty> paginationResponse = tDutyService.findDutiesByPagination(paginationRequest);
  72. List<RoleDto> content = new ArrayList<>();
  73. if (paginationResponse.getContent().size() > 0) {
  74. for (TDuty tDuty : paginationResponse.getContent()) {
  75. RoleDto dto = new RoleDto();
  76. dto.setId(tDuty.getDutyId().longValue());
  77. dto.setName(tDuty.getDutyName());
  78. String type = AdminRoleEnum.getName(tDuty.getDutyType());
  79. dto.setType(type);
  80. dto.setStatus(tDuty.getStatus());
  81. content.add(dto);
  82. }
  83. }
  84. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  85. Page pageImpl = new PageImpl(content, pageRequest, paginationResponse.getTotalElements());
  86. PaginationResponse<RoleDto> result = new PaginationResponse(pageImpl, AccountDto.class);
  87. result.setTotalPages(paginationResponse.getTotalPages());
  88. result.setTotalElements(paginationResponse.getTotalElements());
  89. result.setContent(content);
  90. return getSuccessResult(result);
  91. }
  92. /**
  93. * 新增角色
  94. *
  95. * @param request HttpServletRequest对象
  96. * @return 角色列表页面
  97. * @throws BusinessCheckException
  98. */
  99. @ApiOperation(value = "新增角色")
  100. @RequestMapping(value = "/add", method = RequestMethod.POST)
  101. @CrossOrigin
  102. public ResponseObject addHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  103. String token = request.getHeader("Access-Token");
  104. List<Integer> menuIds = (List) param.get("menuIds");
  105. String name = param.get("roleName").toString();
  106. String type = param.get("roleType").toString();
  107. String status = param.get("status").toString();
  108. String description = param.get("description").toString();
  109. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  110. if (accountInfo == null) {
  111. return getFailureResult(1001, "请先登录");
  112. }
  113. // 获取角色所分配的菜单
  114. List<TSource> sources = null;
  115. if (menuIds.size() > 0) {
  116. String[] sourceIds = new String[menuIds.size()];
  117. for (int i = 0; i < sourceIds.length; i++) {
  118. sourceIds[i] = menuIds.get(i).toString();
  119. }
  120. sources = tSourceService.findDatasByIds(sourceIds);
  121. }
  122. TDuty tDuty = new TDuty();
  123. tDuty.setDutyName(name);
  124. tDuty.setDutyType(type);
  125. tDuty.setStatus(status);
  126. tDuty.setDescription(description);
  127. // 添加角色信息
  128. try {
  129. tDutyService.saveDuty(tDuty, sources);
  130. } catch (Exception e) {
  131. return getFailureResult(201, e.getMessage());
  132. }
  133. return getSuccessResult(true);
  134. }
  135. /**
  136. * 获取角色详情
  137. *
  138. * @param roleId
  139. * @return 账户信息
  140. */
  141. @ApiOperation(value = "获取角色详情")
  142. @RequestMapping(value = "/info/{roleId}", method = RequestMethod.GET)
  143. @CrossOrigin
  144. public ResponseObject info(HttpServletRequest request, @PathVariable("roleId") Long roleId) {
  145. String token = request.getHeader("Access-Token");
  146. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  147. if (accountInfo == null) {
  148. return getFailureResult(1001, "请先登录");
  149. }
  150. TDuty htDuty = tDutyService.getRoleById(roleId);
  151. Map<String, Object> result = new HashMap<>();
  152. RoleDto roleInfo = new RoleDto();
  153. roleInfo.setId(htDuty.getDutyId().longValue());
  154. roleInfo.setName(htDuty.getDutyName());
  155. roleInfo.setType(htDuty.getDutyType());
  156. roleInfo.setStatus(htDuty.getStatus());
  157. roleInfo.setDescription(htDuty.getDescription());
  158. result.put("roleInfo", roleInfo);
  159. List<Long> checkedKeys = tDutyService.getSourceIdsByDutyId(roleId.intValue());
  160. if (checkedKeys != null && checkedKeys.size() > 0) {
  161. result.put("checkedKeys", checkedKeys);
  162. }
  163. return getSuccessResult(result);
  164. }
  165. /**
  166. * 修改角色
  167. *
  168. * @param request
  169. * @return
  170. */
  171. @ApiOperation(value = "修改角色")
  172. @RequestMapping(value = "/update", method = RequestMethod.POST)
  173. @CrossOrigin
  174. public ResponseObject updateHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  175. String token = request.getHeader("Access-Token");
  176. List<Integer> menuIds = (List) param.get("menuIds");
  177. String id = param.get("id").toString();
  178. String name = param.get("roleName").toString();
  179. String type = param.get("roleType").toString();
  180. String status = param.get("status").toString();
  181. String description = param.get("description").toString();
  182. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  183. if (accountInfo == null) {
  184. return getFailureResult(1001, "请先登录");
  185. }
  186. if (StringUtil.isEmpty(id)) {
  187. return getFailureResult(201, "信息提交有误");
  188. }
  189. TDuty duty = tDutyService.getRoleById(Long.parseLong(id));
  190. duty.setDescription(description);
  191. duty.setDutyName(name);
  192. duty.setStatus(status);
  193. duty.setDutyType(type);
  194. // 获取角色所分配的菜单
  195. List<TSource> sources = null;
  196. if (menuIds.size() > 0) {
  197. String[] sourceIds = new String[menuIds.size()];
  198. for (int i = 0; i < sourceIds.length; i++) {
  199. sourceIds[i] = menuIds.get(i).toString();
  200. }
  201. sources = tSourceService.findDatasByIds(sourceIds);
  202. }
  203. try {
  204. tDutyService.updateDuty(duty, sources);
  205. } catch (Exception e) {
  206. return getFailureResult(201, e.getMessage());
  207. }
  208. return getSuccessResult(true);
  209. }
  210. /**
  211. * 删除角色信息
  212. *
  213. * @return
  214. * @throws BusinessCheckException
  215. */
  216. @ApiOperation(value = "删除角色信息")
  217. @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.POST)
  218. @CrossOrigin
  219. public ResponseObject deleteAccount(@PathVariable("roleId") Long roleId) {
  220. try {
  221. tDutyService.deleteDuty(roleId);
  222. } catch (BusinessRuntimeException e) {
  223. return getFailureResult(201, e.getMessage() == null ? "角色删除失败" : e.getMessage());
  224. }
  225. return getSuccessResult(true);
  226. }
  227. /**
  228. * 修改角色状态
  229. *
  230. * @return
  231. * @throws BusinessCheckException
  232. */
  233. @ApiOperation(value = "修改角色状态")
  234. @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
  235. @CrossOrigin
  236. public ResponseObject changeStatus(@RequestBody DutyStatusRequest dutyStatusRequest) {
  237. try {
  238. tDutyService.updateStatus(dutyStatusRequest);
  239. } catch (BusinessCheckException e) {
  240. return getFailureResult(201, e.getMessage() == null ? "操作失败" : e.getMessage());
  241. }
  242. return getSuccessResult(true);
  243. }
  244. }