|  |  | 
 |  |  | package com.doumee.service.system.impl; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.Wrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.doumee.core.model.PageData; | 
 |  |  | import com.doumee.core.model.PageWrap; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import com.doumee.dao.business.dao.CompanyMapper; | 
 |  |  | import com.doumee.dao.business.model.Company; | 
 |  |  | import com.doumee.dao.system.SystemDataPermissionMapper; | 
 |  |  | import com.doumee.dao.system.SystemRoleMapper; | 
 |  |  | import com.doumee.dao.system.model.SystemDataPermission; | 
 |  |  | import com.doumee.dao.system.model.SystemRole; | 
 |  |  | import com.doumee.dao.system.model.SystemUser; | 
 |  |  | import com.doumee.dao.system.vo.SystemDataPermissionListVO; | 
 |  |  | import com.doumee.service.system.SystemDataPermissionService; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.Wrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.github.pagehelper.PageHelper; | 
 |  |  | import com.github.pagehelper.PageInfo; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.util.CollectionUtils; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.Collections; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 数据权限配置Service实现 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemRoleMapper systemRoleMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private CompanyMapper companyMapper; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Integer create(SystemDataPermission systemDataPermission) { | 
 |  |  | 
 |  |  |         Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission); | 
 |  |  |         return systemDataPermissionMapper.selectCount(wrapper); | 
 |  |  |     } | 
 |  |  |     @Override | 
 |  |  |     public SystemDataPermission findByRoleId(SystemDataPermission pageWrap){ | 
 |  |  |         if ( pageWrap.getRoleId() == null) { | 
 |  |  |             return null; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 查询数据权限 | 
 |  |  |         QueryWrapper<SystemDataPermission> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.lambda() | 
 |  |  |                 .eq(SystemDataPermission::getBusinessCode, "DEPARTMENT") | 
 |  |  |                 .eq(SystemDataPermission::getRoleId, pageWrap.getRoleId()) | 
 |  |  |                 .eq(SystemDataPermission::getDeleted, Boolean.FALSE) | 
 |  |  |                 .eq(SystemDataPermission::getDisabled, Boolean.FALSE); | 
 |  |  |         List<SystemDataPermission> result = systemDataPermissionMapper.selectList(queryWrapper); | 
 |  |  |         if(result!=null && result.size()>0){ | 
 |  |  |             return result.get(0); | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<Integer> selectHighRole(SystemDataPermission model, SystemRole role, SystemUser user) { | 
 |  |  |         List<SystemDataPermissionListVO>list =   systemDataPermissionMapper.selectUserList(model,role,user.getId()) ; | 
 |  |  |         if(list !=null){ | 
 |  |  |             boolean all = false; | 
 |  |  |             boolean departAndChild=false; | 
 |  |  |             boolean depart=false; | 
 |  |  |             boolean departAndLeaf=false; | 
 |  |  |             boolean custom=false; | 
 |  |  |             String c = ""; | 
 |  |  |             for(SystemDataPermissionListVO d : list){ | 
 |  |  |                 if(!all && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.all)){ | 
 |  |  |                     all =true; | 
 |  |  |                 } | 
 |  |  |                 if(!depart && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.depart)){ | 
 |  |  |                     depart =true; | 
 |  |  |                 } | 
 |  |  |                 if(!departAndChild && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.departAndChild)){ | 
 |  |  |                     departAndChild =true; | 
 |  |  |                 } | 
 |  |  |                 if(!departAndLeaf && Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.departAndLeaf)){ | 
 |  |  |                     departAndLeaf =true; | 
 |  |  |                 } | 
 |  |  |                 if(  Constants.equalsInteger(d.getType(),Constants.DATAPERMISSION_TYPE.custom)){ | 
 |  |  |                     custom =true; | 
 |  |  |                     c += StringUtils.defaultString(d.getCustomData(),""); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |             if(!all &&!departAndChild&& !depart && !departAndLeaf && !custom ){ | 
 |  |  |                 return  new ArrayList<>(); | 
 |  |  |             } | 
 |  |  |             Company department = new Company(); | 
 |  |  |             department.setId(user.getCompanyId()); | 
 |  |  |  | 
 |  |  |             if(all){ | 
 |  |  |                 return  null; | 
 |  |  |             } else{ | 
 |  |  |                 List<Integer> dList = new ArrayList<>(); | 
 |  |  |                 dList.add(-1);//虚拟部门,排查空集合 | 
 |  |  |                 if(custom) { | 
 |  |  |                     //如果有自定义部门 | 
 |  |  |                     String[] idStrs = c.split(","); | 
 |  |  |                     for(String s :idStrs){ | 
 |  |  |                         try { | 
 |  |  |                             Integer si = Integer.parseInt(s); | 
 |  |  |                             if(!isExists(si,dList)){ | 
 |  |  |                                 dList.add(si); | 
 |  |  |                             } | 
 |  |  |                         }catch (Exception e){ | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 if(departAndLeaf){ | 
 |  |  |                     if(department!= null){ | 
 |  |  |                         dList.addAll(userDataPermissonList(department,true)); | 
 |  |  |                     } | 
 |  |  |                 }else if(departAndChild){ | 
 |  |  |                     if(department!= null) { | 
 |  |  |                         dList.addAll(userDataPermissonList(department, false)); | 
 |  |  |                     } | 
 |  |  |                 }else if(depart){ | 
 |  |  |                     if(department!= null && !isExists(department.getId(), dList)){ | 
 |  |  |                         dList.add(department.getId()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 return  dList; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return new ArrayList<>(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public List<Integer> userDataPermissonList(Company depart ,boolean isleaf) { | 
 |  |  |         List<Integer> pool = new ArrayList<>(); | 
 |  |  |         List<Company> departList = companyMapper.selectList(new QueryWrapper<Company>().lambda() | 
 |  |  |                 .eq(Company::getIsdeleted,Constants.ZERO ) ); | 
 |  |  |         pool.add(depart.getId()); | 
 |  |  |         this.fillDepartChildren(pool, Arrays.asList(depart.getId()),isleaf,departList); | 
 |  |  |         return pool; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void fillDepartChildren(List<Integer> pool, List<Integer> asList, boolean isleaf, List<Company> departList) { | 
 |  |  |         List<Company> departments = getDepartListByParentIds(asList,departList); | 
 |  |  |         List<Integer> ids = departments.stream().map(Company::getId).collect(Collectors.toList()); | 
 |  |  |         if(isleaf){ | 
 |  |  |             if (ids.size() > 0) { | 
 |  |  |                 pool.addAll(ids); | 
 |  |  |                 this.fillDepartChildren(pool, ids,isleaf,departList); | 
 |  |  |             } | 
 |  |  |         }else{ | 
 |  |  |             pool.addAll(ids); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private List<Company> getDepartListByParentIds(List<Integer> asList, List<Company> departList) { | 
 |  |  |         List<Company> list = new ArrayList<>(); | 
 |  |  |         if(asList.size()>0){ | 
 |  |  |             for(Integer id : asList){ | 
 |  |  |                 list.addAll(getDepartListByParentId(id,departList)); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return list; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private List<Company> getDepartListByParentId(Integer pId, List<Company> departList) { | 
 |  |  |         List<Company> list = new ArrayList<>(); | 
 |  |  |         if(departList!= null){ | 
 |  |  |             for(Company d :departList){ | 
 |  |  |                 if(Constants.equalsInteger(d.getParentId(),pId)){ | 
 |  |  |                     list.add(d); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return  list; | 
 |  |  |     } | 
 |  |  |     private boolean isExists(Integer s, List<Integer> dList) { | 
 |  |  |         if(dList!=null){ | 
 |  |  |             for(Integer t : dList){ | 
 |  |  |                 if(Constants.equalsInteger(s,t)){ | 
 |  |  |                     return true; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return false; | 
 |  |  |     } | 
 |  |  | } |