TaskAssignServiceImpl.java 15 KB
package com.huaheng.api.wcs.service.taskAssignService;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.AjaxResultWCS;
import com.huaheng.framework.web.service.ConfigService;
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.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.TransferTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 任务下发接口ServiceImpl层
 *
 * @author ricard
 * @date 2019/10/11
 */

@Service
public class TaskAssignServiceImpl implements TaskAssignService {

    @Resource
    private AddressService addressService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private LocationService locationService;
    @Resource
    private TransferTaskService transferTaskService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private ConfigService configService;


    public static String platform = "wms";

    //wms下发任务给wcs

    /**
     * 1、判断taskHeader是否为空
     * 2、判断必填字段是否满足
     * 3、转换实体
     * 4、发送数据
     *
     * @param taskHeader
     * @return
     */
    @Override
    @Transactional
    public AjaxResult wcsTaskAssign(TaskHeader taskHeader) {
        //1、判断taskHeader是否为空
        int preTaskNo = 0;
        if (taskHeader == null) {
            return AjaxResult.error("wms任务为空");
        }

        //2、判断必填字段是否满足
        if (taskHeader.getId() == null) {
            return AjaxResult.error("wms任务号Id为空");
        }
        if (taskHeader.getTaskType() == null) {
            return AjaxResult.error("wms任务类型为空");
        }
        String containerCode = taskHeader.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            return AjaxResult.error("wms任务中容器为空");
        }

        int taskType = taskHeader.getTaskType().intValue();

        // 出库性质的任务圆库位不能为空
        if (taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT || taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT
                || taskType == QuantityConstant.TASK_TYPE_SORTINGSHIPMENT
                || taskType == QuantityConstant.TASK_TYPE_EMPTYSHIPMENT || taskType == QuantityConstant.TASK_TYPE_CYCLECOUNT
                || taskType == QuantityConstant.TASK_TYPE_TRANSFER || taskType == QuantityConstant.TASK_TYPE_VIEW) {
            if (StringUtils.isEmpty(taskHeader.getFromLocation())) {
                return AjaxResult.error("源库位为空");
            }
        }
        /*if(taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT || taskType == QuantityConstant.TASK_TYPE_SORTINGSHIPMENT){
            taskHeader.setToLocation("");
        }*/
        String warehouseCode = taskHeader.getWarehouseCode();

        //3、转换实体,初始化wcs任务实体
        Location location;
        boolean direction = true;  // true 执行时是入库动作, false 执行时是出库动作
        switch (taskType) {
            case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
            case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
            case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
            case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT:
            case QuantityConstant.TASK_TYPE_VIEW:
            case QuantityConstant.TASK_TYPE_CYCLECOUNT:
            case QuantityConstant.TASK_TYPE_TRANSFER:
                location = locationService.getLocationByCode(taskHeader.getFromLocation(), warehouseCode);
                direction = false;
                break;
            case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
            case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
                location = locationService.getLocationByCode(taskHeader.getToLocation(), warehouseCode);
                direction = true;
                break;
            default:
                location = locationService.getLocationByCode(taskHeader.getToLocation(), warehouseCode);
                break;
        }

        // 出库动作 补充入库 移库
        if (!direction) {
            int rowFlag = location.getRowFlag().intValue();
            if (rowFlag == QuantityConstant.ROW_OUT) {
                if (warehouseCode.equalsIgnoreCase("KS0001") && location.getIRow() == 1) {
                } else {
                    Location insideLocation = locationService.getInsideNear(location);
                    String insideLocationCode = insideLocation.getCode();
                    if (StringUtils.isNotEmpty(insideLocation.getContainerCode())) {
                        LambdaQueryWrapper<TaskHeader> lambdaWrapper = Wrappers.lambdaQuery();
                        lambdaWrapper.eq(TaskHeader::getFromLocation, insideLocationCode);
                        lambdaWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                        TaskHeader taskHeader2 = taskHeaderService.getOne(lambdaWrapper);
                        if (taskHeader2 != null) {
                            preTaskNo = taskHeader2.getId();
                        } else {
                            Location destinationLocation = locationService.getEmptyLocation(insideLocation);
                            if (destinationLocation == null) {
                                return AjaxResult.error("移库没有剩余库位");
                            }
                            AjaxResult ajaxResult = transferTaskService.createTransferTask(
                                    insideLocation.getCode(), destinationLocation.getCode(), warehouseCode);
                            preTaskNo = (Integer) ajaxResult.getData();
                            if (ajaxResult.hasErr()) {
                                return AjaxResult.error("创建移库任务失败");
                            }
//                        TaskHeader transferTaskHeader = taskHeaderService.getById(preTaskNo);
//                        sendToWcs(transferTaskHeader, 0, location);
                            if (warehouseCode.equals("XZ0001")) {
                                taskHeader.setPreTaskNo(preTaskNo);
                                taskHeaderService.updateById(taskHeader);
                                TaskHeader transferTaskHeader = taskHeaderService.getById(preTaskNo);
                                sendToWcs(transferTaskHeader, 0, location);
                            }
                            return AjaxResult.error("先执行移库任务");
                        }
                        taskHeader.setPreTaskNo(preTaskNo);
                        taskHeaderService.updateById(taskHeader);
                        return AjaxResult.error("先执行移库任务");
                    } else {
                        LambdaQueryWrapper<TaskHeader> lambdaWrapper = Wrappers.lambdaQuery();
                        lambdaWrapper.eq(TaskHeader::getToLocation, insideLocationCode);
                        lambdaWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                        TaskHeader taskHeader1 = taskHeaderService.getOne(lambdaWrapper);
                        if (taskHeader1 != null) {
                            return AjaxResult.error("执行外侧库位出库时,相应的内侧库位有任务,请先执行完内侧库位任务");
                        }
//                        taskHeader.setPreTaskNo(preTaskNo);
                        taskHeaderService.updateById(taskHeader);
                    }
                }

            }

        }


        AjaxResult ajaxResult = sendToWcs(taskHeader, preTaskNo, location);
        return ajaxResult;
    }

    @Override
    @Transactional
    public AjaxResult sendToWcs(TaskHeader taskHeader, int preTaskNo, Location location) {
        int taskType = taskHeader.getTaskType();
        String warehouseCode = taskHeader.getWarehouseCode();
        String containerCode = taskHeader.getContainerCode();
        WcsTask wcsTask = new WcsTask();
        wcsTask.setTaskNo(taskHeader.getId().toString());
        wcsTask.setWarehouseCode(warehouseCode);
        wcsTask.setTaskType(taskType);
        wcsTask.setFromPort("");
        wcsTask.setToPort(taskHeader.getPort());
        wcsTask.setContainerCode(taskHeader.getContainerCode());
        if (preTaskNo != 0) {
            wcsTask.setPreTaskNo(String.valueOf(preTaskNo));
        }

        if (location != null) {
            wcsTask.setStation(location.getRoadway());
            wcsTask.setRoadWay(location.getRoadway());
            if (StringUtils.isEmpty(taskHeader.getFromLocation())) {
                wcsTask.setFromLocationCode("");
            } else {
                wcsTask.setFromLocationCode(taskHeader.getFromLocation());
            }
            if (StringUtils.isEmpty(taskHeader.getToLocation())) {
                wcsTask.setToLocationCode("");
            } else {
                wcsTask.setToLocationCode(taskHeader.getToLocation());
            }
        }


        wcsTask.setPriority(10);
        if (QuantityConstant.TASK_TYPE_TRANSFER == taskType) {
            wcsTask.setPriority(100);
        }
        wcsTask.setRemark("0");
        wcsTask.setPlatform(platform);

        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        String area = container.getArea();
        if (StringUtils.isEmpty(area)) {
            return AjaxResult.error("容器未配置库区 area");
        }
        //4、发送数据
        String value = configService.getKey(QuantityConstant.RULE_CONNECT_WCS, warehouseCode);
        String val = configService.getKey(QuantityConstant.ZONE_MUTLI_ROADWAY, warehouseCode);
        int connectWCS = Integer.parseInt(value);
        if (connectWCS == QuantityConstant.RULE_WCS_CONNECT) {
            String url = "";
            //是否有一个库区多个巷道的 堆垛机
            //有,需要不同巷道的URL
            if (StringUtils.isEmpty(val)) {
                url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN, warehouseCode, area);
            } else {
                String[] strs = Convert.toStrArray(val);
                List<String> strings = Arrays.asList(strs);
                if (strings.contains(container.getArea())) {
                    ContainerType containerType = containerTypeService.getContainerTypeByCode(container.getContainerType(), container.getWarehouseCode());
                    Integer roadway = containerType.getRoadway();
                    url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN, warehouseCode, area, roadway);
                }
            }
            wcsTask = switchTaskTypeToWcs(wcsTask);
            String jsonParam = JSON.toJSONString(wcsTask);
            System.out.println(jsonParam);
            url = url + "TaskAssign";
            String result = OkHttpUtils.bodypost(url, jsonParam, warehouseCode);
            if (StringUtils.isNull(result) || StringUtils.isEmpty(result)) {
                throw new ServiceException("WCS:接口地址错误或返回为空");
            }
//            AjaxResultWCS ajaxResult = new AjaxResultWCS();
//            ajaxResult.setCode(200);
            AjaxResultWCS ajaxResult = JSON.parseObject(result, AjaxResultWCS.class);
            if (ajaxResult.getCode() != HttpConstant.OK) {
                return AjaxResult.error("源库位:" + wcsTask.getFromLocationCode() + "目标库位:" + wcsTask.getToLocationCode() + "," + ajaxResult.getMessage());
            }
            taskHeader.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
            taskHeaderService.updateById(taskHeader);
            //总有个别数据任务详情没有修改状态
            LambdaQueryWrapper<TaskDetail> lambdaWrapper = com.huaheng.common.utils.Wrappers.lambdaQuery();
            lambdaWrapper.eq(TaskDetail::getTaskId, taskHeader.getId());
//            List<TaskDetail> taskDetailList = taskDetailService.list(lambdaWrapper);
            TaskDetail item = new TaskDetail();
            item.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
            item.setLastUpdated(new Date());
            item.setLastUpdatedBy(ShiroUtils.getLoginName());
            item.setProcessStamp("100");
            taskDetailService.update(item, lambdaWrapper);
        }
        return AjaxResult.success("");
    }

    // wcs任务类型 入库 = 100 , 出库 = 300,分拣出库 = 400 ,移库 = 800,换站=1000
    @Override
    public WcsTask switchTaskTypeToWcs(WcsTask wcsTask) {
        int taskType = wcsTask.getTaskType().intValue();
        switch (taskType) {
            case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
            case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
            case QuantityConstant.TASK_TYPE_MANY_EMPTYRECEIPT:
                wcsTask.setTaskType(100);
                break;
            case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
            case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
            case QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT:
                wcsTask.setTaskType(300);
                break;
            case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
            case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT:
            case QuantityConstant.TASK_TYPE_CYCLECOUNT:
            case QuantityConstant.TASK_TYPE_VIEW:
                wcsTask.setTaskType(400);
                break;
            case QuantityConstant.TASK_TYPE_TRANSFER:
                wcsTask.setTaskType(800);
                break;
            case QuantityConstant.TASK_TYPE_OVER_STATION:
                wcsTask.setTaskType(1000);
                break;
            default:
                throw new ServiceException("不支持的任务类型");

        }
        return wcsTask;
    }

}