| package com.zy.crm.common.web; | 
|   | 
| import com.baomidou.mybatisplus.annotations.TableField; | 
| import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
| import com.baomidou.mybatisplus.plugins.Page; | 
| import com.core.common.BaseRes; | 
| import com.core.common.Cools; | 
| import com.core.controller.AbstractBaseController; | 
| import com.core.exception.CoolException; | 
| import com.zy.crm.system.entity.Dept; | 
| import com.zy.crm.system.entity.Role; | 
| import com.zy.crm.system.service.DeptService; | 
| import com.zy.crm.system.service.RoleService; | 
| import io.swagger.annotations.ApiModelProperty; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import com.zy.crm.manager.entity.Node; | 
| import com.zy.crm.manager.entity.Tag; | 
| import com.zy.crm.manager.service.NodeService; | 
| import com.zy.crm.manager.service.TagService; | 
| import com.zy.crm.system.entity.User; | 
| import com.zy.crm.system.entity.UserLogin; | 
| import com.zy.crm.system.service.UserLoginService; | 
| import com.zy.crm.system.service.UserService; | 
|   | 
| 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 TagService tagService; | 
|     @Autowired | 
|     private NodeService nodeService; | 
|     @Autowired | 
|     private DeptService deptService; | 
|     @Autowired | 
|     private UserLoginService userLoginService; | 
|     @Autowired | 
|     private RoleService roleService; | 
|   | 
|     protected Long getHostId(){ | 
|         if (getUserId() == 9527) { | 
|             return null; | 
|         } | 
|         User user = getUser(); | 
|         if (user.getRoleId() == 1) { | 
|             String hostId = String.valueOf(request.getAttribute("hostId")); | 
|             if (Cools.isEmpty(hostId)) { | 
|                 UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("user_id", user.getId())); | 
|                 if (userLogin != null) { | 
|                     return userLogin.getHostId(); | 
|                 } | 
|             } | 
|             return Long.parseLong(hostId); | 
|         } else { | 
|             return user.getHostId(); | 
|         } | 
|     } | 
|   | 
|     protected Long getHostId(String token){ | 
|         UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("token", token)); | 
|         if (userLogin == null) { | 
|             return null; | 
|         } | 
|         return userLogin.getHostId(); | 
|     } | 
|   | 
|     protected Long getUserId(){ | 
|         return Long.parseLong(String.valueOf(request.getAttribute("userId"))); | 
|     } | 
|   | 
|     protected Long getUserId(String token){ | 
|         UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("token", token)); | 
|         return userLogin.getUserId(); | 
|     } | 
|   | 
|     protected User getUser(){ | 
|         User user = userService.selectById(getUserId()); | 
|         if (null == user) { | 
|             throw new CoolException(BaseRes.DENIED); | 
|         } | 
|         return user; | 
|     } | 
|   | 
|     protected User getUser(String token){ | 
|         User user = userService.selectById(getUserId(token)); | 
|         if (null == user) { | 
|             throw new CoolException(BaseRes.DENIED); | 
|         } | 
|         return user; | 
|     } | 
|   | 
|     protected Long getDeptId() { | 
|         return getUser().getDeptId(); | 
|     } | 
|   | 
|     protected Role getRole(){ | 
|         Role role = roleService.selectById(getUser().getRoleId()); | 
|         if (null == role) { | 
|             throw new CoolException(BaseRes.DENIED); | 
|         } | 
|         return role; | 
|     } | 
|   | 
|     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 ""; | 
|     } | 
|   | 
|     protected Tag getOriginTag(){ | 
|         return tagService.getTop(); | 
|     } | 
|   | 
|     protected Node getOriginNode(){ | 
|         Node node = nodeService.getTop(); | 
|         if (node == null) { | 
|             throw new CoolException("库区数据错误"); | 
|         } | 
|         return node; | 
|     } | 
|   | 
|     protected Dept getOriginDept(){ | 
|         Dept dept = deptService.getTop(getHostId()); | 
|         if (dept == null) { | 
|             throw new CoolException("部门数据错误"); | 
|         } | 
|         return dept; | 
|     } | 
|   | 
|     protected <T> void hostEq(EntityWrapper<T> wrapper){ | 
|         Long hostId = getHostId(); | 
|         if (hostId != null) { | 
|             wrapper.eq("host_id", hostId); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 分页组装 | 
|      * @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; | 
|     } | 
|   | 
|     /** | 
|      * 全字段模糊搜索 | 
|      * @param cls 模型类 | 
|      * @param set 排除字段集合 | 
|      * @param condition 搜索内容 | 
|      */ | 
|     protected <T> void allLike(Class<T> cls, Set<String> set, EntityWrapper<T> wrapper, String condition){ | 
|         if (Cools.isEmpty(condition)) { | 
|             return; | 
|         } | 
|         List<String> columns = new ArrayList<>(); | 
|         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)) { | 
|                 if (!field.getAnnotation(TableField.class).exist()) { | 
|                     continue; | 
|                 } | 
|                 column = field.getAnnotation(TableField.class).value(); | 
|             } | 
|             if (Cools.isEmpty(column)) { | 
|                 column = field.getName(); | 
|             } | 
|             if (!set.contains(column)) { | 
|                 columns.add(column); | 
|             } | 
|         } | 
|         if (columns.isEmpty()) { | 
|             return; | 
|         } | 
|         for (int i=0;i<columns.size();i++){ | 
|             if (i==0){ | 
|                 wrapper.andNew(); | 
|             } else { | 
|                 wrapper.or(); | 
|             } | 
|             wrapper.like(columns.get(i), condition); | 
|         } | 
|     } | 
|   | 
|     //获取当前登录用户,对应权限下所有用户id。(普通职员获取自己,领导获取自己及下属,超级管理员全部) | 
|     protected ArrayList<Long> getUserRoleBelongsToUserId() { | 
|         User user = getUser(); | 
|         ArrayList<Long> userIds = new ArrayList<>(); | 
|         if (user.getRoleCode().equals("boss")) { | 
|             //全部权限 | 
|             userIds = null; | 
|         } else if (user.getRoleCode().equals("manager")) { | 
|             //自己及下属权限 | 
|             userIds.add(getUserId()); | 
|             EntityWrapper<User> entityWrapper = new EntityWrapper<>(); | 
|             entityWrapper.eq("dept_id", user.getDeptId()); | 
|             List<User> userList = userService.selectList(entityWrapper); | 
|             for (User item : userList) { | 
|                 userIds.add(item.getId()); | 
|             } | 
|         }else{ | 
|             //自己权限 | 
|             userIds.add(getUserId()); | 
|         } | 
|   | 
|         return userIds; | 
|     } | 
|   | 
|     protected ArrayList<Long> getUserRoleBelongsToUserId(String permission) { | 
|         User user = getUser(); | 
|         if (user.getDeptName().equals("平库规划组") || user.getDeptName().equals("核价组")) { | 
|             if (permission.equals("allopen")) { | 
|                 //权限全部开放 | 
|                 return null; | 
|             } | 
|         } | 
|   | 
|         //返回默认选择方案 | 
|         return getUserRoleBelongsToUserId(); | 
|     } | 
| } |