1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package com.welampiot.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.welampiot.common.BaseResult;
- import com.welampiot.common.InterfaceResultEnum;
- import com.welampiot.dto.BenchUserInfoDTO;
- import com.welampiot.dto.UserDTO;
- import com.welampiot.exception.SectionAuthException;
- import com.welampiot.service.UserService;
- import com.welampiot.utils.ToolUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.PreDestroy;
- import javax.servlet.http.HttpServletRequest;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- @RestController
- public class BaseController {
- // 获取可用的处理器数量
- private final static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
- // 设置最大线程数为可用处理器数量的两倍
- private final static int MAX_POOL_SIZE = CORE_POOL_SIZE * 2 + 1;
- // 设置空闲线程的存活时间
- private final static long KEEP_ALIVE_TIME = 60L;
- // 设置时间单位
- private final static TimeUnit TIME_UNIT = TimeUnit.SECONDS;
- // 设置任务队列
- private final static BlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>();
- // 创建动态线程池对象实例
- public final static ThreadPoolExecutor EXECUTOR_SERVICE = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, WORK_QUEUE);
- @Autowired
- private UserService userService;
- @Autowired
- private UserDTO userDTO;
- @Autowired
- protected ToolUtils toolUtils;
- protected List<Integer> getSectionList(String username) {
- List<Integer> sectionList = new ArrayList<>();
- UserDTO userDTO = userService.findUserByUserName(username);
- if (userDTO == null) {
- sectionList.add(0);
- return sectionList;
- }
- if (userDTO.getRole() != 1) {
- String[] split = userDTO.getZoneList().split(",");
- if (split.length == 0) {
- sectionList.add(0);
- } else {
- for (String s : split) {
- sectionList.add(Integer.valueOf(s));
- }
- }
- }
- return sectionList;
- }
- @PreDestroy
- public void shutdown() {
- EXECUTOR_SERVICE.shutdown();
- }
- protected void checkSectionAuth(Integer sectionId)throws Exception{
- String zoneList = userDTO.getZoneList();
- String[] split = zoneList.split(",");
- List<String> strings = Arrays.asList(split);
- if (split.length == 0 || !strings.contains(sectionId.toString())){
- SectionAuthException exception = new SectionAuthException();
- throw exception;
- }
- }
- }
|