BackendDutyController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.pagination.PaginationRequest;
  12. import com.fuint.framework.pagination.PaginationResponse;
  13. import com.fuint.framework.web.BaseController;
  14. import com.fuint.framework.web.ResponseObject;
  15. import com.fuint.module.backendApi.request.DutyStatusRequest;
  16. import com.fuint.repository.model.TDuty;
  17. import com.fuint.repository.model.TSource;
  18. import com.fuint.utils.StringUtil;
  19. import io.swagger.annotations.Api;
  20. import io.swagger.annotations.ApiOperation;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.data.domain.Page;
  23. import org.springframework.data.domain.PageImpl;
  24. import org.springframework.data.domain.PageRequest;
  25. import org.springframework.security.access.prepost.PreAuthorize;
  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. @PreAuthorize("@pms.hasPermission('system:role:index')")
  56. public ResponseObject list(HttpServletRequest request) {
  57. String token = request.getHeader("Access-Token");
  58. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  59. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  60. String name = request.getParameter("name") == null ? "" : request.getParameter("name");
  61. String status = request.getParameter("status") == null ? "" : request.getParameter("status");
  62. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  63. if (accountInfo == null) {
  64. return getFailureResult(1001, "请先登录");
  65. }
  66. PaginationRequest paginationRequest = new PaginationRequest();
  67. paginationRequest.setCurrentPage(page);
  68. paginationRequest.setPageSize(pageSize);
  69. Map<String, Object> searchParams = new HashMap<>();
  70. if (StringUtil.isNotEmpty(name)) {
  71. searchParams.put("name", name);
  72. }
  73. if (StringUtil.isNotEmpty(status)) {
  74. searchParams.put("status", status);
  75. }
  76. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  77. searchParams.put("merchantId", accountInfo.getMerchantId());
  78. }
  79. paginationRequest.setSearchParams(searchParams);
  80. PaginationResponse<TDuty> paginationResponse = tDutyService.findDutiesByPagination(paginationRequest);
  81. List<RoleDto> content = new ArrayList<>();
  82. if (paginationResponse.getContent().size() > 0) {
  83. for (TDuty tDuty : paginationResponse.getContent()) {
  84. RoleDto dto = new RoleDto();
  85. dto.setId(tDuty.getDutyId().longValue());
  86. dto.setName(tDuty.getDutyName());
  87. String type = AdminRoleEnum.getName(tDuty.getDutyType());
  88. dto.setType(type);
  89. dto.setStatus(tDuty.getStatus());
  90. content.add(dto);
  91. }
  92. }
  93. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  94. Page pageImpl = new PageImpl(content, pageRequest, paginationResponse.getTotalElements());
  95. PaginationResponse<RoleDto> result = new PaginationResponse(pageImpl, AccountDto.class);
  96. result.setTotalPages(paginationResponse.getTotalPages());
  97. result.setTotalElements(paginationResponse.getTotalElements());
  98. result.setContent(content);
  99. return getSuccessResult(result);
  100. }
  101. /**
  102. * 新增角色
  103. *
  104. * @param request HttpServletRequest对象
  105. * @return 角色列表页面
  106. * @throws BusinessCheckException
  107. */
  108. @ApiOperation(value = "新增角色")
  109. @RequestMapping(value = "/add", method = RequestMethod.POST)
  110. @CrossOrigin
  111. @PreAuthorize("@pms.hasPermission('system:role:add')")
  112. public ResponseObject addHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  113. String token = request.getHeader("Access-Token");
  114. List<Integer> menuIds = (List) param.get("menuIds");
  115. String name = param.get("roleName").toString();
  116. String type = param.get("roleType").toString();
  117. String status = param.get("status").toString();
  118. String description = param.get("description").toString();
  119. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  120. if (accountInfo == null) {
  121. return getFailureResult(1001, "请先登录");
  122. }
  123. // 获取角色所分配的菜单
  124. List<TSource> sources = null;
  125. if (menuIds.size() > 0) {
  126. String[] sourceIds = new String[menuIds.size()];
  127. for (int i = 0; i < sourceIds.length; i++) {
  128. sourceIds[i] = menuIds.get(i).toString();
  129. }
  130. sources = tSourceService.findDatasByIds(sourceIds);
  131. }
  132. TDuty tDuty = new TDuty();
  133. tDuty.setMerchantId(accountInfo.getMerchantId());
  134. tDuty.setDutyName(name);
  135. tDuty.setDutyType(type);
  136. tDuty.setStatus(status);
  137. tDuty.setDescription(description);
  138. // 保存角色
  139. tDutyService.saveDuty(tDuty, sources);
  140. return getSuccessResult(true);
  141. }
  142. /**
  143. * 获取角色详情
  144. *
  145. * @param roleId
  146. * @return 账户信息
  147. */
  148. @ApiOperation(value = "获取角色详情")
  149. @RequestMapping(value = "/info/{roleId}", method = RequestMethod.GET)
  150. @CrossOrigin
  151. @PreAuthorize("@pms.hasPermission('system:role:index')")
  152. public ResponseObject info(HttpServletRequest request, @PathVariable("roleId") Long roleId) {
  153. String token = request.getHeader("Access-Token");
  154. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  155. if (accountInfo == null) {
  156. return getFailureResult(1001, "请先登录");
  157. }
  158. TDuty htDuty = tDutyService.getRoleById(roleId);
  159. Map<String, Object> result = new HashMap<>();
  160. RoleDto roleInfo = new RoleDto();
  161. roleInfo.setMerchantId(htDuty.getMerchantId());
  162. roleInfo.setId(htDuty.getDutyId().longValue());
  163. roleInfo.setName(htDuty.getDutyName());
  164. roleInfo.setType(htDuty.getDutyType());
  165. roleInfo.setStatus(htDuty.getStatus());
  166. roleInfo.setDescription(htDuty.getDescription());
  167. result.put("roleInfo", roleInfo);
  168. List<Long> checkedKeys = tDutyService.getSourceIdsByDutyId(roleId.intValue());
  169. if (checkedKeys != null && checkedKeys.size() > 0) {
  170. result.put("checkedKeys", checkedKeys);
  171. }
  172. return getSuccessResult(result);
  173. }
  174. /**
  175. * 修改角色
  176. *
  177. * @param request
  178. * @return
  179. */
  180. @ApiOperation(value = "修改角色")
  181. @RequestMapping(value = "/update", method = RequestMethod.POST)
  182. @CrossOrigin
  183. @PreAuthorize("@pms.hasPermission('system:role:edit')")
  184. public ResponseObject updateHandler(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  185. String token = request.getHeader("Access-Token");
  186. List<Integer> menuIds = (List) param.get("menuIds");
  187. String id = param.get("id").toString();
  188. String name = param.get("roleName").toString();
  189. String type = param.get("roleType").toString();
  190. String status = param.get("status").toString();
  191. String description = param.get("description").toString();
  192. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  193. if (accountInfo == null) {
  194. return getFailureResult(1001, "请先登录");
  195. }
  196. if (StringUtil.isEmpty(id)) {
  197. return getFailureResult(201, "信息提交有误");
  198. }
  199. TDuty duty = tDutyService.getRoleById(Long.parseLong(id));
  200. if (!duty.getMerchantId().equals(accountInfo.getMerchantId()) && accountInfo.getMerchantId() > 0) {
  201. return getFailureResult(201, "抱歉,您没有修改权限");
  202. }
  203. duty.setDescription(description);
  204. duty.setDutyName(name);
  205. duty.setStatus(status);
  206. duty.setDutyType(type);
  207. // 获取角色所分配的菜单
  208. List<TSource> sources = null;
  209. if (menuIds.size() > 0) {
  210. String[] sourceIds = new String[menuIds.size()];
  211. for (int i = 0; i < sourceIds.length; i++) {
  212. sourceIds[i] = menuIds.get(i).toString();
  213. }
  214. sources = tSourceService.findDatasByIds(sourceIds);
  215. }
  216. tDutyService.updateDuty(duty, sources);
  217. return getSuccessResult(true);
  218. }
  219. /**
  220. * 删除角色信息
  221. *
  222. * @return
  223. * @throws BusinessCheckException
  224. */
  225. @ApiOperation(value = "删除角色信息")
  226. @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.POST)
  227. @CrossOrigin
  228. @PreAuthorize("@pms.hasPermission('system:role:delete')")
  229. public ResponseObject deleteRole(HttpServletRequest request, @PathVariable("roleId") Long roleId) throws BusinessCheckException {
  230. String token = request.getHeader("Access-Token");
  231. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  232. if (accountInfo == null) {
  233. return getFailureResult(1001, "请先登录");
  234. }
  235. tDutyService.deleteDuty(accountInfo.getMerchantId(), roleId);
  236. return getSuccessResult(true);
  237. }
  238. /**
  239. * 修改角色状态
  240. *
  241. * @return
  242. * @throws BusinessCheckException
  243. */
  244. @ApiOperation(value = "修改角色状态")
  245. @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
  246. @CrossOrigin
  247. @PreAuthorize("@pms.hasPermission('system:role:edit')")
  248. public ResponseObject changeStatus(HttpServletRequest request, @RequestBody DutyStatusRequest dutyStatusRequest) throws BusinessCheckException {
  249. String token = request.getHeader("Access-Token");
  250. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  251. if (accountInfo == null) {
  252. return getFailureResult(1001, "请先登录");
  253. }
  254. tDutyService.updateStatus(accountInfo.getMerchantId(), dutyStatusRequest);
  255. return getSuccessResult(true);
  256. }
  257. }