package com.mes.hollow.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Assert; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mes.common.config.Const; import com.mes.common.config.ConstSysConfig; import com.mes.glassinfo.entity.GlassInfo; import com.mes.glassinfo.service.GlassInfoService; import com.mes.hollow.entity.HollowBigStorageCage; import com.mes.hollow.entity.HollowBigStorageCageDetails; import com.mes.hollow.entity.HollowGlassOutRelationInfo; import com.mes.hollow.entity.HollowGlassRelationInfo; import com.mes.hollow.entity.dto.*; import com.mes.hollow.entity.vo.HollowAllFlowCardVO; import com.mes.hollow.entity.vo.HollowBigStorageDetailsQueryVO; import com.mes.hollow.mapper.HollowGlassRelationInfoMapper; import com.mes.hollow.service.HollowBigStorageCageDetailsService; import com.mes.hollow.service.HollowBigStorageCageService; import com.mes.hollow.service.HollowGlassOutRelationInfoService; import com.mes.hollow.service.HollowGlassRelationInfoService; import com.mes.sysconfig.service.SysConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigInteger; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * (HollowGlassRelationInfo)表服务实现类 * * @author makejava * @since 2024-11-23 15:59:30 */ @Service @Slf4j public class HollowGlassRelationInfoServiceImpl extends ServiceImpl implements HollowGlassRelationInfoService { @Resource GlassInfoService glassInfoService; @Resource HollowBigStorageCageService hollowBigStorageCageService; @Resource HollowGlassRelationInfoService hollowGlassRelationInfoService; @Resource HollowGlassOutRelationInfoService hollowGlassOutRelationInfoService; @Resource HollowBigStorageCageDetailsService hollowBigStorageCageDetailsService; @Resource SysConfigService sysConfigService; // @Value("${mes.slotWidth}") // private Integer slotWidth; // @Value("${mes.glassGap}") // private Integer glassGap; // // @Value("${mes.outCarMaxSize}") // private Integer outCarMaxSize; @Override public HollowBigStorageDTO queryHollowTargetSlot(String flowCardId, double width, double height, int totalLayer, int layer) { //按照玻璃信息获取关系表中对应的大理片笼格子号 HollowGlassRelationInfo relationInfoOne = hollowGlassRelationInfoService.getOne(new LambdaQueryWrapper() .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId) .eq(HollowGlassRelationInfo::getWidth, width) .eq(HollowGlassRelationInfo::getHeight, height) .eq(HollowGlassRelationInfo::getTotalLayer, totalLayer) .eq(HollowGlassRelationInfo::getLayer, layer) .eq(HollowGlassRelationInfo::getState, Const.HOLLOW_RELATION_NEW) .orderByAsc(HollowGlassRelationInfo::getHollowSequence) .last("limit 1") ); if (relationInfoOne == null) { //查看mes是否有对应的流程卡信息 int count = hollowGlassRelationInfoService.count(new LambdaQueryWrapper() .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId) .eq(HollowGlassRelationInfo::getLayer, layer)); if (count == 0) { generateHollowGlassInfo(flowCardId, totalLayer, layer); } else { //比较关系表及中空理片笼详情表的流程卡数据,处理脏数据:将不在笼内的流程卡匹配数据职位空 this.baseMapper.clearDirtyFlowCardData(flowCardId, layer); } //理片笼关系表中没有对应的数据,查看理片笼虚拟位置表是否有本工程下的所有玻璃虚拟信息 //虚拟位置表没有本工程下的所有玻璃虚拟信息,按照玻璃id生成本工程下所有玻璃的虚拟信息 relationInfoOne = this.getOne(new LambdaQueryWrapper() .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId) .eq(HollowGlassRelationInfo::getWidth, width) .eq(HollowGlassRelationInfo::getHeight, height) .eq(HollowGlassRelationInfo::getTotalLayer, totalLayer) .eq(HollowGlassRelationInfo::getLayer, layer) .eq(HollowGlassRelationInfo::getState, Const.HOLLOW_RELATION_NEW) .orderByAsc(HollowGlassRelationInfo::getHollowSequence) .last("limit 1") ); } Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_SLOT_WIDTH); //详情表内获取本组是否已经有玻璃在笼子内(0表示提前占用) int taskCount = hollowGlassOutRelationInfoService.count(new LambdaQueryWrapper() .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId)); HollowBigStorageCage storageCage = null; //如果不存在则选择笼内未用的新格子 if (taskCount > 0) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth) .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness()) .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness()) .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1")); HollowBigStorageDTO storageDTO = new HollowBigStorageDTO(); BeanUtils.copyProperties(storageCage, storageDTO); BeanUtils.copyProperties(relationInfoOne, storageDTO); return storageDTO; } //详情表内获取本组是否已经有玻璃在笼子内(0表示提前占用) List hollowDetailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper() .eq(HollowBigStorageCageDetails::getFlowCardId, relationInfoOne.getFlowCardId()) .eq(HollowBigStorageCageDetails::getTotalLayer, totalLayer) .eq(HollowBigStorageCageDetails::getLayer, layer) .eq(HollowBigStorageCageDetails::getVirtualSlot, relationInfoOne.getVirtualSlot()) .in(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO)); //如果不存在则选择笼内未用的新格子 if (CollectionUtil.isEmpty(hollowDetailsList)) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth) .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness()) .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness()) .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1")); HollowBigStorageDTO storageDTO = new HollowBigStorageDTO(); BeanUtils.copyProperties(storageCage, storageDTO); BeanUtils.copyProperties(relationInfoOne, storageDTO); return storageDTO; } //获取当前组在笼子内的格子号(按照组内序号-1计算 如果没有则新开一格) HollowGlassRelationInfo relationInfoBefore = hollowGlassRelationInfoService.getOne(new LambdaQueryWrapper() .eq(HollowGlassRelationInfo::getFlowCardId, relationInfoOne.getFlowCardId()) .eq(HollowGlassRelationInfo::getTotalLayer, relationInfoOne.getTotalLayer()) .eq(HollowGlassRelationInfo::getLayer, relationInfoOne.getLayer()) .eq(HollowGlassRelationInfo::getVirtualSlot, relationInfoOne.getVirtualSlot()) .eq(HollowGlassRelationInfo::getSlotSequence, relationInfoOne.getSlotSequence() - 1)); if (null == relationInfoBefore) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth) .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness()) .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness()) .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1")); } else { //获取详情表组内前一片玻璃的笼内详情数据 HollowBigStorageCageDetails beforeGlass = hollowBigStorageCageDetailsService.getOne(new LambdaQueryWrapper() .in(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL_ZERO) .eq(HollowBigStorageCageDetails::getEngineerId, relationInfoBefore.getEngineerId()) .eq(HollowBigStorageCageDetails::getTemperingLayoutId, relationInfoBefore.getTemperingLayoutId()) .eq(HollowBigStorageCageDetails::getTemperingFeedSequence, relationInfoBefore.getTemperingFeedSequence()) ); //改组在笼内有玻璃但是前一块玻璃没有:原因 玻璃破损处理 if (null == beforeGlass) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth) .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness()) .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness()) .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1")); } else { //取出每组玻璃占用笼子的格子并计算格子内的最大组序号,避免玻璃顺序错乱 List hollowSequenceList = hollowBigStorageCageDetailsService .querySlotMaxSequence(beforeGlass.getFlowCardId(), beforeGlass.getTotalLayer(), beforeGlass.getLayer(), beforeGlass.getVirtualSlot()); for (HollowBigStorageCageDetails hollowBigStorageCageDetail : hollowSequenceList) { if (relationInfoOne.getSlotSequence() - 1 == hollowBigStorageCageDetail.getSequence()) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getSlot, beforeGlass.getSlot())); break; } } if (storageCage == null) { storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper() .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth) .le(HollowBigStorageCage::getMinThickness, relationInfoOne.getThickness()) .ge(HollowBigStorageCage::getMaxThickness, relationInfoOne.getThickness()) .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1")); } } } Assert.isTrue(null != storageCage, "没有空余的笼子存放玻璃"); HollowBigStorageDTO storageDTO = new HollowBigStorageDTO(); BeanUtils.copyProperties(storageCage, storageDTO); BeanUtils.copyProperties(relationInfoOne, storageDTO); return storageDTO; } @Override public void generateHollowGlassInfo(String flowCardId, int totalLayer, int layer) { GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper().eq(GlassInfo::getFlowCardId, flowCardId) .eq(GlassInfo::getLayer, layer).orderByDesc(GlassInfo::getId).last("limit 1")); if (null == glassInfo) { log.info("当前流程卡信息为导入mes系统流程卡:{},层数{}", flowCardId, layer); return; } //按照流程卡获取本流程卡最后一层或第一次的玻璃数据 List glassDetailsDTOS = this.baseMapper.queryFlowCardIdMaxLayerGlassInfo(flowCardId, totalLayer); if (CollectionUtil.isEmpty(glassDetailsDTOS)) { log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{}", flowCardId, totalLayer); return; } if (totalLayer != layer) { glassDetailsDTOS = this.baseMapper.queryFlowCardIdLayerGlassInfo(flowCardId, totalLayer, layer); } if (CollectionUtil.isEmpty(glassDetailsDTOS)) { log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{},层数{}", flowCardId, totalLayer, layer); return; } ArrayList tempGlassList = new ArrayList<>(); int hollowSequence = 1; for (HollowGlassDetailsDTO item : glassDetailsDTOS) { for (int i = 0; i < item.getQuantity(); i++) { HollowGlassDetailsDTO dto = new HollowGlassDetailsDTO(); BeanUtils.copyProperties(item, dto); dto.setHollowSequence(hollowSequence++); tempGlassList.add(dto); } } //方式一:将玻璃按流程卡、尺寸、版图、版序 依次生成虚拟格子信息,格子一直往后累加 // flowCardIdMap.forEach((e, v) -> { // int remainWidth = slotWidth; // int slotNumber = 1; // for (GlassInfo item : v) { // int maxLength = (int) Math.max(item.getWidth(), item.getHeight()); // if (remainWidth > maxLength) { // remainWidth = remainWidth - maxLength - glassGap; // } else { // slotNumber = slotNumber + 1; // remainWidth = slotWidth - maxLength - glassGap; // } // HollowGlassInfo hollow = new HollowGlassInfo(); // BeanUtils.copyProperties(item, hollow); // hollow.setSlot(slotNumber); // hollowGlassInfoList.add(hollow); // } // }); // this.saveBatch(hollowGlassInfoList); //获取中空大理片笼的所有空闲格子 // List hollowSlotList = HollowBigStorageCageService.list(new LambdaQueryWrapper() // .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)); //方式二:将玻璃按流程卡、尺寸、版图、版序 ,优先将格子全部补全后 依次计算后面的格子号 Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_SLOT_WIDTH); // Integer glassGap = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_GLASS_GAP); Integer glassGap = getGlassGapByThickness(tempGlassList.get(0).getThickness()); Integer outCarMaxSize = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_OUT_CAR_SIZE); List relationInfoList = new ArrayList(); List> tempHollowList = new ArrayList<>(); int slotNumber = 1; for (HollowGlassDetailsDTO item : tempGlassList) { boolean flag = false; for (List temp : tempHollowList) { int sum = 0; for (HollowGlassRelationInfo i : temp) { sum = sum + (int) Math.max(i.getHeight(), i.getWidth()) + glassGap; } if (sum + (int) Math.max(item.getHeight(), item.getWidth()) <= slotWidth && temp.size() < outCarMaxSize) { HollowGlassRelationInfo hollow = new HollowGlassRelationInfo(); BeanUtils.copyProperties(item, hollow); hollow.setSlotSequence(temp.size() + 1); hollow.setTotalLayer(totalLayer); hollow.setVirtualSlot(temp.get(0).getVirtualSlot()); hollow.setFilmsId(glassInfo.getFilmsid()); hollow.setThickness(glassInfo.getThickness()); hollow.setState(Const.HOLLOW_RELATION_NEW); temp.add(hollow); flag = true; break; } } if (!flag) { List newList = new ArrayList<>(); HollowGlassRelationInfo hollow = new HollowGlassRelationInfo(); BeanUtils.copyProperties(item, hollow); hollow.setSlotSequence(1); hollow.setTotalLayer(totalLayer); hollow.setVirtualSlot(slotNumber++); hollow.setFilmsId(glassInfo.getFilmsid()); hollow.setThickness(glassInfo.getThickness()); hollow.setState(Const.HOLLOW_RELATION_NEW); newList.add(hollow); tempHollowList.add(newList); } } for (List item : tempHollowList) { relationInfoList.addAll(item); } log.info("分配完毕"); this.saveBatch(relationInfoList); } @Override public List queryHollowAllFlowCard(HollowBigStorageDetailsQueryVO query) { List detailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper() .eq(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN) .like(StringUtils.isNotBlank(query.getFilmsId()), HollowBigStorageCageDetails::getFilmsId, query.getFilmsId()) .like(StringUtils.isNotBlank(query.getFlowCardId()), HollowBigStorageCageDetails::getFlowCardId, query.getFlowCardId()) .eq(query.getThickness() != 0, HollowBigStorageCageDetails::getThickness, query.getThickness()) .orderByAsc(HollowBigStorageCageDetails::getFlowCardId) ); if (CollectionUtil.isEmpty(detailsList)) { log.info("笼内无玻璃"); return new ArrayList<>(); } Map> listMap = detailsList.stream().collect(Collectors.groupingBy(HollowBigStorageCageDetails::getFlowCardId)); List resultList = new ArrayList<>(); listMap.forEach((e, v) -> { HollowAllFlowCardVO hollowAllFlowCardVO = new HollowAllFlowCardVO(); HollowBigStorageCageDetails cageDetails = v.get(0); //按照流程卡获取对应的产品名称 OrderDetailsDTO orderDetails = baseMapper.queryProductNameByFlowCardId(cageDetails.getFlowCardId()); if (null != orderDetails) { BeanUtils.copyProperties(orderDetails, hollowAllFlowCardVO); } hollowAllFlowCardVO.setFlowCardId(e); if (cageDetails.getHollowSequence() == 0) { hollowAllFlowCardVO.setIsThroughSlot(Boolean.TRUE); } else { hollowAllFlowCardVO.setIsThroughSlot(Boolean.FALSE); } List flowCardInfoList = hollowBigStorageCageDetailsService.hollowIsAll(e, cageDetails.getTotalLayer(), Boolean.FALSE); hollowAllFlowCardVO.setFlowCardGlassInfoDTOList(flowCardInfoList); resultList.add(hollowAllFlowCardVO); }); sortFlowCardIdList(resultList); return resultList; } @Override public List queryHollowAllFlowCardSummary(HollowBigStorageDetailsQueryVO query) { List detailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper() .eq(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN) .like(StringUtils.isNotBlank(query.getFilmsId()), HollowBigStorageCageDetails::getFilmsId, query.getFilmsId()) .like(StringUtils.isNotBlank(query.getFlowCardId()), HollowBigStorageCageDetails::getFlowCardId, query.getFlowCardId()) .eq(query.getThickness() != 0, HollowBigStorageCageDetails::getThickness, query.getThickness()) .orderByAsc(HollowBigStorageCageDetails::getFlowCardId) ); if (CollectionUtil.isEmpty(detailsList)) { log.info("笼内无玻璃"); return null; } Map> listMap = detailsList.stream().collect(Collectors.groupingBy(HollowBigStorageCageDetails::getFlowCardId)); List dtos = new ArrayList<>(); listMap.forEach((e, v) -> { HollowBigStorageCageDetails cageDetails = v.get(0); dtos.addAll(hollowBigStorageCageDetailsService.hollowIsAll(e, cageDetails.getTotalLayer(), Boolean.FALSE)); }); Map result = dtos.stream() .collect(Collectors.toMap( FlowCardGlassInfoDTO::getFlowCardId, dto -> { FlowCardGlassInfoDTO newDto = new FlowCardGlassInfoDTO(); newDto.setFlowCardId(dto.getFlowCardId()); newDto.setSumCount(dto.getSumCount()); newDto.setPairCount(dto.getPairCount()); newDto.setRealCount(dto.getRealCount()); newDto.setLayer(dto.getLayer()); newDto.setSlotCount(dto.getSlotCount()); return newDto; }, (dto1, dto2) -> { dto1.setRealCount(dto1.getRealCount() + dto2.getRealCount()); // 累加 realCount dto1.setLayer(Math.max(dto1.getLayer(), dto2.getLayer())); // 累加 最大层数 dto1.setSlotCount(dto1.getSlotCount() + dto2.getSlotCount()); return dto1; // 返回合并后的对象 } )); List resultList = new ArrayList<>(result.values()); List sortedList = resultList.stream() .sorted(Comparator.comparing(FlowCardGlassInfoDTO::getRealCount).reversed() // 第一个字段倒序 .thenComparing(FlowCardGlassInfoDTO::getSumCount, Comparator.reverseOrder())) // 第二个字段倒序 .collect(Collectors.toList()); return sortedList; } @Override public Map> queryLackByFlowCard(String flowCardId) { List lackDetailsList = this.baseMapper.queryLackByFlowCard(flowCardId); Map> listMap = lackDetailsList.stream().collect(Collectors.groupingBy(LackDetailsDTO::getLayer)); return listMap; } @Override public int queryLayerByFlowCardId(String flowCardId) { return baseMapper.queryLayerByFlowCardId(flowCardId); } @Override public Integer getGlassGapByThickness(Double thickness) { int sysKey = 0; if (thickness >= 12) { sysKey = ConstSysConfig.HOLLOW_GLASS_GAP_12; } else if (thickness == 8) { sysKey = ConstSysConfig.HOLLOW_GLASS_GAP_8; } else if (thickness == 10) { sysKey = ConstSysConfig.HOLLOW_GLASS_GAP_10; } else { sysKey = ConstSysConfig.HOLLOW_GLASS_GAP; } return sysConfigService.queryConfigValue(sysKey); } // @Override // public List queryLackByFlowCard(String flowCardId) { // List lackDetailsList = this.baseMapper.queryLackByFlowCard(flowCardId); // return lackDetailsList; // } private void sortFlowCardIdList(List list) { Pattern pattern = Pattern.compile("^NG(\\d+)([A-Za-z]+)(\\d+)$"); list.sort((v1, v2) -> { Matcher m1 = pattern.matcher(v1.getFlowCardId()); Matcher m2 = pattern.matcher(v2.getFlowCardId()); if (!m1.find() || !m2.find()) { throw new IllegalArgumentException("获取到的流程卡不符合校验规则"); } // 提取部分 BigInteger order1 = new BigInteger(m1.group(1)); BigInteger order2 = new BigInteger(m2.group(1)); String layer1 = m1.group(2); String layer2 = m2.group(2); BigInteger seq1 = new BigInteger(m1.group(3)); BigInteger seq2 = new BigInteger(m2.group(3)); // 优先级排序 int cmp = order1.compareTo(order2); if (cmp != 0) return cmp; cmp = layer1.compareTo(layer2); if (cmp != 0) return cmp; return seq1.compareTo(seq2); }); } }