| | |
| | | package com.yuanchu.limslaboratory.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.yuanchu.limslaboratory.mapper.MenuMapper; |
| | | import com.yuanchu.limslaboratory.mapper.RoleManageMapper; |
| | | import com.yuanchu.limslaboratory.pojo.LimitPage; |
| | | import com.yuanchu.limslaboratory.pojo.Menu; |
| | | import com.yuanchu.limslaboratory.pojo.RoleInfo; |
| | | import com.yuanchu.limslaboratory.pojo.RoleMenu; |
| | | import com.yuanchu.limslaboratory.pojo.dto.GetAllRoleAndMenuByConditionPageDto; |
| | | import com.yuanchu.limslaboratory.pojo.dto.RoleAndMenuDto; |
| | | import com.yuanchu.limslaboratory.pojo.dto.RoleInfoDto; |
| | | import com.yuanchu.limslaboratory.service.RoleManagerService; |
| | | import com.yuanchu.limslaboratory.utils.JsonUtil; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | @Resource |
| | | private MenuMapper menuMapper; |
| | | |
| | | @Resource |
| | | private RoleManageMapper roleManageMapper; |
| | | |
| | | @Override |
| | | public Object getMenusTree() { |
| | | List<Menu> menus = menuMapper.getMenuList(); |
| | |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean addRoleInfo(RoleInfoDto dto) { |
| | | //新增角色 |
| | | //System.out.println(dto); |
| | | LocalDateTime createTime = DateUtil.toLocalDateTime(DateUtil.date()); |
| | | RoleInfo role=new RoleInfo(); |
| | | long nextId = IdUtil.getSnowflake().nextId(); |
| | | role.setId(nextId); |
| | | role.setName(dto.getRoleName()); |
| | | role.setCreateTime(createTime); |
| | | int insert = roleManageMapper.insertRole(role); |
| | | List<Map<String, Object>>addBranch=new ArrayList<>(); |
| | | reverseTree(dto.getMenuData(),addBranch); |
| | | List<RoleMenu> roleMenuList =new ArrayList<>(); |
| | | addBranch.forEach(item->{ |
| | | roleMenuList.add(new RoleMenu(null,nextId, |
| | | Long.valueOf(String.valueOf(item.get("id"))) |
| | | ,Boolean.valueOf(String.valueOf(item.get("selected"))) |
| | | ,Boolean.valueOf(String.valueOf(item.get("added"))) |
| | | ,Boolean.valueOf(String.valueOf(item.get("deleted"))) |
| | | ,Boolean.valueOf(String.valueOf(item.get("updated"))) |
| | | ,createTime |
| | | )); |
| | | }); |
| | | //roleMenuAddList.forEach(System.out::println); |
| | | int i = roleManageMapper.insertBatchRoleMenu(roleMenuList); |
| | | return insert>0&&i>0; |
| | | } |
| | | |
| | | @Override |
| | | public boolean assertRepeat(String roleName) { |
| | | List<RoleInfo> assertName = roleManageMapper.getAssertName(roleName); |
| | | return assertName.size()<1; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getAllRoleAndMenuInfo(GetAllRoleAndMenuByConditionPageDto dto) { |
| | | dto.setCurrentPage((dto.getCurrentPage()-1)*dto.getCurrentPage()); |
| | | List<RoleAndMenuDto> roleAndMenuDtos = roleManageMapper.selectAllRoleAndMenu(dto); |
| | | roleAndMenuDtos.forEach(r->{ |
| | | List<RoleMenu>roleMenuList=r.getRoleMenuList(); |
| | | System.out.println(roleMenuList); |
| | | List<RoleMenu> collect = roleMenuList.stream() |
| | | .filter(t -> t.getParentId() == 0) |
| | | .peek((menu) -> menu.setChildren(this.getRoleMenuChildren(menu, roleMenuList))) |
| | | .collect(Collectors.toList()); |
| | | r.setRoleMenuList(collect); |
| | | }); |
| | | roleAndMenuDtos.forEach(r->{ |
| | | r.getRoleMenuList().forEach(System.out::println); |
| | | }); |
| | | Map<String, Object>result=new HashMap<>(3); |
| | | result.put("list",roleAndMenuDtos); |
| | | int size = roleManageMapper.getRoleByName(dto.getName()).size(); |
| | | result.put("total",size); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | private List<Menu> getEnumChildren(Menu root, List<Menu> all) { |
| | | return all.stream() |
| | | .filter(t -> Objects.equals(t.getParentId(), root.getValue())) |
| | | .filter(t -> Objects.equals(t.getParentId(), root.getId())) |
| | | .peek(g -> { |
| | | //找子菜单 |
| | | g.setChildren(getEnumChildren(g, all)); |
| | |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | private List<RoleMenu> getRoleMenuChildren(RoleMenu root, List<RoleMenu> all) { |
| | | return all.stream() |
| | | .filter(t -> Objects.equals(t.getParentId(), root.getMenuId())) |
| | | .peek(g -> { |
| | | //找子菜单 |
| | | g.setChildren(getRoleMenuChildren(g, all)); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | private void reverseTree(List<Map<String, Object>>tree, List<Map<String, Object>>addBranch){ |
| | | tree.forEach(t->{ |
| | | addBranch.add(t); |
| | | if(t.get("children")!=null){ |
| | | List<Map<String, Object>> children = JsonUtil.jsonToPojo( JsonUtil.jsonToString(t.get("children")), List.class); |
| | | addBranch.addAll(children); |
| | | } |
| | | }); |
| | | } |
| | | } |