package org.jeecg.modules.mdc.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.base.CaseFormat; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.mdc.dto.EquipmentTemperatureDto; import org.jeecg.modules.mdc.dto.MdcEquDepDto; import org.jeecg.modules.mdc.dto.MdcEquProDto; import org.jeecg.modules.mdc.dto.MdcEquipmentDto; import org.jeecg.modules.mdc.entity.*; import org.jeecg.modules.mdc.mapper.MdcEquipmentMapper; import org.jeecg.modules.mdc.mapper.XYZAliasesMapper; import org.jeecg.modules.mdc.model.MdcEquipmentTree; import org.jeecg.modules.mdc.service.*; import org.jeecg.modules.mdc.util.DateUtils; import org.jeecg.modules.mdc.util.FindsEquipmentDepartUtil; import org.jeecg.modules.mdc.util.FindsEquipmentProductionUtil; import org.jeecg.modules.mdc.vo.MdcEquipmentDepVo; import org.jeecg.modules.mdc.vo.MdcEquipmentProVo; import org.jeecg.modules.mdc.vo.MdcEquipmentVo; import org.jeecg.modules.system.entity.MdcEquipmentDepart; import org.jeecg.modules.system.entity.MdcProduction; import org.jeecg.modules.system.entity.MdcProductionEquipment; import org.jeecg.modules.system.entity.SysDepart; import org.jeecg.modules.system.mapper.MdcEquipmentDepartMapper; import org.jeecg.modules.system.mapper.MdcProductionEquipmentMapper; import org.jeecg.modules.system.service.IMdcProductionService; import org.jeecg.modules.system.service.IMdcUserProductionService; import org.jeecg.modules.system.service.ISysDepartService; import org.jeecg.modules.system.service.ISysUserDepartService; import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @Description: 设备表 * @Author: liuS * @Date: 2023-03-22 * @Version: V1.0 */ @Service public class MdcEquipmentServiceImpl extends ServiceImpl implements IMdcEquipmentService { @Resource private MdcEquipmentDepartMapper mdcEquipmentDepartMapper; @Resource private MdcProductionEquipmentMapper mdcProductionEquipmentMapper; @Resource private ISysUserDepartService sysUserDepartService; @Resource private IMdcUserProductionService mdcUserProductionService; @Resource private ISysDepartService sysDepartService; @Resource private IMdcProductionService mdcProductionService; @Resource private IControlSystemService controlSystemService; @Resource private IEquipmentWorkLineService equipmentWorkLineService; @Resource private IMdcDriveTypeParamConfigService mdcDriveTypeParamConfigService; @Resource private XYZAliasesMapper xyzAliasesMapper; @Resource private IEquipmentXYZService equipmentXYZService; @Resource private IEquipmentBaseInfoService equipmentBaseInfoService; @Resource private IEquipmentTemperatureService equipmentTemperatureService; @Override public Map getDepNamesByEquipmentIds(List equipmentIds) { List list = this.baseMapper.getDepNamesByEquipmentIds(equipmentIds); Map res = new HashMap(5); list.forEach(item -> res.merge(item.getEquipmentId(), item.getDepartName(), (a, b) -> a + "," + b)); return res; } @Override public Map getProNamesByEquipmentIds(List equipmentIds) { List list = this.baseMapper.getProNamesByEquipmentIds(equipmentIds); Map res = new HashMap(5); list.forEach(item -> res.merge(item.getEquipmentId(), item.getProductionName(), (a, b) -> a + "," + b)); return res; } @Override @Transactional(rollbackFor = Exception.class) public void saveMdcEquipment(MdcEquipment mdcEquipment, String selectedDeparts, String selectedProduction) { //step.1 保存设备 this.save(mdcEquipment); //step.2 保存所属部门 if (oConvertUtils.isNotEmpty(selectedDeparts)) { String[] arr = selectedDeparts.split(","); for (String departId : arr) { MdcEquipmentDepart equipmentDepart = new MdcEquipmentDepart(mdcEquipment.getId(), departId); mdcEquipmentDepartMapper.insert(equipmentDepart); } } //step.3 保存所属产线 if (oConvertUtils.isNotEmpty(selectedProduction)) { String[] arr = selectedProduction.split(","); for (String productionId : arr) { MdcProductionEquipment mdcProductionEquipment = new MdcProductionEquipment(mdcEquipment.getId(), productionId); mdcProductionEquipmentMapper.insert(mdcProductionEquipment); } } } @Override @Transactional(rollbackFor = Exception.class) @CacheEvict(value = {"mdc:cache:encrypt:equipment"}, allEntries = true) public void editMdcEquipment(MdcEquipment mdcEquipment) { //step.1 修改设备基础信息 this.updateById(mdcEquipment); //step.2 修改部门 String departs = mdcEquipment.getSelectedDeparts(); String[] arr = {}; if (oConvertUtils.isNotEmpty(departs)) { arr = departs.split(","); } //先删后加 mdcEquipmentDepartMapper.delete(new LambdaQueryWrapper().eq(MdcEquipmentDepart::getEquipmentId, mdcEquipment.getId())); if (oConvertUtils.isNotEmpty(departs)) { for (String departId : arr) { MdcEquipmentDepart equipmentDepart = new MdcEquipmentDepart(mdcEquipment.getId(), departId); mdcEquipmentDepartMapper.insert(equipmentDepart); } } //step.3 修改产线 String productions = mdcEquipment.getSelectedProduction(); String[] array = {}; if (oConvertUtils.isNotEmpty(productions)) { array = productions.split(","); } //先删后加 mdcProductionEquipmentMapper.delete(new LambdaQueryWrapper().eq(MdcProductionEquipment::getEquipmentId, mdcEquipment.getId())); if (oConvertUtils.isNotEmpty(productions)) { for (String productionId : array) { MdcProductionEquipment productionEquipment = new MdcProductionEquipment(mdcEquipment.getId(), productionId); mdcProductionEquipmentMapper.insert(productionEquipment); } } } @Override @Transactional(rollbackFor = Exception.class) @CacheEvict(value = {"mdc:cache:encrypt:equipment"}, allEntries = true) public boolean deleteById(String id) { // 1. 删除设备 int line = this.baseMapper.deleteById(id); // 2. 删除设备部门关系 line += mdcEquipmentDepartMapper.delete(new LambdaQueryWrapper().eq(MdcEquipmentDepart::getEquipmentId, id)); // 3. 删除设备产线关系 line += mdcProductionEquipmentMapper.delete(new LambdaQueryWrapper().eq(MdcProductionEquipment::getEquipmentId, id)); return line != 0; } @Override @Transactional(rollbackFor = Exception.class) @CacheEvict(value = {"mdc:cache:encrypt:equipment"}, allEntries = true) public boolean deleteByIds(List equipmentIds) { // 1. 删除设备 int line = this.baseMapper.deleteBatchIds(equipmentIds); // 2. 删除设备部门关系 line += mdcEquipmentDepartMapper.delete(new LambdaQueryWrapper().in(MdcEquipmentDepart::getEquipmentId, equipmentIds)); // 3. 删除设备产线关系 line += mdcProductionEquipmentMapper.delete(new LambdaQueryWrapper().in(MdcProductionEquipment::getEquipmentId, equipmentIds)); return line != 0; } @Override public IPage pageListByDepId(Page page, String departId, String equipmentId) { return this.baseMapper.pageListByDepId(page, departId, equipmentId); } @Override public IPage pageListByProId(Page page, String productionId, String equipmentId) { return this.baseMapper.pageListByProId(page, productionId, equipmentId); } @Override public void removeEquipmentForDepart(String departId, String equipmentId) { mdcEquipmentDepartMapper.delete(new LambdaQueryWrapper().eq(MdcEquipmentDepart::getDepId, departId).eq(MdcEquipmentDepart::getEquipmentId, equipmentId)); } @Override public void removeEquipmentsForDepart(String departId, List equipmentIdList) { mdcEquipmentDepartMapper.delete(new LambdaQueryWrapper().eq(MdcEquipmentDepart::getDepId, departId).in(MdcEquipmentDepart::getEquipmentId, equipmentIdList)); } @Override public void removeEquipmentForProduction(String productionId, String equipmentId) { mdcProductionEquipmentMapper.delete(new LambdaQueryWrapper().eq(MdcProductionEquipment::getProductionId, productionId).eq(MdcProductionEquipment::getEquipmentId, equipmentId)); } @Override public void removeEquipmentsForProduction(String productionId, List equipmentIdList) { mdcProductionEquipmentMapper.delete(new LambdaQueryWrapper().eq(MdcProductionEquipment::getProductionId, productionId).in(MdcProductionEquipment::getEquipmentId, equipmentIdList)); } @Override public List loadTreeListByDepart(String userId) { //获取所有部门数据 List departList = sysDepartService.list(new LambdaQueryWrapper().eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(SysDepart::getDepartOrder)); //根据用户ID获取拥有的部门id集合 List departIds = sysUserDepartService.queryDepartIdsByUserId(userId); List allDepartIds = new ArrayList<>(); //找到所有部门id的上级id if (departIds != null && !departIds.isEmpty()) { for (String departId : departIds) { this.getAllDepartIds(departList, departId, allDepartIds); } } //过滤部门数据 List list = departList.stream().filter((SysDepart sysDepart) -> allDepartIds.contains(sysDepart.getId())).collect(Collectors.toList()); //组装部门设备树 List treeList = FindsEquipmentDepartUtil.wrapEquipmentDepartTreeList(list); //填充设备数据 FillEquipmentByDepart(treeList); return treeList; } /** * 部门设备树填充设备数据 * * @param treeList */ private void FillEquipmentByDepart(List treeList) { for (MdcEquipmentTree mdcEquipmentTree : treeList) { List equipmentList = this.baseMapper.queryByDepartId(mdcEquipmentTree.getKey()); if (equipmentList != null && !equipmentList.isEmpty()) { for (MdcEquipment mdcEquipment : equipmentList) { MdcEquipmentTree tree = new MdcEquipmentTree().convert(mdcEquipment); tree.setParentId(mdcEquipmentTree.getKey()); mdcEquipmentTree.getChildren().add(tree); } mdcEquipmentTree.setLeaf(false); } if (!mdcEquipmentTree.getChildren().isEmpty()) { FillEquipmentByDepart(mdcEquipmentTree.getChildren()); } } } /** * 获取所有的部门id(包含所有上级) */ private void getAllDepartIds(List departList, String departId, List allDepartIds) { if (!allDepartIds.contains(departId)) { allDepartIds.add(departId); } for (SysDepart sysDepart : departList) { if (StringUtils.isEmpty(sysDepart.getParentId())) { continue; } if (departId.equals(sysDepart.getId())) { if (!allDepartIds.contains(sysDepart.getParentId())) { allDepartIds.add(sysDepart.getParentId()); getAllDepartIds(departList, sysDepart.getParentId(), allDepartIds); } } } } @Override public List loadTreeListByProduction(String userId) { //获取所有产线数据 List productionList = mdcProductionService.list(new LambdaQueryWrapper().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder)); //根据用户id获取拥有的产线信息集合 List productionIds = mdcUserProductionService.queryProductionIdsByUserId(userId); List allProductionIds = new ArrayList<>(); //找到所有产线id的上级id if (productionIds != null && !productionIds.isEmpty()) { for (String productionId : productionIds) { this.getAllProductionIds(productionList, productionId, allProductionIds); } } //过滤产线数据 List list = productionList.stream().filter((MdcProduction mdcProduction) -> allProductionIds.contains(mdcProduction.getId())).collect(Collectors.toList()); //组装产线设备树 List treeList = FindsEquipmentProductionUtil.wrapEquipmentProductionTreeList(list); //填充设备数据 FillEquipmentByProduction(treeList); return treeList; } /** * 设备监控列表 */ @Override public List queryEquipmentMonitorList(String userId, String key) { List equipmentIds = this.getEquipmentIdsProduction(userId, key); if (equipmentIds == null || equipmentIds.isEmpty()) { return Collections.emptyList(); } List result = this.baseMapper.checkStatusFromEquipmentIds(equipmentIds); if (result != null && !result.isEmpty()) { for (MdcEquipmentMonitor mdcEquipmentMonitor : result) { if (mdcEquipmentMonitor.getOporation() != null) { switch (mdcEquipmentMonitor.getOporation()) { case 1: case 2: mdcEquipmentMonitor.setOporationDict("待机"); break; case 3: mdcEquipmentMonitor.setOporationDict("运行"); break; case 22: mdcEquipmentMonitor.setOporationDict("报警"); break; default: mdcEquipmentMonitor.setOporationDict("关机"); break; } } else { mdcEquipmentMonitor.setOporationDict("关机"); mdcEquipmentMonitor.setOporation(0); } } } return result; } /** * 设备监控 - 设备详细信息 */ @Override public MdcEquipmentDto mdcEquipmentDetailedInfo(String id) { MdcEquipmentDto mdcEquipmentDto = new MdcEquipmentDto(); MdcEquipment mdcEquipment = this.baseMapper.selectById(id); //封装基础信息 mdcEquipmentDto.setSystemVersion(mdcEquipment.getSystemVersion()); mdcEquipmentDto.setSystemType(mdcEquipment.getControlSystem()); mdcEquipmentDto.setDevicePower(mdcEquipment.getDevicePower()); mdcEquipmentDto.setDeviceType(mdcEquipment.getDriveType()); mdcEquipmentDto.setRemark(mdcEquipment.getRemark()); mdcEquipmentDto.setEquipmentID(mdcEquipment.getEquipmentId());//设备编号 mdcEquipmentDto.setEquipmentName(mdcEquipment.getEquipmentName()); mdcEquipmentDto.setEquipmentIP(mdcEquipment.getEquipmentIp()); mdcEquipmentDto.setEquipmentModel(mdcEquipment.getEquipmentModel());//设备型号 mdcEquipmentDto.setDriveType(mdcEquipment.getDriveType()); mdcEquipmentDto.setDataPort(mdcEquipment.getDataPort()); mdcEquipmentDto.setDataFlag(Integer.parseInt(CommonConstant.STATUS_1)); ControlSystem controlSystem = controlSystemService.getByDriveType(mdcEquipmentDto.getDriveType()); if (controlSystem != null) { //获取工作数据并初始化 String saveTableName = mdcEquipment.getSaveTableName(); MdcEquipmentDto dto = equipmentWorkLineService.getMacingDataList(saveTableName); if (dto != null) { mdcEquipmentDto.setCollectTime(dto.getCollectTime()); mdcEquipmentDto.setSpindlespeed(dto.getSpindlespeed()); mdcEquipmentDto.setSpindleload(dto.getSpindleload()); mdcEquipmentDto.setSpindlebeilv(dto.getSpindlebeilv()); mdcEquipmentDto.setFeedrate(dto.getFeedrate()); mdcEquipmentDto.setFeedbeilv(dto.getFeedbeilv()); mdcEquipmentDto.setProgramnumber(dto.getProgramnumber()); mdcEquipmentDto.setSequencenumber(dto.getSequencenumber()); mdcEquipmentDto.setExecutingcode(dto.getExecutingcode()); mdcEquipmentDto.setProductName(dto.getProductName()); //获取 MDC 驱动对应的展示参数 并根据key 拼装从 workData 查询的数据 List mdcDriveTypeParamList = mdcDriveTypeParamConfigService.getShowDriveParam(mdcEquipment.getDriveType()); if (mdcDriveTypeParamList != null && !mdcDriveTypeParamList.isEmpty()) { for (MdcDriveTypeParamConfig mdcDriveTypeParamConfig : mdcDriveTypeParamList) { String englishName = mdcDriveTypeParamConfig.getEnglishName(); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(dto); Object result = jsonObject.get(englishName); String value = ""; if ("CollectTime".equals(englishName)) { Date date = result == null ? null : (Date) result; value = DateUtils.format(date, DateUtils.STR_DATE_TIME_SMALL); } else { value = result == null ? null : result.toString(); } mdcDriveTypeParamConfig.setValue(value); } mdcEquipmentDto.setMdcDriveTypeParamConfigList(mdcDriveTypeParamList); } } // // 设备坐标数 获取设备需要采集的坐标信息 // List equipmentCoordinateList = xyzAliasesMapper.getCoordinateByEquipmentId(mdcEquipment.getEquipmentId()); // // 设备采集的坐标信息 // EquipmentXYZ equipmentXYZ = equipmentXYZService.getByEquipmentId(mdcEquipment.getEquipmentId()); // JSONObject jsonEquipmentXYZ = (JSONObject) JSONObject.toJSON(equipmentXYZ); // if (equipmentCoordinateList != null && !equipmentCoordinateList.isEmpty()) { // for (XYZAliases xyzAliases : equipmentCoordinateList) { // String englishName = xyzAliases.getXYZFieldName().toLowerCase(); // englishName = englishName.substring(0, 1) + "_" + englishName.substring(1, englishName.length()); // englishName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, englishName); // Object orgValue = jsonEquipmentXYZ.get(englishName); // String value = orgValue != null ? orgValue.toString() : null; // // 坐标数据的title 来自 xyzAliases 表, 根据 是否包含 absolute 和 machine 判断是绝对坐标还是机床坐标 // String title = englishName.substring(0, 1).toUpperCase(); // title = englishName.contains("absolute") ? "绝对坐标" + title : "机床坐标" + title; // xyzAliases.setTitle(title); // xyzAliases.setValue(value); // } // mdcEquipmentDto.setXyzAliasesList(equipmentCoordinateList); // } // if (equipmentXYZ != null) { // mdcEquipmentDto.setXmachine(equipmentXYZ.getXMachine()); // mdcEquipmentDto.setYmachine(equipmentXYZ.getYMachine()); // mdcEquipmentDto.setZmachine(equipmentXYZ.getZMachine()); // mdcEquipmentDto.setAmachine(equipmentXYZ.getAMachine()); // mdcEquipmentDto.setBmachine(equipmentXYZ.getBMachine()); // mdcEquipmentDto.setXabsolute(equipmentXYZ.getXAbsolute()); // mdcEquipmentDto.setYabsolute(equipmentXYZ.getYAbsolute()); // mdcEquipmentDto.setZabsolute(equipmentXYZ.getZAbsolute()); // mdcEquipmentDto.setAabsolute(equipmentXYZ.getAAbsolute()); // mdcEquipmentDto.setBabsolute(equipmentXYZ.getBAbsolute()); // } // EquipmentBaseInfo equipmentBaseInfo = equipmentBaseInfoService.getByEquipmentId(mdcEquipment.getEquipmentId()); // if (equipmentBaseInfo != null) { // mdcEquipmentDto.setMaxAxis(equipmentBaseInfo.getMaxAxis()); // mdcEquipmentDto.setValidAxis(equipmentBaseInfo.getValidAxis()); // } // if (controlSystem.getDriveType().equals("SIEMENS840DSL")) { // EquipmentTemperatureDto equipmentTemperature = equipmentTemperatureService.getEquipmentTemperature(mdcEquipment.getEquipmentId()); // if (equipmentTemperature != null) { // mdcEquipmentDto.setTemperatureX(equipmentTemperature.getX()); // mdcEquipmentDto.setTemperatureY(equipmentTemperature.getY()); // mdcEquipmentDto.setTemperatureZ(equipmentTemperature.getZ()); // mdcEquipmentDto.setTemperatureA(equipmentTemperature.getA()); // mdcEquipmentDto.setTemperatureB(equipmentTemperature.getB()); // } // EquipmentTemperatureDto equipmentLagError = equipmentTemperatureService.getEquipmentLagError(mdcEquipment.getEquipmentId()); // if (equipmentLagError != null) { // mdcEquipmentDto.setEquipmentLagErrorX(equipmentLagError.getX()); // mdcEquipmentDto.setEquipmentLagErrorY(equipmentLagError.getY()); // mdcEquipmentDto.setEquipmentLagErrorZ(equipmentLagError.getZ()); // mdcEquipmentDto.setEquipmentLagErrorA(equipmentLagError.getA()); // mdcEquipmentDto.setEquipmentLagErrorB(equipmentLagError.getB()); // } // } // EquipmentTemperatureDto equipmentCurrent = equipmentTemperatureService.getEquipmentCurrent(mdcEquipment.getEquipmentId()); // if (equipmentCurrent != null) { // mdcEquipmentDto.setEquipmentCurrentX(equipmentCurrent.getX()); // mdcEquipmentDto.setEquipmentCurrentY(equipmentCurrent.getY()); // mdcEquipmentDto.setEquipmentCurrentZ(equipmentCurrent.getZ()); // mdcEquipmentDto.setEquipmentCurrentA(equipmentCurrent.getA()); // mdcEquipmentDto.setEquipmentCurrentB(equipmentCurrent.getB()); // } } return mdcEquipmentDto; } /** * 通过用户部门关系获取设备id集合 */ @Override public List getEquipmentIdsByDepart(String userId, String key) { //获取所有部门数据 List departList = sysDepartService.list(new LambdaQueryWrapper().eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(SysDepart::getDepartOrder)); //根据用户id获取拥有的部门信息集合 List departIds = sysUserDepartService.queryDepartIdsByUserId(userId); List allDepartIds = new ArrayList<>(); if (departIds != null && !departIds.isEmpty()) { for (String departId : departIds) { this.getAllDepartIds(departList, departId, allDepartIds); } } //根据产线集合查找所有设备id if (allDepartIds.isEmpty()) { return null; } List equipmentIds = this.baseMapper.queryIdsByDeparts(allDepartIds); if (StringUtils.isNotEmpty(key)) { //key不为空,查询所有下级部门id List partDepart = this.findAllDeparts(key); partDepart.add(key); //过滤无权限部门id List allDepartIdsByKey = partDepart.stream().filter(allDepartIds::contains).collect(Collectors.toList()); equipmentIds = this.baseMapper.queryIdsByDeparts(allDepartIdsByKey); } return equipmentIds; } /** * 根据父级id递归查询所有下级id集合 */ private List findAllDeparts(String key) { List departs = new ArrayList<>(); List sysDeparts = sysDepartService.queryDeptByPid(key); if (sysDeparts != null && !sysDeparts.isEmpty()) { List ids = sysDeparts.stream().map(SysDepart::getId).collect(Collectors.toList()); departs.addAll(ids); for (SysDepart sysDepart : sysDeparts) { List allDeparts = findAllDeparts(sysDepart.getId()); departs.addAll(allDeparts); } } return departs; } /** * 通过用户产线关系获取设备id集合 */ @Override public List getEquipmentIdsProduction(String userId, String key) { //获取所有产线数据 List productionList = mdcProductionService.list(new LambdaQueryWrapper().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder)); //根据用户id获取拥有的产线信息集合 List productionIds = mdcUserProductionService.queryProductionIdsByUserId(userId); List allProductionIds = new ArrayList<>(); //找到所有产线id的上级id if (productionIds != null && !productionIds.isEmpty()) { for (String productionId : productionIds) { this.getAllProductionIds(productionList, productionId, allProductionIds); } } //根据产线集合查找所有设备id if (allProductionIds.isEmpty()) { return null; } List equipmentIds = this.baseMapper.queryIdsByProductions(allProductionIds); if (StringUtils.isNotEmpty(key)) { //key不为空,查询所有下级产线id List partProduction = this.findAllProductions(key); partProduction.add(key); //过滤无权限产线id List allProductionIdsByKey = partProduction.stream().filter(allProductionIds::contains).collect(Collectors.toList()); equipmentIds = this.baseMapper.queryIdsByProductions(allProductionIdsByKey); } return equipmentIds; } /** * 根据设备id查询设备名称 */ @Override public MdcEquipment findEquipmentNameByEquipmentId(String equipmentId) { return this.getOne(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentId, equipmentId)); } /** * 选择设备分页列表 */ @Override public IPage findEquipmentList(Page page, String userId, MdcEquipmentVo mdcEquipment) { List equipmentIdsByDepart = new ArrayList<>(); if (StringUtils.isNotEmpty(mdcEquipment.getDepartId())) { equipmentIdsByDepart = getEquipmentIdsByDepart(userId, mdcEquipment.getDepartId()); } List equipmentIdsProduction = new ArrayList<>(); if (StringUtils.isNotEmpty(mdcEquipment.getProductionId())) { equipmentIdsProduction = getEquipmentIdsProduction(userId, mdcEquipment.getProductionId()); } List allEquipments = new ArrayList<>(); if (StringUtils.isEmpty(mdcEquipment.getDepartId()) && StringUtils.isEmpty(mdcEquipment.getProductionId())) { List idsByDepart = getEquipmentIdsByDepart(userId, null); List idsProduction = getEquipmentIdsProduction(userId, null); // 去重并集 allEquipments = Stream.of(idsByDepart, idsProduction).flatMap(Collection::stream).distinct().collect(Collectors.toList()); } if (StringUtils.isNotEmpty(mdcEquipment.getDepartId()) && StringUtils.isNotEmpty(mdcEquipment.getProductionId())) { // 交集 allEquipments = equipmentIdsByDepart.stream().filter(equipmentIdsProduction::contains).collect(Collectors.toList()); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.isNotNull(MdcEquipment::getEquipmentId); if (StringUtils.isNotEmpty(mdcEquipment.getEquipmentId())) { queryWrapper.like(MdcEquipment::getEquipmentId, mdcEquipment.getEquipmentId()); } if (StringUtils.isNotEmpty(mdcEquipment.getEquipmentName())) { queryWrapper.like(MdcEquipment::getEquipmentName, mdcEquipment.getEquipmentName()); } if (!allEquipments.isEmpty()) { queryWrapper.in(MdcEquipment::getEquipmentId, allEquipments); } else if (!equipmentIdsByDepart.isEmpty() && equipmentIdsProduction.isEmpty()) { queryWrapper.in(MdcEquipment::getEquipmentId, equipmentIdsByDepart); } else if (equipmentIdsByDepart.isEmpty() && !equipmentIdsProduction.isEmpty()) { queryWrapper.in(MdcEquipment::getEquipmentId, equipmentIdsProduction); } queryWrapper.orderByAsc(MdcEquipment::getEquipmentName); return this.baseMapper.selectPage(page, queryWrapper); } /** * 根据父级id递归查询所有下级id集合 */ private List findAllProductions(String key) { List productions = new ArrayList<>(); List mdcProductions = mdcProductionService.queryProdByPid(key); if (mdcProductions != null && !mdcProductions.isEmpty()) { List ids = mdcProductions.stream().map(MdcProduction::getId).collect(Collectors.toList()); productions.addAll(ids); for (MdcProduction mdcProduction : mdcProductions) { List allProductions = findAllProductions(mdcProduction.getId()); productions.addAll(allProductions); } } return productions; } /** * 产线设备树填充设备数据 */ private void FillEquipmentByProduction(List treeList) { for (MdcEquipmentTree mdcEquipmentTree : treeList) { List equipmentList = this.baseMapper.queryByProductionId(mdcEquipmentTree.getKey()); if (equipmentList != null && !equipmentList.isEmpty()) { for (MdcEquipment mdcEquipment : equipmentList) { MdcEquipmentTree tree = new MdcEquipmentTree().convert(mdcEquipment); tree.setParentId(mdcEquipmentTree.getKey()); mdcEquipmentTree.getChildren().add(tree); } mdcEquipmentTree.setLeaf(false); } if (!mdcEquipmentTree.getChildren().isEmpty()) { FillEquipmentByProduction(mdcEquipmentTree.getChildren()); } } } /** * 获取所有的产线id(包含所有上级) */ private void getAllProductionIds(List productionList, String productionId, List 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); } } } } /** * 根据产线层级查询设备单个 */ @Override public MdcEquipment getEquipmentByPid(String pid, String userId) { List idsProduction = this.getEquipmentIdsProduction(userId, pid); if (idsProduction != null && !idsProduction.isEmpty()) { return super.getOne(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentId, idsProduction.get(0))); } return null; } /** * 根据设备编号查询设备信息和部门信息 * * @param equipmentIdList * @return */ @Override public List findEquDepList(List equipmentIdList) { return this.baseMapper.findEquDepList(equipmentIdList); } /** * 根据设备编号查询设备信息和产线信息 * * @param equipmentIdList * @return */ @Override public List findEquProList(List equipmentIdList) { return this.baseMapper.findEquProList(equipmentIdList); } /** * 根据部门层级查询设备单个 */ @Override public MdcEquipment getEquipmentByDepPid(String pid, String userId) { List idsByDepart = this.getEquipmentIdsByDepart(userId, pid); if (idsByDepart != null && !idsByDepart.isEmpty()) { return super.getOne(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentId, idsByDepart.get(0))); } return null; } }