AgvTaskController.java 15.6 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.AgvTask;
import com.huaheng.api.acs.service.AcsService;
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.agv.domain.AgvPort;
import com.huaheng.pc.agv.domain.AgvTaskDetail;
import com.huaheng.pc.agv.service.IAgvTaskDetailService;
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.pointPosition.domain.PointPosition;
import com.huaheng.pc.config.pointPosition.service.PointPositionService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
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.List;
import java.util.Map;

/**
 * @author dell
 */
@Controller("AgvTaskController1")
@RequestMapping("/agv")
public class AgvTaskController extends BaseController {
    private String prefix = "task/taskHeader";

    @Resource
    private AcsService acsService;

    @Resource
    private ContainerService containerService;

    @Resource
    private ContainerTypeService containerTypeService;

    @Resource
    private PointPositionService pointPositionService;

    @Resource
    private IAgvTaskDetailService agvTaskDetailService;

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

    @RequiresPermissions("task:taskHeader:view")
    @GetMapping("/detail")
    public String taskDetail(HttpServletRequest request, ModelMap mmap) {
        String agvTaskId = request.getParameter("agvTaskId");
        mmap.put("agvTask", acsService.getById(agvTaskId));
        return prefix + "/agvDetail";
    }

    @RequiresPermissions("task:taskHeader:view")
    @PostMapping("/detail/list")
    @ResponseBody
    public AjaxResult listTaskDetail(int agvTaskId) {
        List<AgvTaskDetail> list = agvTaskDetailService.getListByAgvTaskId(agvTaskId);
        return AjaxResult.success(list);
    }

    @RequiresPermissions("task:taskHeader:list")
    @Log(title = "任务-上架任务", operating = "查看任务列表", action = BusinessType.GRANT)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgvTask agvTask, @ApiParam(name = "createdBegin", value = "起止时间") String createdBegin,
        @ApiParam(name = "createdEnd", value = "结束时间") String createdEnd, @ApiParam(name = "taskComplete", value = "完成") String taskComplete,
        @ApiParam(name = "createdBegin2", value = "起止时间") String createdBegin2, @ApiParam(name = "createdEnd2", value = "结束时间") String createdEnd2) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String isAsc = pageDomain.getIsAsc();
        String orderByColumn = pageDomain.getOrderByColumn();
        LambdaQueryWrapper<AgvTask> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AgvTask::getWarehouseCode, ShiroUtils.getWarehouseCode()).eq(StringUtils.isNotNull(agvTask.getId()), AgvTask::getId, agvTask.getId())
            .eq(StringUtils.isNotEmpty(agvTask.getContainerCode()), AgvTask::getContainerCode, agvTask.getContainerCode())
            .eq(StringUtils.isNotEmpty(agvTask.getFromPort()), AgvTask::getFromPort, agvTask.getFromPort())
            .eq(StringUtils.isNotEmpty(agvTask.getToPort()), AgvTask::getToPort, agvTask.getToPort())
            .eq(StringUtils.isNotNull(agvTask.getPriority()), AgvTask::getPriority, agvTask.getPriority())
            .eq(StringUtils.isNotEmpty(agvTask.getCarNo()), AgvTask::getCarNo, agvTask.getCarNo())
            .ge(StringUtils.isNotEmpty(createdBegin), AgvTask::getCreatedTime, createdBegin)
            .le(StringUtils.isNotEmpty(createdEnd), AgvTask::getCreatedTime, createdEnd)
            .ge(StringUtils.isNotEmpty(createdBegin2), AgvTask::getCompleteTime, createdBegin2)
            .le(StringUtils.isNotEmpty(createdEnd2), AgvTask::getCompleteTime, createdEnd2)
            .le(StringUtils.isNotEmpty(taskComplete), AgvTask::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<AgvTask> page = new Page<>(pageNum, pageSize);
            IPage<AgvTask> iPage = acsService.page(page, lambdaQueryWrapper);
            List<AgvTask> iPages = iPage.getRecords();
            return getMpDataTable(iPages, iPage.getTotal());
        } else {
            List<AgvTask> list = acsService.list(lambdaQueryWrapper);
            return getDataTable(list);
        }
    }

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

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

    @ApiOperation(value = "AGV创建任务-电脑", httpMethod = "POST")
    @PostMapping("/createAGVTask2")
    @ResponseBody
    @ApiLogger(from = "平板", apiName = "创建agv任务-电脑")
    public AjaxResult createAGVTask2(String sourceLocation, String destinationLocation, String containerCode, String preTaskNo) {
        AgvTask agvTask = new AgvTask();
        agvTask.setFromPort(sourceLocation);
        agvTask.setToPort(destinationLocation);
        agvTask.setContainerCode(containerCode);
        agvTask.setPreTaskNo(preTaskNo);
        agvTask.setWarehouseCode("CS0001");
        return acsService.createAGVTask2(agvTask);
    }

    @ApiOperation(value = "AGV叫空托", httpMethod = "POST")
    @PostMapping("/getEmptyContainer")
    @ResponseBody
    public AjaxResult getEmptyContainer(String destinationLocation, String type) {
        return acsService.getEmptyContainer(destinationLocation, type);
    }

    @ApiOperation(value = "AGV空托叫回", httpMethod = "POST")
    @PostMapping("/emptyContainer")
    @ResponseBody
    @ApiLogger(apiName = "MES空托叫回")
    public AjaxResult emptyContainer(@RequestBody AgvTask agvTask) {
        if (StringUtils.isEmpty(agvTask.getFromPort())) {
            return AjaxResult.error("起始点位为空");
        }
        if (StringUtils.isEmpty(agvTask.getContainerCode())) {
            return AjaxResult.error("托盘为空");
        }
        agvTask.setPlatform("MES");
        return acsService.emptyContainer(agvTask);
    }

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

    @Log(title = "回传MES异常", operating = "回传MES异常", action = BusinessType.UPDATE)
    @PostMapping("/huichuan")
    @ResponseBody
    public AjaxResult huichuan(String taskId) throws Exception {
        if (StringUtils.isEmpty(taskId)) {
            return AjaxResult.error("taskId不能为空");
        }
        AjaxResult<AgvTask> ajaxResult = acsService.huichuanMesError(Convert.toIntArray(taskId));
        return ajaxResult;
    }

    @GetMapping("/editagv/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        AgvTask agvTask = acsService.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(AgvTask agvTask) {
        AgvTask agvTaskResult = acsService.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("目标点位有托盘,请选择其他点位");
        }
        AgvTask one = acsService
            .getOne(new LambdaQueryWrapper<AgvTask>().eq(AgvTask::getToPort, agvTask.getToPort()).lt(AgvTask::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        if (one != null) {
            return AjaxResult.error("目标点位存在未完成的任务");
        }
        return toAjax(acsService.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) {
            AgvTask agvTask = acsService.getById(id);
            if (agvTask.getStatus().intValue() <= QuantityConstant.TASK_STATUS_BUILD) {
                continue;
            }
//            if(agvTask.getStatus().intValue()>QuantityConstant.TASK_STATUS_RELEASE){
//                return AjaxResult.error("任务已经下发,不能删除");
//            }
            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 = acsService.cancelTask(id, agvTask.getWarehouseCode(), containerType.getArea());
            if (ajaxResult.hasErr()) {
                return ajaxResult;
            }
        }
        return acsService.cancelAGVTask(Convert.toIntArray(ids));
    }

    @Log(title = "任务-任务管理", operating = "下发AGV任务", action = BusinessType.DELETE)
    @PostMapping("/updateAGVTask")
    @ResponseBody
    public AjaxResult updateAGVTask(String taskId, Integer priority) {
        if (StringUtils.isEmpty(taskId)) {
            return AjaxResult.error("taskId不能为空");
        }
        Integer id = Integer.valueOf(taskId);
        AgvTask agvTask = acsService.getById(id);
        if (agvTask.getStatus().intValue() > QuantityConstant.TASK_STATUS_BUILD) {
            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 = acsService.updateTask(id, agvTask.getWarehouseCode(), priority);
            if (ajaxResult.hasErr()) {
                return ajaxResult;
            }
        }
        return acsService.updateAGVTask(taskId, priority);
    }

    @Log(title = "任务-任务管理", operating = "更新AGV点位信息", action = BusinessType.UPDATE)
    @PostMapping("/notifyAGVPort")
    @ResponseBody
    public AjaxResult notifyAGVPort(@RequestBody Map map) {
        AjaxResult ajaxResult = acsService.notifyAGVPort(map);
        return ajaxResult;
    }

    @Log(title = "任务-任务管理", operating = "AGV任务状态通知", action = BusinessType.UPDATE)
    @PostMapping("/notifyAGVTask")
    @ResponseBody
    @ApiLogger(apiName = "更新AGV任务状态", from = "AGV")
    public AjaxResult notifyAGVTask(@RequestBody Map map) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = acsService.notifyAGVTask(map);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    @Log(title = "任务-任务管理", operating = "AGV完成任务", action = BusinessType.UPDATE)
    @PostMapping("/completeTaskByWMS")
    @ResponseBody
    public AjaxResult completeTaskByWMS(String taskId) {
        AjaxResult ajaxResult = acsService.completeTaskByWMS(taskId);
        return ajaxResult;
    }
}