| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.modules.dnc.entity.DevicePermission; |
| | | import org.jeecg.modules.dnc.mapper.DevicePermissionMapper; |
| | | import org.jeecg.modules.dnc.service.IDevicePermissionService; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | @Service |
| | | public class DevicePermissionServiceImpl extends ServiceImpl<DevicePermissionMapper, DevicePermission> implements IDevicePermissionService { |
| | |
| | | |
| | | |
| | | /** |
| | | * 加载产线设备树 |
| | | * * 加载产线设备树 |
| | | */ |
| | | @Override |
| | | public List<MdcEquipmentTree> DncLoadTreeListByProduction(String userId){ |
| | | //获取所有产线数据 |
| | | List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder)); |
| | | //根据用户id获取拥有的产线信息集合 |
| | | List<String> productionIds = mdcUserProductionService.queryProductionIdsByUserId(userId); |
| | | List<String> allProductionIds = new ArrayList<>(); |
| | | //找到所有产线id的上级id |
| | | if (productionIds != null && !productionIds.isEmpty()) { |
| | | for (String productionId : productionIds) { |
| | | this.getAllProductionIds(productionList, productionId, allProductionIds); |
| | | } |
| | | } |
| | | //过滤产线数据 |
| | | List<MdcProduction> list = productionList.stream().filter((MdcProduction mdcProduction) -> allProductionIds.contains(mdcProduction.getId())).collect(Collectors.toList()); |
| | | //组装产线设备树 |
| | | List<MdcEquipmentTree> treeList = FindsEquipmentProductionUtil.wrapEquipmentProductionTreeList(list); |
| | | //填充设备数据 |
| | | FillEquipmentByProduction(treeList,userId); |
| | | public List<MdcEquipmentTree> DncLoadTreeListByProduction() { |
| | | LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | |
| | | // 获取所有有效产线并建立快速查询映射 |
| | | Map<String, MdcProduction> productionMap = getValidProductions(); |
| | | |
| | | // 获取用户权限相关的所有产线ID(包含层级祖先) |
| | | Set<String> authorizedProductionIds = getAuthorizedProductionIds(user.getId(), productionMap); |
| | | |
| | | // 构建产线树结构 |
| | | List<MdcEquipmentTree> treeList = buildProductionTree(new ArrayList<>(authorizedProductionIds), productionMap); |
| | | |
| | | // 批量填充设备数据 |
| | | fillEquipmentData(treeList, user); |
| | | |
| | | return treeList; |
| | | } |
| | | |
| | | /** |
| | | * 获取所有的产线id(包含所有上级) |
| | | */ |
| | | private void getAllProductionIds(List<MdcProduction> productionList, String productionId, List<String> allProductionIds) { |
| | | if (!allProductionIds.contains(productionId)) { |
| | | allProductionIds.add(productionId); |
| | | } |
| | | for (MdcProduction mdcProduction : productionList) { |
| | | if (StringUtils.isEmpty(mdcProduction.getParentId())) { |
| | | continue; |
| | | } |
| | | if (productionId.equals(mdcProduction.getId())) { |
| | | if (!allProductionIds.contains(mdcProduction.getParentId())) { |
| | | allProductionIds.add(mdcProduction.getParentId()); |
| | | getAllProductionIds(productionList, mdcProduction.getParentId(), allProductionIds); |
| | | } |
| | | } |
| | | * 获取所有有效产线并建立ID映射 |
| | | */ |
| | | private Map<String, MdcProduction> getValidProductions() { |
| | | List<MdcProduction> productions = mdcProductionService.list( |
| | | new LambdaQueryWrapper<MdcProduction>() |
| | | .eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0) |
| | | .orderByAsc(MdcProduction::getProductionOrder) |
| | | ); |
| | | return productions.stream().collect(Collectors.toMap(MdcProduction::getId, Function.identity())); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户权限相关的所有产线ID(包含层级祖先) |
| | | */ |
| | | private Set<String> getAuthorizedProductionIds(String userId, Map<String, MdcProduction> productionMap) { |
| | | Set<String> result = new HashSet<>(); |
| | | |
| | | // 获取直接关联的产线ID |
| | | List<String> directProductionIds = Optional.ofNullable(mdcUserProductionService.queryProductionIdsByUserId(userId)) |
| | | .orElse(Collections.emptyList()); |
| | | |
| | | // 递归收集所有层级ID |
| | | directProductionIds.forEach(id -> collectHierarchyIds(id, productionMap, result)); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 递归收集层级ID(包含自身及所有祖先) |
| | | */ |
| | | private void collectHierarchyIds(String productionId, Map<String, MdcProduction> productionMap, Set<String> collector) { |
| | | if (collector.contains(productionId)) return; |
| | | |
| | | MdcProduction current = productionMap.get(productionId); |
| | | if (current == null) return; |
| | | |
| | | collector.add(productionId); |
| | | if (StringUtils.isNotBlank(current.getParentId())) { |
| | | collectHierarchyIds(current.getParentId(), productionMap, collector); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 产线设备树填充设备数据 |
| | | */ |
| | | private void FillEquipmentByProduction(List<MdcEquipmentTree> treeList,String userId) { |
| | | //获取已分配权限设备数据 |
| | | List<String> deviceIds=super.list(new QueryWrapper<DevicePermission>().eq("user_id",userId)).stream().map(DevicePermission::getDeviceId).collect(Collectors.toList()); |
| | | if (!deviceIds.isEmpty()){ |
| | | for (MdcEquipmentTree mdcEquipmentTree : treeList) { |
| | | List<MdcEquipment> equipmentList = mdcEquipmentMapper.queryByProductionIdAndType(mdcEquipmentTree.getKey(),deviceIds); |
| | | if (equipmentList != null && !equipmentList.isEmpty()) { |
| | | for (MdcEquipment mdcEquipment : equipmentList) { |
| | | if (mdcEquipment.getCreateBy()!=null&& !mdcEquipment.getCreateBy().isEmpty()){ |
| | | mdcEquipment.setCreateBy(sysUserService.getUserByName(mdcEquipment.getCreateBy()).getRealname()); |
| | | } |
| | | if (mdcEquipment.getUpdateBy()!=null&& !mdcEquipment.getUpdateBy().isEmpty()){ |
| | | mdcEquipment.setUpdateBy(sysUserService.getUserByName(mdcEquipment.getUpdateBy()).getRealname()); |
| | | } |
| | | MdcEquipmentTree tree = new MdcEquipmentTree().convert(mdcEquipment); |
| | | tree.setParentId(mdcEquipmentTree.getKey()); |
| | | tree.setEntity(mdcEquipment); |
| | | tree.setType(2); |
| | | mdcEquipmentTree.getChildren().add(tree); |
| | | } |
| | | mdcEquipmentTree.setLeaf(false); |
| | | } |
| | | if (!mdcEquipmentTree.getChildren().isEmpty()) { |
| | | FillEquipmentByProduction(mdcEquipmentTree.getChildren(),userId); |
| | | } |
| | | } |
| | | 构建产线树结构 |
| | | */ |
| | | private List<MdcEquipmentTree> buildProductionTree(List<String> productionIds, Map<String, MdcProduction> productionMap) { |
| | | |
| | | List<MdcProduction> filteredProductions = new ArrayList<>(); |
| | | productionIds.forEach(id -> { |
| | | MdcProduction production = productionMap.get(id); |
| | | if (production != null) { |
| | | filteredProductions.add(production); |
| | | } |
| | | }); |
| | | return FindsEquipmentProductionUtil.wrapEquipmentProductionTreeList(filteredProductions); |
| | | } |
| | | /** |
| | | * 批量填充设备数据 |
| | | */ |
| | | private void fillEquipmentData(List<MdcEquipmentTree> treeList, LoginUser user) { |
| | | // 获取用户设备权限 |
| | | Set<String> deviceIds = getAuthorizedDeviceIds(user.getId()); |
| | | if (deviceIds.isEmpty()) return; |
| | | |
| | | // 批量获取所有相关设备 |
| | | Map<String, List<MdcEquipment>> equipmentMap = getEquipmentMap(user, deviceIds); |
| | | |
| | | // 批量获取用户信息 |
| | | Map<String, String> userRealNameMap = getUserRealNameMapping(equipmentMap.values()); |
| | | |
| | | // 填充设备数据到树节点 |
| | | populateEquipmentNodes(treeList, equipmentMap, userRealNameMap); |
| | | } |
| | | |
| | | /** |
| | | * 获取授权设备ID集合 |
| | | */ |
| | | private Set<String> getAuthorizedDeviceIds(String userId) { |
| | | return super.list(new QueryWrapper<DevicePermission>().select("device_id").eq("user_id", userId)) |
| | | .stream() |
| | | .map(DevicePermission::getDeviceId) |
| | | .collect(Collectors.toSet()); |
| | | } |
| | | |
| | | /** |
| | | * 批量获取设备数据并按产线分组 |
| | | */ |
| | | private Map<String, List<MdcEquipment>> getEquipmentMap(LoginUser user, Set<String> deviceIds) { |
| | | List<String> productionIds=Arrays.asList(user.getProductionIds().split(",")); |
| | | return mdcEquipmentMapper.queryByDepartIdsAndType(productionIds, new ArrayList<>(deviceIds)) |
| | | .stream() |
| | | .collect(Collectors.groupingBy(MdcEquipment::getProductionId)); |
| | | } |
| | | |
| | | |
| | | /*** 批量获取用户真实姓名映射 |
| | | */ |
| | | private Map<String, String> getUserRealNameMapping(Collection<List<MdcEquipment>> equipmentLists) { |
| | | Set<String> userNames = equipmentLists.stream() |
| | | .flatMap(List::stream) |
| | | .flatMap(e -> Stream.of(e.getCreateBy(), e.getUpdateBy())) |
| | | .filter(StringUtils::isNotBlank) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | return sysUserService.getUserRealNamesByUserNames(new ArrayList<>(userNames)); |
| | | } |
| | | |
| | | /** |
| | | * 填充设备节点数据 |
| | | */ |
| | | private void populateEquipmentNodes(List<MdcEquipmentTree> nodes, |
| | | Map<String, List<MdcEquipment>> equipmentMap, |
| | | Map<String, String> userRealNameMap) { |
| | | for (MdcEquipmentTree node : nodes) { |
| | | if (!isProductionNode(node)) continue; |
| | | |
| | | List<MdcEquipment> equipments = equipmentMap.getOrDefault(node.getKey(), Collections.emptyList()); |
| | | List<MdcEquipmentTree> equipmentNodes = convertToEquipmentNodes(equipments, userRealNameMap, node.getKey()); |
| | | |
| | | node.getChildren().addAll(equipmentNodes); |
| | | node.setLeaf(equipmentNodes.isEmpty()); |
| | | |
| | | // 递归处理子节点 |
| | | populateEquipmentNodes(node.getChildren(), equipmentMap, userRealNameMap); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换设备数据为树节点 |
| | | */ |
| | | private List<MdcEquipmentTree> convertToEquipmentNodes(List<MdcEquipment> equipments, |
| | | Map<String, String> userRealNameMap, |
| | | String parentId) { |
| | | return equipments.stream().map(e -> { |
| | | // 更新用户显示名称 |
| | | Optional.ofNullable(e.getCreateBy()).ifPresent(name -> |
| | | e.setCreateBy(userRealNameMap.getOrDefault(name, name))); |
| | | Optional.ofNullable(e.getUpdateBy()).ifPresent(name -> |
| | | e.setUpdateBy(userRealNameMap.getOrDefault(name, name))); |
| | | |
| | | MdcEquipmentTree node = new MdcEquipmentTree().convert(e); |
| | | node.setParentId(parentId); |
| | | node.setEntity(e); |
| | | node.setType(2); // 设备节点类型 |
| | | return node; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 判断是否为产线节点(根据业务逻辑定义) |
| | | */ |
| | | private boolean isProductionNode(MdcEquipmentTree node) { |
| | | return node.getType() == 1; // 假设类型1表示产线节点 |
| | | } |
| | | |
| | | } |