| | |
| | | package com.zy.asrs.common.web; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.zy.asrs.common.sys.entity.User; |
| | | import com.zy.asrs.common.sys.entity.UserLogin; |
| | | import com.zy.asrs.common.sys.service.UserLoginService; |
| | | import com.zy.asrs.common.sys.service.UserService; |
| | | import com.zy.asrs.framework.common.BaseRes; |
| | | import com.zy.asrs.framework.common.Cools; |
| | | import com.zy.asrs.framework.controller.AbstractBaseController; |
| | | import com.zy.asrs.framework.exception.CoolException; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Modifier; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Created by vincent on 2019-09-09 |
| | | */ |
| | | public class BaseController extends AbstractBaseController { |
| | | |
| | | protected static final String RANGE_TIME_LINK = " - "; |
| | | |
| | | @Autowired |
| | | protected HttpServletRequest request; |
| | | @Autowired |
| | | private UserService userService; |
| | | @Autowired |
| | | private UserLoginService userLoginService; |
| | | |
| | | protected Long getHostId(){ |
| | | if (getUserId() == 9527) { |
| | | return null; |
| | | } |
| | | User user = getUser(); |
| | | if (user.getRoleId() == 2) { |
| | | String hostId = String.valueOf(request.getAttribute("hostId")); |
| | | if (Cools.isEmpty(hostId)) { |
| | | UserLogin userLogin = userLoginService.getOne(new LambdaQueryWrapper<UserLogin>().eq(UserLogin::getUserId, user.getId())); |
| | | if (userLogin != null) { |
| | | return userLogin.getHostId(); |
| | | } |
| | | } |
| | | return Long.parseLong(hostId); |
| | | } else { |
| | | return user.getHostId(); |
| | | } |
| | | } |
| | | |
| | | protected Long getUserId(){ |
| | | return Long.parseLong(String.valueOf(request.getAttribute("userId"))); |
| | | } |
| | | |
| | | protected User getUser(){ |
| | | User user = userService.getById(getUserId()); |
| | | if (null == user) { |
| | | throw new CoolException(BaseRes.DENIED); |
| | | } |
| | | return user; |
| | | } |
| | | |
| | | protected String getComment(Class<?> cls, String fieldName){ |
| | | Field[] fields = Cools.getAllFields(cls); |
| | | for (Field field : fields){ |
| | | if (fieldName.equals(field.getName())){ |
| | | return field.getAnnotation(ApiModelProperty.class).value(); |
| | | } |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 分页组装 |
| | | * @param pageNumber |
| | | * @param pageSize |
| | | * @param map |
| | | * @param cls |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | protected <T> Page<T> toPage(Integer pageNumber, Integer pageSize, Map<String, Object> map, Class<T> cls){ |
| | | // 分页索引和单页数量组装 |
| | | pageNumber = Optional.ofNullable(pageNumber).orElse(1); |
| | | pageSize = Optional.ofNullable(pageSize).orElse(10); |
| | | Page<T> page = new Page<>(pageNumber, pageSize); |
| | | map.put("pageNumber", pageNumber); |
| | | map.put("pageSize", pageSize); |
| | | |
| | | // 全字段模糊搜索 todo |
| | | if (!Cools.isEmpty(map.get("condition"))) { |
| | | Set<String> columns = new HashSet<>(); |
| | | for (Field field : Cools.getAllFields(cls)){ |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | | || Modifier.isTransient(field.getModifiers())){ |
| | | continue; |
| | | } |
| | | String column = null; |
| | | if (field.isAnnotationPresent(TableField.class)) { |
| | | column = field.getAnnotation(TableField.class).value(); |
| | | } |
| | | if (Cools.isEmpty(column)) { |
| | | column = field.getName(); |
| | | } |
| | | if (!map.keySet().contains(column)) { |
| | | columns.add(column); |
| | | } |
| | | } |
| | | columns.forEach(col->map.put(col, map.get("condition"))); |
| | | } |
| | | // page.setCondition(map); |
| | | return page; |
| | | } |
| | | |
| | | } |
| | | package com.zy.asrs.common.web;
|
| | |
|
| | | import com.baomidou.mybatisplus.annotation.TableField;
|
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
| | | import com.zy.asrs.common.sys.entity.User;
|
| | | import com.zy.asrs.common.sys.entity.UserLogin;
|
| | | import com.zy.asrs.common.sys.service.UserLoginService;
|
| | | import com.zy.asrs.common.sys.service.UserService;
|
| | | import com.zy.asrs.framework.common.BaseRes;
|
| | | import com.zy.asrs.framework.common.Cools;
|
| | | import com.zy.asrs.framework.controller.AbstractBaseController;
|
| | | import com.zy.asrs.framework.exception.CoolException;
|
| | | import io.swagger.annotations.ApiModelProperty;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | |
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | | import java.lang.reflect.Field;
|
| | | import java.lang.reflect.Modifier;
|
| | | import java.util.*;
|
| | |
|
| | | /**
|
| | | * Created by vincent on 2019-09-09
|
| | | */
|
| | | public class BaseController extends AbstractBaseController {
|
| | |
|
| | | protected static final String RANGE_TIME_LINK = " - ";
|
| | |
|
| | | @Autowired
|
| | | protected HttpServletRequest request;
|
| | | @Autowired
|
| | | private UserService userService;
|
| | | @Autowired
|
| | | private UserLoginService userLoginService;
|
| | |
|
| | | protected Long getHostId(){
|
| | | if (getUserId() == 9527) {
|
| | | return null;
|
| | | }
|
| | | User user = getUser();
|
| | | if (user.getRoleId() == 2) {
|
| | | String hostId = String.valueOf(request.getAttribute("hostId"));
|
| | | if (Cools.isEmpty(hostId)) {
|
| | | UserLogin userLogin = userLoginService.getOne(new LambdaQueryWrapper<UserLogin>().eq(UserLogin::getUserId, user.getId()));
|
| | | if (userLogin != null) {
|
| | | return userLogin.getHostId();
|
| | | }
|
| | | }
|
| | | return Long.parseLong(hostId);
|
| | | } else {
|
| | | return user.getHostId();
|
| | | }
|
| | | }
|
| | |
|
| | | protected Long getUserId(){
|
| | | return Long.parseLong(String.valueOf(request.getAttribute("userId")));
|
| | | }
|
| | |
|
| | | protected User getUser(){
|
| | | User user = userService.getById(getUserId());
|
| | | if (null == user) {
|
| | | throw new CoolException(BaseRes.DENIED);
|
| | | }
|
| | | return user;
|
| | | }
|
| | |
|
| | | protected String getComment(Class<?> cls, String fieldName){
|
| | | Field[] fields = Cools.getAllFields(cls);
|
| | | for (Field field : fields){
|
| | | if (fieldName.equals(field.getName())){
|
| | | return field.getAnnotation(ApiModelProperty.class).value();
|
| | | }
|
| | | }
|
| | | return "";
|
| | | }
|
| | |
|
| | | /**
|
| | | * 分页组装
|
| | | * @param pageNumber
|
| | | * @param pageSize
|
| | | * @param map
|
| | | * @param cls
|
| | | * @param <T>
|
| | | * @return
|
| | | */
|
| | | protected <T> Page<T> toPage(Integer pageNumber, Integer pageSize, Map<String, Object> map, Class<T> cls){
|
| | | // 分页索引和单页数量组装
|
| | | pageNumber = Optional.ofNullable(pageNumber).orElse(1);
|
| | | pageSize = Optional.ofNullable(pageSize).orElse(10);
|
| | | Page<T> page = new Page<>(pageNumber, pageSize);
|
| | | map.put("pageNumber", pageNumber);
|
| | | map.put("pageSize", pageSize);
|
| | |
|
| | | // 全字段模糊搜索 todo
|
| | | if (!Cools.isEmpty(map.get("condition"))) {
|
| | | Set<String> columns = new HashSet<>();
|
| | | for (Field field : Cools.getAllFields(cls)){
|
| | | if (Modifier.isFinal(field.getModifiers())
|
| | | || Modifier.isStatic(field.getModifiers())
|
| | | || Modifier.isTransient(field.getModifiers())){
|
| | | continue;
|
| | | }
|
| | | String column = null;
|
| | | if (field.isAnnotationPresent(TableField.class)) {
|
| | | column = field.getAnnotation(TableField.class).value();
|
| | | }
|
| | | if (Cools.isEmpty(column)) {
|
| | | column = field.getName();
|
| | | }
|
| | | if (!map.keySet().contains(column)) {
|
| | | columns.add(column);
|
| | | }
|
| | | }
|
| | | columns.forEach(col->map.put(col, map.get("condition")));
|
| | | }
|
| | | // page.setCondition(map);
|
| | | return page;
|
| | | }
|
| | |
|
| | | }
|