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;
@@ -23,10 +26,13 @@
import com.mes.hollow.service.HollowBigStorageCageService;
import com.mes.hollow.service.HollowGlassOutRelationInfoService;
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;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -61,6 +67,8 @@
    SysConfigService sysConfigService;
    @Resource
    DamageService damageService;
    @Resource
    OrdersService ordersService;
//    @Value("${mes.slotWidth}")
//    private Integer slotWidth;
//    @Value("${mes.glassGap}")
@@ -108,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>()
@@ -208,13 +229,13 @@
            return;
        }
        //按照流程卡获取本流程卡最后一层或第一次的玻璃数据
        List<HollowGlassDetailsDTO> glassDetailsDTOS = this.baseMapper.queryFlowCardIdMaxLayerGlassInfo(flowCardId, totalLayer);
        List<HollowGlassDetailsDTO> glassDetailsDTOS = ordersService.queryFlowCardIdMaxLayerGlassInfo(flowCardId, totalLayer);
        if (CollectionUtil.isEmpty(glassDetailsDTOS)) {
            log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{}", flowCardId, totalLayer);
            return;
        }
        if (totalLayer != layer) {
            glassDetailsDTOS = this.baseMapper.queryFlowCardIdLayerGlassInfo(flowCardId, totalLayer, layer);
            glassDetailsDTOS = ordersService.queryFlowCardIdLayerGlassInfo(flowCardId, totalLayer, layer);
        }
        if (CollectionUtil.isEmpty(glassDetailsDTOS)) {
            log.info("当前流程卡最外层数据未找到,请在erp确认数据无误,流程卡:{},总层数{},层数{}", flowCardId, totalLayer, layer);
@@ -299,33 +320,32 @@
        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
    public List<HollowAllFlowCardVO> queryHollowAllFlowCard(HollowBigStorageDetailsQueryVO query) {
        Date startDate = new Date();
        log.info("开始查询中空流程卡任务信息,开始时间{}", startDate);
        List<HollowBigStorageCageDetails> detailsList = hollowBigStorageCageDetailsService.list(new LambdaQueryWrapper<HollowBigStorageCageDetails>()
                .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)
        );
        List<FlowCardGlassInfoDTO> detailsList = hollowBigStorageCageDetailsService.queryHollowAllFlowCard(query);
        if (CollectionUtil.isEmpty(detailsList)) {
            log.info("笼内无玻璃");
            return new ArrayList<>();
        }
        Date middleDate = new Date();
        log.info("中空理片笼详情数据已查询完毕,耗时:{}ms", middleDate.getTime() - startDate.getTime());
        Map<String, List<HollowBigStorageCageDetails>> listMap = detailsList.stream().collect(Collectors.groupingBy(HollowBigStorageCageDetails::getFlowCardId));
        Map<String, List<FlowCardGlassInfoDTO>> listMap = detailsList.stream().collect(Collectors.groupingBy(FlowCardGlassInfoDTO::getFlowCardId));
        List<HollowAllFlowCardVO> resultList = new ArrayList<>();
        AtomicInteger pairTotalCount = new AtomicInteger();
        listMap.forEach((e, v) -> {
            HollowAllFlowCardVO hollowAllFlowCardVO = new HollowAllFlowCardVO();
            HollowBigStorageCageDetails cageDetails = v.get(0);
            FlowCardGlassInfoDTO cageDetails = v.get(0);
            //按照流程卡获取对应的产品名称
            OrderDetailsDTO orderDetails = this.queryProductNameByFlowCardId(cageDetails.getFlowCardId(), query.getProductName(), query.getCustomerName());
            if (null != orderDetails) {
@@ -336,12 +356,8 @@
                } else {
                    hollowAllFlowCardVO.setIsThroughSlot(Boolean.FALSE);
                }
                List<FlowCardGlassInfoDTO> flowCardInfoList = hollowBigStorageCageDetailsService.hollowIsAll(e, cageDetails.getTotalLayer(), Boolean.FALSE);
                log.info("获取到的流程卡信息为:{}", flowCardInfoList);
                if (CollectionUtil.isNotEmpty(flowCardInfoList)) {
                    hollowAllFlowCardVO.setFlowCardGlassInfoDTOList(flowCardInfoList);
                    pairTotalCount.addAndGet(flowCardInfoList.get(0).getPairCount());
                }
                hollowAllFlowCardVO.setFlowCardGlassInfoDTOList(v);
                pairTotalCount.addAndGet(cageDetails.getPairCount());
                resultList.add(hollowAllFlowCardVO);
            }
        });
@@ -403,14 +419,102 @@
    }
    @Override
    public List<LackDetailsDTO> queryLackByFlowCard() {
        List<LackDetailsDTO> lackDetailsList = this.baseMapper.queryLackByFlowCard();
    public List<LackDetailsDTO> queryAllLackByFlowCard() {
        List<LackDetailsDTO> lackDetailsList = this.baseMapper.queryAllLackByFlowCard();
        return lackDetailsList;
    }
    @Override
    public int queryLayerByFlowCardId(String flowCardId) {
        return baseMapper.queryLayerByFlowCardId(flowCardId);
    }
    @Override
    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()));
    }
    @Override
@@ -433,7 +537,9 @@
        List<GlassInfo> glassInfos = glassInfoService.list(new LambdaQueryWrapper<GlassInfo>()
                .eq(GlassInfo::getFlowCardId, request.getFlowCardId())
                .eq(GlassInfo::getLayer, request.getLayer())
                .eq(GlassInfo::getGlassType, request.getGlassType()));
                .eq(GlassInfo::getGlassType, request.getGlassType())
                .eq(request.getGlassId() != null, GlassInfo::getGlassId, request.getGlassId())
        );
        for (GlassInfo glassInfo : glassInfos) {
            //掰片报破损
            damageService.autoSubmitReport(glassInfo.getGlassId(), request.getLine(), request.getWorkingProcedure(), request.getRemark(), request.getState());
@@ -442,8 +548,13 @@
    }
    @Override
    public List<LackDetailsDTO> queryLackGlassByFlowCard(HollowBigStorageDetailsQueryVO query) {
        return baseMapper.queryLackGlassByFlowCard(query.getFlowCardId(), query.getOrderSort(), query.getLayer());
    }
    @Override
    public OrderDetailsDTO queryProductNameByFlowCardId(String flowCardId, String productName, String customerName) {
        OrderDetailsDTO dto = hollowGlassOutRelationInfoService.queryProductNameByFlowCardId(flowCardId);
        OrderDetailsDTO dto = ordersService.queryProductNameByFlowCardId(flowCardId);
        if ((StringUtils.isBlank(productName) || dto.getProductName().contains(productName)) && (StringUtils.isBlank(customerName) || dto.getCustomerName().contains(customerName))) {
            return dto;
        }
@@ -452,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);