Junjie
2025-11-19 40d95599c1ac6ef89d465e031132d9c627a7f0bc
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -5,15 +5,12 @@
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.BasCrnErrorMapper;
import com.zy.asrs.mapper.WaitPakinMapper;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.constant.RedisConstantType;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.MatDto;
import com.zy.common.model.SearchLocParam;
@@ -29,28 +26,23 @@
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.*;
import com.zy.core.model.command.CrnCommand;
import com.zy.core.model.command.LedCommand;
import com.zy.core.model.command.RgvCommand;
import com.zy.core.model.command.LiftCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.RgvProtocol;
import com.zy.core.model.protocol.LiftProtocol;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.BarcodeThread;
import com.zy.core.thread.LedThread;
import com.zy.core.thread.RgvThread;
import com.zy.core.thread.SiemensDevpThread;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 立体仓库WCS系统主流程业务
@@ -74,8 +66,6 @@
    private WrkDetlService wrkDetlService;
    @Autowired
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private WaitPakinMapper waitPakinMapper;
    @Autowired
    private LocMastService locMastService;
    @Autowired
@@ -136,17 +126,13 @@
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk()
                        && staProtocol.getWorkNo() == 9999
                        && staProtocol.isPakMk() && (staProtocol.getEmptyInType() != 1 && staProtocol.getEmptyInType() != 2)) {
                        && staProtocol.isPakMk()) {
                    News.warnNoLog("" + mark + " - 0" + " - 开始执行");
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        News.info("" + mark + " - 1" + " - {}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                        if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
//                            staProtocol.setWorkNo((short) 9992);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            News.info("" + mark + " - 2" + " - 扫码失败1 ===>> {}号条码扫描器检测条码信息:{},站点:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                            // led 异常显示
                            if (ledThread != null) {
@@ -156,11 +142,6 @@
                            continue;
                        }
                    } else {
//                        staProtocol.setWorkNo((short) 9992);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                        News.info("" + mark + " - 3" + " - 扫码失败2 ===>> {}号条码扫描器检测条码信息:{},站点:{}", inSta.getBarcode(), barcode, inSta.getStaNo());
                        // led 异常显示
                        if (ledThread != null) {
@@ -181,13 +162,6 @@
                    }
                    if (wrkMast != null) {
                        News.error("" + mark + " - 4" + " - 工作档中已存在该站状态为( 2.设备上走 )的数据,工作号={}", wrkMast.getWrkNo());
//                        staProtocol.setWorkNo((short)9992);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                        if (!result) {
//                            throw new CoolException("更新plc站点信息失败");
//                        }
                        // led 异常显示
                        if (ledThread != null) {
@@ -197,16 +171,21 @@
                        continue;
                    }
                    if (inSta.getStaNo() == 1002 || inSta.getStaNo() == 1007) {
                        //检测是否有出库任务
                        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                                .in("io_type", 101, 103, 107)
                                .in("sta_no", 1003, 1004)
                        );
                        if (!wrkMasts.isEmpty()) {
                            News.error("" + mark + " - 4" + " - 入库,检测存在出库任务,等待出库任务执行完成,托盘码={}", barcode);
                            continue;
                        }
                    LiftProtocol liftProtocol = devpThread.getLiftStatus();
                    if(liftProtocol == null) {
                        continue;
                    }
                    if (liftProtocol.getMode() != 1) {
                        continue;
                    }
                    if (liftProtocol.getDeviceStatus() != 0) {
                        continue;
                    }
                    if (liftProtocol.getWorkNo() > 0) {
                        continue;
                    }
                    try {
@@ -227,54 +206,29 @@
                        if (jsonObject.getInteger("code").equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                                    .eq("loc_no", dto.getLocNo()));
                            short staNo = dto.getStaNo().shortValue();
                            int sourceStaNo = dto.getSourceStaNo();
                            if (sourceStaNo == 1007) {
                                staNo = 2002;
                            WrkMast currentWrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", dto.getWorkNo()));
                            if (currentWrkMast == null) {
                                throw new CoolException("WMS生成任务后,WCS查询为空");
                            }
                            if (inSta.getStaNo() == 1002) {
                                staNo = 2002;
                            }
                            Integer staNo = Utils.getStaNoByLocNo(dto.getLocNo());
                            LiftCommand command = new LiftCommand();
                            command.setWorkNo(dto.getWorkNo());
                            command.setSourceStaNo(inSta.getStaNo());
                            command.setTargetStaNo(staNo);
                            barcodeThread.setBarcode("");
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(staNo);
                            staProtocol.setPalletSize(locMast.getLocType2());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, command));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
//                            // led 入库信息显示
//                            if (ledThread != null) {
//                                // 命令集合
//                                List<LedCommand> commands = new ArrayList<>();
//                                // 组装命令
//                                LedCommand ledCommand = new LedCommand();
//                                ledCommand.setWorkNo(dto.getWorkNo());
//                                ledCommand.setIoType(1);
//                                ledCommand.setTitle("全板入库");
//                                ledCommand.setLocNo(dto.getLocNo());
//                                ledCommand.setStaNo(dto.getStaNo());
//                                commands.add(ledCommand);
//                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, commands));
////                                ledThread.errorReset();
//                            }
                            currentWrkMast.setWrkSts(3L);// 3.设备执行中
                            currentWrkMast.setCrnStrTime(new Date());
                            wrkMastService.updateById(currentWrkMast);
                        } else {
                            News.error("" + mark + " - 5" + " - 请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
//                            staProtocol.setWorkNo((short)9992);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
                            // led 异常显示
                            if (ledThread != null) {
@@ -358,11 +312,6 @@
                                .eq("crn_no", wrkMast.getCrnNo()); // 堆垛机号
                        StaDesc staDesc = staDescService.selectOne(wrapper);
                        if (Cools.isEmpty(staDesc)) {
//                        News.error(""+mark+" - 2"+" - 入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
//                        staProtocol.setWorkNo((short) 9989);
//                        staProtocol.setStaNo((short) (pickSta.getStaNo().shortValue()-(short)1));
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            //LED
                            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
                            // led 异常显示
@@ -370,6 +319,23 @@
                                String errorMsg = "此为拣料、并板、盘点再入库.请放在" + pickSta.getBackSta().shortValue() + "站点";
                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(5, errorMsg));
                            }
                            continue;
                        }
                        LiftProtocol liftProtocol = devpThread.getLiftStatus();
                        if(liftProtocol == null) {
                            continue;
                        }
                        if (liftProtocol.getMode() != 1) {
                            continue;
                        }
                        if (liftProtocol.getDeviceStatus() != 0) {
                            continue;
                        }
                        if (liftProtocol.getWorkNo() > 0) {
                            continue;
                        }
@@ -405,16 +371,20 @@
                                    continue;
                                }
                                LocMast locMast = locMastService.selectById(newWrkMast.getLocNo());
                                // 更新站点信息 且 下发plc命令
                                staProtocol.setWorkNo(newWrkMast.getWrkNo().shortValue());
                                staProtocol.setStaNo(newWrkMast.getStaNo().shortValue());
                                staProtocol.setPalletSize(locMast.getLocType2());
                                devpThread.setPakMk(staProtocol.getSiteId(), false);
                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                                Integer staNo = Utils.getStaNoByLocNo(newWrkMast.getLocNo());
                                LiftCommand command = new LiftCommand();
                                command.setWorkNo(newWrkMast.getWrkNo());
                                command.setSourceStaNo(pickSta.getStaNo());
                                command.setTargetStaNo(staNo);
                                boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, command));
                                if (!result) {
                                    News.error("" + mark + " - 3" + " - 发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                                    throw new CoolException("更新plc站点信息失败");
                                }
                                newWrkMast.setWrkSts(3L);// 3.设备执行中
                                newWrkMast.setCrnStrTime(new Date());
                                wrkMastService.updateById(newWrkMast);
                            } else {
                                News.error("" + mark + " - 5" + " - 请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/pick/loc/v1", JSON.toJSONString(param), response);
                            }
@@ -436,6 +406,128 @@
    }
    /**
     * 执行出库任务
     */
    public synchronized void executeOutTask() {
        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, 1);
        if(devpThread == null) {
            return;
        }
        LiftProtocol liftProtocol = devpThread.getLiftStatus();
        if(liftProtocol == null) {
            return;
        }
        if (liftProtocol.getMode() != 1) {
            return;
        }
        if (liftProtocol.getDeviceStatus() != 0) {
            return;
        }
        if (liftProtocol.getWorkNo() > 0) {
            return;
        }
        //检测出库站是否可出
        StaProtocol staProtocol = devpThread.getStation().get(100);
        if (staProtocol == null) {
            return;
        }
        if (!staProtocol.isAutoing()) {
            return;
        }
        if (staProtocol.isLoading()) {
            return;
        }
        if (!staProtocol.isOutEnable()) {
            return;
        }
        //检查是否有正在执行的任务
        List<WrkMast> workingWrkMast = wrkMastService.selectList(new EntityWrapper<WrkMast>().in("wrk_sts", 3, 12));
        if (!workingWrkMast.isEmpty()) {
            return;
        }
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 11).orderBy("create_time"));
        if (wrkMasts.isEmpty()) {
            return;
        }
        WrkMast wrkMast = wrkMasts.get(0);
        Integer sourceStaNo = Utils.getStaNoByLocNo(wrkMast.getSourceLocNo());
        LiftCommand command = new LiftCommand();
        command.setWorkNo(wrkMast.getWrkNo());
        command.setSourceStaNo(sourceStaNo);
        command.setTargetStaNo(100);
        boolean result = MessageQueue.offer(SlaveType.Devp, 1, new Task(2, command));
        if (!result) {
            throw new CoolException("更新plc站点信息失败");
        }
        wrkMast.setWrkSts(12L);// 12.设备执行中
        wrkMast.setCrnStrTime(new Date());
        wrkMastService.updateById(wrkMast);
    }
    /**
     * 检测任务完成
     */
    public synchronized void deviceFinished() {
        // 根据plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
            if (devpThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = devpThread.getLiftStatus();
            if (liftProtocol == null) {
                continue;
            }
            if (liftProtocol.getWorkNo() <= 0) {
                continue;
            }
            if (liftProtocol.getMode() != 1) {
                continue;
            }
            if (liftProtocol.getDeviceStatus() != 0) {
                continue;
            }
            WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", liftProtocol.getWorkNo()));
            if (wrkMast == null) {
                continue;
            }
            if (wrkMast.getWrkSts() == 3) {
                wrkMast.setWrkSts(4L);
                wrkMast.setCrnEndTime(new Date());
                wrkMast.setIoTime(new Date());
                wrkMastService.updateById(wrkMast);
            }else if (wrkMast.getWrkSts() == 12) {
                wrkMast.setWrkSts(14L);
                wrkMast.setCrnEndTime(new Date());
                wrkMast.setIoTime(new Date());
                wrkMastService.updateById(wrkMast);
            }else {
                News.error("未知的工作状态");
            }
        }
    }
    /**
     * 堆垛机异常信息记录
     */
    public synchronized void recCrnErr(Integer mark) {