zhoushihao
15 小时以前 97789ba474555471e0347928e148f5971ad4285a
hangzhoumesParent/moduleService/hollowGlassModule/src/main/java/com/mes/hollow/service/impl/HollowGlassRelationInfoServiceImpl.java
@@ -7,6 +7,7 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mes.common.config.Const;
import com.mes.common.config.ConstSysConfig;
import com.mes.damage.entity.Damage;
import com.mes.damage.entity.request.DamageRequest;
import com.mes.damage.service.DamageService;
import com.mes.glassinfo.entity.GlassInfo;
@@ -15,7 +16,9 @@
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.dto.FlowCardGlassInfoDTO;
import com.mes.hollow.entity.dto.HollowBigStorageDTO;
import com.mes.hollow.entity.dto.LackDetailsDTO;
import com.mes.hollow.entity.vo.HollowAllFlowCardVO;
import com.mes.hollow.entity.vo.HollowBigStorageDetailsQueryVO;
import com.mes.hollow.mapper.HollowGlassRelationInfoMapper;
@@ -25,6 +28,7 @@
import com.mes.hollow.service.HollowGlassRelationInfoService;
import com.mes.order.entity.HollowGlassDetailsDTO;
import com.mes.order.entity.OrderDetailsDTO;
import com.mes.order.entity.ProcessCardReport;
import com.mes.order.service.OrdersService;
import com.mes.sysconfig.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
@@ -112,11 +116,24 @@
        if (null == relationInfoOne) {
            throw new RuntimeException("相关流程卡未找到对应的组号信息,玻璃流程卡:" + flowCardId + ",序号:" + glassType + ",总层数:" + totalLayer + ",层数:" + layer);
        }
        Integer slotMaxHeight = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_SLOT_MAX_HEIGHT);
        Integer slotWidth = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_SLOT_WIDTH);
        HollowBigStorageCage storageCage = null;
        if (Math.min(width, height) > slotMaxHeight) {
            storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
                    .eq(HollowBigStorageCage::getEnableState, Const.SLOT_ON).eq(HollowBigStorageCage::getRemainWidth, slotWidth)
                    .eq(HollowBigStorageCage::getDeviceId, 6)
                    .orderByAsc(HollowBigStorageCage::getMaxThickness).last("limit 1"));
            HollowBigStorageDTO storageDTO = new HollowBigStorageDTO();
            BeanUtils.copyProperties(storageCage, storageDTO);
            BeanUtils.copyProperties(relationInfoOne, storageDTO);
            return storageDTO;
        }
        //详情表内获取本组是否已经有玻璃在笼子内(0表示提前占用)
        int taskCount = hollowGlassOutRelationInfoService.count(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId));
        HollowBigStorageCage storageCage = null;
        //如果不存在则选择笼内未用的新格子
        if (taskCount > 0) {
            storageCage = hollowBigStorageCageService.getOne(new LambdaQueryWrapper<HollowBigStorageCage>()
@@ -303,8 +320,12 @@
        for (List<HollowGlassRelationInfo> item : tempHollowList) {
            relationInfoList.addAll(item);
        }
        log.info("分配完毕");
        this.saveBatch(relationInfoList);
        log.info("分配完毕:{}", relationInfoList);
        try {
            this.saveBatch(relationInfoList);
        } catch (Exception e) {
            log.error("保存失败:{}", e);
        }
    }
    @Override
@@ -409,8 +430,90 @@
    }
    @Override
    public Map<Integer,List<LackDetailsDTO>> queryLackByFlowCard(String flowCardId) {
    public Map<Integer, List<LackDetailsDTO>> queryLackByFlowCard(String flowCardId) {
        List<LackDetailsDTO> detailsDTOS = baseMapper.queryLackByFlowCard(flowCardId);
        return detailsDTOS.stream().collect(Collectors.groupingBy(item -> item.getLayer()));
    }
    @Override
    public Map<Integer, List<LackDetailsDTO>> queryLackByFlowCardByERP(String flowCardId) {
        List<ProcessCardReport> processCardReports = ordersService.queryLackByERP(flowCardId);
        if (CollectionUtil.isEmpty(processCardReports)) {
            return null;
        }
        List<HollowBigStorageCageDetails> hollowBigStorageCageDetails = hollowBigStorageCageDetailsService.list(
                new LambdaQueryWrapper<HollowBigStorageCageDetails>()
                        .eq(HollowBigStorageCageDetails::getFlowCardId, flowCardId)
                        .in(HollowBigStorageCageDetails::getState, Const.GLASS_STATE_IN_ALL)
        );
        // 1. 统计每个 (glassType, layer) 组合的出现次数
        Map<String, Long> typeLayerCountMap = hollowBigStorageCageDetails.stream()
                .map(detail -> detail.getGlassType() + "_" + detail.getLayer())
                .collect(Collectors.groupingBy(
                        key -> key,  // 以复合键为分组依据
                        Collectors.counting()  // 统计每个键的出现次数
                ));
        // 2. 遍历并按次数减 quantity(避免负数)
        processCardReports.forEach(report -> {
            String reportPair = report.getOrderNumber() + "_" + report.getTechnologyNumber();
            Long count = typeLayerCountMap.getOrDefault(reportPair, 0L);
            if (count > 0) {
                int newQuantity = Math.max(0, report.getLackQuantity() - count.intValue());
                report.setLackQuantity(newQuantity);
            }
        });
        List<LackDetailsDTO> detailsDTOS = baseMapper.queryLackByFlowCardByERP(processCardReports);
        List<Damage> damages = damageService.queryUnTempByFlowCardId(flowCardId);
// 1. 按(orderNumber+layer)分组,同时缓存DTO的其他字段作为模板
        Map<String, List<Damage>> damageMap = new HashMap<>();
        Map<String, LackDetailsDTO> dtoTemplateMap = new HashMap<>(); // 存储分组对应的DTO模板
// 1.1 初始化damage分组和DTO模板
        damages.forEach(damage -> {
            if (damage.getOrderNumber() == null || damage.getTechnologyNumber() == null) {
                return;
            }
            String key = damage.getOrderNumber() + "_" + damage.getTechnologyNumber();
            damageMap.computeIfAbsent(key, k -> new ArrayList<>()).add(damage);
        });
        detailsDTOS.forEach(dto -> {
            if (dto.getGlassType() == null || dto.getLayer() == null) {
                return;
            }
            String key = dto.getGlassType() + "_" + dto.getLayer();
            // 缓存第一个DTO作为模板(包含其他字段值)
            dtoTemplateMap.putIfAbsent(key, dto);
        });
// 2. 匹配并更新原始DTO
        detailsDTOS.forEach(dto -> {
            if (dto.getGlassType() == null || dto.getLayer() == null) {
                return;
            }
            String key = dto.getGlassType() + "_" + dto.getLayer();
            List<Damage> damagess = damageMap.get(key);
            if (damagess != null && !damagess.isEmpty()) {
                Damage damage = damagess.remove(0);
                dto.setGlassId(damage.getGlassId());
                dto.setWorkingProcedure(damage.getWorkingProcedure());
            }
        });
// 3. 处理剩余damage:复用同组DTO模板的其他字段
        damageMap.values().forEach(damagess -> damagess.forEach(damage -> {
            String key = damage.getOrderNumber() + "_" + damage.getTechnologyNumber();
            LackDetailsDTO template = dtoTemplateMap.get(key); // 获取同组模板
            if (template == null) return; // 无模板则跳过(理论上不会出现)
            LackDetailsDTO newDto = new LackDetailsDTO();
            // 1. 复制模板中的其他字段(除了glassId和workproduce)
            BeanUtils.copyProperties(template, newDto); // 用Spring的工具类复制属性
            // 2. 覆盖glassId和workproduce为当前damage的值
            newDto.setGlassId(damage.getGlassId());
            newDto.setWorkingProcedure("未知");
            detailsDTOS.add(newDto);
        }));
        return detailsDTOS.stream().collect(Collectors.groupingBy(item -> item.getLayer()));
    }
@@ -460,23 +563,23 @@
    private void sortFlowCardIdList(List<HollowAllFlowCardVO> list) {
        Pattern pattern = Pattern.compile("^NG(\\d+)([A-Za-z]+)(\\d+)$");
        Pattern pattern = Pattern.compile("^(NG|R)(\\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("获取到的流程卡不符合校验规则");
                log.info("获取到的流程卡不符合校验规则:流程卡A:{},流程卡B:{}", v1.getFlowCardId(), v2.getFlowCardId());
                return -1;
            }
            // 提取部分
            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));
            BigInteger order1 = new BigInteger(m1.group(2));
            BigInteger order2 = new BigInteger(m2.group(2));
            String layer1 = m1.group(3);
            String layer2 = m2.group(3);
            BigInteger seq1 = new BigInteger(m1.group(4));
            BigInteger seq2 = new BigInteger(m2.group(4));
            // 优先级排序
            int cmp = order1.compareTo(order2);