TaskAssignServiceImpl.java 14.7 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.TaskDetails;
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.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
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.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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

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

@Service
public class TaskAssignServiceImpl implements TaskAssignService {

    @Autowired
    private AddressService addressService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Resource
    private LocationService locationService;
    @Resource
    private TransferTaskService transferTaskService;
    @Resource
    private TaskHeaderService taskHeaderService;


    public static String platform = "wms";

    //wms下发任务给wcs
    /**
     * 1、判断taskHeader是否为空
     * 2、判断必填字段是否满足
     * 3、转换实体
     * 4、发送数据
     *
     * @param taskHeader
     * @return
     */
    @Override
    @Transactional
    public AjaxResult wcsTaskAssign(TaskHeader taskHeader) {
        //1、判断taskHeader是否为空
        Integer 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任务类型为空");
        }
        if(StringUtils.isEmpty(taskHeader.getContainerCode())){
            return AjaxResult.error("wms任务中容器为空");
        }

        int taskType = taskHeader.getTaskType().intValue();
        //入库性质的任务源库位不能为空
        if(taskType == QuantityConstant.TASK_TYPE_WHOLERECEIPT || taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT
                || taskType == QuantityConstant.TASK_TYPE_EMPTYRECEIPT || taskType == QuantityConstant.TASK_TYPE_TRANSFER) {
            if (taskHeader.getFromLocation() == null) {
                return AjaxResult.error("源库位为空");
            }
        }

       // 出库性质的任务目的库位不能为空
        if(taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT || 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 (taskHeader.getToLocation() == null) {
                return AjaxResult.error("目的库位为空");
            }
        }

        //3、转换实体,初始化wcs任务实体
        WcsTask wcsTask = new WcsTask();
        wcsTask.setTaskNo(taskHeader.getId().toString());
        wcsTask.setWarehouseCode(ShiroUtils.getWarehouseCode());
        wcsTask.setTaskType(taskType);
        wcsTask.setFromPort("");
        wcsTask.setToPort(taskHeader.getPort());
        wcsTask.setContainerCode(taskHeader.getContainerCode());
        Location location;
        switch(wcsTask.getTaskType().intValue()) {
            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:
                location = locationService.getLocationByCode(taskHeader.getFromLocation());
                break;
            case QuantityConstant.TASK_TYPE_TRANSFER:
            case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
            case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
                location = locationService.getLocationByCode(taskHeader.getToLocation());
                break;
            default:
                location = locationService.getLocationByCode(taskHeader.getToLocation());
                break;
        }
        if (taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT
                || taskType == QuantityConstant.TASK_TYPE_WHOLESHIPMENT
                || taskType == QuantityConstant.TASK_TYPE_SORTINGSHIPMENT
                || taskType == QuantityConstant.TASK_TYPE_EMPTYSHIPMENT
                || taskType == QuantityConstant.TASK_TYPE_VIEW ) {
            int rowFlag = location.getRowFlag().intValue();
            if(rowFlag == QuantityConstant.ROW_OUT) {
                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());
                        preTaskNo = (Integer) ajaxResult.getData();
                        if (ajaxResult.hasErr()) {
                            return AjaxResult.error("创建移库任务失败");
                        }
                        taskHeaderService.sendTaskToWcs(Convert.toIntArray(String.valueOf(preTaskNo)));
                    }
                } 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);
            LambdaQueryWrapper<TaskHeader> lambdaWrapper2 = Wrappers.lambdaQuery();
            lambdaWrapper2.eq(TaskHeader::getId, taskHeader.getId());
            taskHeaderService.update(taskHeader, lambdaWrapper2);
        }

        if(location != null) {
            Location insideLocation = locationService.getNear(location);
            if (insideLocation != null) {
                String nearLocationCode = insideLocation.getCode();
                LambdaQueryWrapper<TaskHeader> lambdaWrapper = Wrappers.lambdaQuery();
                lambdaWrapper.eq(TaskHeader::getFromLocation, nearLocationCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                        .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT,
                                QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_VIEW)
                        .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_RELEASE);
                List<TaskHeader> taskHeader2 = taskHeaderService.list(lambdaWrapper);
                if (taskHeader2 != null && taskHeader2.size() > 0) {
                    return AjaxResult.error("执行任务出库时,相应的旁边库位有分拣出库或者补充入库任务,请先执行完旁边库位任务");
                }
            }

            Location insideNear = locationService.getInsideNear(location);
            if (location.getRowFlag().intValue() == QuantityConstant.ROW_OUT) {
                String locationCode = location.getCode();
                LambdaQueryWrapper<TaskHeader> lambdaWrapper3 = Wrappers.lambdaQuery();
                lambdaWrapper3.eq(TaskHeader::getFromLocation, locationCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                        .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT,
                                QuantityConstant.TASK_TYPE_SORTINGSHIPMENT, QuantityConstant.TASK_TYPE_VIEW)
                        .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD);
                List<TaskHeader> taskHeader3 = taskHeaderService.list(lambdaWrapper3);
                if (taskHeader3 != null && taskHeader3.size() > 0) {
                    String insideNearCode = insideNear.getCode();
                    LambdaQueryWrapper<TaskHeader> lambdaWrapper4 = Wrappers.lambdaQuery();
                    lambdaWrapper4.eq(TaskHeader::getFromLocation, insideNearCode)
                            .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                            .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD);
                    List<TaskHeader> taskHeader4 = taskHeaderService.list(lambdaWrapper4);
                    if (taskHeader4 != null && taskHeader4.size() > 0) {
                        return AjaxResult.error("执行任务出库时,请先执行完旁边库位任务");
                    }
                }
            }
        }
        if (0 != preTaskNo) {
            wcsTask.setPreTaskNo(String.valueOf(preTaskNo));
        }
        if(location != null) {
            wcsTask.setStation(location.getRoadway());
            wcsTask.setDestination(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);

        //找到任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
        taskDetailLam.eq(TaskDetail::getTaskId,taskHeader.getId());
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLam);
        if(taskDetailList == null){
            return AjaxResult.error("没有子任务");
        }
        List<TaskDetails> taskDetails =new ArrayList<>();
        for(TaskDetail item : taskDetailList) {
            TaskDetails details = new TaskDetails();
            details.setMaterialCode(item.getMaterialCode());
            details.setMaterialName(item.getMaterialName());
            if(StringUtils.isEmpty(item.getMaterialUnit())){
                details.setUnit("PCS");
            }else {
                details.setUnit(item.getMaterialUnit());
            }
            details.setQty(item.getQty());
            details.setReferLineNo(item.getId().toString());
            taskDetails.add(details);
        }

        wcsTask.setTaskDetails(taskDetails);

        //4、发送数据
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_ASSIGN);
        wcsTask = switchTaskTypeToWcs(wcsTask);
        String JsonParam = JSON.toJSONString(wcsTask);
        System.out.println(JsonParam);
        String result = HttpUtils.bodypost(url, JsonParam);
        if(StringUtils.isEmpty(result)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if(ajaxResult.getCode()!= HttpConstant.OK) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
        return AjaxResult.success("");
    }

    // wcs任务类型 入库 = 100 , 出库 = 300,分拣出库 = 400 ,移库 = 800,换站=1000
    private WcsTask switchTaskTypeToWcs(WcsTask wcsTask) {
        int taskType = wcsTask.getTaskType().intValue();
        switch (taskType) {
            case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
            case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
                wcsTask.setTaskType(100);
                break;
            case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
            case QuantityConstant.TASK_TYPE_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;
    }

}