wangfei
2025-10-30 cbf12607ca1179d98f76d9b3a475d6224e85d59a
hangzhoumesParent/moduleService/hollowGlassModule/src/main/java/com/mes/hollow/service/impl/HollowGlassOutRelationInfoServiceImpl.java
@@ -1,17 +1,24 @@
package com.mes.hollow.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.smallbun.screw.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.controller.HollowBigStorageCageController;
import com.mes.hollow.entity.HollowBigStorageCageDetails;
import com.mes.hollow.entity.HollowFormulaDetails;
import com.mes.hollow.entity.HollowGlassOutRelationInfo;
import com.mes.hollow.entity.dto.*;
import com.mes.hollow.entity.dto.LisecHollowDetails;
import com.mes.hollow.entity.dto.LisecHollowFrameDetails;
import com.mes.hollow.entity.dto.LisecHollowGlassAndFrameDetails;
import com.mes.hollow.entity.dto.LisecHollowGlassDetails;
import com.mes.hollow.entity.request.HollowHistoryTaskRequest;
import com.mes.hollow.entity.request.HollowTaskRequest;
import com.mes.hollow.mapper.HollowGlassOutRelationInfoMapper;
import com.mes.hollow.service.HollowBigStorageCageDetailsService;
@@ -20,6 +27,12 @@
import com.mes.hollow.service.HollowGlassRelationInfoService;
import com.mes.hollowqueue.entity.HollowGlassQueueInfo;
import com.mes.hollowqueue.service.HollowGlassQueueInfoService;
import com.mes.largenscreen.entity.PieChartVO;
import com.mes.order.entity.HollowOrderDTO;
import com.mes.order.entity.OrderDetailsDTO;
import com.mes.order.service.OrdersService;
import com.mes.sysconfig.service.SysConfigService;
import com.mes.tools.DateUtil;
import com.mes.utils.Blank;
import com.mes.utils.RedisUtil;
import freemarker.template.Configuration;
@@ -28,8 +41,9 @@
import freemarker.template.Version;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.*;
@@ -57,24 +71,30 @@
    HollowFormulaDetailsService hollowFormulaDetailsService;
    @Resource
    HollowGlassRelationInfoService hollowGlassRelationInfoService;
    @Resource
    SysConfigService sysConfigService;
    @Resource
    OrdersService ordersService;
    @Resource
    RedisUtil redisUtil;
    @Value("${mes.glassGap}")
    private Integer glassGap;
    @Value("${mes.carWidth}")
    private Integer carWidth;
//    @Value("${mes.glassGap}")
//    private Integer glassGap;
//
//    @Value("${mes.carWidth}")
//    private Integer carWidth;
    private static final int ID_RATIO = 10;
    @Override
//    @Transactional
    public HollowGlassOutRelationInfo receiveTask(HollowTaskRequest request) {
        return childrenTask(request, 0);
    }
    @Override
//    @Transactional
    public HollowGlassOutRelationInfo forceOutGlass(HollowTaskRequest request) {
        return childrenTask(request, 1);
    }
@@ -83,6 +103,12 @@
    public Boolean dispatchHollowSwitch(Boolean flag) {
        redisUtil.setCacheObject("dispatchHollowSwitch", flag);
        return redisUtil.getCacheObject("dispatchHollowSwitch");
    }
    @Override
    public Boolean priorityHollowSwitch(Boolean flag) {
        redisUtil.setCacheObject("priorityHollowSwitch", flag);
        return redisUtil.getCacheObject("priorityHollowSwitch");
    }
    @Override
@@ -98,44 +124,44 @@
    }
    @Override
    public List<HollowGlassQueueInfo> appointHollowTaskDetails(String flowCardId, int cell) {
    public Map<String, List<HollowGlassQueueInfo>> appointHollowTaskDetails(int cell) {
        //按照流程卡及路线,查找对应的任务信息
        HollowGlassOutRelationInfo hollowGlassOutRelationInfo = this.getOne(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
        List<HollowGlassOutRelationInfo> hollowGlassOutRelationInfos = this.list(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .in(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_NEW, Const.HOLLOW_FLOW_CARD_START, Const.HOLLOW_FLOW_CARD_PAUSE)
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId)
                .eq(HollowGlassOutRelationInfo::getCell, cell).last("limit 1"));
        if (null == hollowGlassOutRelationInfo) {
            return new ArrayList<HollowGlassQueueInfo>();
                .eq(HollowGlassOutRelationInfo::getCell, cell));
        if (CollectionUtil.isEmpty(hollowGlassOutRelationInfos)) {
            return null;
        }
        List<Long> ids = hollowGlassOutRelationInfos.stream()
                .map(HollowGlassOutRelationInfo::getId) // 提取每个元素的id
                .collect(Collectors.toList());
        //按照任务id查询对列表中的队列信息
        return hollowGlassQueueInfoService.list(new LambdaQueryWrapper<HollowGlassQueueInfo>()
                .eq(HollowGlassQueueInfo::getRelationId, hollowGlassOutRelationInfo.getId()));
        List<HollowGlassQueueInfo> hollowGlassQueueInfos = hollowGlassQueueInfoService.list(new LambdaQueryWrapper<HollowGlassQueueInfo>()
                .in(HollowGlassQueueInfo::getRelationId, ids)
                .orderByAsc(HollowGlassQueueInfo::getId));
        Map<String, List<HollowGlassQueueInfo>> groupedByFlowCardId = hollowGlassQueueInfos.stream()
                .collect(Collectors.groupingBy(
                        HollowGlassQueueInfo::getFlowCardId,
                        LinkedHashMap::new,
                        Collectors.toList()
                ));
        return groupedByFlowCardId;
    }
    @Override
    public Boolean startTask(String flowCardId, int cell) {
        log.info("查看该流程卡是否由正在执行的任务,流程卡:{}", flowCardId);
        int taskCount = this.count(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .in(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_NEW, Const.HOLLOW_FLOW_CARD_START, Const.HOLLOW_FLOW_CARD_PAUSE)
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId));
        if (taskCount == 0 || taskCount > 1) {
            log.info("该流程卡不存在任务或者有正在执行中,无法再次执行");
            return Boolean.FALSE;
        }
    public Boolean startTask(int cell) {
        //更新任务状态为开始
        return this.update(new LambdaUpdateWrapper<HollowGlassOutRelationInfo>()
                .set(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_START)
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId)
                .ne(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_SUCCESS)
                .eq(HollowGlassOutRelationInfo::getCell, cell));
    }
    @Override
    public Boolean pauseTask(String flowCardId, int cell) {
    public Boolean pauseTask(int cell) {
//        更新任务状态为暂停
        return this.update(new LambdaUpdateWrapper<HollowGlassOutRelationInfo>()
                .set(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_PAUSE)
                .eq(HollowGlassOutRelationInfo::getFlowCardId, flowCardId)
                .ne(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_SUCCESS)
                .eq(HollowGlassOutRelationInfo::getCell, cell));
    }
@@ -177,15 +203,17 @@
        if (CollectionUtil.isEmpty(outRelationList) || outRelationList.size() != 1) {
            return "本条线不存在该流程任务或者同流程卡任务数大于1";
        }
        log.info("李赛克流程卡:{}", flowCardId);
        HollowGlassOutRelationInfo relationInfo = outRelationList.get(0);
        if (relationInfo.getTotalLayer() < 2) {
            return "任务总层数小于2,不生成李赛克文件";
        }
        //获取订单相关信息
        OrderDTO order = baseMapper.queryOrderByFlowCardId(flowCardId);
        HollowOrderDTO order = ordersService.queryOrderByFlowCardId(flowCardId);
        if (null == order) {
            return "生成失败,相关订单信息不存在";
        }
        log.info("李赛克相关订单信息{}", order);
        //获取配方相关信息
        HollowFormulaDetails formulaDetails = hollowFormulaDetailsService.getById(relationInfo.getFormulaId());
        if (null == formulaDetails) {
@@ -196,6 +224,8 @@
        if (layerCount != relationInfo.getTotalLayer() && isForce == 0) {
            return "生成失败,该流程卡内层数与进笼关系表内层数数量不相同";
        }
        log.info("李赛克总层数{}", layerCount);
        //设置文件的主体内容
        LisecHollowDetails details = new LisecHollowDetails();
        String randomNumber = "" + (int) (Math.random() * 100000 + 100000);
@@ -209,14 +239,16 @@
        //设置文件第一层列表数据
        //暂时生成笼内所有的玻璃信息(已配对和未配对的)
//        List<HollowGlassRelationInfo> hollowGlassRelationInfos = hollowGlassRelationInfoService.list(new LambdaQueryWrapper<HollowGlassRelationInfo>()
//                .eq(HollowGlassRelationInfo::getFlowCardId, flowCardId).orderByAsc(HollowGlassRelationInfo::getHollowSequence));
        List<HollowBigStorageCageDetails> hollowGlassRelationInfos = hollowBigStorageCageDetailsService.queryPairGlassList(flowCardId, relationInfo.getTotalLayer(), relationInfo.getTotalPairQuantity(), isOut);
        Map<Integer, List<HollowBigStorageCageDetails>> listMap = hollowGlassRelationInfos.stream().collect(Collectors.groupingBy(HollowBigStorageCageDetails::getHollowSequence));
        Map<Integer, List<HollowBigStorageCageDetails>> listMap = hollowGlassRelationInfos.stream()
                .collect(Collectors.groupingBy(HollowBigStorageCageDetails::getHollowSequence,
                        Collectors.collectingAndThen(Collectors.toList(),
                                list -> list.stream().sorted(Comparator.comparing(HollowBigStorageCageDetails::getLayer)).collect(Collectors.toList()))));
        Map<Integer, List<HollowBigStorageCageDetails>> sortListMap = new TreeMap<>();
        sortListMap.putAll(listMap);
        //设置间隔板数据共所有配对玻璃使用
        List<LisecHollowGlassAndFrameDetails> glassAndFrameList = new ArrayList<>();
        listMap.forEach((e, v) -> {
        sortListMap.forEach((e, v) -> {
            LisecHollowGlassAndFrameDetails glassAndFrame = new LisecHollowGlassAndFrameDetails();
            List<LisecHollowGlassDetails> glassList = new ArrayList<>();
            List<LisecHollowFrameDetails> frameList = new ArrayList<>();
@@ -278,13 +310,12 @@
            glassAndFrameList.add(glassAndFrame);
        });
        details.setGlassAndFrameList(glassAndFrameList);
        Configuration cfg = new Configuration(new Version("2.3.29"));
        cfg.setClassForTemplateLoading(HollowBigStorageCageController.class, "/templates/");
        // 创建Calculator实例
        Blank blank = new Blank();
        log.info("李赛克基础数据已生成:{}", details);
        // 创建数据模型
        Map<String, Object> root = new HashMap<>();
        root.put("blank", blank);
@@ -304,91 +335,130 @@
            writer.write(out.toString());
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
            log.info("李赛克生成异常:{}", e.getMessage());
        }
        return "success";
    }
    @Override
    public Page<HollowGlassOutRelationInfo> queryHollowHistoryTask(HollowHistoryTaskRequest request) {
        Page<HollowGlassOutRelationInfo> page = new Page<>(request.getPageNo(), request.getPageSize());
        if (null == request.getBeginDate()) {
            request.setBeginDate(DateUtil.getBeginDate());
            request.setEndDate(DateUtil.getEndDate());
        }
        LambdaQueryWrapper<HollowGlassOutRelationInfo> wrapper = new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .eq(HollowGlassOutRelationInfo::getCell, request.getCell())
                .like(StringUtils.isNotBlank(request.getFlowCardId()), HollowGlassOutRelationInfo::getFlowCardId, request.getFlowCardId())
                .in(CollectionUtil.isNotEmpty(request.getIsForceList()), HollowGlassOutRelationInfo::getIsForce, request.getIsForceList())
                .in(CollectionUtil.isNotEmpty(request.getStateList()), HollowGlassOutRelationInfo::getState, request.getStateList())
                .between(HollowGlassOutRelationInfo::getCreateTime, request.getBeginDate(), request.getEndDate())
                .orderByDesc(HollowGlassOutRelationInfo::getCreateTime);
        return this.page(page, wrapper);
    }
    @Override
    @Cacheable(value = "orderDetails", key = "#flowCardId")
    public OrderDetailsDTO queryProductNameByFlowCardId(String flowCardId) {
        log.info("查询数据库一次:{}", flowCardId);
        return ordersService.queryProductNameByFlowCardId(flowCardId);
    }
    private HollowGlassOutRelationInfo childrenTask(HollowTaskRequest request, int isForce) {
        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getFlowCardId, request.getFlowCardId()).last("limit 1"));
        HollowGlassOutRelationInfo info = new HollowGlassOutRelationInfo();
        if (null == glassInfo) {
            log.info("该流程卡信息系统未找到");
            return info;
        }
        List<HollowGlassOutRelationInfo> outRelationInfos = this.list(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                .eq(HollowGlassOutRelationInfo::getFlowCardId, request.getFlowCardId())
                .in(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_NEW, Const.HOLLOW_FLOW_CARD_START, Const.HOLLOW_FLOW_CARD_PAUSE));
        if (CollectionUtil.isNotEmpty(outRelationInfos)) {
            log.info("当前流程卡有未完成的任务");
            return null;
        }
        //保存任务关系主表
        info.setFlowCardId(request.getFlowCardId());
        info.setCell(request.getCell());
        info.setIsForce(isForce);
        info.setTotalLayer(glassInfo.getTotalLayer());
        info.setState(Const.HOLLOW_FLOW_CARD_NEW);
        info.setTotalPairQuantity(request.getTotalPairQuantity());
        info.setFormulaId(request.getFormulaId());
        this.save(info);
        // 查询出需要出玻璃的队列
        List<HollowBigStorageCageDetails> hollowBigStorageCageDetailsList = hollowBigStorageCageDetailsService
                .queryOutGlassList(request.getFlowCardId(), request.getCell());
        int isPairCount = glassInfo.getTotalLayer() * request.getTotalPairQuantity();
        List<HollowGlassQueueInfo> hollowQueues = new ArrayList<>();
        if (930 == request.getCell()) {
            Map<Integer, List<HollowBigStorageCageDetails>> listMap = hollowBigStorageCageDetailsList.stream()
                    .collect(Collectors.groupingBy(HollowBigStorageCageDetails::getHollowSequence));
            for (Map.Entry<Integer, List<HollowBigStorageCageDetails>> entry : listMap.entrySet()) {
                //创建队列接收不满足成对情况下:一对玻璃分几次上车的次序问题
                List<HollowGlassQueueInfo> tempList = new ArrayList<>();
                //先将玻璃按照正常顺序排列,计算一车最多放几块,算好块数之后将一车的玻璃按照倒序存储
                List<HollowBigStorageCageDetails> reverse = CollectionUtil.reverse(entry.getValue());
                int remainWidth = carWidth;
                for (HollowBigStorageCageDetails item : reverse) {
                    remainWidth = remainWidth - (int) Math.max(item.getWidth(), item.getHeight());
                    if (remainWidth < 0) {
                        hollowQueues.addAll(CollectionUtil.reverse(tempList));
                        tempList = new ArrayList<>();
                        remainWidth = carWidth - (int) Math.max(item.getWidth(), item.getHeight());
                    }
                    HollowGlassQueueInfo queueInfo = HollowBDetailToQueue(item, info.getId(), request.getCell());
                    tempList.add(queueInfo);
                    remainWidth = remainWidth - glassGap;
                }
                hollowQueues.addAll(CollectionUtil.reverse(tempList));
                HollowBigStorageCageDetails cageDetails = entry.getValue().get(0);
                if (cageDetails.getIsPair() == 1) {
                    isPairCount = isPairCount - cageDetails.getTotalLayer();
                    if (isPairCount == 0) {
                        break;
                    }
                }
            }
        } else {
            loop:
            for (HollowBigStorageCageDetails item : hollowBigStorageCageDetailsList) {
                HollowGlassQueueInfo queueInfo = HollowBDetailToQueue(item, info.getId(), request.getCell());
                hollowQueues.add(queueInfo);
                if (item.getIsPair() == 1) {
                    isPairCount = isPairCount - 1;
                    if (isPairCount == 0) {
                        break loop;
                    }
                }
            }
        }
        hollowGlassQueueInfoService.saveBatch(hollowQueues);
        try {
            if (request.getCell() == 930) {
                generateHollowLisecFile(request.getFlowCardId(), 930, isForce, 0);
            GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>().eq(GlassInfo::getFlowCardId, request.getFlowCardId()).last("limit 1"));
            HollowGlassOutRelationInfo info = new HollowGlassOutRelationInfo();
            if (null == glassInfo) {
                log.info("该流程卡信息系统未找到");
                return info;
            }
            log.error("创建任务时获取玻璃信息:{}", info);
            List<HollowGlassOutRelationInfo> outRelationInfos = this.list(new LambdaQueryWrapper<HollowGlassOutRelationInfo>()
                    .eq(HollowGlassOutRelationInfo::getFlowCardId, request.getFlowCardId())
                    .in(HollowGlassOutRelationInfo::getState, Const.HOLLOW_FLOW_CARD_NEW, Const.HOLLOW_FLOW_CARD_START, Const.HOLLOW_FLOW_CARD_PAUSE));
            if (CollectionUtil.isNotEmpty(outRelationInfos)) {
                log.info("当前流程卡有未完成的任务");
                return null;
            }
            //保存任务关系主表
            info.setFlowCardId(request.getFlowCardId());
            info.setCell(request.getCell());
            info.setIsForce(isForce);
            info.setTotalLayer(glassInfo.getTotalLayer());
            info.setState(Const.HOLLOW_FLOW_CARD_NEW);
            info.setTotalPairQuantity(request.getTotalPairQuantity());
            info.setFormulaId(request.getFormulaId());
            this.save(info);
            // 查询出需要出玻璃的队列
            List<HollowBigStorageCageDetails> hollowBigStorageCageDetailsList = hollowBigStorageCageDetailsService
                    .queryOutGlassList(request.getFlowCardId(), request.getCell());
            int isPairCount = glassInfo.getTotalLayer() * request.getTotalPairQuantity();
            List<HollowGlassQueueInfo> hollowQueues = new ArrayList<>();
            Integer carWidth = sysConfigService.queryConfigValue(ConstSysConfig.HOLLOW_CAR_WIDTH);
            Integer glassGap = hollowGlassRelationInfoService.getGlassGapByThickness(hollowBigStorageCageDetailsList.get(0).getThickness());
            if (930 == request.getCell()) {
                Map<Integer, List<HollowBigStorageCageDetails>> listMap = hollowBigStorageCageDetailsList.stream()
                        .collect(Collectors.groupingBy(HollowBigStorageCageDetails::getHollowSequence));
                Map<Integer, List<HollowBigStorageCageDetails>> sortListMap = new TreeMap<>();
                sortListMap.putAll(listMap);
                for (Map.Entry<Integer, List<HollowBigStorageCageDetails>> entry : sortListMap.entrySet()) {
                    //创建队列接收不满足成对情况下:一对玻璃分几次上车的次序问题
                    List<HollowGlassQueueInfo> tempList = new ArrayList<>();
                    //先将玻璃按照正常顺序排列,计算一车最多放几块,算好块数之后将一车的玻璃按照倒序存储
                    List<HollowBigStorageCageDetails> reverse = CollectionUtil.reverse(entry.getValue());
                    int remainWidth = carWidth;
                    for (HollowBigStorageCageDetails item : reverse) {
                        remainWidth = remainWidth - (int) Math.max(item.getWidth(), item.getHeight());
                        if (remainWidth < 0) {
                            hollowQueues.addAll(CollectionUtil.reverse(tempList));
                            tempList = new ArrayList<>();
                            remainWidth = carWidth - (int) Math.max(item.getWidth(), item.getHeight());
                        }
                        HollowGlassQueueInfo queueInfo = HollowBDetailToQueue(item, info.getId(), request.getCell());
                        tempList.add(queueInfo);
                        remainWidth = remainWidth - glassGap;
                    }
                    hollowQueues.addAll(CollectionUtil.reverse(tempList));
                    HollowBigStorageCageDetails cageDetails = entry.getValue().get(0);
                    if (cageDetails.getIsPair() == 1) {
                        isPairCount = isPairCount - cageDetails.getTotalLayer();
                        if (isPairCount == 0) {
                            break;
                        }
                    }
                }
            } else {
                loop:
                for (HollowBigStorageCageDetails item : hollowBigStorageCageDetailsList) {
                    HollowGlassQueueInfo queueInfo = HollowBDetailToQueue(item, info.getId(), request.getCell());
                    hollowQueues.add(queueInfo);
                    if (item.getIsPair() == 1) {
                        isPairCount = isPairCount - 1;
                        if (isPairCount == 0) {
                            break loop;
                        }
                    }
                }
            }
            hollowGlassQueueInfoService.saveBatch(hollowQueues);
            try {
                log.info("李赛克生成文件:{}", request);
                if (request.getCell() == 930) {
                    generateHollowLisecFile(request.getFlowCardId(), 930, isForce, 0);
                }
            } catch (Exception e) {
                log.info("生成李赛克文件时发生异常,流程卡号为{}", request.getFlowCardId());
            }
            return info;
        } catch (Exception e) {
            log.info("生成李赛克文件时发生异常,流程卡号为{}", request.getFlowCardId());
            log.error("创建任务时发生异常:{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return info;
    }
    private HollowGlassQueueInfo HollowBDetailToQueue(HollowBigStorageCageDetails details, Long taskId, int targetCell) {
@@ -401,5 +471,10 @@
        queueInfo.setUpdateTime(new Date());
        return queueInfo;
    }
    @Override
    public List<PieChartVO> queryPieChart() {
        return baseMapper.queryPieChart();
    }
}