ZengTao
2025-09-11 61cae17b15d6a7d256e25ce42f0a4ef4163fd088
hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java
@@ -36,6 +36,7 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * @Author : zhoush
@@ -93,6 +94,15 @@
        String confirmationWrodValue = plcParameterObject.getPlcParameter("confirmationWord").getValue();
        String confirmationWrodAddress = plcParameterObject.getPlcParameter("confirmationWord").getAddress();
        String currentSlot = plcParameterObject.getPlcParameter("currentCell").getValue();
        requestWord = "2";
        glassIdeValue = "";
        out06Glassstate = "1";
        out08Glassstate = "1";
        out11Glassstate = "1";
        out13Glassstate = "1";
        currentSlot = "1";
        confirmationWrodValue = "0";
        log.info("1、获取到的请求字为:{},获取到的扫描ID为:{},获取到的确认字为:{},获取到的出片状态分别为:g06:{}、g08:{}、g11:{}、g13:{},当前格子号为:{}",
@@ -227,6 +237,7 @@
        //玻璃尺寸是否走人工下片
        if (glassWidth > maxWidth || glassHeight > maxHeight || glassWidth < minWidth || glassHeight < minHeight) {
            log.info("该玻璃尺寸不符合要求,需要走人工下片直接进片");
            return;
        } else {
            log.info("该玻璃尺寸非人工下片");
            //获取该玻璃的流程卡是否已绑定架子
@@ -352,6 +363,145 @@
            log.info("笼内没有玻璃,无法执行出片");
            return Boolean.FALSE;
        }
//        //获取最小工程与炉数
//        Map<String, List<DownStorageCageDetails>> groupedMap = tempList.stream()
//                .collect(Collectors.groupingBy(item -> item.getEngineerId() + "_" + item.getTemperingLayoutId()));
//        // 获取第一个 先进卧理那一炉的信息
//        List<DownStorageCageDetails> cageList =
//                new ArrayList<>(groupedMap.values().stream().findFirst().orElse(Collections.emptyList()));
//
//        if (groupedMap.size() > 1) {
//            //按流程卡进行分组
//            Map<String, List<DownStorageCageDetails>> getFlowCardIds = cageList.stream()
//                    .collect(Collectors.groupingBy(item -> item.getFlowCardId() + "_" + item.getLayer()));
//            // 按 短边 降序,长边 降序,炉内片序 降序
//            getFlowCardIds.forEach((key, list) -> {
//                list.sort(Comparator.comparingDouble((DownStorageCageDetails item) -> Math.min(item.getWidth(), item.getHeight()))
//                        .reversed()
//                        .thenComparingDouble(item -> Math.max(item.getWidth(), item.getHeight()))
//                        .reversed()
//                        .thenComparingInt(item -> item.getTemperingFeedSequence()));
//            });
//            //获取每个流程卡出片优先级最高的玻璃
//            tempList = getFlowCardIds.values().stream()
//                    .map(list -> list.isEmpty() ? null : list.get(0))
//                    .filter(Objects::nonNull)
//                    .collect(Collectors.toList());
//        } else {
//            //笼内加扫码位玻璃id
//            List<String> cageGlassIds = cageList.stream()
//                    .map(DownStorageCageDetails::getGlassId)
//                    .collect(Collectors.toList());
//            //获取破损拿走的玻璃并过滤调已经在笼内的,防止乱报破损
//            List<Damage> damageList = damageService.list(
//                    new LambdaQueryWrapper<Damage>()
//                            .eq(Damage::getEngineerId, cageList.get(0).getEngineerId())
//                            .eq(Damage::getTemperingLayoutId, cageList.get(0).getTemperingLayoutId())
//                            .and(item -> item.eq(Damage::getType, 8).or().eq(Damage::getType, 9))
//                            .lt(Damage::getStatus, 4)
//                            .notIn(Damage::getGlassId, cageGlassIds)
//            );
//            //获取破损/拿走玻璃id
//            List<String> damageGlassIds = damageList.stream()
//                    .map(Damage::getGlassId)
//                    .collect(Collectors.toList());
//            //获取已出片玻璃信息
//            List<DownStorageCageDetails> downStorageCageDetails = downStorageCageDetailsService.list(
//                    new LambdaQueryWrapper<DownStorageCageDetails>()
//                            .eq(DownStorageCageDetails::getEngineerId, cageList.get(0).getEngineerId())
//                            .eq(DownStorageCageDetails::getTemperingLayoutId, cageList.get(0).getTemperingLayoutId())
//            );
//            //获取已出片玻璃id
//            List<String> downGlassIds = downStorageCageDetails.stream()
//                    .map(DownStorageCageDetails::getGlassId)
//                    .collect(Collectors.toList());
//            //获取所有未下片的玻璃信息
//            List<GlassInfo> glassInfoList = glassInfoService.list(
//                    new LambdaQueryWrapper<GlassInfo>()
//                            .eq(GlassInfo::getEngineerId, cageList.get(0).getEngineerId())
//                            .eq(GlassInfo::getTemperingLayoutId, cageList.get(0).getTemperingLayoutId())
//                            .notIn(CollectionUtils.isNotEmpty(damageGlassIds), GlassInfo::getGlassId, damageGlassIds)
//                            .notIn(CollectionUtils.isNotEmpty(downGlassIds), GlassInfo::getGlassId, downGlassIds)
//            );
//            //按流程卡进行分组
//            Map<String, List<GlassInfo>> getFlowCardIds = glassInfoList.stream()
//                    .collect(Collectors.groupingBy(item -> item.getFlowCardId() + "_" + item.getLayer()));
//            // 按 短边 降序,长边 降序,炉内片序 降序
//            getFlowCardIds.forEach((key, list) -> {
//                list.sort(Comparator.comparingDouble((GlassInfo item) -> Math.min(item.getWidth(), item.getHeight()))
//                        .reversed()
//                        .thenComparingDouble(item -> Math.max(item.getWidth(), item.getHeight()))
//                        .reversed()
//                        .thenComparingInt(item -> item.getTemperingFeedSequence()));
//            });
//            //获取一炉所有未出片流程卡优先级最高的玻璃
//            List<GlassInfo> glassInfos = getFlowCardIds.values().stream()
//                    .map(list -> list.isEmpty() ? null : list.get(0))
//                    .filter(Objects::nonNull)
//                    .collect(Collectors.toList());
//            //获取一炉所有未出片流程卡优先级最高的玻璃id
//            List<String> noOutGlassIds = glassInfos.stream()
//                    .map(GlassInfo::getGlassId)
//                    .collect(Collectors.toList());
//            //获取匹配的玻璃信息
//            List<DownStorageCageDetails> filteredList = tempList.stream()
//                    .filter(item -> noOutGlassIds.contains(item.getGlassId()))
//                    .collect(Collectors.toList());
//            if (CollectionUtils.isNotEmpty(filteredList)) {
//                tempList = filteredList;
//            } else {
//                //是否满笼
//                if (tempList.size() == 9) {
//                    // 结果列表
//                    List<DownStorageCageDetails> resultList = new ArrayList<>();
//
//                    // 将 glassInfos 转换成 Map,以 flowCardId_layer 作为 key
//                    Map<String, GlassInfo> glassInfoMap = glassInfos.stream()
//                            .collect(Collectors.toMap(
//                                    item -> item.getFlowCardId() + "_" + item.getLayer(),
//                                    item -> item
//                            ));
//
//                    // 按 flowCardId 和 layer 进行分组
//                    Map<String, List<DownStorageCageDetails>> groupedByFlowCardLayer = tempList.stream()
//                            .collect(Collectors.groupingBy(item -> item.getFlowCardId() + "_" + item.getLayer()));
//
//                    // 遍历分组,找出每一组中误差最小的 DownStorageCageDetails
//                    for (Map.Entry<String, List<DownStorageCageDetails>> entry : groupedByFlowCardLayer.entrySet()) {
//                        String key = entry.getKey();
//                        List<DownStorageCageDetails> groupList = entry.getValue();
//
//                        // 只有当 glassInfoMap 里有相应的 key 时,才计算最接近的匹配项
//                        if (glassInfoMap.containsKey(key)) {
//                            GlassInfo glassInfo = glassInfoMap.get(key);
//                            double glassShortEdge = Math.min(glassInfo.getWidth(), glassInfo.getHeight());
//                            double glassLongEdge = Math.max(glassInfo.getWidth(), glassInfo.getHeight());
//
//                            // 找到误差最小的 DownStorageCageDetails
//                            Optional<DownStorageCageDetails> bestMatch = groupList.stream()
//                                    .min(Comparator.comparingDouble(item -> {
//                                        double itemShortEdge = Math.min(item.getWidth(), item.getHeight());
//                                        double itemLongEdge = Math.max(item.getWidth(), item.getHeight());
//
//                                        // 先比较短边误差
//                                        double shortEdgeDiff = Math.abs(itemShortEdge - glassShortEdge);
//                                        // 再比较长边误差(仅当短边误差相同时)
//                                        double longEdgeDiff = Math.abs(itemLongEdge - glassLongEdge);
//
//                                        return shortEdgeDiff * 1000 + longEdgeDiff; // 让短边误差优先级更高
//                                    }));
//
//                            bestMatch.ifPresent(resultList::add);
//                        }
//                    }
//                    tempList = resultList;
//                } else {
//                    return Boolean.FALSE;
//                }
//            }
//        }
        //优先走08片台的玻璃:走人工下片或者2号机械臂
        //1、08台忙碌,仅走1号机械臂
        //2、08台空闲,先走扔工下片或2号机械臂,无玻璃出片 在走1号机械臂
@@ -677,21 +827,28 @@
            wrapper.eq(DownGlassInfo::getLayer, downGlassInfo.getLayer());
        }
//        downGlassInfo.setSequence(downGlassInfoService.queryMaxSequence(downGlassInfo.getFlowCardId(), downGlassInfo.getLayer()));
        int k = 0;
        DownGlassInfo downGlassInfo1 = downGlassInfoService.getOne(wrapper);
        if (downGlassInfo1 != null) {
            k = 1;
        } else {
            k = 1;
        }
        int k = 1;
//        DownGlassInfo downGlassInfo1 = downGlassInfoService.getOne(wrapper);
//        if (downGlassInfo1 != null) {
//            k = 1;
//        } else {
//            k = 1;
//        }
        GlassInfo glassInfo = new GlassInfo();
        BeanUtils.copyProperties(downStorageCageDetails, glassInfo);
        GlassInfo glassInfo1 = glassInfoService.getOne(
                new LambdaUpdateWrapper<GlassInfo>().eq(GlassInfo::getGlassId, glassInfo.getGlassId())
        );
        downGlassInfo.setSequence(k);
        downGlassInfo.setWorkStationId(endCell);
        downGlassInfo.setGmtCreate(new Date());
        downGlassInfo.setLayer(glassInfo1.getLayer());
        downGlassInfoService.save(downGlassInfo);
        //生成任务信息
        GlassInfo glassInfo = new GlassInfo();
        BeanUtils.copyProperties(downStorageCageDetails, glassInfo);
        //删除理片笼表拿走/破损数据数据
        glassInfo.setLayer(glassInfo1.getLayer());
        damageService.deleteByGlassId(glassInfo.getGlassId());
        return initDownGlassTask(glassInfo, downStorageCageDetails.getSlot(), endCell, taskType);
    }