ZarshService.java 48.3 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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
package com.huaheng.pc.sap.service;

import static java.util.stream.Collectors.toList;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.huaheng.pc.config.material.domain.MaterialDiameter;
import com.huaheng.pc.config.material.service.MaterialDiameterService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.general.service.BasicDataApiService;
import com.huaheng.api.sap.domain.ZarDomain;
import com.huaheng.api.sap.service.ZarApiService;
import com.huaheng.api.utils.SAPUtils;
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.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.ProcessTrack;
import com.huaheng.framework.aspectj.lang.constant.ProcessCode;
import com.huaheng.framework.web.domain.AjaxResult;
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.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.sap.domain.SAPTaskDomain;
import com.huaheng.pc.sap.domain.Zarsh;
import com.huaheng.pc.sap.domain.Zarsi;
import com.huaheng.pc.sap.mapper.ZarshMapper;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskEmptyContainerService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;

/**
 * Created by Enzo Cotter on 2022/5/11.
 * @author zhouhong
 */
@Service
public class ZarshService extends ServiceImpl<ZarshMapper, Zarsh> {
    @Resource
    private ZarsiService zarsiService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private BackSapStatusService backSapStatusService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private StationService stationService;
    @Resource
    private LocationService locationService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private WorkTaskService workTaskService;
    @Resource
    private WorkTaskEmptyContainerService workTaskEmptyContainerService;
    @Resource
    private ZarshService zarshService;
    @Resource
    private ZarApiService zarApiService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private MaterialDiameterService materialDiameterService;
    @Resource
    private BasicDataApiService basicDataApiService;
    @Resource
    private WarecellAllocationService warecellAllocationService;
    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;

    public Zarsh checkZarshByUniqueId(String uniqueId) {
        LambdaQueryWrapper<Zarsh> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Zarsh::getUniqueId, uniqueId);
        lambdaQueryWrapper.last("limit 1");
        Zarsh zarsh = this.getOne(lambdaQueryWrapper);
        if (zarsh != null) {
            return zarsh;
        }
        return null;
    }

    public boolean saveZarsh(Zarsh zarsh) {
        Date date = new Date();
        String uniqueId = UUID.randomUUID().toString();
        zarsh.setUniqueId(uniqueId);
        zarsh.setCreated(date);
        zarsh.setCreateBy("wms");
        zarsh.setLastUpdatedBy("wms");
        zarsh.setLastUpdated(date);
        return this.save(zarsh);
    }

    public boolean editZarsh(Zarsh zarsh) {
        Date date = new Date();
        zarsh.setLastUpdatedBy("wms");
        zarsh.setLastUpdated(date);
        return this.updateById(zarsh);
    }

    public void removeByZarshId(String uniqueId, Integer id) {
        LambdaQueryWrapper<Zarsh> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(uniqueId), Zarsh::getUniqueId, uniqueId);
        queryWrapper.eq(StringUtils.isNotNull(id), Zarsh::getId, id);
        queryWrapper.last("limit 1");
        Zarsh zarsh = this.getOne(queryWrapper);
        if (zarsh != null) {
            LambdaQueryWrapper<Zarsi> query = Wrappers.lambdaQuery();
            query.eq(Zarsi::getUniqueId, zarsh.getUniqueId());
            List<Zarsi> list = zarsiService.list(query);
            List<Integer> ids = list.stream().map(detail -> detail.getId()).collect(Collectors.toList());
            if (ids.size() > 0) {
                zarsiService.removeByIds(ids);
            }
            this.removeById(zarsh.getId());
        }

    }

    public Zarsh getZarshByUnique(String uniqueId) {
        LambdaQueryWrapper<Zarsh> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(uniqueId), Zarsh::getUniqueId, uniqueId);
        queryWrapper.last("limit 1");
        Zarsh zarsh = this.getOne(queryWrapper);
        return zarsh;
    }

    public AjaxResult saveSapDataByApi(ZarDomain zarDomain) {
        // 用主表UniqueId查找子表zarsi信息
        List<Zarsi> zarsiList = zarDomain.getZarsiList();
        Zarsh zarsh = zarDomain.getZarsh();
        /**
         * 任务取消
         */
        if (StringUtils.isNotEmpty(zarsh.getCFlag()) && zarsh.getCFlag().equals("D")) {
            // 取消
            return zarApiService.cancelTaskBySap(zarsh);
        }
        Zarsh zarsh1 = zarshService.checkZarshByUniqueId(zarsh.getUniqueId());
        if (zarsh1 != null) {
            throw new ServiceException("单据已存在,请不要重复下发");
        }
        try{
            zarshService.createTaskBySAP(zarsiList, zarsh);
        }catch (Exception e){
            e.printStackTrace();
            backSapStatusService.addBackSapStatus( zarsh.getUniqueId(), zarsh.getLgnum(), null, zarsh.getMFlag(), "C", null, e.getMessage());
        }
        return AjaxResult.success("添加任务成功");
    }

    // 入库
    public TaskHeader receiptBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
        TaskHeader taskHeader = null;
        // 空托盘入库
        if (zarsiList == null || zarsiList.size() == 0) {
            String area = zoneService.getZoneAreaByCode(zarsh.getLgnum());
            // SAP创建 空容器号的入库任务
            AjaxResult result = workTaskEmptyContainerService.createEmptyIn(zarsh.getDrumId(), "", area, zarsh.getFromPos(), zarsh.getCrnIn());
            if (result.hasErr()) {
                throw new ServiceException(result.getMsg());
            }
            Integer taskId = (Integer)result.getData();
            taskHeader = taskHeaderService.getById(taskId);
            taskHeader.setUniqueIds(zarsh.getUniqueId());
            taskHeaderService.updateById(taskHeader);
            // 调用sap的api,传递任务状态
            backSapStatusService.addBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, "1", "1", null, null);
            return null;
        }
        // 保存入库信息
        ReceiptHeader receiptHeader = null;
        for (Zarsi zarsi : zarsiList) {
            if (zarsi.getVerme() == null || zarsi.getVerme().compareTo(BigDecimal.ZERO) == 0) {
                throw new ServiceException("入库数量不能为0");
            }
        }
        AjaxResult ajaxResult = receiptHeaderService.saveReceiptBySap(zarsh, zarsiList);
        if (ajaxResult != null && ajaxResult.hasErr()) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        receiptHeader = (ReceiptHeader)ajaxResult.getData();
        // 调用sap的api,传递任务状态
        backSapStatusService.addBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, "1", "1", null, null);
        // 如果是玻璃布2楼入库,机台到立库,站台机台,线边库到立库,站台立库口,机台到线边库

        Container container = containerService.getContainerByCode(zarsh.getDrumId(), QuantityConstant.WAREHOUSECODE);
        // 指定一个托盘生成任务
        if (StringUtils.isEmpty(zarsh.getDrumId()) || container == null) {
            if (zarsh.getLgnum().contains("A")) {
                Container container1 = containerService.createContainerByZoneNoTrasa("A");
                zarsh.setDrumId(container1.getCode());
                container = container1;
                // 创建临时托盘
            }
        }
        // 判断有没有托盘号,有则生成组盘
        if (StringUtils.isNotEmpty(zarsh.getDrumId()) && container != null) {
            // 保存组盘,生成任务信息
            // 设置wcs固定登录缓存,调用公共组盘接口
            ShiroUtils.setUserByWcsZone("SAP", QuantityConstant.WAREHOUSECODE, receiptHeader.getZoneCode());

            AjaxResult ajaxResult_receiptcontainerH = receiptContainerHeaderService.saveContainerByReceiptHeaderId(receiptHeader.getId(), zarsh.getDrumId());
            if (ajaxResult_receiptcontainerH != null && ajaxResult_receiptcontainerH.hasErr()) {
                throw new ServiceException(ajaxResult_receiptcontainerH.getMsg());
            }
//            AjaxResult ajaxResult_receiptcontainer =
//                receiptContainerHeaderService.saveReceiptContainerBySap(receiptHeader, zarsh.getDrumId(), zarsh.getFromPos(), zarsh.getToPos());
//            if (ajaxResult_receiptcontainer != null && ajaxResult_receiptcontainer.hasErr()) {
//                throw new ServiceException(ajaxResult_receiptcontainer.getMsg());
//            }
        }
        return taskHeader;
    }

    // 出库
    public TaskHeader shipmentBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
        TaskHeader taskHeader = null;
        // 空托盘出库
        if (zarsiList == null || zarsiList.size() == 0) {
            // 获取对应库区编码 zarsh.getLgnum() --> F
            List<Location> list = containerService.getEmptyContainerInLocation(zarsh.getLgnum(), "", "", QuantityConstant.WAREHOUSECODE);
            List<String> emptyContainerCodeList = list.stream().map(t -> t.getContainerCode()).collect(Collectors.toList());
            List<String> removeContainerList = new ArrayList<>();
            for (String containerCode : emptyContainerCodeList) {
                Container container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
                if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
                    removeContainerList.add(containerCode);
                }
            }
            emptyContainerCodeList.removeAll(removeContainerList);

            if (emptyContainerCodeList == null || emptyContainerCodeList.isEmpty()) {
                throw new ServiceException("库内没有找到空容器");
            }
            // 自动匹配空容器出库
            Container container = null;
            if (StringUtils.isNotEmpty(zarsh.getDrumId())) {
                container = containerService.getContainerByCode(zarsh.getDrumId(), QuantityConstant.WAREHOUSECODE);
            } else {
                container = containerService.getContainerByCode(emptyContainerCodeList.get(0), QuantityConstant.WAREHOUSECODE);
            }
            if (container == null) {
                throw new ServiceException("请检查容器码:" + zarsh.getDrumId() + "是否正确!");
            }
            // 创建空容器出库任务
            AjaxResult result = workTaskService.createEmptyOut(container.getCode(), container.getLocationCode(), zarsh.getToPos(), QuantityConstant.WAREHOUSECODE,
                zarsh.getUniqueId());

            if (result.hasErr()) {
                throw new ServiceException(result.getMsg());
            }
            // 调用sap的api,传递任务状态
            backSapStatusService.addBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, "2", "1", null, null);
            return taskHeader;
        }
        // 有料出库
        // 保存出库信息
        ShipmentHeader shipmentHeader = null;
        AjaxResult ajaxResult = shipmentHeaderService.saveShipmentBySap(zarsh, zarsiList);
        if (ajaxResult != null && ajaxResult.hasErr()) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        shipmentHeader = (ShipmentHeader)ajaxResult.getData();
        // 调用sap的api,传递任务状态
        backSapStatusService.addBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, "2", "1", null, null);
        // 判断有没有库位号,有则生成组盘
        if (StringUtils.isNotEmpty(zarsh.getLocation())) {
            // 保存组盘信息
            ShipmentContainerHeader shipmentContainerHeader = null;
            AjaxResult result = shipmentContainerHeaderService.saveShipmentContainerBySap(shipmentHeader, zarsh);
            if (result != null && result.hasErr()) {
                throw new ServiceException(result.getMsg());
            }
            shipmentContainerHeader = (ShipmentContainerHeader)result.getData();
            // 创建任务表数据
            AjaxResult ajaxResult_taskheader = taskHeaderService.saveTaskBySapLK(shipmentContainerHeader);
            // 生成任务成功之后在修改 组盘状态
            if (ajaxResult_taskheader.hasErr()) {
                throw new ServiceException("生成任务失败");
            }
        }
        return taskHeader;
    }

    @Transactional(rollbackFor = Exception.class)
    public void createTaskBySAP(List<Zarsi> zarsiList, Zarsh zarsh) {
        String zoneCode = SAPUtils.getZoneCode(zarsh);
        /**
         * 1:整盘 / 等待组盘
         * 2:空托
         */
        Integer billType = zarsh.getInKind();
        String type = zarsh.getMFlag();
        String uniqueId = zarsh.getUniqueId();
        SAPTaskDomain domain = new SAPTaskDomain();
        domain.setZarsh(zarsh);
        domain.setZarsiList(zarsiList);

        if (!zarshService.save(zarsh)) {
            throw new ServiceException("保存主表数据失败");
        }
        if (!zarsiService.saveBatchs(zarsiList, zarsh)) {
            throw new ServiceException("保存子表数据失败");
        }

        switch (billType) {
            case 1:
                createHavingTask(domain);
                break;
            case 0:
                createEmptyTask(domain);
                break;
            default:
                throw new ServiceException("inKind不支持的业务类型");
        }
        backSapStatusService.addBackSapStatus(uniqueId, zoneCode, null, type, "1", null, null);
    }

    /**
     * SAP无货指令
     * @param domain
     */
    public void createEmptyTask(SAPTaskDomain domain) {
        Zarsh zarsh = domain.getZarsh();
        String formPort = zarsh.getFromPos();
        String toPort = zarsh.getToPos();
        String type = zarsh.getMFlag();
        Integer containerType = zarsh.getPlType();
        String containerCode = zarsh.getDrumId();
        String locationCode = zarsh.getLocation();
        String uniqueId = zarsh.getUniqueId();
        String crnIn = zarsh.getCrnIn();
        String zoneCode = SAPUtils.getZoneCode(zarsh);
        List<Zarsi> zarsiList = domain.getZarsiList();

        // 入库站台属性
        switch (type) {
            // 入库
            case "2":
            case "B":
                LambdaQueryWrapper<Station> lambdaQuery1 = Wrappers.lambdaQuery();
                lambdaQuery1.eq(Station::getByName, formPort);
                Station station = stationService.getOne(lambdaQuery1);
                if (station == null) {
                    throw new ServiceException(toPort + "站台异常,未找到站台");
                }
                // 判断站台属性是否为AGV站台
                if (station.getDefineProperty() == 1) {
                    // 获取 AGV立库交互站台
                    createReceiptAGVTask(zarsh, zarsiList, station.getCode());
                } else {
                    String area = SAPUtils.getArea(zarsh);
                    workTaskService.createEmptyIn(containerCode, null, area, station.getCode(), uniqueId);
                }
                break;
            // 出库
            case "1":
            case "3":
                LambdaQueryWrapper<Station> lambdaQuery2 = Wrappers.lambdaQuery();
                lambdaQuery2.eq(Station::getByName, toPort);
                Station station1 = stationService.getOne(lambdaQuery2);
                if (station1 == null) {
                    throw new ServiceException(toPort + "站台异常,未找到站台");
                }
                Integer defineProperty = station1.getDefineProperty();
                // 判断库区
                if (StringUtils.isNotEmpty(zarsh.getCrnIn())) {
                    zoneCode = zarsh.getCrnIn();
                } else {
                    Location location1 = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
                    if (location1 != null) {
                        zoneCode = location1.getZoneCode();
                    }
                }
                if (StringUtils.isEmpty(zoneCode)) {
                    zoneCode = SAPUtils.getZoneCode(zarsh);
                }
                // 获取库区
                if (zoneCode.contains("X")) {
                    if (2 == defineProperty) {
                        createShipmentAGVTask(zarsh, zarsiList, station1.getCode());
                    }
                    if (1 == defineProperty) {
                        createShipmentAGVTask(zarsh, zarsiList, station1.getCode());
                    }
                } else {
                    // 区域移库 映射站台 SAP任务下发的是移入库区的站台 通过移入站台获取到 出库的站台
                    if (1 == defineProperty) {
                        String port = getLKAGVPort(zoneCode, station1.getLayer(),null);
                        if (StringUtils.isEmpty(port)) {
                            throw new ServiceException("立库AGV交互站台未配置请先配置立库AGV交互站台");
                        }
                        station1 = stationService.getStaionByCode(port);
                    }
                    String shipmentStationCode = null;
                    if (StringUtils.isEmpty(station1.getByPassPort())) {
                        shipmentStationCode = station1.getCode();
                    } else {
                        shipmentStationCode = station1.getByPassPort();
                    }

                    Station station2 = stationService.getOne(new LambdaQueryWrapper<Station>().eq(Station::getCode, shipmentStationCode));
                    if (station2 == null) {
                        throw new ServiceException(shipmentStationCode + "站台异常,未找到站台");
                    }
                    String roadWay = station2.getRoadWay();
                    String[] split = roadWay.split(",");
                    List<String> roadWays = Arrays.asList(split);
                    // 根据 SAP containerType 来获取托盘类型
                    String palletCode = null;
                    if (StringUtils.isNotEmpty(locationCode)) {
                        Location location = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
                        if (location == null) {
                            palletCode = location.getContainerCode();
                        }
                    } else {
                        palletCode = containerService.getEmptyContainerList(zoneCode, roadWays, station1.getAreaByWcs(), containerType);
                    }
                    if (StringUtils.isEmpty(palletCode)) {
                        throw new ServiceException(containerType + "容器类型未匹配成功");
                    }
                    Container container = containerService.getContainerByCode(palletCode, QuantityConstant.WAREHOUSECODE);
                    if (container == null) {
                        throw new ServiceException(palletCode + "容器异常,未找到容器");
                    }
                    AjaxResult emptyOut = workTaskService.createEmptyOut(container.getCode(), container.getLocationCode(), shipmentStationCode,
                        QuantityConstant.WAREHOUSECODE, uniqueId,station1.getCode());
                    if (emptyOut.hasErr()) {
                        throw new ServiceException(emptyOut.getMsg());
                    }
                }

                break;
            default:
                throw new ServiceException("MFlag不支持的业务类型");
        }
    }

    /**
     * SAP有货指令
     * @param domain
     */
    public void createHavingTask(SAPTaskDomain domain) {
        Zarsh zarsh = domain.getZarsh();
        String crnIn = zarsh.getCrnIn();
        String formPort = zarsh.getFromPos();
        String toPort = zarsh.getToPos();
        String type = zarsh.getMFlag();
        String zoneCode = null;
        String containerCode = zarsh.getDrumId();
        String locationCode = zarsh.getLocation();
        String uniqueId = zarsh.getUniqueId();
        List<Zarsi> zarsiList = domain.getZarsiList();

        //校验或添加物料
        checkMaterialCode(zarsiList);

        switch (type) {
            // 入库
            case "2":
            case "B":
                // 标志是否有 容器编码
                int containerFlag = 0;
                Container container = null;
                if (StringUtils.isNotEmpty(containerCode)) {
                    container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
                    if (container != null) {
                        containerFlag = 1;
                        if (QuantityConstant.STATUS_LOCATION_LOCK.equals(container.getStatus())) {
                            throw new ServiceException("容器状态为锁定" + containerCode);
                        }
                        if (QuantityConstant.STATUS_LOCATION_SOME.equals(container.getStatus())) {
                            throw new ServiceException("容器状态为有货" + containerCode);
                        }
                        List<InventoryDetail> inventoryDetails =
                                inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getContainerCode, container.getCode()));
                        if (!inventoryDetails.isEmpty()) {
                            throw new ServiceException("容器存在库存" + containerCode);
                        }
                    }
                }
                Station station = stationService.getStationBySAPCode(formPort);
                if (station == null) {
                    throw new ServiceException("站台未找到!" + formPort);
                }
                // 入库站台编码
                String formPortCode = station.getCode();
                // 入库站台属性
                Integer defineProperty = station.getDefineProperty();
                // 确定库区
                if (StringUtils.isNotEmpty(crnIn)) {
                    zoneCode = crnIn;
                } else {
                    zoneCode = station.getZoneCode();
                }
                switch (containerFlag) {
                    // 无托盘
                    case 0:
                        // agv站台
                        if (defineProperty == 1) {
                            // 创建agv任务
                            AgvTask agvTask = createReceiptAGVTask(zarsh, zarsiList, formPortCode);
                            if (StringUtils.isEmpty(crnIn)) {
                                // 获取分配agv任务去向的站台
                                String toPoint = agvTask.getToPoint();
                                zarsh.setFromPos(toPoint);
                                receiptHeaderService.saveReceiptHeaderBySap(uniqueId, zarsh, zarsiList);
                            }
                            break;
                        }
                        // 生成入库单 用于AGV调用组盘接口
                        receiptHeaderService.saveReceiptHeaderBySap(uniqueId, zarsh, zarsiList);
                        break;
                    // 有托盘
                    case 1:
                        // 判断 from_pos 来源
                        domain.setFormPort(formPortCode);
                        // 立库agv交互点入库
                        //指定去那边
                        if (( defineProperty == 2 && StringUtils.isEmpty(zarsh.getCrnIn())) || defineProperty == 0) {
                            createSAPReceiptTask(uniqueId, zarsh,zarsiList);
                        }
                        // agv站台入库
                        if (defineProperty == 1 || ( defineProperty == 2 && StringUtils.isNotEmpty(zarsh.getCrnIn()))) {
                            createReceiptAGVTask(zarsh, zarsiList, formPortCode);
                        }
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + containerFlag);
                }
                break;
            // 出库
            case "1":
            case "3":
                // 判断库区
                Location location = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
                if (location == null) {
                    throw new ServiceException("SAP库位为空请检查数据" + zarsh.getLocation());
                }
                zoneCode = location.getZoneCode();
                Station toStation = stationService.getStationBySAPCode(toPort);
                if (toStation == null) {
                    throw new ServiceException("站台未找到!" + formPort);
                }
                // 出库站台编码
                String toPortCode1 = toStation.getCode();
                Integer defineProperty1 = toStation.getDefineProperty();
                if (StringUtils.isEmpty(locationCode)) {
                    throw new ServiceException("SAP数据异常 locationCode 值为空");
                }
                // 边线库区
                if (zoneCode.contains("X")) {
                    // 先生成AGV任务
                    switch (defineProperty1) {
                        case 1:
                            createShipmentAGVTask(zarsh, zarsiList, toPortCode1);
                            break;
                        case 2:
                            createShipmentAGVTask(zarsh, zarsiList, toPortCode1);
                            // 临时托盘生成 入库单
                            if (!location.getContainerCode().contains("TP")) {
                                receiptHeaderService.saveReceiptHeaderBySap(uniqueId, zarsh, zarsiList);
                            }
                            break;
                        default:
                            throw new ServiceException("SAP数据异常");
                    }
                } else {
                    // 生成立库任务
                    switch (defineProperty1) {
                        case 1:
                            // 获取立库AGV交互站台
                            String port = getLKAGVPort(zoneCode, toStation.getLayer(),location.getRoadway());
                            createSAPShipmentTask(zarsh,port);
//                            createShipmentAGVTask(zarsh, zarsiList, port);
                            break;
                        default:
                            createSAPShipmentTask(zarsh, toPortCode1);
                    }
                }
            default:
        }
    }

    private void checkMaterialCode(List<Zarsi> zarsiList) {
        Set<String> materialCodes = zarsiList.stream().map(Zarsi::getMatnr).collect(Collectors.toSet());
        for (String materialCode : materialCodes) {
            Material material = materialService.getMaterialByCode(materialCode, QuantityConstant.WAREHOUSECODE);
            if (StringUtils.isNull(material)) {
                material = new Material();
                material.setCode(materialCode);
                material.setName(materialCode);
                basicDataApiService.material(material);
            }
        }



    }

    /**
     * 根据sap创建入库任务
     */
    @ProcessTrack(taskName = "创建入库任务", processCode = ProcessCode.RECEIPT_TASK_CODE)
    public void createSAPReceiptTask(String uniqueId, Zarsh zarsh,List<Zarsi> zarsiList) {
        String warehouseCode = QuantityConstant.WAREHOUSECODE;
        String containerCode = zarsh.getDrumId();
        String port = zarsh.getFromPos();
        Station station = stationService.getStationBySAPCode(port);
        if(station != null){
            port = station.getCode();
        }
        String zoneCode = SAPUtils.getZoneCode(zarsh);
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (StringUtils.isNull(container)) {
            throw new ServiceException("托盘不存在!");
        }
        if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
            throw new ServiceException("托盘已经锁定!");
        }

        containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);

        // 创建任务头
        TaskHeader task = new TaskHeader();
        task.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        task.setUniqueIds(uniqueId);
        task.setZoneCode(zoneCode);
        task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
        task.setAllocationHeadId(0);
        task.setWarehouseCode(warehouseCode);
        task.setAssignedUser(ShiroUtils.getLoginName() == null ? QuantityConstant.PLATFORM_WCS : ShiroUtils.getLoginName());
        task.setConfirmedBy(ShiroUtils.getLoginName() == null ? QuantityConstant.PLATFORM_WCS : ShiroUtils.getLoginName());
        task.setPort(port);
        task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        task.setContainerCode(container.getCode());
        task.setCompanyCode(QuantityConstant.COMPANYCODE);
        task.setCreatedBy(QuantityConstant.PLATFORM_SAP);
        task.setCreated(DateUtils.getNowDate());
        // 剪切线分配库位
        task = receiptTaskService.getLocationByH(task.getPort(), task);
        if (!taskHeaderService.save(task)) {
            throw new ServiceException("生成任务失败");
        }
        List<TaskDetail> taskDetailList = new ArrayList<>();
        for (Zarsi zarsi : zarsiList) {
            String materialCode = zarsi.getMatnr();
            Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
            if (material == null) {
                material = new Material();
                material.setCode(materialCode);
                material.setName(materialCode);
                basicDataApiService.material(material);
            }
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(task.getId());
            taskDetail.setTaskType(task.getTaskType());
            taskDetail.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
            taskDetail.setWarehouseCode(task.getWarehouseCode());
            taskDetail.setAllocationId(0);
            taskDetail.setCompanyCode(task.getCompanyCode());
            taskDetail.setMaterialCode(material.getCode());
            taskDetail.setMaterialName(material.getName());
            taskDetail.setMaterialSpec(material.getSpec());
            taskDetail.setMaterialUnit(material.getUnit());
            taskDetail.setInventorySts(QuantityConstant.GOOD);
            taskDetail.setBillCode(QuantityConstant.EMPTY_STRING);
            taskDetail.setContainerDetailNumber(zarsi.getPosnr());
            taskDetail.setBillDetailId(0);
            // 查找上游号
            taskDetail.setReferenceCode(uniqueId);
            taskDetail.setQty(zarsi.getVerme());
            taskDetail.setContainerCode(task.getContainerCode());
            taskDetail.setFromLocation(task.getFromLocation());
            taskDetail.setRollNumber(zarsi.getCharg());
            taskDetail.setCreatedBy(QuantityConstant.PLATFORM_SAP);
            taskDetail.setCreated(DateUtils.getNowDate());
            taskDetailList.add(taskDetail);
        }
        taskDetailService.saveBatch(taskDetailList);

    }

    /**
     * 根据sap创建出库任务
     */
    public void createSAPShipmentTask(Zarsh zarsh, String allocationStationCode) {
        String warehouseCode = QuantityConstant.WAREHOUSECODE;
        String locationCode = zarsh.getLocation();
        String uniqueId = zarsh.getUniqueId();
        Location location = locationService.getLocationByCode(locationCode, warehouseCode);
        if (StringUtils.isNull(location)) {
            throw new ServiceException("库位禁用或不存在!");
        }
        if (StringUtils.isEmpty(location.getContainerCode())) {
            throw new ServiceException("库位容器不存在 请联系现场人员是否通过WMS出库!");
        }
        Container container = containerService.getContainerByCode(location.getContainerCode(), warehouseCode);
        if (StringUtils.isNull(container)) {
            throw new ServiceException("托盘不存在!");
        }
        if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
            throw new ServiceException("托盘已经锁定!");
        }

        containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        locationService.updateStatus(location.getCode(), QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);

        List<InventoryDetail> inventoryDetails =
            inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getLocationCode, locationCode));

        TaskHeader task = new TaskHeader();
        task.setAllocationHeadId(0);
        task.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_SHIPMENT);
        task.setWarehouseCode(warehouseCode);
        task.setZoneCode(location.getZoneCode());
        task.setCompanyCode(QuantityConstant.COMPANYCODE);
        task.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        task.setTaskType(QuantityConstant.TASK_TYPE_WHOLESHIPMENT);
        task.setFromLocation(locationCode);
        task.setCrnIn(zarsh.getCrnIn());
        task.setContainerCode(container.getCode());
        task.setPort(allocationStationCode);
        task.setSendTo(0);
        task.setUniqueIds(uniqueId);
        task.setCreatedBy(QuantityConstant.PLATFORM_SAP);
        // 剪切线检验库位和站台的限制
        locationService.checkPreLocationByCode(locationCode, allocationStationCode, task.getZoneCode());
        taskHeaderService.save(task);

        List<TaskDetail> taskDetailList = new ArrayList<>();
        for (InventoryDetail inventoryDetail : inventoryDetails) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(task.getId());
            taskDetail.setInternalTaskType(task.getInternalTaskType());
            taskDetail.setWarehouseCode(task.getWarehouseCode());
            taskDetail.setCompanyCode(task.getCompanyCode());
            taskDetail.setTaskType(task.getTaskType());
            taskDetail.setToInventoryId(inventoryDetail.getId());
            taskDetail.setFromInventoryId(inventoryDetail.getId());
            taskDetail.setAllocationId(0);
            taskDetail.setBillCode(QuantityConstant.EMPTY_STRING);
            taskDetail.setBillDetailId(0);
            taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            taskDetail.setMaterialName(inventoryDetail.getMaterialName());
            taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            taskDetail.setLevel(inventoryDetail.getLevel());
            taskDetail.setQty(inventoryDetail.getQty());
            taskDetail.setContainerCode(task.getContainerCode());
            taskDetail.setFromLocation(task.getFromLocation());
            taskDetail.setLot(inventoryDetail.getLot());
            taskDetail.setBatch(inventoryDetail.getBatch());
            taskDetail.setProjectNo(inventoryDetail.getProjectNo());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setWaveId(0);
            taskDetail.setInventorySts(inventoryDetail.getInventorySts());
            taskDetail.setReferenceCode(inventoryDetail.getReferCode());
            taskDetail.setRollNumber(inventoryDetail.getRollNumber());
            taskDetail.setContainerDetailNumber(inventoryDetail.getContainerDetailNumber());
            taskDetailList.add(taskDetail);
            inventoryDetail.setTaskQty(inventoryDetail.getQty());
        }
        inventoryDetailService.updateBatchById(inventoryDetails);
        taskDetailService.saveBatch(taskDetailList);

    }

    public AgvTask createReceiptAGVTask(Zarsh zarsh, List<Zarsi> zarsiList, String allocationStationCode) {
        Station station = stationService.getStaionByCode(allocationStationCode);
        if (station == null) {
            throw new ServiceException(allocationStationCode + "站台异常,未找到站台");
        }
        int layer = station.getLayer();
        // 来源点位
        String fromPoint = station.getCode();
        String taskTypes = QuantityConstant.RECEIPT_AGV;
        // 来源点位
        String toPoint = "";
        String toPos = zarsh.getToPos();
        // 去向位置 如果找不到去向位置 就在后面分配库位 或者 分配立库交互站台 根据 库区类型来
        if (StringUtils.isNotEmpty(toPos)) {
            Station station1 = stationService.getStationBySAPCode(toPos);
            toPoint = station1.getCode();
        }

        String zoneCode = zarsh.getCrnIn();
        if (StringUtils.isEmpty(zoneCode)) {
            zoneCode = station.getZoneCode();
        }
        String containerCode = zarsh.getDrumId();
        Integer pointType = -1;
        Container container = null;
        if (StringUtils.isEmpty(containerCode)) {
            containerCode = containerService.createContainerByZone(zoneCode);
        }
        container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
        //没有托盘创建虚拟托盘
        if (container == null) {
            containerCode = containerService.createContainerByZone(zoneCode);
            container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
        }
        if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
            throw new ServiceException("托盘已经锁定!" + containerCode);
        }
        if (StringUtils.isNotNull(container.getPlType())) {
            if (StringUtils.isNotNull(container.getPlType())) {
                if (container.getPlType() == 0) {
                    pointType = 1;
                }
                if (container.getPlType() == 1) {
                    pointType = 2;
                }
            }
        }
        if (StringUtils.isNotEmpty(container.getLocationCode())) {
            throw new ServiceException("容器在库位" + container.getLocationCode() + "上!");
        }
        int unCompleteTask = agvTaskService.getUnCompleteTask(containerCode);
        if (unCompleteTask > 0) {
            throw new ServiceException(containerCode + "容器存在未完成的AGV任务!");
        }

        AgvTask agvTask = new AgvTask();
        agvTask.setLayer(layer);
        agvTask.setIsEmpty(zarsh.getInKind());
        agvTask.setFromPoint(fromPoint);
        agvTask.setToPoint(toPoint);
        agvTask.setZoneCode(zoneCode);
        // AGV任务类型都为配送
        agvTask.setTaskType(QuantityConstant.PEISONG_AGV);
        agvTask.setTaskTypes(taskTypes);
        agvTask.setPalletNo(containerCode);
        agvTask.setUniqueId(zarsh.getUniqueId());
        agvTask.setCreatedBy(QuantityConstant.PLATFORM_SAP);
        agvTask.setCreated(DateUtils.getNowDate());
        agvTask.setPointType(pointType);
        //添加上布物料直径
        if(zarsiList.size() == 1){
            agvTask.setLot(zarsiList.get(0).getCharg());
            MaterialDiameter materialDiameter = materialDiameterService.getOne(new LambdaQueryWrapper<MaterialDiameter>()
                    .eq(MaterialDiameter::getLot, agvTask.getLot()));
            if(materialDiameter != null){
                agvTask.setMaterialDiameter(materialDiameter.getDiameter());
            }
        }
        agvTaskService.save(agvTask);
        containerService.updateStatus(containerCode, QuantityConstant.STATUS_CONTAINER_LOCK, QuantityConstant.WAREHOUSECODE);
        if (StringUtils.isEmpty(zarsh.getCrnIn())) {
            //根据库区 楼层 巷道 获取终点站台
            String port = getLKAGVPort(SAPUtils.getZoneCode(zarsh), layer,null);
            agvTask.setToPoint(port);
            agvTaskService.updateById(agvTask);
        }else{
            warecellAllocationService.agvWarecellAllocation(agvTask);
        }
        return agvTask;
    }

    public AgvTask createShipmentAGVTask(Zarsh zarsh, List<Zarsi> zarsiList, String allocationStationCode) {
        String zoneCode = null;
        String locationCode = zarsh.getLocation();
        Station station = stationService.getStaionByCode(allocationStationCode);
        if (station == null) {
            throw new ServiceException(allocationStationCode + "站台异常,未找到站台");
        }
        int layer = station.getLayer();
        // 校验站台是否可以直接使用
        if (StringUtils.isEmpty(locationCode)) {
            List<String> arys = new ArrayList<>();
            arys.add("0");
            zoneCode = SAPUtils.getZoneCode(zarsh);
            String palletCode = containerService.getEmptyContainerList(zoneCode, arys, null, zarsh.getPlType());
            if (StringUtils.isEmpty(palletCode)) {
                throw new ServiceException(zoneCode + " AGV缓存区没有空容器可以出");
            }
            Container container1 = containerService.getContainerByCode(palletCode, QuantityConstant.WAREHOUSECODE);
            locationCode = container1.getLocationCode();
        }
        Location location = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
        if (location == null) {
            throw new ServiceException(locationCode + " 系统内未找到改库位");
        }
        Container container = containerService.getContainerByCode(location.getContainerCode(), QuantityConstant.WAREHOUSECODE);
        if (container == null) {
            throw new ServiceException(locationCode + " 系统内未找到改库位");
        }
        if (zarsh.getInKind() == 0) {
            if (!container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_EMPTY)) {
                throw new ServiceException(locationCode + " 容器状态不为空");
            }
        }
        int pointType = -1;
        if (StringUtils.isNotNull(container.getPlType())) {
            if (container.getPlType() == 0) {
                pointType = 3;
            }
            if (container.getPlType() == 1) {
                pointType = 0;
            }
        }
        String fromPoint = locationCode;
        String taskTypes = QuantityConstant.SHIPMENT_AGV;
        String toPoint = station.getCode();

        AgvTask agvTask = new AgvTask();
        agvTask.setLayer(layer);
        agvTask.setIsEmpty(zarsh.getInKind());
        agvTask.setFromPoint(fromPoint);
        agvTask.setToPoint(toPoint);
        agvTask.setZoneCode(location.getZoneCode());
        // AGV任务类型都为配送
        agvTask.setTaskType(QuantityConstant.PEISONG_AGV);
        agvTask.setTaskTypes(taskTypes);
        agvTask.setPalletNo(location.getContainerCode());
        agvTask.setUniqueId(zarsh.getUniqueId());
        agvTask.setCreatedBy(QuantityConstant.PLATFORM_SAP);
        agvTask.setCreated(DateUtils.getNowDate());
        agvTask.setPointType(pointType);
        if(zarsiList.size() == 1){
            agvTask.setLot(zarsiList.get(0).getCharg());
            MaterialDiameter materialDiameter = materialDiameterService.getOne(new LambdaQueryWrapper<MaterialDiameter>()
                    .eq(MaterialDiameter::getLot, agvTask.getLot()));
            if(materialDiameter != null){
                agvTask.setMaterialDiameter(materialDiameter.getDiameter());
            }
        }
        agvTaskService.save(agvTask);
        containerService.updateStatus(container.getCode(), QuantityConstant.STATUS_CONTAINER_LOCK, QuantityConstant.WAREHOUSECODE);
        locationService.updateStatus(locationCode, QuantityConstant.STATUS_CONTAINER_LOCK, QuantityConstant.WAREHOUSECODE);
        return agvTask;
    }

    /**
     * 获取立库 agv交互站台
     * @param  toZoneCode
     * @return
     */
    public String getLKAGVPort(String toZoneCode, int layer,String roadWay) {
        List<Station> list = stationService.list(new LambdaQueryWrapper<Station>().eq(Station::getZoneCode, toZoneCode)
            .eq(Station::getDefineProperty, QuantityConstant.LK_AGV_PORT).eq(Station::getLayer, layer).like(StringUtils.isNotEmpty(roadWay),Station::getRoadWay,roadWay));
        List<String> stationCodes = list.stream().map(Station::getCode).collect(Collectors.toList());

        if (stationCodes.isEmpty()) {
            throw new ServiceException("该库区没有配置AGV 立库站台请前往配置");
        }

        LambdaQueryWrapper<TaskHeader> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TaskHeader::getZoneCode, toZoneCode).in(TaskHeader::getPort, stationCodes)
            .in(TaskHeader::getTaskType, 100, 200, 300, 400, 500, 600, 1100, 1200).le(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_ARRIVED_STATION);
        List<TaskHeader> list1 = taskHeaderService.list(queryWrapper);

        if (list1.isEmpty()) {
            return stationCodes.get(0);
        }
        Map<String, Integer> map = new HashMap<>();
        for (String stationCode : stationCodes) {
            map.put(stationCode,0);
        }
        for (TaskHeader taskHeader : list1) {
            if (map.containsKey(taskHeader)) {
                int num = map.get(taskHeader.getPort());
                num++;
                map.put(taskHeader.getPort(), num);
            } else {
                map.put(taskHeader.getPort(), 1);
            }
        }
        List<Map.Entry<String, Integer>> list2 = new ArrayList(map.entrySet());
        Collections.sort(list2, (o1, o2) -> (o1.getValue() - o2.getValue()));
        List<String> ports = list2.stream().map(Map.Entry::getKey).collect(toList());
        String s = ports.get(0);
        return s;
    }

}