skyouc
2025-06-20 65fd6576b61848c95681356c680c63fc010a0d5e
zy-asrs-wms/src/main/java/com/zy/asrs/wms/apis/wcs/services/Impl/WcsApiServiceImpl.java
@@ -1,8 +1,13 @@
package com.zy.asrs.wms.apis.wcs.services.Impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.zy.asrs.framework.common.R;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.apis.wcs.entity.domain.SystemProperties;
@@ -10,6 +15,7 @@
import com.zy.asrs.wms.apis.wcs.entity.request.*;
import com.zy.asrs.wms.apis.wcs.entity.response.CommonReponse;
import com.zy.asrs.wms.apis.wcs.services.WcsApiService;
import com.zy.asrs.wms.apis.wcs.utils.HttpEssUtils;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.enums.CacheSiteStatusType;
import com.zy.asrs.wms.asrs.entity.enums.OrderPickStatus;
@@ -17,6 +23,8 @@
import com.zy.asrs.wms.asrs.service.*;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@@ -28,11 +36,9 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@@ -62,6 +68,9 @@
    @Autowired
    private PlatformService platformService;
    @Resource
    private SystemProperties systemProperties;
    /**
     * 容器到达接收
@@ -77,35 +86,74 @@
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R containerArrivedNotify(ContainerArrivedParam arrivedParam, String taskType, Long hostId) {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, arrivedParam.getContainerCode()));
        if (Collections.isEmpty(tasks)) {
    public R containerArrivedNotify(ContainerArrivedParam arrivedParam, String taskType, Long hostId){
        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getBarcode, arrivedParam.getContainerCode()));
        if (task == null) {
            return R.error("任务不存在!!");
        }
        log.info("容器到达接收,任务号:{},当前状态:{},上报状态:{}",task.getTaskNo(),task.getTaskSts$(),TaskStsType.WCS_CONTAINER_RECEIVE.desc);
        taskType = task.getTaskType() > 100 ? "outStock":"inStock";
        if (taskType.equals("inStock")) {
            //DONE 根据ESS返回的容器编码修改任务档中的输送线起始位置节点,及任务档容器到达状态
            taskService.update(new LambdaUpdateWrapper<Task>()
                    .set(Task::getTaskSts, TaskStsType.WCS_CONTAINER_RECEIVE.id)
                    //输送线节点编码,不是源库位
                    .set(Task::getOriginLoc, arrivedParam.getSlotCode())
                    .eq(Task::getBarcode, arrivedParam.getContainerCode()));
        } else {
            //容器到达时更新任务状态为:入库完成,定时任务根据状态码更新库存
            //当状态为入库完成时,更新库存信息
//            boolean update = taskService.update(new LambdaUpdateWrapper<Task>()
//                    .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id)
//                    .set(Task::getOriginLoc, arrivedParam.getSlotCode())
//                    .eq(Task::getBarcode, arrivedParam.getContainerCode()));
            //fixme 容器到达状态更改为播种中...
            boolean update = taskService.update(new LambdaUpdateWrapper<Task>()
                    .set(Task::getTaskSts, TaskStsType.WAVE_SEED.id)
                    .set(Task::getOriginLoc, arrivedParam.getSlotCode())
                    .eq(Task::getBarcode, arrivedParam.getContainerCode()));
            if (!update) {
                throw new CoolException("容器到达任务更新失败!!");
            if (task.getTaskSts() == 198) {
                return R.error("播种中!!");
            }
            if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_IN.id ) {
                //DONE 根据ESS返回的容器编码修改任务档中的输送线起始位置节点,及任务档容器到达状态
                taskService.update(new LambdaUpdateWrapper<Task>()
                        .set(Task::getTaskSts, TaskStsType.WCS_CONTAINER_RECEIVE.id)
                        //输送线节点编码,不是源库位
                        .set(Task::getOriginSite, arrivedParam.getSlotCode())
                        .eq(Task::getBarcode, arrivedParam.getContainerCode()));
            } else {
                  log.warn("{}没有下发任务",task.getTaskNo());
                  R.error(task.getTaskNo()+"没有下发任务");
            }
        } else {
            if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_ARRIVED.id
            || task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.id ) {
                // 到达拣选位,控制播种墙亮灯
                    List<CacheSite> cacheSites = cacheSiteService.list(new LambdaQueryWrapper<CacheSite>().eq(CacheSite::getBarcode, task.getBarcode()));
                    if(cacheSites.size() == 0) {
                        log.error("条码:{},未找到播种墙亮灯",task.getBarcode());
                        throw new CoolException("未找到播种墙");
                    }
                    for (CacheSite cacheSite:cacheSites) {
                        // 获取任务明细数量
                        List<TaskDetl> taskDetlByTaskId = taskDetlService.getTaskDetlByTaskId(task.getId());
                        double sum = taskDetlByTaskId.stream().mapToDouble(TaskDetl::getAnfme).sum();
                        SlapLightControlParam slapLightControlParam = new SlapLightControlParam();
                        slapLightControlParam.setControllerCode(cacheSite.getMemo()).setTagCode(cacheSite.getSiteNo())
                                .setColor("GREEN").setMode("LIGHT").setDisplay(String.valueOf(sum));
                        // 发起亮灯请求
                        log.info("任务{}到达拣选位,播种墙亮灯下发",task.getTaskNo(), JSONObject.toJSONString(slapLightControlParam));
                        CommonReponse response = HttpEssUtils.post("到达拣选位,播种墙亮灯", HttpEssUtils.PLT_SEND_COMMAND, slapLightControlParam);
                        if (response.getCode().equals(0)) {
                            log.info("任务{}到达拣选位,播种墙{}亮灯成功",task.getTaskNo(),cacheSite.getSiteNo());
                        } else {
                            log.info("任务{}到达拣选位,播种墙{}亮灯失败",task.getTaskNo(),cacheSite.getSiteNo());
                        }
                    }
                boolean update = taskService.update(new LambdaUpdateWrapper<Task>()
                        .set(Task::getTaskSts, TaskStsType.WAVE_SEED.id)
                        .set(Task::getOriginSite, arrivedParam.getSlotCode())
                        .eq(Task::getBarcode, arrivedParam.getContainerCode()));
                if (!update) {
                    throw new CoolException("容器到达任务更新失败!!");
                }
            } else if(task.getTaskSts().equals(TaskStsType.UPDATED_OUT.id)) {
                ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
                conveyorStarParam.setSlotCode(task.getOriginSite())
                        .setContainerCode(task.getBarcode())
                        .setDirection("200");
                // 出库完成,清除输送线条码,取下箱子
                    CommonReponse response = HttpEssUtils.post("清除输送线条码", HttpEssUtils.CONVEYOR_START, conveyorStarParam);
                    if (response.getCode().equals(200)) {
                        log.info("{}出库完成,清除{}输送线条码{}成功",task.getTaskNo(),task.getOriginSite(),task.getBarcode());
                    }
            }
        }
        return R.success("success");
@@ -126,37 +174,43 @@
                .eq(Task::getTaskNo, callbackParam.getTaskCode()));
        if (!Collections.isEmpty(list)) {
            list.forEach(task -> {
                log.info("容器执行状态上报,任务号:{},当前状态:{},上报状态:{}",task.getTaskNo(),task.getTaskSts$(),callbackParam.getEventType());
                if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_ALLOCATED.event)) {
                    return;
                }
                // ESS只能配置一个状态上报接口,根据任务号判断是入库任务或出库任务
                String taskType = task.getTaskType() > 100 ? "outStock":"inStock";
                TaskStsType taskStsType = null;
                if (stockType.equals("inStock")) { //入库任务
                if (taskType.equals("inStock")) { //入库任务
                    if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_TOTE_LOAD.event)) { //上报取箱状态
                        if (task.getTaskSts() == TaskStsType.WCS_CONVEYOR_START.id) {
//                        if (task.getTaskSts() == TaskStsType.WCS_CONVEYOR_START.id) {
                            taskStsType = TaskStsType.WCS_TOTE_LOAD;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_TOTE_LOAD.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_TOTE_LOAD.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    } else if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_TOTE_UNLOAD.event)) { //上报放箱状态
                        if (task.getTaskSts() == TaskStsType.WCS_TOTE_LOAD.id) {
//                        if (task.getTaskSts() == TaskStsType.WCS_TOTE_LOAD.id) {
                            taskStsType = TaskStsType.WCS_TOTE_UNLOAD;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_TOTE_UNLOAD.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_TOTE_UNLOAD.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    } else if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_STATUS.event)) { //TODO 需确认上报任务中,任务完成是哪个事件,目前暂定task事件
                        if (task.getTaskSts() == TaskStsType.WCS_TOTE_UNLOAD.id) {
//                        if (task.getTaskSts() == TaskStsType.WCS_TOTE_UNLOAD.id) {
                            taskStsType = TaskStsType.WCS_PUTAWAY_SUCESS;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_PUTAWAY_SUCESS.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_PUTAWAY_SUCESS.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    }
                    boolean result = taskService.update(new LambdaUpdateWrapper<Task>()
                            .set(Task::getRobotCode, callbackParam.getRobotCode())
                            .set(Task::getSysTaskCode, callbackParam.getSysTaskCode())
                            .set(Task::getTaskSts, taskStsType.id)
//                            .set(Task::getSysTaskCode, callbackParam.getSysTaskCode())
                            .set(Task::getTaskSts, taskStsType != null ? taskStsType.id : task.getTaskType())
                            .set(Task::getExcudeStatus, callbackParam.getStatus())
                            .set(Task::getTaskDesc, callbackParam.getMessage())
                            .eq(Task::getBarcode, callbackParam.getContainerCode())
@@ -169,37 +223,36 @@
                    }
                } else { //出库任务
                    if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_TOTE_LOAD.event)) { //上报取箱状态
                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT.id ) {
//                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT.id ) {
                            taskStsType = TaskStsType.WCS_EXECUTE_OUT_TOTE_LOAD;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TOTE_LOAD.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TOTE_LOAD.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    } else if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_TOTE_UNLOAD.event)) { //上报放箱状态
                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TOTE_LOAD.id) {
//                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TOTE_LOAD.id) {
                            taskStsType = TaskStsType.WCS_EXECUTE_OUT_TOTE_UNLOAD;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TOTE_UNLOAD.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TOTE_UNLOAD.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    } else if (callbackParam.getEventType().equals(EssTaskStatus.TASK_EVENT_STATUS.event)) { //上报完成状态
                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TOTE_UNLOAD.id || task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.id) {
//                        if (task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TOTE_UNLOAD.id || task.getTaskSts() == TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.id) {
                            //出库任务完成,修改状态为容器到达,定时任务生成播种波次
                            taskStsType = TaskStsType.WCS_EXECUTE_OUT_TASK_DONE;
                        } else {
                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.desc + "任务";
                            log.error(errMsg);
                            throw new CoolException(errMsg);
                        }
//                        } else {
//                            String errMsg = "任务编号:" + task.getTaskNo() + "状态为不匹配," + "不能执行:" + TaskStsType.WCS_EXECUTE_OUT_TASK_DONE.desc + "任务";
//                            log.error(errMsg);
//                            throw new CoolException(errMsg);
//                        }
                    }
                    //更新出库状态及相关字段
                    boolean update = taskService.update(new LambdaUpdateWrapper<Task>()
                            .set(Task::getRobotCode, callbackParam.getRobotCode())
                            .set(Task::getSysTaskCode, callbackParam.getSysTaskCode())
                            .set(Task::getTaskSts, taskStsType.id)
                            .set(Task::getTaskSts, taskStsType != null ? taskStsType.id : task.getTaskType())
                            .set(Task::getExcudeStatus, callbackParam.getStatus())
                            .set(Task::getTaskDesc, callbackParam.getMessage())
                            .eq(Task::getBarcode, callbackParam.getContainerCode())
@@ -232,56 +285,53 @@
            return R.error("容器码任务错误!!");
        }
        tasks.forEach(task -> {
            //            try {
//                ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
//                conveyorStarParam.setSlotCode(task.getOriginLoc())
//                        .setContainerCode(task.getBarcode());
//                if (task.getTaskType() == 101) { //任务类型为101全盘出库,直接取下容器,传200
//                    conveyorStarParam.setDirection("200");
//                } else if (task.getTaskType() == 103) { //如果为任务类型为103,需走回库操作,传100
//                    conveyorStarParam.setDirection("100");
//                }
//                //调用三方接口,将任务推送至ESS平台
//                MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
//                // 设置请求参数
//                params.add("params", JSONObject.toJSONString(conveyorStarParam));
//                log.info("请求地址:{},请求参数:{}", Constant.CONVEYOR_START, JSONObject.toJSONString(conveyorStarParam));
            try {
                ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
                conveyorStarParam.setSlotCode(task.getOriginSite())
                        .setContainerCode(task.getBarcode());
                if (task.getTaskType() == 101) { //任务类型为101全盘出库,直接取下容器,传200
                    conveyorStarParam.setDirection("200");
                } else if (task.getTaskType() == 103 || task.getTaskType() == 107) { //如果为任务类型为103,需走回库操作,传100
                    conveyorStarParam.setDirection("100");
                }
                //调用三方接口,将任务推送至ESS平台
                MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
                // 设置请求参数
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type", "application/json");
                headers.add("api-version", "v2.0");
                HttpEntity httpEntity = new HttpEntity<>(conveyorStarParam, headers);
                log.info("请求地址:{},请求参数:{}", HttpEssUtils.CONVEYOR_START, JSONObject.toJSONString(conveyorStarParam));
                ResponseEntity<String> exchange = restTemplate.exchange("http://192.168.2.200:9046/conveyor/moveContainer", HttpMethod.POST, httpEntity, String.class);
//                HttpHeaders headers = new HttpHeaders();
//                headers.add("Content-Type", "application/json");
//                HttpEntity httpEntity = new HttpEntity<>(params, headers);
//                // 请求
//                ResponseEntity<String> exchange = restTemplate.exchange(Constant.CONVEYOR_START, HttpMethod.POST, httpEntity, String.class);
//                log.info("下发流动通知 返回结果:{}", exchange);
//                if (exchange.getBody() == null) {
//                    throw new CoolException("下发流动通知失败!!");
//                } else {
//                    CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
//                    if (commonReponse.getCode() == 0) {
            //流动通知下发完成后,修改任务状态为输送线流动中。。
            //TODO 判断任务是否为103拣料出库,103拣料流动后修改为4(RCS容器流动任务已下发)
            if (task.getTaskType() == 103) {
                //TODO 需找海柔确认是否需要重新调用入库接口
                //更新库存信息,修改任务状态为4 (RCS容器流动任务已下发)
                workService.pickTask(task.getId());
//                taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START))
            } else {
                taskService.update(new LambdaUpdateWrapper<Task>()
                        .eq(Task::getId, task.getId())
                        .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
//                ResponseEntity<String> exchange = restTemplate.exchange(HttpEssUtils.CONVEYOR_START, HttpMethod.POST, httpEntity, String.class);
                log.info("下发流动通知 返回结果:{}", exchange);
                if (exchange.getBody() == null) {
                    throw new CoolException("下发流动通知失败!!");
                } else {
                    CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
                    if (commonReponse.getCode() == 0) {
                        //流动通知下发完成后,修改任务状态为输送线流动中。。
                        if (task.getTaskType() == 103 || task.getTaskType() == 107) {
                            //更新库存信息,修改任务状态为4 (RCS容器流动任务已下发)
                            workService.pickTask(task.getId());
            //                taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskSts, TaskStsType.WCS_CONVEYOR_START))
                        } else {
                            taskService.update(new LambdaUpdateWrapper<Task>()
                                    .eq(Task::getId, task.getId())
                                    .set(Task::getTaskSts, TaskStsType.COMPLETE_OUT.id));
                        }
                        log.info(task.getTaskNo() + "下发流动通知" + commonReponse.getMsg());
                    } else {
                        throw new CoolException("下发流动通知失败!!");
                    }
                }
            } catch (Exception ex) {
                log.error(ex.getMessage());
            }
//                        log.info(task.getTaskNo() + "下发流动通知" + commonReponse.getMsg());
//                    } else {
//                        throw new CoolException("下发流动通知失败!!");
//                    }
//                }
//            } catch (Exception ex) {
//                log.error(ex.getMessage());
//            } finally {
//                //如果异常修改禁用状态
//                taskService.update(new LambdaUpdateWrapper<Task>().set(Task::getStatus, 0)
//                        .set(Task::getUpdateTime, new Date())
//                        .eq(Task::getId, task.getId()));
//            }
        });
        return R.ok();
    }
@@ -323,7 +373,7 @@
            //调用三方接口,闪灯不做操作
            // 设置请求参数
            params.add("params", JSONObject.toJSONString(slapParam));
            log.info("请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
            log.info("未完成闪灯请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
        } else {
            //调用三方接口,灭灯通知容器流动, 传灭灯参数
            //todo 判断当前订单是否完成,完成灭灯,未完成保持拣货状态亮灯
@@ -358,7 +408,7 @@
            }
            converyor = true;
            params.add("params", JSONObject.toJSONString(slapParam));
            log.info("请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
            log.info("完成灭灯请求地址:{},请求参数:{}", SystemProperties.SLAP_LIGHT, JSONObject.toJSONString(slapParam));
        }
        if (converyor) {