¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.dnc.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | 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.jeecg.modules.dnc.ucenter.UserDepartExt; |
| | | import org.jeecg.modules.dnc.utils.ValidateUtil; |
| | | import org.jeecg.modules.mdc.entity.MdcEquipment; |
| | | import org.jeecg.modules.mdc.mapper.MdcEquipmentMapper; |
| | | import org.jeecg.modules.mdc.model.MdcEquipmentTree; |
| | | import org.jeecg.modules.mdc.util.FindsEquipmentProductionUtil; |
| | | import org.jeecg.modules.system.entity.MdcProduction; |
| | | import org.jeecg.modules.system.entity.SysUser; |
| | | import org.jeecg.modules.system.service.IMdcProductionService; |
| | | import org.jeecg.modules.system.service.IMdcUserProductionService; |
| | | import org.jeecg.modules.system.service.ISysUserService; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | 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 { |
| | | @Resource |
| | | private IMdcProductionService mdcProductionService; |
| | | @Resource |
| | | private IMdcUserProductionService mdcUserProductionService; |
| | | @Resource |
| | | private MdcEquipmentMapper mdcEquipmentMapper; |
| | | @Resource |
| | | private ISysUserService sysUserService; |
| | | |
| | | @Override |
| | | public DevicePermission getByUserIdAndDeviceId(String userId, String deviceId) { |
| | | if(!ValidateUtil.validateString(userId) || !ValidateUtil.validateString(deviceId)) |
| | | return null; |
| | | List<DevicePermission> list = super.lambdaQuery().eq(DevicePermission::getDeviceId, deviceId).eq(DevicePermission::getUserId, userId).list(); |
| | | if(list == null || list.isEmpty()) |
| | | return null; |
| | | return list.get(0); |
| | | } |
| | | |
| | | @Override |
| | | public List<UserDepartExt> getUserPermsByDeviceId(String deviceId) { |
| | | return super.getBaseMapper().getUserPermsByDeviceId(deviceId); |
| | | } |
| | | |
| | | @Override |
| | | public List<SysUser> getUserNonPermsByDeviceId(String deviceId) { |
| | | return super.getBaseMapper().getUserNonPermsByDeviceId(deviceId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class}) |
| | | public boolean removeByCollection(List<DevicePermission> devicePermissions) { |
| | | if(devicePermissions == null || devicePermissions.isEmpty()) |
| | | return false; |
| | | if(devicePermissions.size() == 1) |
| | | return super.removeById(devicePermissions.get(0).getDevicePermId()); |
| | | List<String> ids = new ArrayList<>(); |
| | | devicePermissions.forEach(item -> { |
| | | ids.add(item.getDevicePermId()); |
| | | }); |
| | | return super.removeByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class}) |
| | | public boolean deleteByDeviceId(String deviceId) { |
| | | LambdaQueryWrapper<DevicePermission> lambdaQueryWrapper = Wrappers.lambdaQuery(); |
| | | lambdaQueryWrapper.eq(DevicePermission::getDeviceId, deviceId); |
| | | return super.remove(lambdaQueryWrapper); |
| | | } |
| | | |
| | | @Override |
| | | public List<DevicePermission> getByDeviceId(String deviceId) { |
| | | List<DevicePermission> list = super.lambdaQuery().eq(DevicePermission::getDeviceId, deviceId).list(); |
| | | if(list == null) |
| | | list = Collections.emptyList(); |
| | | return list; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * * å è½½äº§çº¿è®¾å¤æ |
| | | */ |
| | | @Override |
| | | 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 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 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); |
| | | } |
| | | }); |
| | | // æå建æ¶é´ååºæåºï¼ç©ºå¼æå¨æåï¼ |
| | | filteredProductions.sort( |
| | | Comparator.comparing( |
| | | MdcProduction::getCreateTime, |
| | | Comparator.nullsLast(Comparator.naturalOrder()) // èªç¶é¡ºåºå³ååº |
| | | ) |
| | | ); |
| | | 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表示产线èç¹ |
| | | } |
| | | |
| | | } |