| 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.service.business.third.model.PageData; | 
| import com.doumee.service.business.third.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.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实现 | 
|  * @author Eva.Caesar Liu | 
|  * @date 2023/03/21 14:49 | 
|  */ | 
| @Service | 
| public class SystemDataPermissionServiceImpl implements SystemDataPermissionService { | 
|   | 
|     @Autowired | 
|     private SystemDataPermissionMapper systemDataPermissionMapper; | 
|   | 
|     @Autowired | 
|     private SystemRoleMapper systemRoleMapper; | 
|     @Autowired | 
|     private CompanyMapper companyMapper; | 
|   | 
|     @Override | 
|     public Integer create(SystemDataPermission systemDataPermission) { | 
|         systemDataPermissionMapper.insert(systemDataPermission); | 
|         return systemDataPermission.getId(); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteById(Integer id) { | 
|         SystemDataPermission dataPermission = new SystemDataPermission(); | 
|         dataPermission.setId(id); | 
|         dataPermission.setDeleted(Boolean.TRUE); | 
|         this.updateById(dataPermission); | 
|     } | 
|   | 
|     @Override | 
|     public void delete(SystemDataPermission systemDataPermission) { | 
|         UpdateWrapper<SystemDataPermission> deleteWrapper = new UpdateWrapper<>(systemDataPermission); | 
|         systemDataPermissionMapper.delete(deleteWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteByIdInBatch(List<Integer> ids) { | 
|         if (CollectionUtils.isEmpty(ids)) { | 
|             return; | 
|         } | 
|         systemDataPermissionMapper.deleteBatchIds(ids); | 
|     } | 
|   | 
|     @Override | 
|     public void updateById(SystemDataPermission systemDataPermission) { | 
|         systemDataPermissionMapper.updateById(systemDataPermission); | 
|     } | 
|   | 
|     @Override | 
|     public void updateByIdInBatch(List<SystemDataPermission> systemDataPermissions) { | 
|         if (CollectionUtils.isEmpty(systemDataPermissions)) { | 
|             return; | 
|         } | 
|         for (SystemDataPermission systemDataPermission: systemDataPermissions) { | 
|             this.updateById(systemDataPermission); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public SystemDataPermission findById(Integer id) { | 
|         return systemDataPermissionMapper.selectById(id); | 
|     } | 
|   | 
|     @Override | 
|     public SystemDataPermission findOne(SystemDataPermission systemDataPermission) { | 
|         Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission); | 
|         return systemDataPermissionMapper.selectOne(wrapper); | 
|     } | 
|   | 
|     @Override | 
|     public List<SystemDataPermission> findList(SystemDataPermission systemDataPermission) { | 
|         Wrapper<SystemDataPermission> wrapper = new QueryWrapper<>(systemDataPermission); | 
|         return systemDataPermissionMapper.selectList(wrapper); | 
|     } | 
|    | 
|     @Override | 
|     public PageData<SystemDataPermissionListVO> findPage(PageWrap<SystemDataPermission> pageWrap) { | 
|         PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         return PageData.from(new PageInfo<>(systemDataPermissionMapper.selectManageList(pageWrap.getModel(), pageWrap.getOrderByClause()))); | 
|     } | 
|   | 
|     @Override | 
|     public List<SystemDataPermission> findDataPermission(String businessCode, List<String> roles) { | 
|         if (CollectionUtils.isEmpty(roles)) { | 
|             return Collections.emptyList(); | 
|         } | 
|         // 查询角色 | 
|         QueryWrapper<SystemRole> queryRoleWrapper = new QueryWrapper<>(); | 
|         queryRoleWrapper.lambda().in(SystemRole::getCode, roles).eq(SystemRole::getDeleted, Boolean.FALSE); | 
|         List<SystemRole> systemRoles = systemRoleMapper.selectList(queryRoleWrapper); | 
|         if (CollectionUtils.isEmpty(systemRoles)) { | 
|             return Collections.emptyList(); | 
|         } | 
|         List<Integer> roleIds = new ArrayList<>(); | 
|         for (SystemRole role : systemRoles) { | 
|             roleIds.add(role.getId()); | 
|         } | 
|         // 查询数据权限 | 
|         QueryWrapper<SystemDataPermission> queryWrapper = new QueryWrapper<>(); | 
|         queryWrapper.lambda() | 
|                 .eq(SystemDataPermission::getBusinessCode, businessCode) | 
|                 .in(SystemDataPermission::getRoleId, roleIds) | 
|                 .eq(SystemDataPermission::getDeleted, Boolean.FALSE) | 
|                 .eq(SystemDataPermission::getDisabled, Boolean.FALSE); | 
|         return systemDataPermissionMapper.selectList(queryWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public long count(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; | 
|     } | 
| } |