BaseController.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.welampiot.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.welampiot.common.BaseResult;
  5. import com.welampiot.common.InterfaceResultEnum;
  6. import com.welampiot.dto.BenchUserInfoDTO;
  7. import com.welampiot.dto.UserDTO;
  8. import com.welampiot.exception.SectionAuthException;
  9. import com.welampiot.service.UserService;
  10. import com.welampiot.utils.ToolUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.annotation.PreDestroy;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.io.IOException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.concurrent.BlockingQueue;
  24. import java.util.concurrent.LinkedBlockingQueue;
  25. import java.util.concurrent.ThreadPoolExecutor;
  26. import java.util.concurrent.TimeUnit;
  27. @RestController
  28. public class BaseController {
  29. // 获取可用的处理器数量
  30. private final static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
  31. // 设置最大线程数为可用处理器数量的两倍
  32. private final static int MAX_POOL_SIZE = CORE_POOL_SIZE * 2 + 1;
  33. // 设置空闲线程的存活时间
  34. private final static long KEEP_ALIVE_TIME = 60L;
  35. // 设置时间单位
  36. private final static TimeUnit TIME_UNIT = TimeUnit.SECONDS;
  37. // 设置任务队列
  38. private final static BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>();
  39. // 创建动态线程池对象实例
  40. public final static ThreadPoolExecutor EXECUTOR_SERVICE = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, WORK_QUEUE);
  41. @Autowired
  42. private UserService userService;
  43. @Autowired
  44. private UserDTO userDTO;
  45. @Autowired
  46. protected ToolUtils toolUtils;
  47. protected List<Integer> getSectionList(String username) {
  48. List<Integer> sectionList = new ArrayList<>();
  49. UserDTO userDTO = userService.findUserByUserName(username);
  50. if (userDTO == null) {
  51. sectionList.add(0);
  52. return sectionList;
  53. }
  54. if (userDTO.getRole() != 1) {
  55. String[] split = userDTO.getZoneList().split(",");
  56. if (split.length == 0) {
  57. sectionList.add(0);
  58. } else {
  59. for (String s : split) {
  60. sectionList.add(Integer.valueOf(s));
  61. }
  62. }
  63. }
  64. return sectionList;
  65. }
  66. @PreDestroy
  67. public void shutdown() {
  68. EXECUTOR_SERVICE.shutdown();
  69. }
  70. protected void checkSectionAuth(Integer sectionId)throws Exception{
  71. String zoneList = userDTO.getZoneList();
  72. String[] split = zoneList.split(",");
  73. List<String> strings = Arrays.asList(split);
  74. if (split.length == 0 || !strings.contains(sectionId.toString())){
  75. SectionAuthException exception = new SectionAuthException();
  76. throw exception;
  77. }
  78. }
  79. }