EmptyContainerController.java 32.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
package com.huaheng.api.wcs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.acs.service.AcsService;
import com.huaheng.api.wcs.domain.ManyEmptyDomain;
import com.huaheng.api.wcs.domain.WarecellLocationDomain;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.containerGroup.IContainerGroupService;
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
import com.huaheng.api.wcs.service.taskInfo.StationPallet;
import com.huaheng.api.wcs.service.taskInfo.TaskInfoService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.constant.SendTypeConstants;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.SendNoticeUtils;
import com.huaheng.common.utils.StringUtils;
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.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.service.ContainerTypeService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationHigh.domain.LocationHigh;
import com.huaheng.pc.config.locationHigh.service.LocationHighService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.sap.domain.Zarsh;
import com.huaheng.pc.sap.service.ZarshService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.system.notice.domain.SysNotice;
import com.huaheng.pc.system.notice.service.SysNoticeService;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskSelectService;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/API/WMS/v2")
public class EmptyContainerController extends BaseController {

    @Resource
    private WorkTaskService workTaskService;
    @Resource
    private LocationAllocationService allocationService;
    @Resource
    private ConfigService configService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationService locationService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private LocationHighService locationHighService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Autowired
    private AddressService addressService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskAssignService taskAssignService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private IDictDataService iDictDataService;
    @Resource
    private StationService stationService;
    @Resource
    private AcsService acsService;
    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private SysNoticeService sysNoticeService;
    @Resource
    private ZarshService zarshService;
    @Resource
    private AgvTaskSelectService agvTaskSelectService;
    @Resource
    private TaskInfoService taskInfoService;
    @Resource
    private IContainerGroupService containerGroupService;

    /**
     * 生成空托盘出库任务
     * return : 0等待 1入库
     * @return
     */
    @PostMapping("/reserveContainer")
    @ResponseBody
    public AjaxResult reserveContainer(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        String containerCode = manyEmptyDomain.getContainerCode();
        String port = manyEmptyDomain.getPort();
        if(StringUtils.isEmpty(containerCode)){
            return AjaxResult.error("容器号不能为空!");
        }
        AjaxResult ajaxResult = handleMultiProcess("reserveContainer", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                return isReserveContainer(containerCode,port);
            }
        });
        return ajaxResult;
    }

    public AjaxResult isReserveContainer(String containerCode,String port){
        //查询该托盘是否在此站台生产入库任务 如果生成了入库任务则回库
        LambdaQueryWrapper<TaskHeader> taskHeaderQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderQueryWrapper
                .eq(TaskHeader::getInternalTaskType,QuantityConstant.TASK_INTENERTYPE_RECEIPT)
                .eq(TaskHeader::getContainerCode,containerCode)
                .eq(TaskHeader::getPort,port)
                .lt(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
                .last("LIMIT 1");
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderQueryWrapper);
        if(taskHeader != null){
            return AjaxResult.success("回库");
        }

        LambdaQueryWrapper<AgvTask> agvTaskQuery = Wrappers.lambdaQuery();
        agvTaskQuery.eq(AgvTask::getPalletNo,containerCode)
                .eq(AgvTask::getState,10)
                .eq(AgvTask::getToPoint,port)
                .gt(AgvTask::getTaskId,0)
                .eq(AgvTask::getZoneCode,"AX")
                .orderByDesc(AgvTask::getTaskNo)
                .last("LIMIT 1");
        AgvTask agvTask1 = agvTaskService.getOne(agvTaskQuery);
        if(agvTask1 != null){
            return AjaxResult.error("不能回库");
        }

        LambdaQueryWrapper<AgvTask> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AgvTask::getTaskId,0)
                .eq(AgvTask::getState,10)
                .eq(AgvTask::getToPoint,port)
                .eq(AgvTask::getZoneCode,"AX")
                .orderByAsc(AgvTask::getTaskNo)
                .last("LIMIT 1");
        AgvTask agvTask = agvTaskService.getOne(queryWrapper);
        if(agvTask == null){
            return AjaxResult.success("回库");
        }
        LambdaUpdateWrapper<AgvTask> agvTaskUpdateWrapper = Wrappers.lambdaUpdate();
        agvTaskUpdateWrapper.eq(AgvTask::getTaskNo,agvTask.getTaskNo())
                .set(AgvTask::getPalletNo,containerCode)
                .set(AgvTask::getTaskId,agvTask.getTaskNo());
        if(!agvTaskService.update(agvTaskUpdateWrapper)){
            return AjaxResult.error("保留容器失败!!");
        }
        return AjaxResult.error("不能回库");
    }

    /**
     * 生成空托盘出库任务
     *
     * @return
     */
    @PostMapping("/emptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托盘出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "emptyOut", from = "ROBOT")
    public AjaxResult emptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("emptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult =
                        inventoryHeaderService.createEmptyOut(manyEmptyDomain.getContainerCode(), null, manyEmptyDomain.getPort(), QuantityConstant.WAREHOUSECODE);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘入库任务
     *
     * @return
     */
    @PostMapping("/emptyIn")
    @Log(title = "任务-任务管理", operating = "生成空托盘入库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "WCS空托入库", from = "WCS")
    public AjaxResult emptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("emptyIn", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult =
                        inventoryHeaderService.createEmptyIn(manyEmptyDomain.getContainerCode(), null, manyEmptyDomain.getArea(), manyEmptyDomain.getPort());
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘组入库任务
     *
     * @return
     */
    @PostMapping("/manyEmptyIn")
    @Log(title = "任务-任务管理", operating = "生成空托盘组入库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "manyEmptyIn", from = "ROBOT")
    public AjaxResult manyEmptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("manyEmptyIn", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerManyEmptyIn(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘组出库任务
     *
     * @return
     */
    @PostMapping("/manyEmptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托盘组出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "manyEmptyOut", from = "ROBOT")
    public AjaxResult manyEmptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("manyEmptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerManyEmptyOut(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    private AjaxResult innerManyEmptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        if (manyEmptyDomain == null) {
            throw new ServiceException("空托盘组参数不对!");
        }
        String containerCode = manyEmptyDomain.getContainerCode();
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String roadWay = manyEmptyDomain.getRoadWay();
        List<String> roadWays = new ArrayList<>();
        String[] str = roadWay.split(",");
        roadWays = Arrays.asList(str);

        if (StringUtils.isEmpty(manyEmptyDomain.getArea())) {
            throw new ServiceException("参数area不能为空!");
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        if (zone == null) {
            return AjaxResult.error("分配库位时,没有找到库区,area" + area);
        }
        // String value = configService.getKey(QuantityConstant.RULE_ALLOCATION);
        String value = iDictDataService.getDictValueByLabel(QuantityConstant.RULE_ALLOCATION, zone.getCode());

        String height = manyEmptyDomain.getHeight();
        /*
         * if (StringUtils.isEmpty(height)) {
         * return AjaxResult.error("高度参数为空,请输入高度");
         * }
         */
        if (StringUtils.isEmpty(value)) {
            return AjaxResult.error(zone.getCode() + "库区未绑定定位规则,请到字典管理中绑定库区定位规则");
        }
        int allocationRule = Integer.parseInt(value);

        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getContainerCode, containerCode).eq(TaskHeader::getWarehouseCode, warehouseCode).lt(TaskHeader::getStatus,
                QuantityConstant.TASK_STATUS_COMPLETED);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader != null) {
            return AjaxResult.success("已经生成空托盘组任务");
        }
        // 查询满足条件的库位类型
        LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(LocationType::getZoneCode, zone.getCode()).eq(LocationType::getWarehouseCode, warehouseCode);
        List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);
        if (locationTypeList == null) {
            return AjaxResult.error(zone.getCode() + "库区分配库位时,没有找到库位类型,请到库位类型确认库区");
        }
        List<String> locationTypeCodeList = locationTypeList.stream().map(t -> t.getCode()).collect(Collectors.toList());
        int highHeight = Float.valueOf(height).intValue();
        LambdaQueryWrapper<LocationHigh> locationHighLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationHighLambdaQueryWrapper.eq(LocationHigh::getHighLevel, highHeight).in(LocationHigh::getLocationTypeCode, locationTypeCodeList);
        LocationHigh locationHigh = locationHighService.getOne(locationHighLambdaQueryWrapper);
        int high = locationHigh.getHigh();

        WarecellLocationDomain domain =
                allocationService.createWarecellLcaotionDoamin(allocationRule, locationTypeCodeList, high, area, roadWays, warehouseCode, containerCode, null);
        String locationCode = allocationService.allocation(domain);
        if (locationCode == null) {
            return AjaxResult.error("分配库位时,没有找到库位");
        }
        return workTaskService.createManyEmptyIn(containerCode, locationCode, warehouseCode);
    }

    private AjaxResult innerManyEmptyOut(ManyEmptyDomain manyEmptyDomain) {
        if (manyEmptyDomain == null) {
            throw new ServiceException("空托盘组参数不对!");
        }
        if (StringUtils.isEmpty(manyEmptyDomain.getArea())) {
            throw new ServiceException("参数area不能为空!");
        }
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String port = manyEmptyDomain.getPort();

        Station station = stationService.getStaionByCode(port);
        if(station == null){
            return AjaxResult.error("未找到站台");
        }
        String lkPort = port;
        if("P2042".equals(port)){
            lkPort = "P2058";
        }

        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper
                .eq(Container::getStatus, QuantityConstant.STATUS_CONTAINER_MANY)
                .eq(Container::getArea, area)
                .eq(Container::getWarehouseCode, warehouseCode);
        List<Container> containerList = containerService.list(containerLambdaQueryWrapper);
        List<Container> removeContainerList = new ArrayList<>();
        if (containerList != null && containerList.size() > 0) {
            for (Container container : containerList) {
                String locationCode = container.getLocationCode();
                // 防止出现空指针异常
                if(StringUtils.isNotEmpty(locationCode)){
                    Location location = locationService.getLocationByCode(locationCode, warehouseCode);
                    if (!location.getArea().equals(area)) {
                        removeContainerList.add(container);
                    }
                }
            }
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT).eq(TaskHeader::getPort, lkPort).eq(TaskHeader::getZoneCode, zone.getCode());
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader != null) {
            WcsTask wcsTask = new WcsTask();
            wcsTask.setTaskType(taskHeader.getTaskType());
            wcsTask.setTaskNo(String.valueOf(taskHeader.getId()));
            wcsTask.setContainerCode(taskHeader.getContainerCode());
            wcsTask.setWarehouseCode(warehouseCode);
            wcsTask.setFromLocationCode(taskHeader.getFromLocation());
            wcsTask.setPlatform(QuantityConstant.PLATFORM_WMS);
            wcsTask.setPriority(999);
            wcsTask.setTaskStatus(taskHeader.getStatus());
            wcsTask.setToPort(port);
            wcsTask = taskAssignService.switchTaskTypeToWcs(wcsTask, taskHeader);
            return AjaxResult.success(wcsTask);
        }else{
            AgvTask agv = agvTaskService.getOne(new LambdaQueryWrapper<AgvTask>().eq(AgvTask::getToPoint, port)
                    .eq(AgvTask::getIsEmpty, 0).eq(AgvTask::getLayer, station.getLayer()).eq(AgvTask::getZoneCode, station.getZoneCode()).lt(AgvTask::getState, 90));
            if (agv != null) {
                return AjaxResult.success("任务创建成功!");
            }
        }
        containerList.removeAll(removeContainerList);
        if (!(containerList != null && containerList.size() > 0)) {
            return AjaxResult.error("没有找到合适的空托盘组!");
//            return acsService.agvCallEmptyOut(Integer.parseInt(area), port, warehouseCode, 1);
        }
        String containerCode = containerList.get(0).getCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            throw new ServiceException("没有找到托盘“");
        }
        Zarsh zarsh = new Zarsh();
        zarsh.setLocation(container.getLocationCode());
        zarsh.setCreateBy("WMS");
        zarsh.setMandt("600");
        zarsh.setWerks("SC00");
        zarsh.setLgnum(zone.getNickName());
        zarsh.setLgort(zone.getUserDef1());
        zarsh.setUniqueId(DataUtils.createUniqueIdV1());
        zarsh.setMFlag("3");
        zarsh.setToPos(station.getByName());
        zarsh.setCFlag(null);
        zarsh.setId(null);
        zarsh.setInKind(0);
        if(!zarshService.save(zarsh)){
            throw new ServiceException("创建出库命令失败");
        }
        return workTaskService.createManyEmptyOut(containerCode, warehouseCode, lkPort,port,zarsh.getUniqueId());
    }

    /**
     * 生成空托出库任务
     *
     * @return
     */
    @PostMapping("/wcsCallEmptyOut")
//    @Log(title = "任务-任务管理", operating = "生成空托出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "生成空托出库任务", from = "WCS")
    public AjaxResult wcsCallEmptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("wcsCallEmptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerWcsCallEmptyOut(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    private AjaxResult innerWcsCallEmptyOut(ManyEmptyDomain manyEmptyDomain) {
        boolean isSuc = false;
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String port = manyEmptyDomain.getPort();
        String areaByWcs = manyEmptyDomain.getAreaByWcs();
        Integer taskId = manyEmptyDomain.getTaskId();
        Integer containerType = null;

        if (warehouseCode == null) {
            return AjaxResult.error("warehouseCode 不能为空");
        }
        if (area == null) {
            return AjaxResult.error("area 不能为空");
        }
        switch (area) {
            case "1":
            case "3":
                containerType = 0;
                break;
            case "6":
            case "2":
            case "7":
                containerType = 1;
                break;
        }
        if (port == null) {
            return AjaxResult.error("port 不能为空");
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getWarehouseCode, warehouseCode).eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        if (zone == null) {
            return AjaxResult.error("area不正确,没有找到对应区域");
        }
        String zoneCode = zone.getCode();
        List<TaskHeader> taskHeaders =
                taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>().eq(TaskHeader::getPort, port).eq(TaskHeader::getZoneCode, zoneCode)
                        .eq(TaskHeader::getWarehouseCode, QuantityConstant.WAREHOUSECODE)
                        .eq(StringUtils.isNotNull(taskId),TaskHeader::getNeedEmptyContainer, taskId)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        isSuc = !taskHeaders.isEmpty();
        if(StringUtils.isNotNull(taskId) && taskHeaders.size() > 0){
            return AjaxResult.success(taskHeaders.get(0).getId());
        }
        switch (area) {
            // 外观仓 可以出多个空托盘
            case "6":
                isSuc = taskHeaders.size() > 2;
                break;
            case "1":
                //玻璃布仓 呼叫空托盘不加并发限制
                isSuc = false;
                break;
            default:
        }
        // 有该站台任务 返回成功
        if (isSuc) {
            return AjaxResult.success();
        }
        // 对应巷道 站台出到 选择对应库内容器
        Station station = stationService.getStaionByCode(port);
        if (area.equals("6")) {
            areaByWcs = station.getAreaByWcs();
        }
        String[] strs = station.getRoadWay().split(",");
        List<String> roadways = Arrays.asList(strs);
        // 根据容器类型、查询对应区域zoneCode的库位、容器状态为空闲的空容器
        LambdaQueryWrapper<Container> query = Wrappers.lambdaQuery();
        query.eq(Container::getPlType, containerType)
                .eq(Container::getStatus,QuantityConstant.STATUS_CONTAINER_EMPTY)
                .likeRight(Container::getLocationCode, zoneCode);
        List<Container> containerList = containerService.list(query);
        if(containerList == null || containerList.isEmpty()){
            return AjaxResult.error("未匹配到,可出空托盘");
        }
        // 获取所有空闲的空容器
        List<String> locationCodeList = containerList.stream().map(Container::getLocationCode).collect(Collectors.toList());

        // 默认按照高层排序
        LambdaQueryWrapper<Location> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.select(Location::getHigh).eq(Location::getZoneCode, zoneCode)
                .groupBy(Location::getHigh);
        List<Location> highLocationList = locationService.list(queryWrapper2);
        List<Integer> allHighList = highLocationList.stream().map(Location::getHigh).collect(Collectors.toList());

        // 查询所有符合条件的库位,便于后续进行过滤分配
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getZoneCode, zoneCode)
                .eq(Location::getWarehouseCode, warehouseCode)
                .in(Location::getCode, locationCodeList)
                .in(Location::getRoadway, roadways)
                .eq(Location::getStatus, QuantityConstant.STATUS_CONTAINER_EMPTY)
                .ne(Location::getContainerCode, QuantityConstant.EMPTY_STRING);
        List<Location> locationList = locationService.list(queryWrapper);
        if(locationList == null || locationList.isEmpty()){
            return AjaxResult.error("没有空托盘可出库");
        }
        // 优先高库位最大值
        List<Integer> emptyCurrentHighList = locationList.stream().map(Location::getHigh).distinct().collect(Collectors.toList());
        // 如果有指定区域,则过滤
        if(!StringUtils.isEmpty(areaByWcs)){
            // 查询当前库区的空闲库位数量
            LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
            locationLambdaQueryWrapper.eq(Location::getZoneCode, zoneCode)
                    .eq(Location::getWarehouseCode, warehouseCode)
                    .in(Location::getRoadway, roadways)
                    .eq(Location::getHigh, 2)
                    .eq(Location::getStatus, QuantityConstant.STATUS_CONTAINER_EMPTY)
                    .eq(Location::getContainerCode, QuantityConstant.EMPTY_STRING);
            int emptyHighLocationCount = locationService.count(locationLambdaQueryWrapper);

            String highEmptyMax = iDictDataService.getDictValueByLabel(QuantityConstant.RULE_HIGH_EMPTY_MAX, zoneCode);
            String finalAreaByWcs = areaByWcs;
            // 对应区域的库位
            List<Location> incluldLocationList = locationList.stream().filter(t -> t.getAreaByWcs().equals(finalAreaByWcs)).collect(Collectors.toList());
            if(!incluldLocationList.isEmpty()){
                // 如果高库位紧张 则优先高库位出库。
                if(emptyHighLocationCount >= Long.parseLong(highEmptyMax)){
                    locationList = incluldLocationList;
                }
            }
        }
        // 降序排序
        Collections.sort(allHighList, new Comparator<Integer>() {
            @Override
                public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        String emptyContainerCode = null;
        // 优先匹配高层的空托盘
        for (Integer high : allHighList) {
            List<Location> locations = locationList.stream().filter(t -> t.getHigh() == high).collect(Collectors.toList());
            if(!locations.isEmpty()){
                emptyContainerCode = locations.get(0).getContainerCode();
                break;
            }
        }
//        String emptyContainerList = containerService.getEmptyContainerList(zone.getCode(), roadways, areaByWcs, containerType);
        if(emptyContainerCode == null){
            String containerTypeStr = containerType == 0 ? "铁架":"栈板";
            String title = station.getLayer()+"楼小车呼叫空托盘【"+containerTypeStr+"】失败";
            LambdaQueryWrapper<SysNotice> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1
                    .eq(SysNotice::getTitle,title)
                    .eq(SysNotice::getRemark,zone.getCode())
                    .apply("TIMESTAMPDIFF(minute,created,NOW()) < 60 order by id desc limit 1");
            SysNotice sysNotice = sysNoticeService.getOne(queryWrapper1);
            if(sysNotice == null){
                String content = "查询立库空托盘为空,请补充空" + containerTypeStr;
                SendNoticeUtils.sendNotice(title,content, SendTypeConstants.WEBSOCKET_USER,null,null,zone.getCode());
            }
            return AjaxResult.error("没有托盘可出库");
        }
        Container container = containerService.getContainerByCode(emptyContainerCode, QuantityConstant.WAREHOUSECODE);
        String containerCode = container.getCode();
        String locationCode = container.getLocationCode();
        String uniqueId = null;
        if("2".equals(area)){
            uniqueId = DataUtils.createUniqueIdV1();
            Zarsh zarsh = new Zarsh();
            zarsh.setLocation(locationCode);
            zarsh.setCreateBy("WMS");
            zarsh.setMandt("600");
            zarsh.setWerks("SC00");
            zarsh.setLgnum(zone.getNickName());
            zarsh.setLgort(zone.getUserDef1());
            zarsh.setUniqueId(uniqueId);
            zarsh.setMFlag("3");
            zarsh.setToPos(station.getByName());
            zarsh.setCFlag(null);
            zarsh.setId(null);
            zarsh.setInKind(0);
            if(!zarshService.save(zarsh)){
                throw new ServiceException("创建出库命令失败");
            }
            if(station.getLayer() == 2){
                port = "P2058";
            }
        }
        return workTaskService.createEmptyOut(containerCode, locationCode, port, warehouseCode,uniqueId,null,taskId);
    }

    /**
     * 生成空托出库任务-商片二楼2058到
     *
     * @return
     */
    @PostMapping("/emptyPalletAgvTask")
    @Log(title = "任务-任务管理", operating = "生成空托出库任务-商片二楼2058到", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "生成空托出库任务-商片二楼2058到", from = "WCS")
    public AjaxResult emptyPalletAgvTask(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleQuest("wcsCallEmptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {

                LambdaQueryWrapper<AgvTask> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.eq(AgvTask::getFromPoint,"P2058")
                        .eq(AgvTask::getToPoint,"P2042")
                        .lt(AgvTask::getState,90)
                        .last("LIMIT 1");
                AgvTask agvTask1 = agvTaskSelectService.getOne(queryWrapper1);
                if(agvTask1 != null){
                    return AjaxResult.success("存在未完成ACS任务"+agvTask1.getTaskNo());
                }

                StationPallet stationPallet = taskInfoService.getStationPallet("P2058", "2");
                if(stationPallet == null){
                    return AjaxResult.error("接口返回内容为空!");
                }
                String containerCode = stationPallet.getContainerCode();
                Integer isGoods = stationPallet.getGoods();
                if(isGoods == 1){
                    return AjaxResult.error("P2058托盘有货不能生成ACS空托盘搬运任务");
                }
                if(StringUtils.isEmpty(containerCode)){

                    LambdaQueryWrapper<TaskHeader> queryWrapper = Wrappers.lambdaQuery();
                    queryWrapper.eq(TaskHeader::getPort,"P2058")
                            .lt(TaskHeader::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
                            .in(TaskHeader::getTaskType,QuantityConstant.TASK_TYPE_EMPTYSHIPMENT,QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT)
                            .last("LIMIT 1");
                    TaskHeader taskHeader = taskHeaderService.getOne(queryWrapper);

                    if(taskHeader == null){
                        manyEmptyDomain.setPort("P2042");
                        AjaxResult ajaxResult1 = innerManyEmptyOut(manyEmptyDomain);
                        if(ajaxResult1.hasErr()){
                            innerWcsCallEmptyOut(manyEmptyDomain);
                        }
                        return ajaxResult1;
                    }
                    return AjaxResult.error("等待空托盘出库!");
                }

                AgvTask agvTask = new AgvTask();
                agvTask.setPalletNo(stationPallet.getContainerCode());
                agvTask.setFromPoint("P2058");
                agvTask.setTaskTypes(QuantityConstant.STATION_2_STATION_AGV);
                agvTask.setToPoint("P2042");
                return agvTaskSelectService.saveAgvTask(agvTask);
            }
        });
        return ajaxResult;
    }


    /**
     * 是否叠盘
     * @return
     */
    @PostMapping("/back")
    @Log(title = "WCS接口", operating = "是否叠盘", action = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult back(@RequestBody ManyEmptyDomain manyEmptyDomain){
        String area = manyEmptyDomain.getArea();
        AjaxResult ajaxResult = handleQuest("back", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult1 = containerGroupService.overFlow(area);
                if(ajaxResult1.hasErr()){
                    return AjaxResult.success(1);
                }
                return AjaxResult.success(0);
            }
        });
        return ajaxResult;
    }
}