New file |
| | |
| | | package com.kidgrow.usercenter.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.kidgrow.common.model.*; |
| | | import com.kidgrow.common.service.impl.SuperServiceImpl; |
| | | import com.kidgrow.usercenter.dto.SysRoleOrganizationDto; |
| | | import com.kidgrow.usercenter.dto.SysRoleOrganizationMybatisDto; |
| | | import com.kidgrow.usercenter.mapper.SysRoleMapper; |
| | | import com.kidgrow.usercenter.mapper.SysRoleOrganizationMapper; |
| | | import com.kidgrow.usercenter.model.SysRoleOrganization; |
| | | import com.kidgrow.usercenter.service.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.collections4.MapUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> |
| | | * |
| | | * @version 1.0 |
| | | * @Description: 角色对应的 部门<br> |
| | | * @Project: 用户中心<br> |
| | | * @CreateDate: Created in 2020-04-21 14:24:41 <br> |
| | | * @Author: <a href="4345453@kidgrow.com">liuke</a> |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class SysRoleOrganizationServiceImpl extends SuperServiceImpl<SysRoleOrganizationMapper, SysRoleOrganization> implements ISysRoleOrganizationService { |
| | | |
| | | |
| | | private final Integer TYPE_ZI_DING_YI = 2;//自定义数据权限 |
| | | @Autowired |
| | | private ISysOrganizationService iSysOrganizationService; |
| | | @Autowired |
| | | private ISysRoleService iSysRoleService; |
| | | @Autowired |
| | | private ISysRoleUserService iSysRoleUserService; |
| | | @Autowired |
| | | private ISysUserOrgService iSysUserOrgService; |
| | | @Autowired |
| | | private SysOrganizationServiceImpl sysOrganizationService; |
| | | @Autowired |
| | | private SysRoleMapper sysRoleMapper; |
| | | |
| | | /** |
| | | * 列表 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public PageResult<SysRoleOrganization> findList(Map<String, Object> params) { |
| | | Page<SysRoleOrganization> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit")); |
| | | List<SysRoleOrganization> list = baseMapper.findList(page, params); |
| | | return PageResult.<SysRoleOrganization>builder().data(list).code(0).count(page.getTotal()).build(); |
| | | } |
| | | |
| | | /** |
| | | * 根据SysRoleOrganization对象当做查询条件进行查询 |
| | | * |
| | | * @param sysRoleOrganization |
| | | * @return SysRoleOrganization |
| | | */ |
| | | @Override |
| | | public SysRoleOrganization findByObject(SysRoleOrganization sysRoleOrganization) { |
| | | return baseMapper.findByObject(sysRoleOrganization); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean saveOrUpdateSer(SysRoleOrganizationDto sysRoleOrganizationDto, SysUser user) { |
| | | if (sysRoleOrganizationDto.getRoleId() != null) { |
| | | SysRole sysRole = iSysRoleService.getById(sysRoleOrganizationDto.getRoleId()); |
| | | sysRole.setType(sysRoleOrganizationDto.getType()); |
| | | boolean b = iSysRoleService.updateById(sysRole); |
| | | } |
| | | SysRoleOrganization sysRoleOrganization = new SysRoleOrganization(); |
| | | sysRoleOrganization.setRoleId(sysRoleOrganizationDto.getRoleId()); |
| | | //先清除数据 |
| | | if (sysRoleOrganizationDto.getRoleId() != null) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("role_id", sysRoleOrganizationDto.getRoleId()); |
| | | map.put("is_del", 0); |
| | | List<SysRoleOrganization> sysRoleOrganizations = baseMapper.selectByMap(map); |
| | | List<Long> collect = sysRoleOrganizations.stream().map(e -> e.getId()).collect(Collectors.toList()); |
| | | if (collect.size() > 0) { |
| | | int i = baseMapper.deleteBatchIds(collect); |
| | | } |
| | | } |
| | | //2自定义数据权限 |
| | | if (sysRoleOrganizationDto.getType() == TYPE_ZI_DING_YI) { |
| | | List<Long> orgIds = sysRoleOrganizationDto.getOrgIds(); |
| | | orgIds.forEach(e -> { |
| | | sysRoleOrganization.setId(null); |
| | | sysRoleOrganization.setOrgId(e); |
| | | SysOrganization byId = iSysOrganizationService.getById(e); |
| | | if (byId != null) { |
| | | sysRoleOrganization.setOrgCode(byId.getOrgCode()); |
| | | } |
| | | baseMapper.insert(sysRoleOrganization); |
| | | }); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public ResultBody getTree(Map<String, Object> params, SysUser user) { |
| | | List<SysOrganization> sysOrganizations = sysOrganizationService.listByMap(params); |
| | | QueryWrapper queryWrapper = new QueryWrapper(); |
| | | List<Long> roleIdList = user.getRoles().stream().map(e -> e.getId()).collect(Collectors.toList()); |
| | | queryWrapper.in("role_id", roleIdList); |
| | | List<SysRoleOrganization> list = baseMapper.selectList(queryWrapper); |
| | | List<Long> collect = list.stream().map(e -> e.getOrgId()).collect(Collectors.toList()); |
| | | List<Map<String, Object>> treeData = getTreeData(Long.valueOf("-1"), sysOrganizations, collect); |
| | | return ResultBody.ok().data(treeData); |
| | | } |
| | | |
| | | /** |
| | | * 将数据 封装成 tree (递归方式) |
| | | * |
| | | * @param MyId |
| | | * @param sysOrganizations |
| | | * @return |
| | | */ |
| | | public List<Map<String, Object>> getTreeData(Long MyId, List<SysOrganization> sysOrganizations, List<Long> col) { |
| | | List<Map<String, Object>> listMap = new ArrayList<>(); |
| | | Map<Long, SysOrganization> collect = sysOrganizations.stream().collect(Collectors.toMap(SysOrganization::getId, SysOrganization -> SysOrganization)); |
| | | List<Long> idList = sysOrganizations.stream().filter(e -> e.getOrgParentId() .equals(MyId)).map(e -> e.getId()).collect(Collectors.toList()); |
| | | for (Long id : idList |
| | | ) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("id", "" + id); |
| | | map.put("name", collect.get(id).getOrgName()); |
| | | map.put("level", collect.get(id).getOrgLevel()); |
| | | map.put("checked", col.contains(id)); |
| | | List<Long> childs = sysOrganizations.stream().filter(e -> e.getOrgParentId().equals(id)).map(e -> e.getId()).collect(Collectors.toList()); |
| | | if (childs.size() > 0) { |
| | | List<Map<String, Object>> treeData = getTreeData(id, sysOrganizations, col); |
| | | map.put("children", treeData); |
| | | } |
| | | listMap.add(map); |
| | | } |
| | | return listMap; |
| | | } |
| | | |
| | | /** |
| | | * 将权限 分装到map对像 |
| | | * |
| | | * @param params |
| | | * @param user |
| | | */ |
| | | |
| | | public void getRoleOrg(Map<String, Object> params, SysUser user) { |
| | | //获取角色接口 |
| | | List<SysRole> roles = iSysRoleUserService.findRolesByUserId(user.getId()); |
| | | List<SysOrganization> listUser = iSysUserOrgService.getListUser(user.getId()); |
| | | List<Map<Integer, Object>> listroleOrg = new ArrayList<>(); |
| | | if(listUser!=null){ |
| | | if (!roles.isEmpty()) { |
| | | int index=0; |
| | | for (SysRole e : roles) { |
| | | Map<Integer, Object> rolemap = new HashMap<>(); |
| | | SysRoleOrganizationMybatisDto dto=new SysRoleOrganizationMybatisDto(); |
| | | if(e.getType() == 1){ |
| | | dto.setFlag(index==0); |
| | | dto.setObject(user.getId()); |
| | | rolemap.put(e.getType(), dto); |
| | | }else if (e.getType() == 2) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("role_id", e.getId()); |
| | | List<SysRoleOrganization> sysRoleOrganizations = baseMapper.selectByMap(map); |
| | | if (sysRoleOrganizations.size() > 0) { |
| | | dto.setFlag(index==0); |
| | | dto.setObject(sysRoleOrganizations); |
| | | rolemap.put(e.getType(), dto); |
| | | } |
| | | } else if (e.getType() == 3) { |
| | | dto.setFlag(index==0); |
| | | dto.setObject(user.getOrganizations().get(user.getOrganizations().size() - 1).getOrgCode()); |
| | | rolemap.put(e.getType(), dto); |
| | | } else if (e.getType() == 4) { |
| | | dto.setFlag(index==0); |
| | | dto.setObject(user.getOrganizations().get(user.getOrganizations().size() - 1).getOrgCode()); |
| | | rolemap.put(e.getType(), dto); |
| | | } else if (e.getType() == 5) { |
| | | dto.setFlag(index==0); |
| | | dto.setObject(user.getId()); |
| | | rolemap.put(e.getType(), dto); |
| | | } |
| | | index+=1; |
| | | listroleOrg.add(rolemap); |
| | | }; |
| | | } |
| | | } |
| | | //添加权限控制 |
| | | params.put("roleOrg", listroleOrg); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getRoleOrgMap(SysUser user) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | getRoleOrg(map, user); |
| | | return map; |
| | | } |
| | | } |