AgvTaskCSController.java 17.3 KB
package com.huaheng.api.acs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaheng.api.acs.domain.AcsStatus;
import com.huaheng.api.acs.domain.AcsWeightHeight;
import com.huaheng.api.acs.domain.AgvTaskCS;
import com.huaheng.api.acs.service.AcsCSService;
import com.huaheng.api.acs.service.AgvTaskCSService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.page.PageDomain;
import com.huaheng.framework.web.page.TableDataInfo;
import com.huaheng.framework.web.page.TableSupport;
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 io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dell
 */
@Controller("AgvTaskController1")
@RequestMapping("/agv")
public class AgvTaskCSController extends BaseController {

    private String prefix = "task/taskHeader";

    @Resource
    private AgvTaskCSService agvTaskService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private AcsCSService acsCSService;
    @Resource
    private LocationService locationService;
    @Resource
    private StationService stationService;



    @RequiresPermissions("task:taskHeader:agvCsView")
    @GetMapping()
    public String taskHeader(HttpServletRequest request, ModelMap mmap) {
        String InternalTaskType = request.getParameter("InternalTaskType");
        mmap.put("InternalTaskType", InternalTaskType);
        return prefix + "/agvHeader";
    }

    @GetMapping("/lookAgvTask/{taskNo}")
    public String taskDetail(@PathVariable("taskNo") String taskNo, ModelMap mmap) {
        mmap.put("taskNo", taskNo);
        return prefix + "/agvHeader";
    }

    @RequiresPermissions("task:taskHeader:agvCsView")
    @GetMapping("/setPort/{taskId}")
    public String setPort(@PathVariable("taskId") Integer taskId,ModelMap mmap) {
        TaskHeader taskHeader=taskHeaderService.getById(taskId);
        mmap.put("taskHeader", taskHeader);
        String containerCode=taskHeader.getContainerCode();
        List<Location> list=locationService.getLocationListByContainerCode(taskHeader.getWarehouseCode(),containerCode,taskHeader.getToLocation());
        mmap.put("locationList", list);
        List<Station> stationList=stationService.getStationByContainers(taskHeader.getContainerCode(),"3",taskHeader.getWarehouseCode());
        mmap.put("stationList", stationList);
        return prefix + "/agvSetPort";
    }

    @RequiresPermissions("task:taskHeader:list")
    @Log(title = "任务-上架任务", operating = "查看任务列表", action = BusinessType.GRANT)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgvTaskCS agvTask, @ApiParam(name = "createdBegin", value = "起止时间") String createdBegin,
                              @ApiParam(name = "createdEnd", value = "结束时间") String createdEnd,
                              @ApiParam(name = "taskComplete", value = "完成") String taskComplete) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String isAsc = pageDomain.getIsAsc();
        String orderByColumn = pageDomain.getOrderByColumn();
        LambdaQueryWrapper<AgvTaskCS> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AgvTaskCS::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(StringUtils.isNotNull(agvTask.getId()), AgvTaskCS::getId, agvTask.getId())
                .eq(StringUtils.isNotEmpty(agvTask.getContainerCode()), AgvTaskCS::getContainerCode, agvTask.getContainerCode())
                .eq(StringUtils.isNotNull(agvTask.getTaskHeaderId()), AgvTaskCS::getTaskHeaderId, agvTask.getTaskHeaderId())
                .eq(StringUtils.isNotEmpty(agvTask.getFromPort()), AgvTaskCS::getFromPort, agvTask.getFromPort())
                .eq(StringUtils.isNotEmpty(agvTask.getToPort()), AgvTaskCS::getToPort, agvTask.getToPort())
                .eq(StringUtils.isNotNull(agvTask.getPriority()), AgvTaskCS::getPriority, agvTask.getPriority())
                .eq(StringUtils.isNotEmpty(agvTask.getCarNo()), AgvTaskCS::getCarNo, agvTask.getCarNo())
                .eq(StringUtils.isNotNull(agvTask.getStatus()), AgvTaskCS::getStatus, agvTask.getStatus())
                .ge(StringUtils.isNotEmpty(createdBegin), AgvTaskCS::getCreatedTime, createdBegin)
                .le(StringUtils.isNotEmpty(createdEnd), AgvTaskCS::getCreatedTime, createdEnd)
                .le(StringUtils.isNotEmpty(taskComplete), AgvTaskCS::getStatus, taskComplete)
                .last(!StringUtils.isAnyEmpty(isAsc, orderByColumn), "order by" + " " + orderByColumn + " " + isAsc)
                .last(StringUtils.isAnyEmpty(isAsc, orderByColumn), "order by id desc");
        /**
         * 使用分页查询
         */
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            Page<AgvTaskCS> page = new Page<>(pageNum, pageSize);
            IPage<AgvTaskCS> iPage = agvTaskService.page(page, lambdaQueryWrapper);
            List<AgvTaskCS> iPages = iPage.getRecords();
            return getMpDataTable(iPages, iPage.getTotal());
        } else {
            List<AgvTaskCS> list = agvTaskService.list(lambdaQueryWrapper);
            return getDataTable(list);
        }
    }


    @GetMapping("/createAGVTask")
    public String create() {
        return prefix + "/agvCreate";
    }

    @ApiOperation(value = "AGV创建任务", httpMethod = "POST")
    @PostMapping("/createAGVTask")
    @ResponseBody
    public AjaxResult createAGVTask(String priority, String sourceLocation, String destinationLocation, String containerCode, String taskType) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AgvTaskCS agvTask = new AgvTaskCS();
                agvTask.setFromPort(sourceLocation);
                agvTask.setToPort(destinationLocation);
                agvTask.setContainerCode(containerCode);
                agvTask.setWarehouseCode("CS0001");
                return agvTaskService.createAGVTask(agvTask);
            }
        });
        return ajaxResult;
    }

    @RequiresPermissions("task:agvtask:createAGVTaskJLK")
    @GetMapping("/createAGVTaskJLK")
    public String createAGVTaskJLK() {
        return prefix + "/agvCreateJLK";
    }
    @RequiresPermissions("task:agvtask:createAGVTaskJLK")
    @ApiOperation(value = "AGV创建任务", httpMethod = "POST")
    @PostMapping("/createAGVTaskJLK")
    @ResponseBody
    public AjaxResult createAGVTaskJLK( String sourceLocation, String destinationLocation, String containerCode) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AgvTaskCS agvTask = new AgvTaskCS();
                agvTask.setFromPort(sourceLocation);
                agvTask.setToPort(destinationLocation);
                agvTask.setContainerCode(containerCode);
                return agvTaskService.createAGVTaskJLK(agvTask);
            }
        });
        return ajaxResult;
    }



    @Log(title = "任务-任务管理", operating = "下发AGV任务", action = BusinessType.UPDATE)
    @PostMapping("/execute")
    @ResponseBody
    public AjaxResult execute(String taskId) {
        if (StringUtils.isEmpty(taskId)) {
            return AjaxResult.error("taskId不能为空");
        }
        AjaxResult<AgvTaskCS> ajaxResult = agvTaskService.sendTaskToAGV(Convert.toIntArray(taskId),false);
        return ajaxResult;
    }





    @GetMapping("/editagv/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        AgvTaskCS agvTask = agvTaskService.getById(id);
        mmap.put("agvTask", agvTask);
        return prefix + "/editagv";
    }

    @RequiresPermissions("task:taskHeader:edit")
    @PostMapping("/editagv")
    @ResponseBody
    @Log(title = "任务-任务管理", operating = "编辑agv任务", action = BusinessType.UPDATE)
    public AjaxResult edit(AgvTaskCS agvTask) {
        AgvTaskCS agvTaskResult = agvTaskService.getById(agvTask.getId());
        if (agvTaskResult.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
            return AjaxResult.error("任务已经下发了,不允许修改");
        }
        agvTask.setUpdated(new Date());
//        PointPosition pointPosition = pointPositionService.getOne(new LambdaQueryWrapper<PointPosition>().eq(PointPosition::getPositionName, agvTask.getToPort()));
//        if (pointPosition == null) {
//            return AjaxResult.error("目标点位不存在");
//        }
//        if (pointPosition != null && StringUtils.isNotEmpty(pointPosition.getContainerCode())) {
//            return AjaxResult.error("目标点位有托盘,请选择其他点位");
//        }
        AgvTaskCS one = agvTaskService.getOne(new LambdaQueryWrapper<AgvTaskCS>().eq(AgvTaskCS::getToPort, agvTask.getToPort()).lt(AgvTaskCS::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        if (one != null) {
            return AjaxResult.error("目标点位存在未完成的任务");
        }
        return toAjax(agvTaskService.updateById(agvTask));
    }


    @Log(title = "任务-任务管理", operating = "取消AGV任务", action = BusinessType.DELETE)
    @PostMapping("/cancelAGVTask")
    @ResponseBody
    @RequiresPermissions("task:agvTask:remove")
    @ApiLogger(apiName = "删除AGV任务")
    public AjaxResult cancelAGVTask(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("taskId不能为空");
        }
        Integer[] idList = Convert.toIntArray(ids);
        for (int id : idList) {
            AgvTaskCS agvTask = agvTaskService.getById(id);
            if (agvTask.getStatus().intValue() <= QuantityConstant.TASK_STATUS_BUILD) {
                continue;
            }
            if(agvTask.getStatus().intValue()>=QuantityConstant.TASK_STATUS_RELEASE){
                return AjaxResult.error("任务已经下发,不能删除");
            }
            if(StringUtils.isNotNull(agvTask.getPreTaskNo())){
                AgvTaskCS agvTaskPre = agvTaskService.getById(agvTask.getPreTaskNo());
                if(agvTaskPre!=null){
                    return AjaxResult.error("AGV前置任务还存在,不能删除");
                }
            }
            if (agvTask.getStatus().intValue() == QuantityConstant.TASK_STATUS_COMPLETED) {
                return AjaxResult.error("任务已经完成");
            }
            String containerCode = agvTask.getContainerCode();
            if (StringUtils.isEmpty(containerCode)) {
                return AjaxResult.error("空托盘");
            }
            Container container = containerService.getContainerByCode(
                    containerCode, agvTask.getWarehouseCode());
            LambdaQueryWrapper<ContainerType> containerTypeLambdaQueryWrapper = Wrappers.lambdaQuery();
            containerTypeLambdaQueryWrapper.eq(ContainerType::getCode, container.getContainerType());
            ContainerType containerType = containerTypeService.getContainerTypeByCode(
                    container.getContainerType(), container.getWarehouseCode());
            AjaxResult ajaxResult = agvTaskService.cancelTask(id,
                    agvTask.getWarehouseCode(), containerType.getArea());
            if (ajaxResult.hasErr()) {
                return ajaxResult;
            }
        }
        return agvTaskService.cancelAGVTask(Convert.toIntArray(ids));
    }

    @Log(title = "任务-任务管理", operating = "AGV任务状态通知", action = BusinessType.UPDATE)
    @PostMapping("/notifyAGVTask")
    @ResponseBody
    @ApiLogger(apiName = "更新AGV任务状态", from = "AGV")
    public AjaxResult notifyAGVTask(@RequestBody AcsStatus acsStatus) {
        if(acsStatus == null) {
            return AjaxResult.error("入库组盘信息为空");
        }
        String taskNo = acsStatus.getTaskNo();
        String carNo = acsStatus.getCarNo();
        int status = acsStatus.getStatus();
        String updateBy = acsStatus.getUpdateBy();
        updateBy = "acs";
        if(StringUtils.isEmpty(taskNo) ) {
            return AjaxResult.error("更新AGV状态,任务号为空");
        }
        /*if(StringUtils.isEmpty(carNo)) {
            return AjaxResult.error("更新AGV状态,车辆编号为空");
        }*/
        if(StringUtils.isEmpty(acsStatus.getWarehouseCode())){
//            return AjaxResult.error("更新AGV状态,仓库为空");
        }
        if(status == 0) {
            return AjaxResult.error("更新AGV状态,状态信息为空");
        }
        if(StringUtils.isEmpty(updateBy)) {
            return AjaxResult.error("更新AGV状态,更新者信息为空");
        }
        String finalUpdateBy = updateBy;
        AjaxResult ajaxResult = handleMultiProcessV1("notifyAGVTask",new BaseController.MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = acsCSService.notifyAGVTask(taskNo, carNo, status, finalUpdateBy);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    @Log(title = "任务-任务管理", operating = "AGV任务完成", action = BusinessType.UPDATE)
    @PostMapping("/completeTaskByWMS")
    @ResponseBody
    @ApiLogger(apiName = "AGV任务完成", from = "WMSS")
    public AjaxResult completeTaskByWMS(String taskId) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                Map map=new HashMap();
                map.put("taskNo",taskId);
                map.put("status","100");
                AjaxResult ajaxResult = acsCSService.notifyAGVTask(taskId, "", 100, "qq");
                return ajaxResult;
            }
        });

        return ajaxResult;
    }
    @PostMapping("/weightAndHeightByContainer")
    @ApiOperation("agv请求库位")
    @ResponseBody
    @ApiLogger(apiName = "agv请求库位", from="acs")
    public AjaxResult weightAndHeightByContainer(@RequestBody AcsWeightHeight acsWeightHeight) {

        AjaxResult ajaxResult = handleMultiProcessV1("weightAndHeightByContainer",new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = acsCSService.weightAndHeightByContainer(acsWeightHeight);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    @ApiOperation(value = "AGV设置站台库位", httpMethod = "POST")
    @PostMapping("/setAgvPort")
    @ResponseBody
    public AjaxResult setAgvPort(TaskHeader taskHeader) {
        AjaxResult ajaxResult = handleMultiProcessV1("setAgvPort",new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                return agvTaskService.setAgvPort(taskHeader);
            }
        });
        return ajaxResult;
    }


    @Log(title = "任务-任务管理", operating = "下发AGV任务", action = BusinessType.UPDATE)
    @PostMapping("/executeByContainerCode")
    @ResponseBody
    public AjaxResult executeByContainerCode(@RequestBody AcsWeightHeight acsWeightHeight) {
        if (StringUtils.isNull(acsWeightHeight)) {
            return AjaxResult.error("数据不能为空");
        }
        String containerCode=acsWeightHeight.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            return AjaxResult.error("容器号不能为空");
        }
        AjaxResult ajaxResult = handleMultiProcessV1("executeByContainerCode",new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                return agvTaskService.excuteAgvContainer(containerCode);
            }
        });
        return ajaxResult;
    }
}