zyx
2024-01-31 ca8f5a8f133e7f5b00b9d35ff6d15720804569af
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -4,21 +4,14 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.asrs.domain.enums.TaskStatusType;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.TaskOverToWms;
import com.zy.asrs.entity.param.WMSAndAGVInterfaceParam;
import com.zy.asrs.entity.param.taskCreateParam;
import com.zy.asrs.mapper.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.CommandUtils;
import com.zy.asrs.utils.PostMesDataUtils;
import com.zy.asrs.utils.Utils;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import com.zy.core.CrnThread;
import com.zy.core.DevpThread;
@@ -34,16 +27,15 @@
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.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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.io.IOException;
import java.util.*;
@@ -94,25 +86,30 @@
    @Value("${wms.url}")
    private String wmsUrl;
    @Value("${wms.inboundTaskApplyPath}")
    private String inboundTaskApplyPath;
    @Value("${wms.movePath}")
    private String movePath;
    public void generateStoreWrkFile() throws IOException, InterruptedException {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                Short stano = staProtocol.getStaNo();
        try {
            // 根据输送线plc遍历
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历入库口
                for (DevpSlave.Sta inSta : devp.getInSta()) {
                    // 获取入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    Short workNo = staProtocol.getWorkNo();
                    Short stano = staProtocol.getStaNo();
@@ -155,12 +152,14 @@
                        if (workNo == 0 && stano == 0){
                            continue;
                        }
                        if (!staProtocol.isPakMk()){
                            continue;
                        }
//                        News.warn("扫码入库失败,{}入库站因{}异常,托盘已被退回", inSta.getStaNo(), errMsg);
                        staProtocol.setWorkNo(workNo);
                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
                        devpThread.setPakMk(staProtocol.getSiteId(), false);
                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, staProtocol));
                        TaskWrk taskWrk = taskWrkMapper.selectByWrkNo(Integer.valueOf(workNo));
                        if (taskWrk != null) {
                            taskWrk.setMemo(errMsg);//将错误码存入备注字段进行展示
@@ -168,93 +167,142 @@
                        }
                        continue;
                    }
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo >= 0)
                        && staProtocol.isPakMk()) {
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if(!Cools.isEmpty(barcode)) {
                        StaDescService staDescService = SpringUtils.getBean(StaDescService.class);
                        ToWmsDTO toWmsDTO = new ToWmsDTO();
                        Map<String, Object> map = new HashMap<>();
                        map.put("x-api-key","7a15b5db-29b6-552c-8cff-0cfec3756da2");
                        List<Integer> list = openServiceImpl.getInEnableRoadway();
                        TaskWrk taskWrk = taskWrkService.selectOne(new EntityWrapper<TaskWrk>().eq("barcode", barcode));
                        if (Cools.isEmpty(taskWrk)){
                    // 判断是否满足入库条件
                    if (staProtocol.isAutoing() && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && !staProtocol.isEmptyMk() && (workNo >= 0)
                            && staProtocol.isPakMk()) {
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                        if (barcodeThread == null) {
                            continue;
                        }
                        //StaDesc staDesc1 = staDescService.selectOne(new EntityWrapper<StaDesc>().eq("stn_no",inSta.getBackSta()));
                        if (taskWrk.getTargetPoint() != null && taskWrk.getStartPoint() != null && taskWrk.getWrkNo() != null && taskWrk.getWrkNo().shortValue() != workNo){
                            //获取堆垛机站点
                            StaDesc staDesc = new StaDesc();
                            if (devp.getId() <= 1){
                                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                                        .eq("crn_no", taskWrk.getCrnNo()).eq("type_no",1).lt("stn_no", 200));
                            }else {
                                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                                        .eq("crn_no", taskWrk.getCrnNo()).eq("type_no",1).lt("stn_no", 400).ge("stn_no", 250));
                            }
                            staProtocol.setWorkNo(taskWrk.getWrkNo().shortValue());
                            staProtocol.setStaNo(staDesc.getCrnStn().shortValue());
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, staProtocol));
                            Thread.sleep(200);
                        }else {
                            //TaskWrk taskWrk = taskWrkService.selectByTaskNo(param.getTaskNo());
                            toWmsDTO.setWarehouseId("1688469798893297665");
                            toWmsDTO.setContainerCode(barcode);
                            toWmsDTO.setApplyType("TUNNEL_LOCATION");
                            toWmsDTO.setTaskTunnel(taskWrk.getCrnNo());
                            toWmsDTO.setCanInboundTunnels(list);
                            String response = null;
                            try {
                                response = new HttpHandler.Builder()
                                        .setHeaders(map)
                                        .setUri(wmsUrl)
                                        .setPath("wcsManager/wcsInterface/inboundTaskApply")
                                        .setJson(JSON.toJSONString(toWmsDTO))
                                        .build()
                                        .doPost();
                            }catch (Exception e){
                                log.error("wms通讯失败,"+e.getMessage());
                                continue;
                            }
                        String barcode = barcodeThread.getBarcode();
                        if(!Cools.isEmpty(barcode)) {
                            // 请求wms接口,获取工作号和目标库位
                            ToWmsDTO toWmsDTO = new ToWmsDTO(barcode,staProtocol.getSiteId());
                            String response;
                            response = new HttpHandler.Builder()
                                    .setHeaders(null)
                                    .setUri(wmsUrl)
                                    .setPath(inboundTaskApplyPath)
                                    .setJson(JSON.toJSONString(toWmsDTO))
                                    .build()
                                    .doPost();
                            JSONObject jsonObject = JSON.parseObject(response);
                            apiLogService.save("wms请求入库货位接口"
                                    ,wmsUrl+"wcsManager/wcsInterface/inboundTaskApply"
                                    ,wmsUrl+inboundTaskApplyPath
                                    ,null
                                    ,"127.0.0.1"
                                    ,JSON.toJSONString(toWmsDTO)
                                    ,response
                                    ,true
                            );
                            JSONObject jsonObject = JSON.parseObject(response);
                            if (jsonObject.getInteger("code").equals(200)) {
                            if (jsonObject.getInteger("code").equals(200) && !Cools.isEmpty(jsonObject.get("data").toString())) {
                                GetWmsDto getWmsDto = JSON.parseObject(jsonObject.get("data").toString(), GetWmsDto.class);
                                taskWrk.setOriginTargetPoint(getWmsDto.getTargetLocationCode());
                                taskWrk.setTargetPoint(Utils.getWcsLocNo(getWmsDto.getTargetLocationCode()));
                                taskWrk.setStartPoint(String.valueOf(inSta.getBackSta()));
                                if (!taskWrkService.updateById(taskWrk)){
                                    log.error("保存wms库位号失败");
                                // 创新一个入库工作档
                                TaskWrk taskWrk = taskWrkService.selectByTaskNo(getWmsDto.getTaskNo());
                                if(Cools.isEmpty(taskWrk)) {
                                    taskWrk = createTask(getWmsDto,staProtocol.getStaNo().intValue());
                                    if (Cools.isEmpty(taskWrk)){
                                        log.error("库位异常,库位号:{}", getWmsDto.getTargetLocationCode());
                                    }else {
                                        taskWrkService.insert(taskWrk);
                                        StaDesc staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                                                .eq("crn_no", taskWrk.getCrnNo()).eq("type_no",1).eq("stn_no",staProtocol.getStaNo().intValue()));
                                        staProtocol.setWorkNo(taskWrk.getWrkNo().shortValue());
                                        staProtocol.setStaNo(staDesc.getCrnStn().shortValue());
                                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                                    }
                                }
                            }else {
                                log.error("wms通讯失败,"+jsonObject.get("msg"));
                                continue;
                            }
//                        StaDescService staDescService = SpringUtils.getBean(StaDescService.class);
//
//                        ToWmsDTO toWmsDTO = new ToWmsDTO();
//                        Map<String, Object> map = new HashMap<>();
//                        map.put("x-api-key","7a15b5db-29b6-552c-8cff-0cfec3756da2");
//                        List<Integer> list = openServiceImpl.getInEnableRoadway();
//                        TaskWrk taskWrk = taskWrkService.selectOne(new EntityWrapper<TaskWrk>().eq("barcode", barcode));
//
//                        if (Cools.isEmpty(taskWrk)){
//                            continue;
//                        }
//                        //StaDesc staDesc1 = staDescService.selectOne(new EntityWrapper<StaDesc>().eq("stn_no",inSta.getBackSta()));
//                        if (taskWrk.getTargetPoint() != null && taskWrk.getStartPoint() != null){
//                            if ( taskWrk.getWrkNo() != null && taskWrk.getWrkNo().shortValue() == workNo ){
//                                continue;
//                            }
//                            //获取堆垛机站点
//                            StaDesc staDesc = new StaDesc();
//                            if (devp.getId() <= 1){
//                                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
//                                        .eq("crn_no", taskWrk.getCrnNo()).eq("type_no",1).lt("stn_no", 200));
//                            }else {
//                                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
//                                        .eq("crn_no", taskWrk.getCrnNo()).eq("type_no",1).lt("stn_no", 400).ge("stn_no", 250));
//                            }
//                            staProtocol.setWorkNo(taskWrk.getWrkNo().shortValue());
//                            staProtocol.setStaNo(staDesc.getCrnStn().shortValue());
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, staProtocol));
//                            Thread.sleep(500);
//                        }else {
//                            //TaskWrk taskWrk = taskWrkService.selectByTaskNo(param.getTaskNo());
//                            toWmsDTO.setWarehouseId("1688469798893297665");
//                            toWmsDTO.setContainerCode(barcode);
//                            toWmsDTO.setApplyType("TUNNEL_LOCATION");
//                            toWmsDTO.setTaskTunnel(taskWrk.getCrnNo());
//                            toWmsDTO.setCanInboundTunnels(list);
//                            String response = null;
//                            try {
//                                response = new HttpHandler.Builder()
//                                        .setHeaders(map)
//                                        .setUri(wmsUrl)
//                                        .setPath(inboundTaskApplyPath)
//                                        .setJson(JSON.toJSONString(toWmsDTO))
//                                        .build()
//                                        .doPost();
//                            }catch (Exception e){
//                                log.error("wms通讯失败,"+e.getMessage());
//                                continue;
//                            }
//                            apiLogService.save("wms请求入库货位接口"
//                                    ,wmsUrl+"wcsManager/wcsInterface/inboundTaskApply"
//                                    ,null
//                                    ,"127.0.0.1"
//                                    ,JSON.toJSONString(toWmsDTO)
//                                    ,response
//                                    ,true
//                            );
//                            JSONObject jsonObject = JSON.parseObject(response);
//                            if (jsonObject.getInteger("code").equals(200)) {
//                                GetWmsDto getWmsDto = JSON.parseObject(jsonObject.get("data").toString(), GetWmsDto.class);
//                                taskWrk.setOriginTargetPoint(getWmsDto.getTargetLocationCode());
//                                taskWrk.setTargetPoint(Utils.getWcsLocNo(getWmsDto.getTargetLocationCode()));
//                                taskWrk.setStartPoint(String.valueOf(inSta.getBackSta()));
//                                if (!taskWrkService.updateById(taskWrk)){
//                                    log.error("保存wms库位号失败");
//                                }
//
//                            }else {
//                                log.error("wms通讯失败,"+jsonObject.get("msg"));
//
//                            }
//                        }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("generateStoreWrkFile e", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
@@ -262,7 +310,6 @@
    /**
     * 堆垛机站出库到出库站
     */
    @Async
    public void crnStnToOutStn() {
        for (CrnSlave crnSlave : slaveProperties.getCrn()) {
            // 遍历堆垛机出库站
@@ -418,51 +465,17 @@
                    continue;
                }
                //取出命令
                List<CommandInfo> commandInfos = commandInfoService.selectByTaskNo(taskWrk.getTaskNo());
                Integer commandStep = taskWrk.getCommandStep();
                if (commandInfos.isEmpty()) {
                    continue;//命令空
                }
                CommandInfo commandInfo = commandInfos.get(commandStep);
                CommandPackage commandPackage = JSON.parseObject(commandInfo.getCommand(), CommandPackage.class);//取出命令报文
                CrnCommand crnCommand = JSON.parseObject(commandPackage.getCommand().toString(), CrnCommand.class);
                if (!MessageQueue.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(2, crnCommand))) {
                    log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
                } else {
                    // 修改工作档状态 2.设备上走 => 3.吊车入库中
                    Date now = new Date();
                    taskWrk.setWrkSts(3);
                    taskWrk.setModiTime(now);
                    if (taskWrkMapper.updateById(taskWrk) == 0) {
                        log.error("修改工作档状态 2.设备上走 => 3.吊车入库中 失败!!,工作号={}", taskWrk.getWrkNo());
                    }
                    //开始运行
                    String response = CrnStartRunning(taskWrk);
                    JSONObject jsonObject = JSON.parseObject(response);
                    if (jsonObject.getInteger("code").equals(200)) {
                    }else {
                        log.error("入库开始运行通讯失败,"+jsonObject.get("msg"));
                    }
                }
//                // 命令下发区 --------------------------------------------------------------------------
//                CrnCommand crnCommand = new CrnCommand();
//                crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
//                crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
//                crnCommand.setAckFinish((short) 0);  // 任务完成确认位
//                crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
//                crnCommand.setSourcePosX(crnStn.getBay().shortValue());     // 源库位列
//                crnCommand.setSourcePosY(crnStn.getLev().shortValue());     // 源库位层
//                crnCommand.setSourcePosZ(crnStn.getRow().shortValue());     // 源库位排
//                crnCommand.setDestinationPosX(Utils.getBayShort(taskWrk.getTargetPoint()));     // 目标库位列
//                crnCommand.setDestinationPosY(Utils.getLevShort(taskWrk.getTargetPoint()));     // 目标库位层
//                crnCommand.setDestinationPosZ(Utils.getRowShort(taskWrk.getTargetPoint()));     // 目标库位排
//                crnCommand.setCommand((short)1);
//                if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(2, crnCommand))) {
//                //取出命令
//                List<CommandInfo> commandInfos = commandInfoService.selectByTaskNo(taskWrk.getTaskNo());
//                Integer commandStep = taskWrk.getCommandStep();
//                if (commandInfos.isEmpty()) {
//                    continue;//命令空
//                }
//                CommandInfo commandInfo = commandInfos.get(commandStep);
//                CommandPackage commandPackage = JSON.parseObject(commandInfo.getCommand(), CommandPackage.class);//取出命令报文
//                CrnCommand crnCommand = JSON.parseObject(commandPackage.getCommand().toString(), CrnCommand.class);
//
//                if (!MessageQueue.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(2, crnCommand))) {
//                    log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
//                } else {
//                    // 修改工作档状态 2.设备上走 => 3.吊车入库中
@@ -472,10 +485,44 @@
//                    if (taskWrkMapper.updateById(taskWrk) == 0) {
//                        log.error("修改工作档状态 2.设备上走 => 3.吊车入库中 失败!!,工作号={}", taskWrk.getWrkNo());
//                    }
//                    //开始运行
//                    String response = CrnStartRunning(taskWrk);
//                    JSONObject jsonObject = JSON.parseObject(response);
//                    if (jsonObject.getInteger("code").equals(200)) {
//
//                    }else {
//                        log.error("入库开始运行通讯失败,"+jsonObject.get("msg"));
//
//                    }
//                }
                // 命令下发区 --------------------------------------------------------------------------
                CrnCommand crnCommand = new CrnCommand();
                crnCommand.setCrnNo(slave.getId()); // 堆垛机编号
                crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
                crnCommand.setAckFinish((short) 0);  // 任务完成确认位
                crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
                crnCommand.setSourcePosX(crnStn.getBay().shortValue());     // 源库位列
                crnCommand.setSourcePosY(crnStn.getLev().shortValue());     // 源库位层
                crnCommand.setSourcePosZ(crnStn.getRow().shortValue());     // 源库位排
                crnCommand.setDestinationPosX(Utils.getBayShort(taskWrk.getTargetPoint()));     // 目标库位列
                crnCommand.setDestinationPosY(Utils.getLevShort(taskWrk.getTargetPoint()));     // 目标库位层
                crnCommand.setDestinationPosZ(Utils.getRowShort(taskWrk.getTargetPoint()));     // 目标库位排
                crnCommand.setCommand((short)1);
                if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(2, crnCommand))) {
                    log.error("堆垛机命令下发失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
                } else {
                    // 修改工作档状态 2.设备上走 => 3.吊车入库中
                    Date now = new Date();
                    taskWrk.setWrkSts(3);
                    taskWrk.setModiTime(now);
                    if (taskWrkMapper.updateById(taskWrk) == 0) {
                        log.error("修改工作档状态 2.设备上走 => 3.吊车入库中 失败!!,工作号={}", taskWrk.getWrkNo());
                    }
                }
            }
           // return;
            // return;
        }
    }
@@ -574,6 +621,13 @@
                        if (commandInfos.isEmpty()) {
                            continue;//命令空
                        }
                        //判断末端命令是否执行
                        CommandInfo commandInfo2 = commandInfos.get(commandInfos.size() - 1);
                        if (commandInfo2.getCommandStatus() != CommandStatusType.CREATE.id) {
                            continue;//指令已执行
                        }
                        CommandInfo commandInfo = commandInfos.get(commandStep);
                        CommandPackage commandPackage = JSON.parseObject(commandInfo.getCommand(), CommandPackage.class);//取出命令报文
                        CrnCommand crnCommand = JSON.parseObject(commandPackage.getCommand().toString(), CrnCommand.class);
@@ -616,6 +670,24 @@
                    .eq("wrk_sts",11)
                    .eq("io_type",3));
            for (TaskWrk taskWrk : taskWrks){
                // 双深库位且浅库位有货,则需先对浅库位进行库位移转
                if (Utils.isDeepLoc(slaveProperties, taskWrk.getStartPoint())) {
                    String shallowLocNo = Utils.getShallowLoc(slaveProperties, taskWrk.getStartPoint());
                    TaskWrk hallowLocNoTask = taskWrkMapper.selectByStartPoint(shallowLocNo);
                    if (!Cools.isEmpty(hallowLocNoTask)){
                        continue;
                    }
                }
                // 堆垛机控制过滤
                if (!crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() != 0) {
                    continue;
                }
                // 已经存在吊车执行任务时,则过滤
                if (taskWrkMapper.selectCrnWorking(slave.getId()) != null) {
                    continue;
                }
                if (Cools.isEmpty(taskWrk.getTargetPoint())){
                    List<Integer> list = openServiceImpl.getInEnableRoadway();
                    Map<String, Object> map = new HashMap<>();
@@ -653,7 +725,7 @@
                        continue;
                    }
                 }
                }
                if (taskWrk == null || Cools.isEmpty(taskWrk.getTargetPoint()) || taskWrk.getTargetPoint().equals("") || taskWrk.getWrkNo() == null) {
                    continue;
                }
@@ -665,15 +737,7 @@
                // 命令下发区 --------------------------------------------------------------------------
                // 堆垛机控制过滤
                if (!crnProtocol.getStatusType().equals(CrnStatusType.IDLE) || crnProtocol.getTaskNo() != 0) {
                    continue;
                }
                // 已经存在吊车执行任务时,则过滤
                if (taskWrkMapper.selectCrnWorking(slave.getId()) != null) {
                    continue;
                }
                // 1.堆垛机开始移动
@@ -719,7 +783,54 @@
    /**
     * 执行对工作档的完成操作
     */
    @Async
//    public void storeFinished() {
//        for (CrnSlave crn : slaveProperties.getCrn()) {
//            // 获取堆垛机信息
//            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
//            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
//            if (crnProtocol == null) { continue; }
//
//            //  状态:等待确认 并且  任务完成位 = 1
//            if (crnProtocol.getTaskFinish() == 0 && crnProtocol.statusType == CrnStatusType.HANDLING_COMPLETED && crnProtocol.getTaskNo() != 0) {
//                //获取入库待确认工作档
//                TaskWrk taskWrk = taskWrkMapper.selectCrnNoInWorking(crn.getId(),crnProtocol.getTaskNo().intValue());
//                if (Cools.isEmpty(taskWrk)) {
////                    log.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
//                    continue;
//                }
//
//                //获取指令ID
//                Integer commandId = crnProtocol.getCommandId();
//                CommandInfo commandInfo = new CommandInfo();
//                if (Cools.isEmpty(commandId)){
//                    commandInfo = commandInfoService.selectOne(new EntityWrapper<CommandInfo>()
//                            .eq("wrk_no",crnProtocol.getTaskNo())
//                            .eq("device","Crn"));
//                }else {
//                    commandInfo = commandInfoService.selectById(commandId);
//                }
//
//
//                if (commandInfo == null) {
//                    //指令不存在
//                    continue;
//                }
//                if (commandInfo.getCommandStatus() == 3){
//                    continue;
//                }
//                commandInfo.setCommandStatus(CommandStatusType.COMPLETE.id);//指令完成
//                commandInfo.setCompleteTime(new Date());//指令完成时间
//                if (commandInfoService.updateById(commandInfo)) {//修改成功后复位堆垛机
//                    // 堆垛机复位
//                    crnThread.setResetFlag(true);
//                }
//            }
//        }
//    }
    /**
     * 执行对工作档的完成操作
     */
    public void storeFinished() {
        for (CrnSlave crn : slaveProperties.getCrn()) {
            // 获取堆垛机信息
@@ -728,81 +839,41 @@
            if (crnProtocol == null) { continue; }
            //  状态:等待确认 并且  任务完成位 = 1
            if (crnProtocol.getTaskFinish() == 0 && crnProtocol.statusType == CrnStatusType.HANDLING_COMPLETED && crnProtocol.getTaskNo() != 0) {
                //获取指令ID
                Integer commandId = crnProtocol.getCommandId();
                CommandInfo commandInfo = new CommandInfo();
                if (Cools.isEmpty(commandId)){
                    commandInfo = commandInfoService.selectOne(new EntityWrapper<CommandInfo>()
                            .eq("wrk_no",crnProtocol.getTaskNo())
                            .eq("device","Crn"));
                }else {
                    commandInfo = commandInfoService.selectById(commandId);
                }
                if (commandInfo == null) {
                    //指令不存在
                // 获取入库待确认工作档
                TaskWrk taskWrk = taskWrkMapper.selectCrnNoInWorking(crn.getId(),crnProtocol.getTaskNo().intValue());
                if (Cools.isEmpty(taskWrk)) {
                    log.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                    continue;
                }
                commandInfo.setCommandStatus(CommandStatusType.COMPLETE.id);//指令完成
                commandInfo.setCompleteTime(new Date());//指令完成时间
                if (commandInfoService.updateById(commandInfo)) {//修改成功后复位堆垛机
                // 入库 + 库位转移  ==> 4.入库完成
                if ((taskWrk.getWrkSts() == 3 && taskWrk.getIoType() == 1) || (taskWrk.getWrkSts() == 12 && taskWrk.getIoType() == 3)) {
                    taskWrk.setWrkSts(4);
                } else {
                    continue;
                }
                Date now = new Date();
                taskWrk.setModiTime(now);
                Integer integerTaskWrk = taskWrkMapper.updateById(taskWrk);
                // 修改成功后复位堆垛机
                if (integerTaskWrk > 0) {
                    // 堆垛机复位
                    crnThread.setResetFlag(true);
                    //更新库位状态
                    LocMast locMast = locMastService.selectByLocNo(taskWrk.getTargetPoint());
                    locMast.setLocSts("F");//F.在库
                    locMast.setBarcode(taskWrk.getBarcode());//托盘码
                    locMast.setModiTime(now);
                    locMast.setModiUser(9999L);
                    locMastService.updateById(locMast);
                }
            }
        }
    }
//    /**
//     * 执行对工作档的完成操作
//     */
//    @Async
//    public void storeFinished() {
//        for (CrnSlave crn : slaveProperties.getCrn()) {
//            // 获取堆垛机信息
//            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crn.getId());
//            CrnProtocol crnProtocol = crnThread.getCrnProtocol();
//            if (crnProtocol == null) { continue; }
//            //  状态:等待确认 并且  任务完成位 = 1
//            if (crnProtocol.getTaskFinish() == 0 && crnProtocol.statusType == CrnStatusType.HANDLING_COMPLETED && crnProtocol.getTaskNo() != 0) {
//                // 获取入库待确认工作档
//                TaskWrk taskWrk = taskWrkMapper.selectCrnNoWorking(crn.getId(),crnProtocol.getTaskNo().intValue());
//                if (Cools.isEmpty(taskWrk)) {
//                    log.error("堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
//                    continue;
//                }
//                // 入库 + 库位转移  ==> 4.入库完成
//                if ((taskWrk.getWrkSts() == 3 && taskWrk.getIoType() == 1) || (taskWrk.getWrkSts() == 12 && taskWrk.getIoType() == 3)) {
//                    taskWrk.setWrkSts(4);
//                } else {
//                    continue;
//                }
//                Date now = new Date();
//                taskWrk.setModiTime(now);
//                Integer integerTaskWrk = taskWrkMapper.updateById(taskWrk);
//                // 修改成功后复位堆垛机
//                if (integerTaskWrk > 0) {
//                    // 堆垛机复位
//                    crnThread.setResetFlag(true);
//
//                    //更新库位状态
//                    LocMast locMast = locMastService.selectByLocNo(taskWrk.getTargetPoint());
//                    locMast.setLocSts("F");//F.在库
//                    locMast.setBarcode(taskWrk.getBarcode());//托盘码
//                    locMast.setModiTime(now);
//                    locMast.setModiUser(9999L);
//                    locMastService.updateById(locMast);
//                }
//            }
//        }
//    }
    /**
     * 堆垛机异常信息记录
     */
    @Async
    public void recCrnErr(){
        Date now = new Date();
        for (CrnSlave crn : slaveProperties.getCrn()) {
@@ -812,20 +883,20 @@
            if (crnProtocol == null) {
                continue;
            }
            if (true) {
            if (false) {
//            if (crnProtocol.getModeType() != CrnModeType.STOP) {
                // 有任务
                if (crnProtocol.getTaskNo() != 0) {
                    BasErrLog latest = basErrLogService.findLatestByTaskNo(crn.getId(), crnProtocol.getTaskNo().intValue());
                    // 有异常
                    if (latest == null) {
                        if (crnProtocol.getAlarm1() != null && crnProtocol.getAlarm1() > 0) {
                        if (crnProtocol.getAlarm() != null && crnProtocol.getAlarm() > 0) {
                            WrkMast wrkMast = wrkMastMapper.selectById(crnProtocol.getTaskNo());
                            if (wrkMast == null) {
                                continue;
                            }
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm1());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm1()):crnError.getErrName();
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                            BasErrLog basErrLog = new BasErrLog(
                                    null,    // 编号
                                    wrkMast.getWrkNo(),    // 工作号
@@ -864,15 +935,15 @@
                            }
                        }
                    }
                // 无任务
                    // 无任务
                } else {
                    BasErrLog latest = basErrLogService.findLatest(crn.getId());
                    // 有异常
                    if (crnProtocol.getAlarm1() != null && crnProtocol.getAlarm1() > 0) {
                    if (crnProtocol.getAlarm1() != null && crnProtocol.getAlarm() > 0) {
                        // 记录新异常
                        if (latest == null || (latest.getErrCode() != crnProtocol.getAlarm1().intValue())) {
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm1());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm1()):crnError.getErrName();
                        if (latest == null || (latest.getErrCode() != crnProtocol.getAlarm().intValue())) {
                            BasCrnError crnError = basCrnErrorMapper.selectById(crnProtocol.getAlarm());
                            String errName = crnError==null? String.valueOf(crnProtocol.getAlarm()):crnError.getErrName();
                            BasErrLog basErrLog = new BasErrLog(
                                    null,    // 编号
                                    null,    // 工作号
@@ -900,7 +971,7 @@
                                log.error("堆垛机plc异常记录失败 ===>> [id:{}] [error:{}]", crn.getId(), errName);
                            }
                        }
                    // 无异常
                        // 无异常
                    } else {
                        // 异常修复
                        if (latest != null && latest.getStatus() == 1) {
@@ -944,7 +1015,7 @@
        for (TaskWrk taskWrk : taskWrks) {
            //获取命令集合
            List<CommandInfo> commandInfos = commandInfoService.selectByTaskNo(taskWrk.getTaskNo());
            if (taskWrk.getCommandStep() != commandInfos.size()) {
            if (taskWrk.getCommandStep() < commandInfos.size()) {
                continue;//当前步序没有到达最后一条命令
            }
@@ -1053,5 +1124,46 @@
        return response;
    }
    private TaskWrk createTask(GetWmsDto dto, Integer staNo){
        String wcsLocNo = Utils.getWcsLocNo(dto.getTargetLocationCode());
        if(Cools.isEmpty(wcsLocNo)){
            return null;
        }
        Date now = new Date();
        TaskWrk taskWrk = new TaskWrk();
        taskWrk.setTaskNo(dto.getTaskNo());//任务号
        taskWrk.setStatus(TaskStatusType.RECEIVE.id);//任务状态:接收
        taskWrk.setCreateTime(now);
        taskWrk.setIoType(1);//任务类型
        taskWrk.setIoPri(Cools.isEmpty(dto.getTaskPriority()) ? 300 : Integer.parseInt(dto.getTaskPriority()));//优先级
        taskWrk.setBarcode(dto.getContainerCode());//条码
        LocMast locMast = locMastService.selectByLocNo(wcsLocNo);
        taskWrk.setCrnNo(locMast.getCrnNo());
        taskWrk.setTargetPoint(dto.getTargetLocationCode());
        taskWrk.setStartPoint(staNo.toString());
        if(taskWrk.getIoType() == 1){
            taskWrk.setWrkSts(2);
            if (!Cools.isEmpty(taskWrk.getTargetPoint())) {
                taskWrk.setTargetPoint(wcsLocNo);//终点
                taskWrk.setOriginTargetPoint(taskWrk.getTargetPoint());
            }
//        } else if (param.getIoType() == 2) {
//            taskWrk.setWrkSts(11);
//            if (!Cools.isEmpty(param.getStartPoint())) {
//                taskWrk.setStartPoint(Utils.getWcsLocNo(param.getStartPoint()));//起点
//                taskWrk.setOriginStartPoint(param.getStartPoint());
//            }
//            taskWrk.setTargetPoint(param.getTargetPoint());
//        }else if (param.getIoType() == 3){
//            taskWrk.setWrkSts(11);
//            if (!Cools.isEmpty(param.getStartPoint())) {
//                taskWrk.setStartPoint(param.getStartPoint());//起点
//                taskWrk.setOriginStartPoint(param.getStartPoint());
//            }
        }
        return taskWrk;
    }
}