RobotController.java 54.8 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 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
package com.huaheng.api.robot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.general.domain.InventoryQueryDomain;
import com.huaheng.api.robot.domain.*;
import com.huaheng.api.robot.service.RobotService;
import com.huaheng.api.wcs.domain.PageModel;
import com.huaheng.api.wcs.domain.RobotInventory;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.emptyOutHandle.EmptyOutHandleService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.controller.BaseController;
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.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.domain.LocationStatus;
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.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.InventoryMaterialSummary.domain.InventoryMaterialSummary;
import com.huaheng.pc.inventory.InventoryMaterialSummary.service.InventoryMaterialSummaryService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
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.ShipmentTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

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

    @Resource
    private EmptyOutHandleService emptyOutHandleService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private StationService stationService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private ShipmentTaskService shipmentTaskService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryMaterialSummaryService inventoryMaterialSummaryService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private RobotService robotService;
    @Resource
    private LocationService locationService;
    @Resource
    private WorkTaskService workTaskService;


    //并发控制
    Map<String, Boolean> runningTaskMap = new HashMap<>();


    @PostMapping("/callReceipt")
    @ApiOperation("生产入库")
    @ApiLogger(apiName = "生产入库", from = "ROBOT")
    @ResponseBody
    public AjaxResult callReceipt(@RequestBody Map<String, String> map) {
        String materialCodePLC = map.get("materialCodePLC");
        String orderNo = map.get("orderNo");
        String line = map.get("line");
        String materialCode = map.get("materialCode");
        String qty = map.get("qty");
        String area = map.get("area");
        String qcc = map.get("qc");
        String typee = map.get("type");
        String from = map.get("from");
        int qc = QuantityConstant.QC_NOCHECK;
        if(StringUtils.isNotEmpty(qcc)) {
            qc = Integer.parseInt(qcc);
        }
        int type = QuantityConstant.QC_NOCHECK;
        if(StringUtils.isNotEmpty(typee)) {
            type = Integer.parseInt(typee);
        }
        if (line == null) {
            return AjaxResult.error("没有line");
        }
        if (materialCode == null) {
            return AjaxResult.error("没有materialCode");
        }
        if (qty == null) {
            return AjaxResult.error("没有qty");
        }
        if (area == null) {
            return AjaxResult.error("没有area");
        }
        if(area.equals("7")){
            materialCodePLC = "0";
        }
        if (materialCodePLC == null) {
            return AjaxResult.error("没有materialCodePLC");
        }
        if (orderNo == null) {
            return AjaxResult.error("没有orderNo");
        }
        int materialCodePLCint = Integer.parseInt(materialCodePLC);
        int finalQc = qc;
        int finalType = type;
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = robotService.innerCallReceipt(line,
                        materialCode, qty, area, materialCodePLCint, orderNo, finalQc, finalType);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }


    @PostMapping("/callShipment")
    @ApiOperation("生产叫料")
    @ApiLogger(apiName = "生产叫料", from = "ROBOT")
    @ResponseBody
    public AjaxResult callShipment(@RequestBody Map<String, Object> map) {
        String materialCodePLC = (String) map.get("materialCodePLC");
        String orderNo = (String) map.get("orderNo");
        String productCode = (String) map.get("productCode");
        String line = (String) map.get("line");
        String materialCode = (String) map.get("materialCode");
        String qty = map.get("qty").toString();
        String area = map.get("area").toString();
        String qc = (String) map.get("qc");
        String from = (String) map.get("from");
        Object typee =  map.get("type");
        //内圈
        String insideOrderNo = (String) map.get("insideOrderNo");
        Integer taskType = (Integer) map.get("taskType");
        Integer isNeedQC = (Integer) map.get("isNeedQC");
        Object rule = map.get("rule");
        String[] objects = null;
        if(rule != null && !"".equals(rule.toString())){
            objects = JSON.parseObject(rule.toString(),String[].class);
        }
        Area7Param area7Param = new Area7Param();
        area7Param.setInsideOrderNo(insideOrderNo);
        area7Param.setTaskType(taskType);
        area7Param.setIsNeedQC(isNeedQC);
        area7Param.setRule(objects);
        if (line == null) {
            return AjaxResult.error("没有line");
        }
        if (materialCode == null) {
            return AjaxResult.error("没有materialCode");
        }
        if (qty == null) {
            return AjaxResult.error("没有qty");
        }
        if (area == null) {
            return AjaxResult.error("没有area");
        }
        if("7".equals(area)){
            materialCodePLC = "0";
        }
        if (materialCodePLC == null || "".equals(materialCodePLC)) {
            return AjaxResult.error("没有materialCodePLC");
        }
        if (orderNo == null) {
            return AjaxResult.error("没有orderNo");
        }
        int type = 0;
        if(typee !=null ) {
            if( StringUtils.isNotEmpty(typee.toString())){
                type = Integer.parseInt(typee.toString());
            }
        }
        int materialCodePLCint = Integer.parseInt(materialCodePLC);
        int finalType = type;
        AjaxResult ajaxResult = null;
        if("7".equals(area)){
            ajaxResult = handleMultiProcess(new MultiProcessListener() {
                @Override
                public AjaxResult doProcess() {
                    AjaxResult ajaxResult = robotService.innerCallShipmentByArea7(line, materialCode, productCode, qty,
                            area, materialCodePLCint, orderNo, finalType,area7Param);
                    return ajaxResult;
                }
            });
        }else{
            ajaxResult = handleMultiProcess(new MultiProcessListener() {
                @Override
                public AjaxResult doProcess() {
                    AjaxResult ajaxResult = robotService.innerCallShipment(line, materialCode, productCode, qty,
                            area, materialCodePLCint, orderNo, finalType);
                    return ajaxResult;
                }
            });
        }


        return ajaxResult;
    }



    /**
     * 查询托盘库存
     */
    @PostMapping("/searchContainerInventory")
    @ApiOperation("查询托盘库存")
    @ApiLogger(apiName = "查询托盘库存", from = "ROBOT")
    @ResponseBody
    public AjaxResult searchContainerInventory(@RequestBody InventoryQueryDomain inventoryQueryDomain) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(StringUtils.isNotEmpty(inventoryQueryDomain.getWarehouseCode()),
                InventoryDetail::getWarehouseCode, inventoryQueryDomain.getWarehouseCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getContainerCode()),
                        InventoryDetail::getContainerCode, inventoryQueryDomain.getContainerCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getLocationCode()),
                        InventoryDetail::getLocationCode, inventoryQueryDomain.getLocationCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getCompanyCode()),
                        InventoryDetail::getCompanyCode, inventoryQueryDomain.getCompanyCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getZoneCode()),
                        InventoryDetail::getZoneCode, inventoryQueryDomain.getZoneCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getMaterialCode()),
                        InventoryDetail::getMaterialCode, inventoryQueryDomain.getMaterialCode());
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        return AjaxResult.success(inventoryDetailList);
    }

    /**
     * 查询托盘库存
     */
    @PostMapping("/searchContainer")
    @ApiOperation("查询托盘库存")
    @ApiLogger(apiName = "查询托盘库存", from = "ROBOT")
    @ResponseBody
    public AjaxResult searchContainer(@RequestBody InventoryQueryDomain inventoryQueryDomain) {
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryDetailLambdaQueryWrapper.eq(StringUtils.isNotEmpty(inventoryQueryDomain.getWarehouseCode()),
                InventoryDetail::getWarehouseCode, inventoryQueryDomain.getWarehouseCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getContainerCode()),
                        InventoryDetail::getContainerCode, inventoryQueryDomain.getContainerCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getLocationCode()),
                        InventoryDetail::getLocationCode, inventoryQueryDomain.getLocationCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getCompanyCode()),
                        InventoryDetail::getCompanyCode, inventoryQueryDomain.getCompanyCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getZoneCode()),
                        InventoryDetail::getZoneCode, inventoryQueryDomain.getZoneCode())
                .eq(StringUtils.isNotEmpty(inventoryQueryDomain.getMaterialCode()),
                        InventoryDetail::getMaterialCode, inventoryQueryDomain.getMaterialCode());
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
        List<InventoryDetail> removeInventoryDetailList = new ArrayList<>();
        if (inventoryDetailList != null && inventoryDetailList.size() > 0) {
            for (InventoryDetail inventoryDetail : inventoryDetailList) {
                String zoneCode = inventoryDetail.getZoneCode();
                if (zoneCode.equals("H") || zoneCode.equals("I")) {
                    int taskQty = inventoryDetail.getTaskQty().intValue();
                    if (taskQty != 0) {
                        removeInventoryDetailList.add(inventoryDetail);
                    }
                }
            }
            inventoryDetailList.removeAll(removeInventoryDetailList);
        }
        return AjaxResult.success(inventoryDetailList);
    }

    @PostMapping("/back")
    @ApiOperation("wcs托盘回库")
    @ResponseBody
    public AjaxResult back(@RequestBody WcsTask wcsTask) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = robotService.innerBack(wcsTask);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 查询库存
     */
    @PostMapping("/searchInventory")
    @ApiOperation("查询库存")
    @ResponseBody
    @ApiLogger(apiName = "查询库存", from = "ROBOT")
    public AjaxResult searchInventory(@RequestBody InventoryMaterialSummary inventoryMaterialSummary) {
        String area = inventoryMaterialSummary.getArea();
        if (StringUtils.isNotEmpty(area)) {
            LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
            zoneLambdaQueryWrapper.eq(Zone::getArea, area);
            Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
            inventoryMaterialSummary.setZoneCode(zone.getCode());
        }
        List<InventoryMaterialSummary> list = inventoryMaterialSummaryService.selectInventoryDetailByInventory(inventoryMaterialSummary);
        if (list == null) {
            list = Collections.emptyList();
        }
        if("7".equals(area)){
            List<InventoryMaterialSummary> inventoryMaterialSummaries = inventoryMaterialSummaryService.inventoryMaterialSummaryByLevel(list);
            return AjaxResult.success(inventoryMaterialSummaries);
        }
        //筛选库存汇总数据的专用方法
        List<InventoryMaterialSummary> details = inventoryMaterialSummaryService.duplicateRemoval(list);
        return AjaxResult.success(details);
    }


    /**
     * 查询是否满足自动出库
     */
    @PostMapping("/searchShipmentTask")
    @ApiOperation("查询出库任务")
    @ResponseBody
    @ApiLogger(apiName = "查询出库任务", from = "ROBOT")
    public AjaxResult searchShipmentTask(@RequestBody Map<String, String> map) {
        String area = map.get("area");
        String line = map.get("line");

        if (line == null) {
            return AjaxResult.error("没有line");
        }
        if (area == null) {
            return AjaxResult.error("没有area");
        }

        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getArea,area);
        Zone zoneServiceOne = zoneService.getOne(zoneLambdaQueryWrapper);


        LambdaQueryWrapper<Station> stationLambdaQueryWrapper = Wrappers.lambdaQuery();
        stationLambdaQueryWrapper.eq(Station::getArea, area)
                .eq(Station::getLine, line);
        List<Station> stationList = stationService.list(stationLambdaQueryWrapper);
        if (stationList == null || stationList.size() == 0) {
            return AjaxResult.error("区域和线体不正确");
        }
        List<String> portCodeList = stationList.stream().map(t -> t.getCode()).collect(Collectors.toList());
        if (portCodeList == null || portCodeList.size() == 0) {
            return AjaxResult.error("线体编码或者区域不正确,没有找到对应出口");
        }
        if (portCodeList.size() > 1) {
            for (String portCode : portCodeList) {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getPort, portCode)
                        .eq(TaskHeader::getZoneCode,zoneServiceOne.getCode())
                        .isNull(TaskHeader::getOrderNo)
                        .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT,
                                QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                List<TaskHeader> taskHeaders = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
                if (taskHeaders != null && !taskHeaders.isEmpty()) {
                    return AjaxResult.error("还存在手动任务,无法下发自动任务!");
                }
            }
        } else {
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getPort, portCodeList.get(0))
                    .eq(TaskHeader::getZoneCode,zoneServiceOne.getCode())
                    .isNull(TaskHeader::getOrderNo)
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_SORTINGSHIPMENT,
                            QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            List<TaskHeader> taskHeaders = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            if (taskHeaders != null && !taskHeaders.isEmpty()) {
                return AjaxResult.error("还存在手动任务,无法下发自动任务!");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 出库抓取信息
     */
    @PostMapping("/addShipmentDetail")
    @ApiOperation("出库抓取信息")
    @ResponseBody
    @ApiLogger(apiName = "出库抓取信息", from="ROBOT")
    public AjaxResult addShipmentDetail(@RequestBody Map<String,String> map)
    {
        String containerCode = map.get("containerCode");
        String port = map.get("port");
        String materialCode = map.get("materialCode");
        String qty = map.get("qty");
        String sn = map.get("sn");
        String isPrint = map.get("isPrint");
        String position = map.get("position");
        String area = map.get("area");
        String qcc = map.get("qc");
        String from = map.get("from");
        String level = map.get("qC_Class");
        if(containerCode == null) {
            return AjaxResult.error("没有containerCode");
        }
        if(port == null) {
            return AjaxResult.error("没有port");
        }
        if(materialCode == null) {
            return AjaxResult.error("没有materialCode");
        }
        if(qty == null) {
            return AjaxResult.error("没有qty");
        }
        if(sn == null) {
            return AjaxResult.error("没有sn");
        }
        if(position == null) {
            return AjaxResult.error("没有position");
        }
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = robotService.innerAddShipmentDetail(containerCode,
                        materialCode, port, qty, sn, position, isPrint, area,level);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 入库抓取信息
     */
    @PostMapping("/addReceiptDetail")
    @ApiOperation("入库抓取信息")
    @ResponseBody
    @ApiLogger(apiName = "入库抓取信息", from="ROBOT")
    public AjaxResult addReceiptDetail(@RequestBody Map<String,String> map)
    {
        String containerCode = map.get("containerCode");
        String port = map.get("port");
        String materialCode = map.get("materialCode");
        String qty = map.get("qty");
        String sn = map.get("sn");
        String isPrint = map.get("isPrint");
        String position = map.get("position");
        String area = map.get("area");
        String qcc = map.get("qc");
        String typee = map.get("type");
        String from = map.get("from");
        String qC_class = map.get("qC_Class");
        String diameter = map.get("qC_diameter");
        String qC_result = map.get("qC_result");
        //钢球直径
        int diameterInt = 0;
        if(StringUtils.isNotEmpty(qC_result)){
            int anInt = Integer.parseInt(qC_result);
            if(anInt== QuantityConstant.QC_NOCHECK){
                qC_class = "NG";
            }
        }
        int qc = QuantityConstant.QC_NOCHECK;
        if(StringUtils.isNotEmpty(qcc)) {
            qc = Integer.parseInt(qcc);
        }
        int type = QuantityConstant.QC_NOCHECK;
        if(StringUtils.isNotEmpty(typee)) {
            type = Integer.parseInt(typee);
        }
        if(StringUtils.isNotEmpty(diameter)) {
            diameterInt = Integer.parseInt(diameter);
        }
        ParamDomain domain =new ParamDomain();
        domain.setLevel(qC_class);
        domain.setDiameter(diameterInt);

        if(containerCode == null) {
            return AjaxResult.error("没有containerCode");
        }
        if(port == null) {
            return AjaxResult.error("没有port");
        }
        if(materialCode == null) {
            return AjaxResult.error("没有materialCode");
        }
        if(qty == null) {
            return AjaxResult.error("没有qty");
        }
        if(position == null) {
            return AjaxResult.error("没有position");
        }
        int finalQc = qc;
        int finalType = type;
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = robotService.innerAddReceiptDetail(containerCode,
                        materialCode, port, qty, sn, position, isPrint, area, finalQc, finalType,domain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    //并发控制
    Map<String, Boolean> runningLocationMap = new HashMap<>();

    /**
     * 查询库位列表
     */
    @PostMapping("/getAllLocation")
    @ApiOperation("查询库位列表")
    @ResponseBody
    @ApiLogger(apiName = "查询库位列表", from="ROBOT")
    public AjaxResult getAllLocation (@RequestBody Map<String,String> map) {
        String area = map.get("area");
        if(StringUtils.isEmpty(area)) {
            return AjaxResult.error("area不能为空");
        }
        String taskKey = "autoExecuteTask";
        if(MapUtils.getBoolean(runningLocationMap, taskKey, false)) {
            AjaxResult.error("系统正在处理上条消息");
        }
        AjaxResult ajxResult = new AjaxResult();
        try {
            runningLocationMap.put(taskKey, true);
            ajxResult = innerGetAllLocation(area);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            runningLocationMap.put(taskKey, false);
        }
        return ajxResult;
    }

    private AjaxResult innerGetAllLocation(String area) {
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.in(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        String type = zone.getCode();
        String warehouseCode = "CS0001";
        String row = null, line = null, layer = null;
        /* 查询库位信息*/
        AjaxResult ajaxResult = locationService.selectLocationByLocationTypeAndWarehouseCode(type, warehouseCode);
        if(ajaxResult.hasErr()){
            return ajaxResult;
        }
        List<Location> locations = (List<Location>)ajaxResult.getData();
        List<Location> locationList = new ArrayList<>();
        /* 查询库存明细*/
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.selectInventoryDetailByWarehouse(warehouseCode);

        for (Location location1 : locations) {
            InventoryDetail inventoryDetail = null;
            String materialName = null;
            for (InventoryDetail inventoryDetail2 : inventoryDetailList) {
                if(location1.getCode().equals(inventoryDetail2.getLocationCode())) {
                    inventoryDetail = inventoryDetail2;
                }
            }
            List<InventoryDetail> inventoryDetails = inventoryDetailList.stream().filter(inventoryDetail1 ->
                    inventoryDetail1.getLocationCode().equals(location1.getCode())).collect(Collectors.toList());
            int userDef3 = 0;
            String status = location1.getStatus();
            String containerCode = location1.getContainerCode();
            List<String> materialNameList = inventoryDetails.stream().map(InventoryDetail::getMaterialName).collect(Collectors.toList());
            List<String> batchList = inventoryDetails.stream().map(InventoryDetail::getBatch).collect(Collectors.toList());
            List<String> materialCodeList = inventoryDetails.stream().map(InventoryDetail::getMaterialCode).collect(Collectors.toList());
            List<BigDecimal> qtyList = inventoryDetails.stream().map(InventoryDetail::getQty).collect(Collectors.toList());
            List<String> snList = inventoryDetails.stream().map(InventoryDetail::getSn).collect(Collectors.toList());
            String matreialName = null;
            String matreialCode = null;
            int qty = 0;
            if(qtyList != null && qtyList.size() > 0) {
                qty = qtyList.size();
            }
            if(materialCodeList != null && materialCodeList.size() > 0) {
                matreialCode = materialCodeList.get(0);
            }
            if(materialNameList != null && materialNameList.size() > 0) {
                matreialName = materialNameList.get(0);
            }
            if(QuantityConstant.STATUS_LOCATION_EMPTY.equals(status)) {
                if(StringUtils.isEmpty(containerCode)) {
                    userDef3 = LocationStatus.IDLE_EMPTY_LOCATION;
                } else {
                    if(inventoryDetail == null) {
                        userDef3 = LocationStatus.IDLE_EMPTY_CONTAINER;
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
                    } else {
                        location1.setMaterialName2(matreialName);
                        location1.setMaterialCode2(matreialCode);
                        location1.setBatch(batchList);
                        location1.setSnList(snList);
                        location1.setQty2(qty);
                        userDef3 = LocationStatus.IDLE_FULL_CONTAINER;
                        if(StringUtils.isNotEmpty(matreialCode)) {
                            location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_SOME);
                        }
                    }
                }
            } else if(QuantityConstant.STATUS_LOCATION_LOCK.equals(status)) {
                if(StringUtils.isEmpty(containerCode)) {
                    userDef3 = LocationStatus.LOCK_EMPTY_LOCATION;
                } else {
                    if(inventoryDetail == null) {
                        userDef3 = LocationStatus.LOCK_EMPTY_CONTAINER;
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                    } else {
                        location1.setMaterialName2(matreialName);
                        location1.setMaterialCode2(matreialCode);
                        location1.setBatch(batchList);
                        location1.setSnList(snList);
                        location1.setQty2(qty);
                        userDef3 = LocationStatus.LOCK_FULL_CONTAINER;
                        if(StringUtils.isNotEmpty(matreialCode)) {
                            location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                        }
                    }
                }
            }


            location1.setUserDef3(String.valueOf(userDef3));
            locationList.add(location1);
        }

        return AjaxResult.success(locationList);
    }

    /**
     * 分页查询库位列表
     */
    @PostMapping("/getPortInPage")
    @ApiOperation("分页查询库位列表")
    @ResponseBody
    @ApiLogger(apiName = "分页查询库位列表", from="ROBOT")
    public AjaxResult getPortInPage (@RequestBody Map<String,String> map) {
        String area = map.get("area");
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        if(StringUtils.isEmpty(area)) {
            return AjaxResult.error("area不能为空");
        }
        if(StringUtils.isEmpty(pageNum)) {
            return AjaxResult.error("pageNum不能为空");
        }
        if(StringUtils.isEmpty(pageSize)) {
            return AjaxResult.error("pageSize不能为空");
        }
        String warehouseCode = "CS0001";
        List<String> list = Arrays.asList(area.split(","));
        LambdaQueryWrapper<Station> stationLambdaQueryWrapper = Wrappers.lambdaQuery();
        stationLambdaQueryWrapper.in(Station::getArea, list)
                .eq(Station::getWarehouseCode, warehouseCode);
        List<Station> stationList = stationService.list(stationLambdaQueryWrapper);
        for (Station station : stationList) {
            List<TaskInfo> taskInfos = new ArrayList<>();
            String port = station.getCode();
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getPort, port)
                                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            for(TaskHeader taskHeader : taskHeaderList) {
                TaskInfo taskInfo = new TaskInfo();
                String containerCode = taskHeader.getContainerCode();
                LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
                inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode);
                List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
                List<String> materialNameList = inventoryDetailList.stream().map(InventoryDetail::getMaterialName).collect(Collectors.toList());
                List<String> batchList = inventoryDetailList.stream().map(InventoryDetail::getBatch).collect(Collectors.toList());
                List<String> materialCodeList = inventoryDetailList.stream().map(InventoryDetail::getMaterialCode).collect(Collectors.toList());
                List<BigDecimal> qtyList = inventoryDetailList.stream().map(InventoryDetail::getQty).collect(Collectors.toList());
                List<String> snList = inventoryDetailList.stream().map(InventoryDetail::getSn).collect(Collectors.toList());
                List<Integer> positionList = inventoryDetailList.stream().map(InventoryDetail::getPosition).collect(Collectors.toList());

                taskInfo.setContainerCode(containerCode);
                taskInfo.setMaterialCode(materialCodeList.get(0));
                taskInfo.setMaterialName(materialNameList.get(0));
                taskInfo.setQty(qtyList.size());
                taskInfo.setBatchList(batchList);
                taskInfo.setSnList(snList);
                taskInfo.setPositionList(positionList);
                taskInfos.add(taskInfo);
            }
            station.setTaskInfo(taskInfos);
        }
        PageModel<Station> pm = new PageModel(stationList, Integer.parseInt(pageSize));
        List<Station> stationList1 = pm.getObjects(Integer.parseInt(pageNum));
        TaskInfoInventory taskInfoInventory = new TaskInfoInventory();
        taskInfoInventory.setStationList(stationList1);
        taskInfoInventory.setTotalSize(stationList1.size());

        return AjaxResult.success(taskInfoInventory);
    }

    /**
     * 分页查询所有库位列表
     */
    @PostMapping("/getAllLocationInPage")
    @ApiOperation("分页查询所有库位列表")
    @ResponseBody
    @ApiLogger(apiName = "分页查询所有库位列表", from="ROBOT")
    public AjaxResult getAllLocationInPage (@RequestBody Map<String,String> map) {
        String area = map.get("area");
        String pageNum = map.get("pageNum");
        String pageSize = map.get("pageSize");
        if(StringUtils.isEmpty(area)) {
            return AjaxResult.error("area不能为空");
        }
        if(StringUtils.isEmpty(pageNum)) {
            return AjaxResult.error("pageNum不能为空");
        }
        if(StringUtils.isEmpty(pageSize)) {
            return AjaxResult.error("pageSize不能为空");
        }

        String warehouseCode = "CS0001";
        String row = null, line = null, layer = null;
        List<String> list = Arrays.asList(area.split(","));

        /* 查询库位信息*/
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.in(Location::getArea, list)
                .eq(Location::getWarehouseCode, warehouseCode);
        List<Location> locations = locationService.list(locationLambdaQueryWrapper);
        List<Location> locationList = new ArrayList<>();

        /* 查询库存明细*/
        LambdaQueryWrapper<InventoryDetail> inventoryDetailLambda = Wrappers.lambdaQuery();
        inventoryDetailLambda.eq(InventoryDetail::getWarehouseCode, warehouseCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambda);

        for (Location location1 : locations) {
            area = location1.getArea();
            InventoryDetail inventoryDetail = null;
            String materialName = null;
            for (InventoryDetail inventoryDetail2 : inventoryDetailList) {
                if(location1.getCode().equals(inventoryDetail2.getLocationCode())) {
                    inventoryDetail = inventoryDetail2;
                }
            }
            List<InventoryDetail> inventoryDetails = inventoryDetailList.stream().filter(inventoryDetail1 ->
                    inventoryDetail1.getLocationCode().equals(location1.getCode())).collect(Collectors.toList());
            int userDef3 = 0;
            String status = location1.getStatus();
            String containerCode = location1.getContainerCode();
            List<String> materialNameList = inventoryDetails.stream().map(InventoryDetail::getMaterialName).collect(Collectors.toList());
            List<String> batchList = inventoryDetails.stream().map(InventoryDetail::getBatch).collect(Collectors.toList());
            List<String> materialCodeList = inventoryDetails.stream().map(InventoryDetail::getMaterialCode).collect(Collectors.toList());
            List<BigDecimal> qtyList = inventoryDetails.stream().map(InventoryDetail::getQty).collect(Collectors.toList());
            List<String> snList = inventoryDetails.stream().map(InventoryDetail::getSn).collect(Collectors.toList());
            List<Integer> positionList = inventoryDetails.stream().map(InventoryDetail::getPosition).collect(Collectors.toList());
            String matreialName = null;
            String matreialCode = null;
            int qty = 0;
            if(qtyList != null && qtyList.size() > 0) {
                qty = qtyList.size();
            }
            if(materialCodeList != null && materialCodeList.size() > 0) {
                matreialCode = materialCodeList.get(0);
            }
            if(materialNameList != null && materialNameList.size() > 0) {
                matreialName = materialNameList.get(0);
            }
            if(QuantityConstant.STATUS_LOCATION_EMPTY.equals(status)) {
                if(StringUtils.isEmpty(containerCode)) {
                    userDef3 = LocationStatus.IDLE_EMPTY_LOCATION;
                } else {
                    if(inventoryDetail == null) {
                        userDef3 = LocationStatus.IDLE_EMPTY_CONTAINER;
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
                    } else {
                        location1.setMaterialName2(matreialName);
                        location1.setMaterialCode2(matreialCode);
                        location1.setBatch(batchList);
                        location1.setSnList(snList);
                        location1.setPositionList(positionList);
                        location1.setQty2(qty);
                        userDef3 = LocationStatus.IDLE_FULL_CONTAINER;
                        if(StringUtils.isNotEmpty(matreialCode)) {
                            Material material = materialService.findAllByCode(matreialCode, warehouseCode);
                            int max = material.getMaxContainer();
                            if(area.equals("4")) {
                                max = material.getMax();
                            }
                            if(qty < max) {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_SOME);
                            } else {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_FULL);
                            }
                        }
                    }
                }
            } else if(QuantityConstant.STATUS_LOCATION_LOCK.equals(status)) {
                if(StringUtils.isEmpty(containerCode)) {
                    userDef3 = LocationStatus.LOCK_EMPTY_LOCATION;
                } else {
                    if(inventoryDetail == null) {
                        userDef3 = LocationStatus.LOCK_EMPTY_CONTAINER;
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                    } else {
                        location1.setMaterialName2(matreialName);
                        location1.setMaterialCode2(matreialCode);
                        location1.setBatch(batchList);
                        location1.setSnList(snList);
                        location1.setPositionList(positionList);
                        location1.setQty2(qty);
                        userDef3 = LocationStatus.LOCK_FULL_CONTAINER;
                        if(StringUtils.isNotEmpty(matreialCode)) {
                            Material material = materialService.findAllByCode(matreialCode, warehouseCode);
                            int max = material.getMaxContainer();
                            if(area.equals("4")) {
                                max = material.getMax();
                            }
                            if(qty < max) {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                            } else {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                            }
                        }
                    }
                }
            }

            if(location1.getDeleted()) {
                if(StringUtils.isEmpty(containerCode)) {
                    userDef3 = LocationStatus.DISABLE_EMPTY_LOCATION;
                } else {
                    if(inventoryDetail == null) {
                        userDef3 = LocationStatus.DISABLE_EMPTY_CONTAINER;
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
                    } else {
                        location1.setMaterialName2(matreialName);
                        location1.setMaterialCode2(matreialCode);
                        location1.setBatch(batchList);
                        location1.setSnList(snList);
                        location1.setPositionList(positionList);
                        location1.setQty2(qty);
                        userDef3 = LocationStatus.DISABLE_FULL_CONTAINER;
                        if(StringUtils.isNotEmpty(matreialCode)) {
                            Material material = materialService.findAllByCode(matreialCode, warehouseCode);
                            int max = material.getMaxContainer();
                            if(area.equals("4")) {
                                max = material.getMax();
                            }
                            if(qty < max) {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_SOME);
                            } else {
                                location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_FULL);
                            }
                        }
                    }
                }
            }
            location1.setUserDef3(String.valueOf(userDef3));
            locationList.add(location1);
        }

        PageModel<Location> pm = new PageModel(locationList, Integer.parseInt(pageSize));
        List<Location> locationList1 = pm.getObjects(Integer.parseInt(pageNum));
        RobotInventory robotInventory = new RobotInventory();
        robotInventory.setLocationList(locationList1);
        robotInventory.setTotalSize(locationList.size());

        return AjaxResult.success(robotInventory);
    }



    /**
     * agv入库
     */
    @PostMapping("/agvReceipt")
    @ApiOperation("agv入库")
    @ResponseBody
    @ApiLogger(apiName = "agv入库", from="ROBOT")
    public AjaxResult agvReceipt (@RequestBody Map<String,String> map) {
        String area = map.get("area");
        String type = map.get("type");
        String containerCode = map.get("containerCode");
        String qty = map.get("qty");
        String materialCode = map.get("materialCode");
        String taskIndex = map.get("taskIndex");
        String destinationLocation = map.get("destinationLocation");
        if(containerCode == null) {
            return AjaxResult.error("没有containerCode");
        }
        if(area == null) {
            return AjaxResult.error("没有area");
        }
        if(type == null) {
            return AjaxResult.error("没有type");
        }
        int areaInt = Integer.parseInt(area);
        if(areaInt != 4) {
            return AjaxResult.error("AGV只有4号区");
        }
        AjaxResult ajaxResult = workTaskService.createEmptyIn(containerCode, destinationLocation);
        if(!ajaxResult.hasErr()) {
            String taskId =  ajaxResult.getData().toString();
            TaskHeader taskHeader  = taskHeaderService.getById(taskId);
            if(taskHeader != null) {
                taskHeader.setTaskIndex(taskIndex);
                taskHeader.setAgv(1);
                taskHeader.setBack(1);
            }
            taskHeaderService.updateById(taskHeader);
            ajaxResult.setData("P5001");
        }
        return ajaxResult;
    }

    /**
     * agv出库
     */
    @PostMapping("/agvShipment")
    @ApiOperation("agv出库")
    @ResponseBody
    @ApiLogger(apiName = "agv出库", from="ROBOT")
    public AjaxResult agvShipment (@RequestBody Map<String,Object> map) {
        String area = String.valueOf( (Integer) map.get("area"));
        String type =String.valueOf( (Integer) map.get("type"));
        List<String> containerCodeList =  (List<String>) map.get("containerCodeList");
//        String stationCode =  (String) map.get("stationCode");
        String taskIndex = (String) map.get("taskIndex");
        if(containerCodeList == null) {
            return AjaxResult.error("没有containerCodeList");
        }
        if(area == null) {
            return AjaxResult.error("没有area");
        }
        if(type == null) {
            return AjaxResult.error("没有type");
        }
//        if(stationCode == null) {
//            return AjaxResult.error("没有stationCode");
//        }
        if(taskIndex == null) {
            return AjaxResult.error("没有taskIndex");
        }
        AjaxResult ajaxResult = null;
        int areaInt = Integer.parseInt(area);
        if(areaInt != 4) {
            return AjaxResult.error("AGV只有4号区");
        }
        String stationCode = "P5001";
        for(String containerCode : containerCodeList) {
            String line = "5001";
            String warehouseCode = "CS0001";
            Container container = containerService.getContainerByCode(containerCode, warehouseCode);
            String locationCode = container.getLocationCode();
            String orderNo = "AGV";
            int materialCodePlc = 0;
            String materialCode = null;
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode)
                    .eq(InventoryDetail::getTaskQty, 0);
            List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            if (inventoryDetailList != null && inventoryDetailList.size() > 0) {
                InventoryDetail inventoryDetail = inventoryDetailList.get(0);
                materialCode = inventoryDetail.getMaterialCode();
            } else {
                return AjaxResult.error("没有找到库存");
            }

            ajaxResult = shipmentTaskService.createWholeShipmentTask(containerCode, locationCode, materialCode,
                    warehouseCode, stationCode, materialCodePlc, orderNo, line, 1);
            if (!ajaxResult.hasErr()) {
                Integer taskId = (Integer) ajaxResult.getData();
                TaskHeader taskHeader = taskHeaderService.getById(taskId);
                if (taskHeader != null) {
                    taskHeader.setTaskIndex(taskIndex);
                }
                taskHeaderService.updateById(taskHeader);
                ajaxResult.setData("P5001");
            }
        }
        return ajaxResult;
    }


    /**
     * 查询最近的一条记录
     */
    @GetMapping("/list")
    @ApiOperation("查询最近的一条记录")
    @ResponseBody
    @ApiLogger(apiName = "查询最近的一条记录", from="ROBOT")
    public AjaxResult search () {
        String taskKey = "search";
        if(MapUtils.getBoolean(runningTaskMap, taskKey, false)) {
            return AjaxResult.error("不允许重复请求");
        }
        AjaxResult ajaxResult = AjaxResult.error("不允许重复请求");
        try {
            runningTaskMap.put(taskKey, true);
            ajaxResult = innerSearch();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            runningTaskMap.put(taskKey, false);
        }
        return ajaxResult;
    }

    private AjaxResult innerSearch() {
        List<String> zoneList = new ArrayList<>();
        zoneList.add("A");
        zoneList.add("B");
        zoneList.add("C");
        zoneList.add("D");
        zoneList.add("E");
        zoneList.add("F");
        zoneList.add("G");
        zoneList.add("H");
        zoneList.add("I");
        List<DigitalInfo> digitals = taskHeaderService.selectTaskIdAndArea(zoneList);
        if(digitals == null || digitals.size() == 0){
            return AjaxResult.error("查询异常,请再次查询!");
        }
        for (DigitalInfo digital : digitals) {
            String zoneCode = digital.getZoneCode();
            int area = digital.getArea();
            int containerSomeSize = inventoryHeaderService.count(new LambdaQueryWrapper<InventoryHeader>().eq(InventoryHeader::getZoneCode, zoneCode));
            int allLocation = locationService.count(new LambdaQueryWrapper<Location>().eq(Location::getArea, area));
            int noGoodsQuantity = allLocation - containerSomeSize;
            String locUtilization  = ((float) containerSomeSize / (float) allLocation) * 100 + "%";
            int id = Integer.parseInt(digital.getCurrentTaskCode());
            TaskHeader taskHeader = taskHeaderService.getOne(new LambdaQueryWrapper<TaskHeader>().eq(TaskHeader::getId, id));
            if(taskHeader == null){
                continue;
            }
            Date taskStartTime = taskHeader.getCreated();
            Date taskEndTime = taskHeader.getLastUpdated();

            digital.setInventoryLocNo(containerSomeSize);
            digital.setLocUtilization(locUtilization);
            digital.setNoGoodsQuantity(noGoodsQuantity);
            digital.setTaskStartTime(taskStartTime);
            digital.setTaskEndTime(taskEndTime);
            List<TaskDetail> list = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getTaskId, id));
            if (list.size() > 0) {
                TaskDetail taskDetail = list.get(0);
                MaterialInfo materialInfo = new MaterialInfo();
                materialInfo.setMaterialName(taskDetail.getMaterialName());
                materialInfo.setMaterialNo(taskDetail.getMaterialCode());
                materialInfo.setReallyNumber(list.size());
                digital.setMaterialInfo(materialInfo);
            }
        }
        return AjaxResult.success(digitals);
    }

    /**
     * 查询库位列表
     */
    @PostMapping("/pullTaskInfo")
    @ApiOperation("拉取出/入任务信息")
    @ResponseBody
    @ApiLogger(apiName = "拉取出/入任务信息", from="ROBOT")
    public AjaxResult pullTaskInfo (@RequestBody Map<String,String> map) {
        String warehouseCode = "CS0001";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startDateStr = map.get("startDate");
        String endDateStr = map.get("endDate");
        if(StringUtils.isEmpty(startDateStr) || StringUtils.isEmpty(endDateStr)){
            return AjaxResult.error("时间日期不能为null");
        }
        Date startDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, startDateStr);
        Date endDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, endDateStr);

        //分拣出库,补充入库,整盘入库,整盘出库
        Integer [] taskType = {QuantityConstant.TASK_TYPE_WHOLERECEIPT,
                QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT,
                QuantityConstant.TASK_TYPE_WHOLESHIPMENT,
                QuantityConstant.TASK_TYPE_SORTINGSHIPMENT};
        List<Integer> taskTypes = Arrays.asList(taskType);
        LambdaQueryWrapper<TaskDetail> taskDetailLambda = Wrappers.lambdaQuery();
        taskDetailLambda.eq(TaskDetail::getWarehouseCode,warehouseCode)
                .gt((startDate != null),TaskDetail::getCreated,startDate)
                .lt((endDate != null),TaskDetail::getCreated,endDate)
                .in(TaskDetail::getTaskType,taskTypes);
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambda);

        return AjaxResult.success(taskDetailList);
    }

    /**
     * 入库残盘返回
     */
    @PostMapping("/receiptContainerBack")
    @ApiOperation("入库残盘返回")
    @ResponseBody
    @ApiLogger(apiName = "入库残盘返回", from="中控")
    public AjaxResult receiptContainerBack (@RequestBody Map<String,String> map) {
        Integer area = Integer.parseInt(map.get("area"));
        String port = map.get("port");
//        String containerCode = map.get("containerCode");
//        if(containerCode == null) {
//            return AjaxResult.error("容器号containerCode不能为空");
//        }
        if(area == null) {
            return AjaxResult.error("库区area不能为空");
        }
        if(port == null) {
            return AjaxResult.error("出口port不能为空");
        }
        LambdaQueryWrapper<TaskHeader> query = Wrappers.lambdaQuery();
//        query.eq(TaskHeader::getContainerCode,containerCode);
        query.isNotNull(TaskHeader::getOrderNo);
        query.eq(TaskHeader::getArea,area);
        query.eq(TaskHeader::getPort,port);
        query.eq(TaskHeader::getStatus,50);
        query.eq(TaskHeader::getBack,0);
        query.in(TaskHeader::getTaskType,100,200)
                .last("LIMIT 1");
        TaskHeader taskheader = taskHeaderService.getOne(query);
        if(taskheader==null){
            return AjaxResult.error("找不到任务或任务已完成。");
        }
        List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskheader.getId());
        int size = taskDetails.size();
        if(size == 0 || size != taskheader.getPush()){
            return AjaxResult.error(taskheader.getOrderNo()+":未收到中控物料抓取信息").setData(taskheader.getOrderNo());
        }
        if(size != taskheader.getPush()){
            return AjaxResult.error(taskheader.getOrderNo()+":没有收到中控物料抓取信息").setData(taskheader.getOrderNo());
        }
        taskheader.setBack(1);
        boolean result = taskHeaderService.updateById(taskheader);
        if(!result){
            return AjaxResult.error("返回失败,重新请求!");
        }
        return AjaxResult.success(taskheader);
    }

    /**
     * 出库残盘返回
     */
    @PostMapping("/shipmentContainerBack")
    @ApiOperation("出库残盘返回")
    @ResponseBody
    @ApiLogger(apiName = "出库残盘返回", from="中控")
    public AjaxResult shipmentContainerBack (@RequestBody Map<String,String> map) {
        Integer area = Integer.parseInt(map.get("area"));
        String port = map.get("port");
        if(area == null) {
            return AjaxResult.error("库区area不能为空");
        }
        if(port == null) {
            return AjaxResult.error("出口port不能为空");
        }
        LambdaQueryWrapper<TaskHeader> query = Wrappers.lambdaQuery();
//        query.eq(TaskHeader::getContainerCode,containerCode);
        query.isNotNull(TaskHeader::getOrderNo);
        query.eq(TaskHeader::getStatus,50);
        query.eq(TaskHeader::getBack,0);
        query.eq(TaskHeader::getArea,area);
        query.eq(TaskHeader::getPort,port);
        query.in(TaskHeader::getTaskType,300,400)
                .last("LIMIT 1");
        TaskHeader taskheader = taskHeaderService.getOne(query);
        if(taskheader==null){
            return AjaxResult.error("找不到任务。");
        }
        List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskheader.getId());
        int size = taskDetails.size();
        if(size == 0 || size != taskheader.getPush()){
            return AjaxResult.error(taskheader.getOrderNo()+":未收到中控物料抓取信息").setData(taskheader.getOrderNo());
        }
        if(size != taskheader.getPush()){
            return AjaxResult.error(taskheader.getOrderNo()+":没有收到中控物料抓取信息").setData(taskheader.getOrderNo());
        }
        taskheader.setBack(1);
        boolean result = taskHeaderService.updateById(taskheader);
        if(!result){
            return AjaxResult.error("返回失败,重新请求!");
        }
        return AjaxResult.success(taskheader);
    }

}