AgvTaskCSCreateService.java 7.44 KB
package com.huaheng.api.acs.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.acs.domain.AgvPort;
import com.huaheng.api.acs.domain.AgvTaskCS;
import com.huaheng.api.acs.domain.ToAgvTask;
import com.huaheng.api.acs.mapper.AgvTaskCsMapper;
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.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.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AgvTaskCSCreateService  {

    @Resource
    private AgvTaskCSService agvTaskCSService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private StationService stationService;

    /**
     * 机加立库创建agv任务
     * 查找taskheader表,JLK库区的未完成任务,根据Taskheader任务创建agv任务
     */
    public void createAgvTaskForJLK(){
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getWarehouseCode, QuantityConstant.WAREHOUSE_KS);
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getZoneCode, "JLK");
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD);
        taskHeaderLambdaQueryWrapper.apply("(pickingCartPos is not null and pickingCartPos != '') ");
//        taskHeaderLambdaQueryWrapper.in(TaskHeader::getInternalTaskType, QuantityConstant.TASK_INTENERTYPE_RECEIPT,QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        taskHeaderLambdaQueryWrapper.last("order by id asc");
        List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
        //查找机加库入库。出库任务
        for (TaskHeader taskHeader : taskHeaderList) {
            try {
               taskHeaderService.sendTaskToWcs(Convert.toIntArray(taskHeader.getId().toString()));
//                createAgvTaskForJLKTask(taskHeader);
            } catch(Exception e){

            }
        }
    }
    public void createAgvTaskForJLKTask(TaskHeader taskHeader ){
        if (StringUtils.isNotEmpty(taskHeader.getPickingCartPos())) {
            String[] pickingCartPos = taskHeader.getPickingCartPos().split(",");
            AgvTaskCS agvTask = new AgvTaskCS();
            switch (taskHeader.getTaskType()) {
                case QuantityConstant.TASK_TYPE_WHOLERECEIPT:
                case QuantityConstant.TASK_TYPE_EMPTYRECEIPT:
                    agvTask.setTaskType(QuantityConstant.AGV_TYPE_TAKE);
                    agvTask.setFromPort(taskHeader.getPickingCartPos());
                    agvTask.setToPort(taskHeader.getPort());
                    break;
                case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
                case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
//                    agvTask.setTaskType(QuantityConstant.AGV_TYPE_RELEASE);
//                    agvTask.setFromPort(taskHeader.getPort());
//                    agvTask.setToPort(taskHeader.getPickingCartPos());
                    break;
                case QuantityConstant.TASK_TYPE_OVER_STATION:
                    agvTask.setTaskType(QuantityConstant.AGV_TYPE_TAKE);
                    agvTask.setFromPort(taskHeader.getFromLocation());
                    agvTask.setToPort(taskHeader.getPort());
                    break;
                default:
                    return;
            }
            AgvTaskCS taskCS=agvTaskCSService.getAgvTaskByHeaderId(taskHeader,agvTask.getTaskType());
            if(taskCS!=null){
                //有agv任务则不重新生成
                return;
            }
            agvTask.setPriority(100);
            agvTask.setTaskHeaderId(taskHeader.getId());
            agvTask.setTaskHeaderType(taskHeader.getTaskType());
            agvTask.setInternalTaskType(taskHeader.getInternalTaskType());
            agvTask.setContainerCode(taskHeader.getContainerCode());

            Container container=containerService.getContainerByCode(taskHeader.getContainerCode(),taskHeader.getWarehouseCode());
            agvTask.setZoneCode(taskHeader.getZoneCode());
            agvTask.setNumber(container.getNumber());
            agvTask.setWarehouseCode(taskHeader.getWarehouseCode());
            agvTask.setStatus(QuantityConstant.AGV_TASK_STATUS_BUILD);
            agvTask.setCreatedTime(new Date());
            agvTask.setCreatedBy(taskHeader.getCreatedBy());
            if (!agvTaskCSService.save(agvTask)) {
                throw new ServiceException("保存AGV任务失败");
            }
        }
    }

    /**
     * 新建线程更改taskheader表
     * @param task
     */
    @Transactional(rollbackFor = Exception.class)
    public void autoSaveAgvTaskBySimpleAgvB(TaskHeader task){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("使用匿名内部类,实例Runnable接口作为构造参数");
                autoSaveAgvTaskBySimpleAgv(task);
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getId, task.getId());
                TaskHeader taskHeader1 = new TaskHeader();
                taskHeader1.setAutoCreateAgv(2);
                taskHeaderService.update(taskHeader1, taskHeaderLambdaQueryWrapper);
            }
        });
        thread.start();
    }
    /**
     * 长沙立库和昆山机加库出库任务,创建agv任务
     * @param taskHeader
     */
    public void autoSaveAgvTaskBySimpleAgv(TaskHeader taskHeader){
        TaskHeader task=taskHeaderService.getById(taskHeader.getId());
        if(task.getStatus()!=100){
            return;
        }
        switch (task.getTaskType()){
            case QuantityConstant.TASK_TYPE_WHOLESHIPMENT:
            case QuantityConstant.TASK_TYPE_EMPTYSHIPMENT:
                break;
            default:
                return;
        }
        if(StringUtils.isEmpty(task.getToPort())){
            return;
        }
        AgvTaskCS agvTask2=agvTaskCSService.saveAgvTaskBySimpleAgv(task,null);
    }

}