FlowApiController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.nokia.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.nokia.common.R;
  4. import com.nokia.pojo.Role;
  5. import com.nokia.service.FlowService;
  6. import com.nokia.vo.flow.FlowRoleCityVo;
  7. import com.nokia.vo.flow.FlowRoleVo;
  8. import com.nokia.vo.flow.FlowUserVo;
  9. import com.nokia.vo.flow.UserTreeReq;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * 提供给工作流的api
  18. */
  19. @Slf4j
  20. @RestController
  21. @RequestMapping("rest")
  22. public class FlowApiController {
  23. private final FlowService flowService;
  24. @Autowired
  25. public FlowApiController(FlowService flowService) {
  26. this.flowService = flowService;
  27. }
  28. /*
  29. * 页面上的用户树结构
  30. */
  31. @PostMapping("flow/api/userTree")
  32. public R getUserTree(@RequestBody UserTreeReq userTreeReq) {
  33. List<Map<String, Object>> userTree = flowService.getUserTree(userTreeReq);
  34. return R.ok().data(userTree);
  35. }
  36. /*
  37. * 根据输入的角色和city信息返回用户手机号列表
  38. */
  39. @PostMapping("flow/api/getPhoneListByRole")
  40. public R getPhoneListByRole(@RequestBody JSONObject req) {
  41. try {
  42. int roleId = req.getIntValue("roleId");
  43. int cityId = req.getIntValue("cityId");
  44. List<String> phoneList = flowService.getPhoneListByRole(roleId, cityId);
  45. return R.ok().data(phoneList);
  46. } catch (NumberFormatException e) {
  47. e.printStackTrace();
  48. return R.error().message("roleId和cityId必需为有意义的整数");
  49. }
  50. }
  51. @PostMapping("flow/api")
  52. @ResponseBody
  53. public R flowApi(@RequestBody JSONObject jsonObject) {
  54. String callType = jsonObject.getString("callType");
  55. if (callType == null) {
  56. return R.error().message("输入必须包含callType");
  57. }
  58. switch (callType) {
  59. case "findRoleList":
  60. return findRoleList(jsonObject);
  61. case "findToPage":
  62. return findToPage();
  63. case "getByLoginId":
  64. return getByLoginId(jsonObject);
  65. case "get":
  66. return get(jsonObject);
  67. case "findRoleIdByUserId":
  68. return findRoleIdByUserId(jsonObject);
  69. case "findUserIdByRoleId":
  70. return findUserIdByRoleId(jsonObject);
  71. case "findAuthorizedUser":
  72. return findAuthorizedUser(jsonObject);
  73. case "getRoleCityByUserId":
  74. return getRoleCityByUserId(jsonObject);
  75. default:
  76. return R.error().message("callType类型错误");
  77. }
  78. }
  79. /**
  80. *
  81. * @param jsonObject
  82. * @return
  83. */
  84. private R getRoleCityByUserId(JSONObject jsonObject) {
  85. Integer userId = jsonObject.getInteger("userId");
  86. if (null == userId) {
  87. return R.error().message("输入必须包含userId");
  88. }
  89. List<FlowRoleCityVo> flowRoleCityVos = new ArrayList<>();
  90. for (Role role : flowService.getRoleCityByUserId(userId)) {
  91. log.debug("查询结果:{}", role);
  92. if ("flow".equals(role.getSystem())) {
  93. flowRoleCityVos.add(new FlowRoleCityVo(role));
  94. }
  95. }
  96. return R.ok().data(flowRoleCityVos);
  97. }
  98. public R findRoleList(JSONObject jsonObject) {
  99. String username = jsonObject.getString("userName");
  100. if (username == null || username.equals("")) {
  101. return R.ok().data(flowService.findRoleList());
  102. }
  103. List<FlowRoleVo> roleList = flowService.findRoleList(username);
  104. return R.ok().data(roleList);
  105. }
  106. public R findToPage() {
  107. List<FlowUserVo> flowUserVos = flowService.findToPage();
  108. return R.ok().data(flowUserVos);
  109. }
  110. public R getByLoginId(JSONObject jsonObject) {
  111. String loginId = jsonObject.getString("loginId");
  112. if (loginId == null || loginId.equals("")) {
  113. return R.error().message("输入必须包含loginId");
  114. }
  115. FlowUserVo flowUserVo = flowService.getByLoginId(loginId);
  116. return R.ok().data(flowUserVo);
  117. }
  118. public R get(JSONObject jsonObject) {
  119. Integer userId = null;
  120. try {
  121. userId = jsonObject.getInteger("userId");
  122. } catch (NumberFormatException e) {
  123. return R.error().message("输入的userId必须为整数格式");
  124. }
  125. if (userId == null) {
  126. return R.error().message("输入必须包含userId");
  127. }
  128. FlowUserVo flowUserVo = flowService.getByUserId(userId);
  129. return R.ok().data(flowUserVo);
  130. }
  131. public R findRoleIdByUserId(JSONObject jsonObject) {
  132. Integer userId = null;
  133. try {
  134. userId = jsonObject.getInteger("userId");
  135. } catch (NumberFormatException e) {
  136. return R.error().message("输入的userId必须为整数格式");
  137. }
  138. if (userId == null) {
  139. return R.error().message("输入必须包含userId");
  140. }
  141. List<Integer> roleIdList = flowService.findRoleIdByUserId(userId);
  142. return R.ok().data(roleIdList);
  143. }
  144. public R findUserIdByRoleId(JSONObject jsonObject) {
  145. Integer roleId = null;
  146. try {
  147. roleId = jsonObject.getInteger("roleId");
  148. } catch (NumberFormatException e) {
  149. return R.error().message("输入的roleId必须为整数格式");
  150. }
  151. if (roleId == null) {
  152. return R.error().message("输入必须包含roleId");
  153. }
  154. List<Integer> userIdList = flowService.findUserIdByRoleId(roleId);
  155. return R.ok().data(userIdList);
  156. }
  157. public R findAuthorizedUser(JSONObject jsonObject) {
  158. String loginId = jsonObject.getString("loginId");
  159. loginId = loginId == null || loginId.equals("") ? null : loginId.trim();
  160. Integer roleId = null;
  161. try {
  162. roleId = jsonObject.getInteger("roleId");
  163. } catch (NumberFormatException ignored) {
  164. }
  165. String userName = jsonObject.getString("userName");
  166. userName = userName == null || userName.equals("") ? null : userName.trim();
  167. List<FlowUserVo> flowUserVos = flowService.findAuthorizedUser(loginId, roleId, userName);
  168. return R.ok().data(flowUserVos);
  169. }
  170. }