AcsCSService.java 15.1 KB
package com.huaheng.api.acs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.acs.domain.AcsWeightHeight;
import com.huaheng.api.acs.domain.AgvTaskCS;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.warecellAllocation.WarecellAllocationService;
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.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
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.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;

import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;

import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.List;

import static com.huaheng.common.constant.QuantityConstant.*;

/**
 * @ClassName AcsServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2019/12/2615:38
 */
@Service
public class AcsCSService {
    private static final Logger logger = LoggerFactory.getLogger(AcsCSService.class);
    @Resource
    private AgvTaskCSService agvTaskService;
    @Resource
    private AddressService addressService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private WarecellAllocationService warecellAllocationService;
    @Resource
    private ConfigService configService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private LocationService locationService;
    @Resource
    private AgvPortService agvPortService;



    /**
     * 更新AGV状态
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult notifyAGVTask(String taskNo, String carNo, int status, String updateBy) {
        AgvTaskCS agvTask = agvTaskService.getById(taskNo);
        if (agvTask == null) {
            return AjaxResult.error("没有找到对应AGV任务,任务号为" + taskNo);
        }
        if (agvTask.getStatus() < QuantityConstant.AGV_TASK_STATUS_RELEASE) {
//            return AjaxResult.success("该任务状态还未下发,任务号为" + taskNo);
        }
        if (StringUtils.isEmpty(agvTask.getFromPort())||StringUtils.isEmpty(agvTask.getToPort())) {
            return AjaxResult.success("该任务无agv起点和终点信息:id=" + taskNo);
        }
        if (agvTask.getStatus() == status) {
            return AjaxResult.success("该任务状态已更新,任务号为" + taskNo);
        }
        if(StringUtils.isNotNull(agvTask.getPreTaskNo())){
            AgvTaskCS agvTaskPre = agvTaskService.getById(agvTask.getPreTaskNo());
            if(agvTaskPre!=null&&agvTaskPre.getStatus()<100){
                return AjaxResult.success("该任务状态已更新,任务号为" + taskNo+"的前置任务号还未完成,请先完成出库任务");
            }
        }
        agvTask.setStatus(status);
        agvTask.setCarNo(carNo);
        agvTask.setUpdatedBy(updateBy);
        boolean result = agvTaskService.updateById(agvTask);

        if (!result) {
            return AjaxResult.error("更新任务信息失败 ");
        }

        //当status为100时,则完成任务
        //补充入库:先出库 agvtaskid2 状态400,后入库 agvtaskid 状态300
        //分拣出库:先出库 agvtaskid 状态400,后入库 agvtaskid2 状态300
        if (status == 100) {
            TaskHeader taskHeader = taskHeaderService.getOne(new LambdaQueryWrapper<TaskHeader>().eq(TaskHeader::getId, agvTask.getTaskHeaderId()));
            String[] weight = new String[]{"0"};
            if (taskHeader != null) {
                switch (taskHeader.getTaskType()) {
                    case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
                    case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
                    case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
                    case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
                        if(!agvTask.getTaskType().equals(AGV_TYPE_MOVE)){
                            return  taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                        }
                        break;
                    case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
                    case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT:
                    case QuantityConstant.TASK_TYPE_VIEW:
                        //agv入库任务时则完成任务
                        if (agvTask.getTaskType().equals(QuantityConstant.AGV_TYPE_TAKE)) {
                            return   taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                        }
                        break;
                    case QuantityConstant.TASK_TYPE_OVER_STATION:
                        if (agvTask.getTaskType().equals(AGV_TYPE_MOVE)) {
                            return taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        if(agvTask.getZoneCode().equals("LK")){
            if(status==100){
                agvPortService.updateStatus(agvTask.getToPort(), QuantityConstant.STATUS_POSITION_EMPTY);
            }
        }
        return AjaxResult.success("更新任务信息成功");
    }

    /**
     * 1、保存重量高度,查找任务,根据重量高度分配库位,任务库位
     * 2、入库更新任务主表站点
     * 3、更新agv任务表起点和终点
     *
     * @param acsWeightHeight
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult weightAndHeightByContainer(AcsWeightHeight acsWeightHeight) {
        //检验参数
        Container container = checkParams(acsWeightHeight);
        //根据托盘号查找未完成任务
        TaskHeader taskHeader  = getTaskHeader(acsWeightHeight);
        if(taskHeader==null){
            return AjaxResult.error("找不到该托盘的任务");
        }
        //更新agv起点终点
        AgvTaskCS agvTask=null;
        //补充入库:先出库 agvtaskid2 状态400,后入库 agvtaskid 状态300
        //分拣出库:先出库 agvtaskid 状态400,后入库 agvtaskid2 状态300
        switch (taskHeader.getTaskType()) {
            case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
            case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
                return AjaxResult.success(acsWeightHeight.getContainerCode() + ",该任务是出库任务,不需要请求库位");
            case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
            case QuantityConstant.TASK_TYPE_SORTINGSHIPMENT:
            case QuantityConstant.TASK_TYPE_VIEW:
                //需要回库的任务,判断是否完成agv出库任务
                AgvTaskCS  agvTaskShip = agvTaskService.getAgvTaskByHeaderId(taskHeader, AGV_TYPE_RELEASE);
                if(agvTaskShip!=null&&agvTaskShip.getStatus()<100){
                    return AjaxResult.success(acsWeightHeight.getContainerCode() + ",该任务出库任务还未完成");
                }
                break;
        }
        agvTask = agvTaskService.getAgvTaskByHeaderId(taskHeader, AGV_TYPE_TAKE);
        if(agvTask==null){
            return AjaxResult.error( "找不到agv入库任务");
        }
        if (agvTask.getStatus() >= QuantityConstant.AGV_TASK_STATUS_RELEASE) {
            return AjaxResult.success(agvTask.getId() + "agv回库任务已下发,任务已有起点中终点");
        }
        switch (taskHeader.getTaskType()) {
            case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
            case TASK_TYPE_SORTINGSHIPMENT:
            case TASK_TYPE_VIEW:
                //整盘入库、补充入库、空托盘入库,没有库位可分配,直接完成任务
                TaskHeader taskHeader1 = new TaskHeader();
                taskHeader1.setId(taskHeader.getId());
                taskHeader1.setToLocation(taskHeader.getFromLocation());
                taskHeaderService.updateById(taskHeader1);
                if (agvTask != null) {
                    agvTask.setToPort(taskHeader.getToLocation());
                    agvTask.setFromPort(acsWeightHeight.getFormPort());
                    agvTaskService.updateById(agvTask);
                }
                return AjaxResult.success().setData(taskHeader.getToLocation());
            default:
                break;
        }

        container.setWeight(acsWeightHeight.getWeight());
        container.setHigh(acsWeightHeight.getHeight());
        containerService.updateById(container);
        //分配库位,更新任务表库位,巷道信息
        taskHeader = updateTaskHeaderLocation(taskHeader, acsWeightHeight);
        //更新agv起点终点
        if (agvTask != null) {
            agvTask.setToPort(taskHeader.getToLocation());
            agvTask.setFromPort(acsWeightHeight.getFormPort());
            agvTaskService.updateById(agvTask);
        }
        return AjaxResult.success().setData(taskHeader.getToLocation());
    }

    /**
     * 根据托盘号查找未完成任务
     */
    public TaskHeader getTaskHeader(AcsWeightHeight acsWeightHeight) {
        List<TaskHeader> taskHeaders = taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>()
                .eq(TaskHeader::getContainerCode, acsWeightHeight.getContainerCode())
                //生成任务
                .lt(TaskHeader::getStatus, TASK_STATUS_COMPLETED)
                //整盘入库、补充入库、出库查询
//                .in(TaskHeader::getTaskType, TASK_TYPE_WHOLERECEIPT, TASK_TYPE_SUPPLEMENTRECEIPT, TASK_TYPE_VIEW, TASK_TYPE_SORTINGSHIPMENT)
                .orderByDesc(TaskHeader::getId));

        if (taskHeaders == null || taskHeaders.size() == 0){
            throw new ServiceException("传递AGV托盘重量高度接口," + acsWeightHeight.getContainerCode() + "该托盘在任务中找不到,请先生成任务");
        }
        TaskHeader taskHeader = taskHeaders.get(0);
        //更新任务主表站点
        taskHeader.setPort(acsWeightHeight.getFormPort());
        taskHeader.setWeight(acsWeightHeight.getWeight().toString());
        taskHeaderService.updateById(taskHeader);
        return taskHeader;
    }

    /**
     * 分配库位,更新任务表库位,巷道信息
     */
    public TaskHeader updateTaskHeaderLocation(TaskHeader taskHeader, AcsWeightHeight acsWeightHeight) {
//        if (taskHeader.getTaskType() == QuantityConstant.TASK_TYPE_WHOLERECEIPT) {
        WcsTask wcsTask = createWcsTask(taskHeader.getId().toString(), acsWeightHeight.getHeight(), acsWeightHeight.getWeight());
        AjaxResult ajaxResult = warecellAllocationService.warecellAllocation(wcsTask);
        if (ajaxResult.hasErr()) {
            throw new ServiceException("传递AGV托盘重量高度,请求库位时:" + ajaxResult.getMsg());
        }
        WcsTask wcsTask1 = (WcsTask) ajaxResult.getData();
        taskHeader.setRoadway(wcsTask1.getRoadWay());
        taskHeader.setToLocation(wcsTask1.getToLocationCode());
//        } else {
//            Location location = locationService.getLocationByCode(taskHeader.getToLocation());
//            taskHeader.setRoadway(location.getRoadway());
//        }
        return taskHeader;
    }

    /**
     * 检验参数
     */
    public Container checkParams(AcsWeightHeight acsWeightHeight) {
        if (acsWeightHeight == null) {
            throw new ServiceException("传递AGV托盘重量高度,对象为空");
        }
        if (StringUtils.isEmpty(acsWeightHeight.getWarehouseCode())) {
            throw new ServiceException("传递AGV托盘重量高度,仓库为空");
        }
        BigDecimal weight = acsWeightHeight.getWeight();
        if (weight == null) {
            throw new ServiceException("传递AGV托盘重量高度,重量为空");
        }
//        String weight_config = configService.getKey(QuantityConstant.CONFIG_WEGHT,acsWeightHeight.getWarehouseCode());//容器重量
//        BigDecimal weight_bigdecimal = new BigDecimal(weight_config);
//        if (weight.compareTo(weight_bigdecimal) > 0) {
//            throw new ServiceException("传递AGV托盘重量高度," + weight + "重量超过单个托盘限定重量");
//        }
        BigDecimal height = acsWeightHeight.getHeight();
        if (height == null) {
            throw new ServiceException("传递AGV托盘重量高度,高度为空");
        }
        /*String height_config = configService.getKey(QuantityConstant.CONFIG_HEIGHT);//容器高度
        BigDecimal height_bigdecimal=new BigDecimal(height_config);
        if(height.compareTo(height_bigdecimal)>0){
            throw  new ServiceException("传递AGV托盘重量高度,"+height+"高度超过单个托盘限定高度");
        }*/
        String containerCode = acsWeightHeight.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            throw new ServiceException("传递AGV托盘重量高度,托盘号为空");
        }
        String fromPort = acsWeightHeight.getFormPort();
        if (StringUtils.isEmpty(fromPort)) {
            throw new ServiceException("传递AGV托盘重量高度,起点为空");
        }
        //log.info(containerCode);
        Container container = containerService.getContainerByCode(containerCode, acsWeightHeight.getWarehouseCode());
        //log.info(container.toString());
        if (container == null) {
            throw new ServiceException("传递AGV托盘重量高度," + containerCode + "容器在容器管理中找不到");
        }
        return container;
    }

    public WcsTask createWcsTask(String taskNo, BigDecimal height, BigDecimal weight) {
        WcsTask wcsTask = new WcsTask();
        wcsTask.setTaskNo(taskNo);
        wcsTask.setWarehouseCode("CS0001");
        wcsTask.setArea("9");
        wcsTask.setRoadWay("1");
        wcsTask.setLength("100");
        //String height_config = configService.getKey(QuantityConstant.CONFIG_AGV_HEIGHT);//容器重量
        //判断高低库位,1.4以下低库位,1.4-1。5高库位
        /*if(height.compareTo(new BigDecimal(height_config))>=0){
            wcsTask.setHeight("2");
        }else{
            wcsTask.setHeight("1");
        }*/
        wcsTask.setHeight(String.valueOf(height.intValue()));
        wcsTask.setWidth("1000");
        wcsTask.setWeight(weight.toString());
        return wcsTask;
    }


}