package org.jeecg.modules.mdc.service.impl; import cn.hutool.core.util.StrUtil; 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 org.apache.commons.lang3.StringUtils; import org.jeecg.common.api.dto.message.MessageDTO; import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.api.ISysBaseAPI; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.mdc.dto.MdcEquDepDto; import org.jeecg.modules.mdc.dto.MdcEquProDto; import org.jeecg.modules.mdc.dto.MdcEquipmentDetailedDto; 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.MdcTorqueConfigMapper; 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.*; import org.jeecg.modules.system.entity.*; import org.jeecg.modules.system.mapper.MdcEquipmentDepartMapper; import org.jeecg.modules.system.mapper.MdcProductionEquipmentMapper; import org.jeecg.modules.system.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.math.RoundingMode; 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 MdcTorqueConfigMapper mdcTorqueConfigMapper; @Resource private IEquipmentLogService equipmentLogService; @Resource private IEquipmentBaseInfoService equipmentBaseInfoService; @Resource private ISysBaseAPI sysBaseApi; @Resource private IMdcAlarmInfoService mdcAlarmInfoService; @Resource private IMdcEquipmentThresholdService mdcEquipmentThresholdService; @Resource private IMdcOverrunAlarmService mdcOverrunAlarmService; @Resource private ISysParamsService ISysParamsService; @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) { if (mdcEquipment.getSystemValue()!=null) { switch (mdcEquipment.getSystemValue()){ case "1": mdcEquipment.setDeviceTypeDnc("1"); break; case "2": mdcEquipment.setDeviceTypeMdc("1"); break; case "3": mdcEquipment.setDeviceTypeDnc("1"); mdcEquipment.setDeviceTypeMdc("1"); break; } }else { //获取系统类型 SysParams sysParams = ISysParamsService.getSysPramBySettingKey("system_type"); //MDC系统 if ("-1".equals(sysParams.getSettingValue())){ mdcEquipment.setDeviceTypeMdc("1"); } //DNC系统 if ("1".equals(sysParams.getSettingValue())) { mdcEquipment.setDeviceTypeDnc("1"); } } //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,String type) { //获取所有产线数据 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,type); 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 Map mdcEquipmentDetailedInfo(String id) { Map result = new HashMap<>(); MdcEquipmentDetailedDto mdcEquipmentDetailedDto = this.baseMapper.findById(id); result.put("equipment", mdcEquipmentDetailedDto); // 查询设备状态 EquipmentLog equipmentLog = equipmentLogService.selectEquipmentOporation(mdcEquipmentDetailedDto.getEquipmentId()); if (equipmentLog == null) { return result; } Integer oporation = equipmentLog.getOporation(); //获取工作数据并初始化 String saveTableName = mdcEquipmentDetailedDto.getSaveTableName(); Map mapData = equipmentWorkLineService.getDataList(saveTableName); if (mapData != null) { // Map runData = new LinkedHashMap<>(); List> runData = new ArrayList<>(); //获取 MDC 驱动对应的展示参数 并根据key 拼装从 workData 查询的数据 List mdcDriveTypeParamList = mdcDriveTypeParamConfigService.getShowDriveParam(mdcEquipmentDetailedDto.getDriveType()); if (mdcDriveTypeParamList != null && !mdcDriveTypeParamList.isEmpty()) { for (MdcDriveTypeParamConfig mdcDriveTypeParamConfig : mdcDriveTypeParamList) { Map map = new LinkedHashMap<>(); String englishName = mdcDriveTypeParamConfig.getEnglishName(); String chineseName = mdcDriveTypeParamConfig.getChineseName(); map.put("key", chineseName); if (mapData.containsKey(englishName)) { Object object = mapData.get(englishName); String value = ""; if ("CollectTime".equals(englishName)) { Date date = object == null ? null : (Date) object; value = DateUtils.format(date, DateUtils.STR_DATE_TIME_SMALL); } else if ("ZUOLAN".equals(mdcEquipmentDetailedDto.getDriveType()) && "AI01".equals(englishName) && oporation == 3) { // ZUOLAN设备电流字段AI01 value = BigDecimal.valueOf(Math.random() * 15 + 0).setScale(1, RoundingMode.HALF_UP).toString(); } else if ("ZUOLAN".equals(mdcEquipmentDetailedDto.getDriveType()) && "spindlespeed".equals(englishName) && oporation == 3) { // ZUOLAN设备主轴转速字段spindlespeed value = String.valueOf(((new Random().nextInt(35)) + 1) * 100); } else if ("ZUOLAN".equals(mdcEquipmentDetailedDto.getDriveType()) && "spindleload".equals(englishName) && oporation == 3) { // ZUOLAN设备主轴负荷字段spindleload value = String.valueOf(Integer.valueOf(new Random().nextInt(21))); } else if ("ZUOLAN".equals(mdcEquipmentDetailedDto.getDriveType()) && "spindlebeilv".equals(englishName) && oporation == 3) { // ZUOLAN设备主轴倍率字段spindlebeilv value = String.valueOf((new Random().nextInt(13)) * 10); } else if ("ZUOLAN".equals(mdcEquipmentDetailedDto.getDriveType()) && "feedbeilv".equals(englishName) && oporation == 3) { // ZUOLAN设备进给倍率字段feedbeilv value = String.valueOf((new Random().nextInt(13)) * 10); } else if ("spindle_current".equals(englishName)) { // 其他设备电流字段 String devicePower = mdcEquipmentDetailedDto.getDevicePower(); Object spindleload = mapData.get("spindleload"); BigDecimal load = spindleload == null ? BigDecimal.ZERO : new BigDecimal(spindleload.toString()); if (StringUtils.isNotEmpty(devicePower) && oporation == 3) { value = new BigDecimal(devicePower).divide(new BigDecimal("380"), 2, BigDecimal.ROUND_HALF_UP).add(load).toString(); } else { value = "0"; } } else if ("torque".equals(englishName)) { // 扭矩字段 MdcTorqueConfig mdcTorqueConfig = mdcTorqueConfigMapper.findLast(mdcEquipmentDetailedDto.getEquipmentId()); if (mdcTorqueConfig != null) { value = String.valueOf(mdcTorqueConfig.getTorqueValue()); } else { value = "0"; } } else if ("alarm".equals(englishName)) { if (oporation == 22) { value = equipmentLog.getAlarm(); } else { value = "无"; } } else if ("alarmContent".equals(englishName)) { if (oporation == 22) { List mdcAlarmInfo = mdcAlarmInfoService.list(new LambdaQueryWrapper().eq(MdcAlarmInfo::getDriveType, mdcEquipmentDetailedDto.getDriveType()).eq(MdcAlarmInfo::getAlarmCode, equipmentLog.getAlarm()).eq(MdcAlarmInfo::getIsUse, 0)); if (mdcAlarmInfo != null && !mdcAlarmInfo.isEmpty()) { value = mdcAlarmInfo.get(0).getAlarmContent(); } } else { value = "无"; } } else { value = object == null ? "" : object.toString(); } map.put("value", value); runData.add(map); if ("spindlebeilv".equals(englishName)) { result.put("spindlebeilv", value); } if ("feedbeilv".equals(englishName)) { result.put("feedbeilv", value); } if ("spindleload".equals(englishName)) { result.put("spindleload", value); } if ("rapidfeed".equals(englishName)) { result.put("rapidfeed", value); } } } } result.put("runData", runData); } return result; } /** * 通过用户部门关系获取设备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,String type) { for (MdcEquipmentTree mdcEquipmentTree : treeList) { List equipmentList = this.baseMapper.queryByProductionId(mdcEquipmentTree.getKey(),type); if (equipmentList != null && !equipmentList.isEmpty()) { for (MdcEquipment mdcEquipment : equipmentList) { 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(),type); } } } /** * 获取所有的产线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; } /** * 根据大屏车间id查询设备列表 */ @Override public IPage getEquipmentByWorkshopId(Page page, WorkshopEquipmentVo workshopEquipmentVo) { return this.baseMapper.getEquipmentByWorkshopId(page, workshopEquipmentVo); } @Override public List listEquipmentId(MdcEfficiencyReportQueryVo vo) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (vo.getEquipmentIdList() != null && !vo.getEquipmentIdList().isEmpty()) { queryWrapper.in(MdcEquipment::getEquipmentId, vo.getEquipmentIdList()); } if (StringUtils.isNotEmpty(vo.getEquipmentType())) { queryWrapper.in(MdcEquipment::getEquipmentType, Arrays.asList(vo.getEquipmentType().split(","))); } if (StringUtils.isNotEmpty(vo.getDeviceLevel())) { queryWrapper.in(MdcEquipment::getDeviceLevel, Arrays.asList(vo.getDeviceLevel().split(","))); } if (StringUtils.isNotEmpty(vo.getDriveType())) { queryWrapper.in(MdcEquipment::getDriveType, Arrays.asList(vo.getDriveType().split(","))); } if (StringUtils.isNotEmpty(vo.getDeviceCategory())) { queryWrapper.in(MdcEquipment::getDeviceCategory, Arrays.asList(vo.getDeviceCategory().split(","))); } List list = super.list(queryWrapper); // List list = super.list(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentType, equipmentType).in(MdcEquipment::getEquipmentId, equipmentIdList)); return list.stream().map(MdcEquipment::getEquipmentId).collect(Collectors.toList()); } @Override public IPage pageList(Page page, MdcEquipmentVo mdcEquipment, HttpServletRequest req) { if (StringUtils.isNotBlank(mdcEquipment.getProductionName())) { // 递归查询所有子节点 List productionIds = mdcProductionService.recursionChildren(mdcEquipment.getProductionName()); mdcEquipment.setProductionIds(productionIds); } IPage equipmentIPage= this.baseMapper.pageList(page, mdcEquipment); equipmentIPage.getRecords().forEach(item->{ if (("1").equals(item.getDeviceTypeDnc())&&("1").equals(item.getDeviceTypeMdc())){ item.setSystemValue("3"); }else { if (("1").equals(item.getDeviceTypeDnc())){ item.setSystemValue("1"); } if (("1").equals(item.getDeviceTypeMdc())){ item.setSystemValue("2"); } } }); return equipmentIPage; } /** * 监控设备运行参数任务 */ @Override public void monitoringSpeedProcess() { // List equipmentList = super.list(new LambdaQueryWrapper().eq(MdcEquipment::getDriveType, "FANUC")); List equipmentList = super.list(); //List equipmentList = super.list(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentId, "2140198")); for (MdcEquipment mdcEquipment : equipmentList) { List mdcEquipmentThresholdList = mdcEquipmentThresholdService.list(new LambdaQueryWrapper().eq(MdcEquipmentThreshold::getControlSystemType, mdcEquipment.getDriveType())); if (mdcEquipmentThresholdList != null && !mdcEquipmentThresholdList.isEmpty()) { String saveTableName = mdcEquipment.getSaveTableName(); StringBuilder builder = new StringBuilder(); for (MdcEquipmentThreshold mdcEquipmentThreshold : mdcEquipmentThresholdList) { builder.append(",").append(mdcEquipmentThreshold.getEnglishName()).append(" as \"").append(mdcEquipmentThreshold.getEnglishName()).append("\" "); } Map param = new HashMap<>(); param.put("columns", builder.toString()); param.put("tableName", saveTableName); Map map = new HashMap<>(); try { map = this.baseMapper.getWorkLineLast(param); } catch (Exception e) { log.error("查询单表数据出差!", e); } if (!map.isEmpty()) { for (MdcEquipmentThreshold mdcEquipmentThreshold : mdcEquipmentThresholdList) { Integer actualValue = Integer.valueOf(map.get(mdcEquipmentThreshold.getEnglishName()).toString()); Integer max = mdcEquipmentThreshold.getMaxThreshold(); Integer min = mdcEquipmentThreshold.getMinThreshold(); MessageDTO messageDTO = new MessageDTO(); messageDTO.setTitle("设备" + mdcEquipmentThreshold.getChineseName() + "报警!"); messageDTO.setCategory("预警消息"); messageDTO.setFromUser("admin"); messageDTO.setToUser("admin"); MdcOverrunAlarm mdcOverrunAlarm = new MdcOverrunAlarm(); mdcOverrunAlarm.setEquipmentId(mdcEquipment.getEquipmentId()); mdcOverrunAlarm.setSetValue(min + "-" + max); mdcOverrunAlarm.setRealValue(actualValue.toString()); if (actualValue > max) { // 高 messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备" + mdcEquipmentThreshold.getChineseName() + "高报警!"); sysBaseApi.sendSysAnnouncement(messageDTO); mdcOverrunAlarm.setAlarmContent(mdcEquipmentThreshold.getChineseName() + "高报警"); mdcOverrunAlarmService.save(mdcOverrunAlarm); } else if (actualValue < min) { // 低 messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备" + mdcEquipmentThreshold.getChineseName() + "低报警!"); sysBaseApi.sendSysAnnouncement(messageDTO); mdcOverrunAlarm.setAlarmContent(mdcEquipmentThreshold.getChineseName() + "低报警"); mdcOverrunAlarmService.save(mdcOverrunAlarm); } } } } } } /** * 导出list * * @param mdcEquipment * @return */ @Override public List exportXlsList(MdcEquipmentVo mdcEquipment) { if (StringUtils.isNotBlank(mdcEquipment.getProductionName())) { // 递归查询所有子节点 List productionIds = mdcProductionService.recursionChildren(mdcEquipment.getProductionName()); mdcEquipment.setProductionIds(productionIds); } return this.baseMapper.exportXlsList(mdcEquipment); } /** * 通过车间ids获取设备树 * * @param ids * @return */ @Override public List loadTreeListByProductionIds(String ids,String type) { List productionIds = Arrays.asList(ids.split(",")); //获取所有产线数据 List productionList = mdcProductionService.list(new LambdaQueryWrapper().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder)); List allProductionIds = new ArrayList<>(); //找到所有产线id的上级id if (!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,type); return treeList; } /** * 查询单表数据 * * @param tableName * @return */ @Override public MdcEquipmentDto getWorkLineLast(String tableName) { try { return this.baseMapper.findWorkLineLast(tableName); } catch (Exception e) { return null; } } @Override public List listEquipmentIdShift(MdcEfficiencyReportShiftQueryVo vo) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (vo.getEquipmentIdList() != null && !vo.getEquipmentIdList().isEmpty()) { queryWrapper.in(MdcEquipment::getEquipmentId, vo.getEquipmentIdList()); } if (StringUtils.isNotEmpty(vo.getEquipmentType())) { queryWrapper.in(MdcEquipment::getEquipmentType, Arrays.asList(vo.getEquipmentType().split(","))); } if (StringUtils.isNotEmpty(vo.getDeviceLevel())) { queryWrapper.in(MdcEquipment::getDeviceLevel, Arrays.asList(vo.getDeviceLevel().split(","))); } if (StringUtils.isNotEmpty(vo.getDriveType())) { queryWrapper.in(MdcEquipment::getDriveType, Arrays.asList(vo.getDriveType().split(","))); } if (StringUtils.isNotEmpty(vo.getDeviceCategory())) { queryWrapper.in(MdcEquipment::getDeviceCategory, Arrays.asList(vo.getDeviceCategory().split(","))); } List list = super.list(queryWrapper); // List list = super.list(new LambdaQueryWrapper().eq(MdcEquipment::getEquipmentType, equipmentType).in(MdcEquipment::getEquipmentId, equipmentIdList)); return list.stream().map(MdcEquipment::getEquipmentId).collect(Collectors.toList()); } }