TaskCancelServiceImpl.java 7.4 KB
package com.huaheng.api.wcs.service.taskCancel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaheng.api.general.domain.ReorderDomain;
import com.huaheng.api.general.domain.TaskShipmentOrder;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.restful.RestUtil;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.containerType.domain.ContainerType;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@Service
public class TaskCancelServiceImpl implements TaskCancelService {


    @Autowired
    private AddressService addressService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private ContainerService containerService;
    /**
     * 取消任务
     * 1、判断参数是否为空
     * 2、转换实体
     * 3、发送数据
     *
     * @param id 任务ID
     * @return 取消结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult TaskCance(Integer id) {
        if (id == null) {
            throw new ServiceException("任务号为空");
        }

        TaskHeader taskHeader = taskHeaderService.getById(id);
        if (taskHeader == null) {
            throw new ServiceException("任务ID[" + id + "]不存在");
        }

        String area = null;
        String warehouseCode = null;
        String url = null;

        // 查询容器信息(防null)
        LambdaQueryWrapper<Container> containerWrapper = Wrappers.lambdaQuery();
        containerWrapper.eq(Container::getCode, taskHeader.getContainerCode());
        Container container = containerService.getOne(containerWrapper, false); // 无数据返回null,不抛异常
        if (container == null) {
            throw new ServiceException("任务关联容器[" + taskHeader.getContainerCode() + "]不存在");
        }

        // 查询容器类型信息(防null)
        LambdaQueryWrapper<ContainerType> containerTypeWrapper = Wrappers.lambdaQuery();
        containerTypeWrapper.eq(ContainerType::getCode, container.getContainerType());
        ContainerType containerType = containerTypeService.getOne(containerTypeWrapper, false);
        if (containerType == null) {
            throw new ServiceException("容器类型[" + container.getContainerType() + "]不存在");
        }
        area = containerType.getArea();
        warehouseCode = containerType.getWarehouseCode();

        // 处理区域编码特殊逻辑
        if (taskHeader.getZoneCode() == null) {
            url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_CANCEL, warehouseCode, area);
        } else {
            if ("C".equals(taskHeader.getZoneCode())) {
                area = "3";
                warehouseCode = taskHeader.getWarehouseCode();
            }
            url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_CANCEL, warehouseCode, area);
        }

        // URL非空校验
        if (StringUtils.isEmpty(url)) {
            throw new ServiceException("未获取到WCS取消任务接口地址");
        }

        // 构建取消任务参数
        ReorderDomain reorderDomain = new ReorderDomain();
        // 防null:任务ID转字符串
        reorderDomain.setTaskNo(Optional.ofNullable(taskHeader.getId()).map(String::valueOf).orElse(""));

        // 整盘出库
        if (taskHeader.getTaskType()==300) {
            LambdaQueryWrapper<TaskHeader> taskWrapper = Wrappers.lambdaQuery();
            taskWrapper
                    .eq(TaskHeader::getShipmentOrder, taskHeader.getShipmentOrder())
                    .eq(TaskHeader::getTaskType, 300);
            List<TaskHeader> taskList = taskHeaderService.list(taskWrapper);

            // 排序(升序)
            taskList.sort(Comparator.comparing(TaskHeader::getSequence, Comparator.nullsLast(Integer::compareTo)));
            // 移除当前任务
            taskList.removeIf(task -> id.equals(task.getId()));

            // 重新编排序号
            int sequence = 1;
            for (TaskHeader task : taskList) {
                if (task.getStatus() != null && task.getStatus() != 1) {
                    task.setSequence(sequence++);
                    task.setSequenceNumber(taskList.size());
                    taskHeaderService.updateById(task);
                }
            }

            // 构建批量取消参数
            for (TaskHeader task : taskList) {
                if (task.getStatus() != null && task.getStatus() != 1) {
                    TaskShipmentOrder shipmentOrder = new TaskShipmentOrder();
                    shipmentOrder.setTaskNo(Optional.ofNullable(task.getId()).map(String::valueOf).orElse(""));
                    shipmentOrder.setSequence(task.getSequence());
                    shipmentOrder.setSequenceNumber(task.getSequenceNumber());
                    shipmentOrder.setShipmentOrder(task.getShipmentOrder());
                    reorderDomain.getTaskShipmentOrders().add(shipmentOrder);
                }
            }
        }

        // 调用WCS接口取消任务
        try {
            String jsonParam = new ObjectMapper().writeValueAsString(reorderDomain);
            ResponseEntity<JSONObject> result = RestUtil.request_post(url, warehouseCode, jsonParam);

            if (result == null || result.getBody() == null) {
                throw new ServiceException("接口地址错误或返回为空:" + url);
            }

            String code = result.getBody().getString("code");
            String msg = result.getBody().getString("msg");
            if (code == null || Integer.parseInt(code) != HttpConstant.OK) {
                throw new ServiceException("WCS接口返回失败:" + msg);
            }
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return mapper.readValue(result.getBody().toString(), AjaxResult.class);
        } catch (Exception e) {
            throw new ServiceException("调用WCS取消任务失败:" + e.getMessage());
        }
    }

}