chen.lin
1 天以前 6e127398cc8b5dcd78a81029f1c747ceee37af18
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/TaskServiceImpl.java
@@ -462,6 +462,9 @@
                if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)) {
                    //1.入库
                    complateInstock(task, loginUserId);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_EMPITY_IN.type)) {
                    //10.空托入库
                    complateEmpityInStock(task, loginUserId);
                } else if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)) {
                    //53.拣料再入库
                    //57.盘点再入库
@@ -744,11 +747,14 @@
            }
            BeanUtils.copyProperties(locWorking, locItem);
            locItem.setWorkQty(0.0).setQty(0.0).setLocCode(loc.getCode()).setLocId(loc.getId()).setId(null).setUpdateBy(loginUserId).setUpdateTime(new Date());
            items.add(locItem);
            //数量为零的不入库
            if (locItem.getAnfme().compareTo(0.0) > 0) {
                items.add(locItem);
            }
        }
        if (!locItemService.saveBatch(items)) {
            throw new CoolException("作业库存回写失败!!");
//            throw new CoolException("作业库存回写失败!!");
        }
        TaskItem taskItem = taskItems.stream().findFirst().get();
@@ -774,16 +780,132 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R removeTask(Long[] ids, Long loginUserId) {
        List<Integer> longs = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id);
        // 先查询所有任务(不限制状态),用于检查RCS任务
        List<Integer> list = Arrays.asList(TaskType.TASK_TYPE_IN.type, TaskType.TASK_TYPE_OUT.type, TaskType.TASK_TYPE_PICK_AGAIN_OUT.type,
                TaskType.TASK_TYPE_CHECK_OUT.type, TaskType.TASK_TYPE_EMPITY_IN.type, TaskType.TASK_TYPE_LOC_MOVE.type,
                TaskType.TASK_TYPE_EMPITY_OUT.type, TaskType.TASK_TYPE_MERGE_OUT.type);
        List<Task> allTasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getId, (Object[]) ids));
        if (allTasks.isEmpty()) {
            throw new CoolException("任务不存在!!");
        }
        // 收集需要取消的RCS任务编号和批次编号(不限制状态,只要已下发到RCS就需要取消)
        List<String> rcsTaskCodes = new ArrayList<>();
        String batchNo = null;
        for (Task task : allTasks) {
            // 判断任务是否已下发到RCS
            // 入库任务:状态 >= WCS_EXECUTE_IN(2) 表示已下发
            // 出库任务:状态 >= WCS_EXECUTE_OUT(102) 表示已下发
            boolean isRcsTask = false;
            if (task.getTaskType() < 100) {
                // 入库任务
                if (task.getTaskStatus() >= TaskStsType.WCS_EXECUTE_IN.id) {
                    isRcsTask = true;
                }
            } else {
                // 出库任务
                if (task.getTaskStatus() >= TaskStsType.WCS_EXECUTE_OUT.id) {
                    isRcsTask = true;
                }
            }
            if (isRcsTask && StringUtils.isNotBlank(task.getTaskCode())) {
                rcsTaskCodes.add(task.getTaskCode());
                // 获取批次编号,优先使用任务的barcode,如果没有则使用任务编号
                if (StringUtils.isBlank(batchNo)) {
                    if (StringUtils.isNotBlank(task.getBarcode())) {
                        batchNo = task.getBarcode();
                    } else if (StringUtils.isNotBlank(task.getTaskCode())) {
                        // 如果任务没有barcode,使用任务编号作为批次编号
                        batchNo = task.getTaskCode();
                    }
                }
                log.info("任务已下发到RCS,需要取消RCS任务 - 任务ID:{},任务编号:{},任务状态:{},托盘码:{}",
                        task.getId(), task.getTaskCode(), task.getTaskStatus(), task.getBarcode());
            }
        }
        // 如果有任务已下发到RCS,先调用RCS取消接口
        boolean rcsCancelSuccess = false;
        if (!rcsTaskCodes.isEmpty()) {
            try {
                log.info("========== 开始取消RCS任务 ==========");
                log.info("需要取消的RCS任务编号:{}", rcsTaskCodes);
                String rcsUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.cancelTask;
                log.info("RCS取消任务请求地址:{}", rcsUrl);
                // 如果没有批次编号,使用第一个任务编号作为批次编号
                if (StringUtils.isBlank(batchNo) && !rcsTaskCodes.isEmpty()) {
                    batchNo = rcsTaskCodes.get(0);
                }
                Map<String, Object> cancelParams = new HashMap<>();
                cancelParams.put("tasks", rcsTaskCodes);
                if (StringUtils.isNotBlank(batchNo)) {
                    cancelParams.put("batchNo", batchNo);
                }
                log.info("RCS取消任务请求参数:{}", JSONObject.toJSONString(cancelParams));
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type", "application/json");
                headers.add("api-version", "v2.0");
                HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(cancelParams, headers);
                long startTime = System.currentTimeMillis();
                ResponseEntity<String> exchange = restTemplate.exchange(rcsUrl, HttpMethod.POST, httpEntity, String.class);
                long endTime = System.currentTimeMillis();
                log.info("RCS取消任务响应耗时:{}ms", (endTime - startTime));
                log.info("RCS取消任务响应状态码:{}", exchange.getStatusCode());
                log.info("RCS取消任务响应体:{}", exchange.getBody());
                if (Objects.isNull(exchange.getBody())) {
                    log.error("RCS取消任务失败:响应体为空");
                    throw new CoolException("RCS取消任务失败:响应体为空");
                }
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.coercionConfigDefaults()
                        .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class);
                if (result.getCode() == 200) {
                    log.info("========== RCS任务取消成功 ==========");
                    log.info("成功取消的RCS任务编号:{}", rcsTaskCodes);
                    rcsCancelSuccess = true;
                } else {
                    log.error("RCS取消任务失败:{}", result.getMsg());
                    throw new CoolException("RCS取消任务失败:" + result.getMsg());
                }
            } catch (JsonProcessingException e) {
                log.error("RCS取消任务响应解析失败:{}", e.getMessage(), e);
                throw new CoolException("RCS取消任务响应解析失败:" + e.getMessage());
            } catch (Exception e) {
                log.error("RCS取消任务异常:{}", e.getMessage(), e);
                throw new CoolException("RCS取消任务异常:" + e.getMessage());
            }
        }
        // 查询符合取消条件的任务(状态为1、101、199)
        List<Integer> allowedStatuses = Arrays.asList(TaskStsType.GENERATE_IN.id, TaskStsType.GENERATE_OUT.id, TaskStsType.WAVE_SEED.id);
        List<Task> tasks = this.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskType, list)
                .in(Task::getId, ids)
                .in(Task::getTaskStatus, longs));
        if (tasks.isEmpty()) {
                .in(Task::getId, (Object[]) ids)
                .in(Task::getTaskStatus, allowedStatuses));
        // 如果符合取消条件的任务为空,但RCS取消成功,允许继续(可能是任务状态已变更)
        if (tasks.isEmpty() && !rcsCancelSuccess) {
            throw new CoolException("任务已处执行状态不可取消!!");
        }
        // 如果符合取消条件的任务为空,但RCS取消成功,记录日志并返回
        if (tasks.isEmpty() && rcsCancelSuccess) {
            log.warn("WMS内部任务状态不符合取消条件,但RCS任务已成功取消 - 任务ID:{}", Arrays.toString(ids));
            return R.ok("RCS任务已取消,WMS内部任务状态已变更");
        }
        for (Task task : tasks) {
            //取消移库任务
@@ -1224,7 +1346,7 @@
                throw new CoolException(e.getMessage());
            }
        }
        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type)) {
        if (task.getTaskType().equals(TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_OUT.type)) {
            /**修改为库位状态为S.预约入库,保留原有库位*/
            if (!locService.update(new LambdaUpdateWrapper<Loc>()
                    .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type)
@@ -1399,13 +1521,28 @@
    public void pubTaskToWcs(List<Task> tasks) {
        WcsTaskParams taskParams = new WcsTaskParams();
        List<TaskItemParam> items = new ArrayList<>();
        // 设置批次编号(使用第一个任务的barcode,如果为null则使用任务编码)
        String batchNo = null;
        if (!tasks.isEmpty()) {
            Task firstTask = tasks.get(0);
            batchNo = StringUtils.isNotBlank(firstTask.getBarcode())
                    ? firstTask.getBarcode()
                    : firstTask.getTaskCode();
        }
        if (StringUtils.isBlank(batchNo)) {
            // 如果批次编号仍为空,生成一个默认值
            batchNo = "BATCH_" + System.currentTimeMillis();
        }
        taskParams.setBatchNo(batchNo);
        log.info("设置批次编号:{}", batchNo);
        tasks.forEach(task -> {
            TaskItemParam itemParam = new TaskItemParam();
            //任务类型,任务编码
            itemParam.setTaskType(RcsTaskType.getTypeDesc(task.getTaskType()))
                    .setSeqNum(task.getTaskCode());
            //主参数
            taskParams.setBatch(task.getBarcode());
            //任务编码(对应seqNum)
            itemParam.setTaskNo(task.getTaskCode());
            itemParam.setPriority(1);
            BasStation station = null;
            if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                station = basStationService.getOne(new LambdaQueryWrapper<BasStation>().eq(BasStation::getStationName, task.getTargSite()));
@@ -1471,28 +1608,50 @@
            }
            items.add(itemParam);
        });
        taskParams.setTaskList(items);
        taskParams.setTasks(items);
        /**任务下发接口*/
        String pubTakUrl = rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask;
        /**RCS基础配置链接*/
        log.info("任务下发,请求地址: {}, 请求参数: {}", pubTakUrl, JSONObject.toJSONString(taskParams));
        log.info("========== 开始下发任务到RCS ==========");
        log.info("RCS请求地址:{}", pubTakUrl);
        log.info("批次编号:{}", batchNo);
        log.info("任务数量:{}", tasks.size());
        log.info("任务列表详情:");
        tasks.forEach(task -> {
            log.info("  - 任务编码:{},任务类型:{},源库位:{},目标库位:{},源站点:{},目标站点:{}",
                    task.getTaskCode(), task.getTaskType(), task.getOrgLoc(),
                    task.getTargLoc(), task.getOrgSite(), task.getTargSite());
        });
        log.info("请求参数:{}", JSONObject.toJSONString(taskParams));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("api-version", "v2.0");
        HttpEntity httpEntity = new HttpEntity(taskParams, headers);
        long startTime = System.currentTimeMillis();
        ResponseEntity<String> exchange = restTemplate.exchange(pubTakUrl, HttpMethod.POST, httpEntity, String.class);
        log.info("任务下发后,响应结果: {}", exchange);
        long endTime = System.currentTimeMillis();
        log.info("RCS响应耗时:{}ms", (endTime - startTime));
        log.info("RCS响应状态码:{}", exchange.getStatusCode());
        log.info("RCS响应头:{}", exchange.getHeaders());
        log.info("RCS响应体:{}", exchange.getBody());
        if (Objects.isNull(exchange.getBody())) {
            throw new CoolException("任务下发失败!!");
            log.error("========== RCS任务下发失败 ==========");
            log.error("RCS响应体为空,无法解析响应结果");
            log.error("请求地址:{}", pubTakUrl);
            log.error("请求参数:{}", JSONObject.toJSONString(taskParams));
            throw new CoolException("任务下发失败,RCS响应体为空!!");
        } else {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.coercionConfigDefaults()
                        .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class);
                log.info("RCS响应解析结果 - code:{},msg:{},data:{}",
                        result.getCode(), result.getMsg(), result.getData());
                if (result.getCode() == 200) {
                    log.info("RCS任务下发成功,开始更新任务状态");
                    tasks.forEach(task -> {
                        log.info("更新任务状态 - 任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType());
                        if (task.getTaskType().equals(TaskType.TASK_TYPE_IN.type)
                                || task.getTaskType().equals(TaskType.TASK_TYPE_PICK_IN.type)
                                || task.getTaskType().equals(TaskType.TASK_TYPE_CHECK_IN.type)
@@ -1505,10 +1664,12 @@
                                throw new CoolException("站点不存在!!");
                            }
                            log.info("更新入库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_IN.id);
                            if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                    .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_IN.id))) {
                                throw new CoolException("任务状态修改失败!!");
                            }
                            log.info("入库任务状态更新成功 - 任务编码:{}", task.getTaskCode());
                            /**排除移库功能*/
                            if (!task.getTaskType().equals(TaskType.TASK_TYPE_LOC_MOVE.type)) {
                                /**如果是普通站点,修改站点状态为出库预约*/
@@ -1529,10 +1690,12 @@
                                throw new CoolException("站点不存在!!");
                            }
                            log.info("更新出库任务状态 - 任务编码:{},新状态:{}", task.getTaskCode(), TaskStsType.WCS_EXECUTE_OUT.id);
                            if (!taskService.update(new LambdaUpdateWrapper<Task>().eq(Task::getTaskCode, task.getTaskCode())
                                    .set(Task::getTaskStatus, TaskStsType.WCS_EXECUTE_OUT.id))) {
                                throw new CoolException("任务状态修改失败!!");
                            }
                            log.info("出库任务状态更新成功 - 任务编码:{}", task.getTaskCode());
                            /**如果是普通站点,修改站点状态为入库预约*/
                            if (curSta.getType().equals(StationTypeEnum.STATION_TYPE_NORMAL.type)) {
                                curSta.setUseStatus(LocStsType.LOC_STS_TYPE_S.type);
@@ -1542,15 +1705,27 @@
                            }
                        }
                    });
                    log.info("========== RCS任务下发完成,共{}个任务状态已更新 ==========", tasks.size());
                } else {
                    log.error(JSONObject.toJSONString(result));
                    log.error("========== RCS任务下发失败 ==========");
                    log.error("RCS返回错误 - code:{},msg:{},data:{}",
                            result.getCode(), result.getMsg(), result.getData());
                    log.error("失败的任务列表:");
                    tasks.forEach(task -> {
                        log.error("  - 任务编码:{},任务类型:{}", task.getTaskCode(), task.getTaskType());
                    });
//                    throw new CoolException("任务下发失败!!");
                }
            } catch (JsonProcessingException e) {
                throw new CoolException(e.getMessage());
                log.error("========== RCS任务下发异常 ==========");
                log.error("解析RCS响应失败,响应体:{}", exchange.getBody(), e);
                throw new CoolException("解析RCS响应失败:" + e.getMessage());
            } catch (Exception e) {
                log.error("========== RCS任务下发异常 ==========");
                log.error("任务下发过程中发生异常", e);
                throw e;
            }
        }
    }/**
@@ -1634,6 +1809,80 @@
    }
    /**
     * 空托入库完成
     *
     * @param task
     * @param loginUserId
     */
    @Transactional(rollbackFor = Exception.class)
    public synchronized void complateEmpityInStock(Task task, Long loginUserId) {
        if (Objects.isNull(task)) {
            return;
        }
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getCode, task.getTargLoc()));
        if (Objects.isNull(loc)) {
            throw new CoolException("目标库位不存在!");
        }
        if (!loc.getUseStatus().equals(LocStsType.LOC_STS_TYPE_S.type)) {
            throw new CoolException("当前库位状态不处于S.入库预约,不可执行入库操作!");
        }
        List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>().eq(TaskItem::getTaskId, task.getId()));
        if (taskItems.isEmpty()) {
            throw new CoolException("任务明细不存在!!");
        }
        //更新库位明细
        saveLocItem(taskItems, task.getId(), loginUserId);
        //空托入库不校验组托信息,直接处理任务明细
        //如果有任务明细关联了组托,也需要保存入出库明细和移出收货区库存
        Map<Long, List<TaskItem>> orderMap = taskItems.stream().collect(Collectors.groupingBy(TaskItem::getSource));
        orderMap.keySet().forEach(key -> {
            // 如果source不为空,说明有关联的组托明细,需要处理
            if (key != null && key > 0) {
                WaitPakinItem pakinItem = waitPakinItemService.getById(key);
                if (!Objects.isNull(pakinItem)) {
                    List<TaskItem> items = orderMap.get(key);
                    //保存入出库明细
                    saveStockItems(items, task, pakinItem.getId(), pakinItem.getAsnCode(), pakinItem.getWkType(), pakinItem.getType(), loginUserId);
                    //移出收货区库存, 修改组托状态
                    removeReceiptStock(pakinItem, loginUserId);
                }
            }
        });
        // 处理组托状态(如果有组托关联)
        Set<Long> pkinItemIds = taskItems.stream()
                .map(TaskItem::getSource)
                .filter(source -> source != null && source > 0)
                .collect(Collectors.toSet());
        if (!pkinItemIds.isEmpty()) {
            List<WaitPakinItem> pakinItems = waitPakinItemService.list(new LambdaQueryWrapper<WaitPakinItem>().in(WaitPakinItem::getId, pkinItemIds));
            if (!pakinItems.isEmpty()) {
                Set<Long> pakinIds = pakinItems.stream().map(WaitPakinItem::getPakinId).collect(Collectors.toSet());
                if (!waitPakinService.update(new LambdaUpdateWrapper<WaitPakin>()
                        .set(WaitPakin::getIoStatus, PakinIOStatus.PAKIN_IO_STATUS_TASK_DONE.val)
                        .set(WaitPakin::getUpdateBy, loginUserId)
                        .in(WaitPakin::getId, pakinIds))) {
                    throw new CoolException("组拖状态修改失败!!");
                }
            }
        }
        /**修改库位状态为F.在库*/
        if (!locService.update(new LambdaUpdateWrapper<Loc>().set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_F.type).eq(Loc::getCode, task.getTargLoc()))) {
            throw new CoolException("库位状态修改失败!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Task>().eq(Task::getId, task.getId()).set(Task::getTaskStatus, TaskStsType.UPDATED_IN.id))) {
            throw new CoolException("任务状态修改失败!!");
        }
    }
    /**
     * @param
     * @param loginUserId
     * @return