ReceiptContainerHeaderServiceImpl.java 56.4 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
package com.huaheng.pc.receipt.receiptContainerHeader.service;

import com.alibaba.fastjson.JSONException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.mobile.receipt.ReceiptBill;
import com.huaheng.pc.config.company.domain.CompanyU8;
import com.huaheng.pc.config.company.service.CompanyU8Service;
import com.huaheng.pc.config.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.mapper.ConfigValueMapper;
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.receiptPreference.domain.ReceiptPreference;
import com.huaheng.pc.config.receiptPreference.mapper.ReceiptPreferenceMapper;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.mapper.ReceiptContainerHeaderMapper;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.srm.service.FinishedProductService;
import com.huaheng.pc.system.dict.service.IDictDataService;
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.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;

/**
 * @author huaheng
 */
@Service
public class ReceiptContainerHeaderServiceImpl extends ServiceImpl<ReceiptContainerHeaderMapper, ReceiptContainerHeader> implements ReceiptContainerHeaderService {

    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private LocationService locationService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private ReceiptPreferenceMapper receiptPreferenceMapper;
    @Resource
    private ConfigValueMapper configValueMapper;
    @Resource
    private ConfigService configService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private CompanyU8Service companyU8Service;
    @Resource
    private FinishedProductService finishedProductService;
    @Resource
    private IDictDataService dictDataService;
    @Resource
    private TaskDetailService taskDetailService;


    /**
     * 保存入库组盘
     * param receiptCode 入库单编码
     * param containerCode 容器编码
     * param receiptDetailId 入库单详情id
     * param locationCode 库位编码
     * param qty 收货数量
     * param locatingRule 定位规则
     *
     * @return 是否保存成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveContainer(ReceiptContainerView receivingDamain) {
        String receiptCode = receivingDamain.getReceiptCode();
        String containerCode = receivingDamain.getContainerCode();
        Integer receiptDetailId = receivingDamain.getReceiptDetailId();
        String locationCode = receivingDamain.getLocationCode();
        BigDecimal qty = receivingDamain.getQty();
        String locatingRule = receivingDamain.getLocatingRule();
        ReceiptDetail detail = receiptDetailService.getById(receiptDetailId);

        BigDecimal receiptQty = detail.getQty().subtract(detail.getTaskQty());
        if (receiptQty.compareTo(qty) < 0) {
            return AjaxResult.error("入库明细可用数量" + receiptQty + ",收货数量" + qty + ";不允许超收");
        }

        if (ShiroUtils.getWarehouseCode().equals("KS0001")) {
            if (StringUtils.isEmpty(detail.getUWarehouseCode())) {
                throw new ServiceException("该单据没有关联u8仓库");
            }
        }
        //检查容器编码合法性
        Integer taskType = checkContainer(containerCode, detail.getMaterialCode());
        switch (taskType) {
            case 1:
                //创建临时容器
//            containerCode = createContainer();
//            taskType = QuantityConstant.TASK_TYPE_WHOLERECEIPT;
                throw new ServiceException("没有容器");
            case 2:
                throw new ServiceException("该物料不能放在这个容器上,请检查该物料是否可以混放");
            case 3:
                return AjaxResult.error("该物料不能放在这个容器或货架上,该容器已被其他操作员使用");
            default:
                if (taskType == 0) {
                    throw new ServiceException("容器状态未知");
                }
                break;
        }

        /*if (taskType == 1) {
            throw new ServiceException("没有容器");
        } else if (taskType == 2) {
            throw new ServiceException("该物料不能放在这个容器上,请检查该物料是否可以混放");
        } else {
            if (taskType == 0){ throw new ServiceException("容器状态未知"); }
        }*/

        if (QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT == taskType.intValue()) {
            Container container = containerService.getContainerByCode(containerCode);
            if (StringUtils.isNotNull(container) && StringUtils.isNotEmpty(container.getLocationCode())) {
                locationCode = container.getLocationCode();
            }
        }
        //检查库位编码合法性
        checkLocationCode(locationCode, containerCode, taskType);

        int unCompleteCombineNumber = shipmentContainerHeaderService.getUnCompleteCombineNumber(containerCode);
        if (unCompleteCombineNumber > 0) {
            throw new ServiceException("该托盘已经用于出库组盘");
        }
        /* 新建保存组盘头表记录*/
        //根据容器编码查询组盘表头记录
        LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .le(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_TASK);
        lambda.last("limit 1");
        ReceiptContainerHeader receiptContainerHeader = this.getOne(lambda);

//        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        //当size大于等于1表示容器已经有组盘了
        if (receiptContainerHeader == null) {
            ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
            // 创建组盘头表
            receiptContainerHeader = createReceiptContainerHeader(receiptContainerHeader, containerCode, locationCode, receiptDetail, taskType, locatingRule);
            if (!this.save(receiptContainerHeader)) {
                throw new ServiceException("入库组盘头表保存失败");
            }
        } else {
//            receiptContainerHeader = list.get(0);
            if (receiptContainerHeader.getStatus() > QuantityConstant.RECEIPT_CONTAINER_TASK && receiptContainerHeader.getStatus() < QuantityConstant.RECEIPT_CONTAINER_FINISHED) {
                throw new ServiceException("容器已经生成任务,不能放物料了!");
            }
        }
//        List<ReceiptContainerHeader> receiptContainerHeaders = this.list(lambda);
        /* 更新入库单详情的收货数量*/
        //根据入库单详情id查询入库详情
        ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
        receiptDetail.setId(receiptDetailId);
        receiptDetail.setTaskQty(qty.add(receiptDetail.getTaskQty()));
        //更新入库单详情的收货数量
        if (!receiptDetailService.updateById(receiptDetail)) {
            throw new ServiceException("更新入库单详情失败");
        }

        receiptDetail = receiptDetailService.getById(receiptDetailId);
        //save组盘明细
        receiptContainerDetailAdd(receiptContainerHeader, receiptDetail, qty, containerCode, locationCode);
        //如果单据数量等于已收数量,更新入库详情状态和入库单状态
        if (receiptDetail.getQty().compareTo(receiptDetail.getTaskQty()) == 0) {
            if (StringUtils.isNotEmpty(locationCode)) {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_POSITION.toString());
            } else {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RECEIVING.toString());
            }
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("更新入库详情处理标记失败");
            }

            ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
            if (!receiptDetailService.updateById(receiptDetail1)) {
                throw new ServiceException("更新入库详情下一流程失败");
            }
        }
        receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
        return AjaxResult.success("组盘成功");
    }

    public ReceiptContainerHeader createReceiptContainerHeader(ReceiptContainerHeader receiptContainerHeader
            , String containerCode, String locationCode, ReceiptDetail receiptDetail, Integer taskType, String locatingRule) {
        Container container = containerService.getContainerByCode(containerCode);
        if (ShiroUtils.getWarehouseCode().equals("KS0001")) {
            if (!container.getCompanyCode().equals(receiptDetail.getCompanyCode())) {
                throw new ServiceException("容器货主属性和单据不一致,请换容器");
            }
            if (StringUtils.isNotEmpty(receiptDetail.getUWarehouseCode()) && StringUtils.isNotEmpty(container.getUWarehouseCode())) {
                if (!container.getUWarehouseCode().equals(receiptDetail.getUWarehouseCode())) {
                    throw new ServiceException("容器仓库属性和单据不一致,请换容器");
                }
            }
        }
        receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptContainerHeader.setCompanyCode(receiptDetail.getCompanyCode());
        receiptContainerHeader.setContainerCode(containerCode);

        receiptContainerHeader.setContainerType(container.getContainerType());
        receiptContainerHeader.setTaskType(taskType);
        //判断地面托盘
        if ("D".equals(container.getContainerType())) {
            receiptContainerHeader.setToLocation(container.getLocationCode());
            receiptContainerHeader.setFromLocation(container.getLocationCode());
            taskType = QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT;
        }
        if (ShiroUtils.getWarehouseCode().equals(QuantityConstant.WAREHOUSE_KS)) {
            //U8工单领料齐套验证
            ksCheckMesReceipt(receiptDetail);
        }

        if (taskType.equals(QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT)) {
            receiptContainerHeader.setFromLocation(container.getLocationCode());
            Location location = locationService.getLocationByCode(container.getLocationCode());
            String value = dictDataService.getDictValueByLabel(QuantityConstant.RULE_TASK_LOCATION, location.getZoneCode(), ShiroUtils.getWarehouseCode());
            int taskLocationRule = Integer.parseInt(value);
            if (taskLocationRule == QuantityConstant.RULE_TASK_SET_LOCATION) {
                receiptContainerHeader.setToLocation(container.getLocationCode());
            }
        } else {
//            receiptContainerHeader.setPort("P1004");
            receiptContainerHeader.setToLocation(locationCode);
        }
        receiptContainerHeader.setProjectNo(receiptDetail.getProjectNo());
        receiptContainerHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptContainerHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        receiptContainerHeader.setLocatingRule(locatingRule);
        return receiptContainerHeader;
    }

    //U8工单领料齐套验证
    public void ksCheckMesReceipt(ReceiptDetail receiptDetail) {
        ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptDetail.getReceiptId());
        if (receiptHeader.getReceiptType().equals("SI") && StringUtils.isNotEmpty(receiptDetail.getReferLineNum())) {
            CompanyU8 companyWu = new CompanyU8();
            companyWu.setCompanyCode(receiptHeader.getCompanyCode());
            companyWu = companyU8Service.getByCode(companyWu);
            if (companyWu == null) {
                throw new ServiceException("入库单对应的U8账套不存在");
            }
            Map detailMap = new HashMap();
            detailMap.put("MODID", receiptDetail.getReferLineNum());
            detailMap.put("iQuantity", receiptDetail.getQty().subtract(receiptDetail.getTaskQty()));
            detailMap.put("itemcode", receiptDetail.getMaterialCode());

            List<Map> maps = new ArrayList<>();
            maps.add(detailMap);

            Map<String, Object> map = new HashMap();
            map.put("CompanyCode", companyWu.getUCompanyCode());
            map.put("details", maps);
            AjaxResult ajaxResult = finishedProductService.checkMesReceipt(map);
            if (ajaxResult.hasErr()) {
                throw new ServiceException(ajaxResult.getMsg());
            }
        }
    }

    /**
     * 超收检查
     *
     * @param detail
     * @param qty
     */
    private void checkOverReceiving(ReceiptDetail detail, BigDecimal qty) {
        BigDecimal _total = detail.getTaskQty().add(qty);
        if (_total.compareTo(detail.getQty()) <= 0) {
            //收货量<=明细总量
            return;
        }

        ConfigValue configValue = configValueMapper.getConfigValue(ShiroUtils.getWarehouseCode(), "receipt");
        ReceiptPreference preference = receiptPreferenceMapper.getReceiptPreference(ShiroUtils.getWarehouseCode(), configValue.getValue());
        if (!preference.getAllowOverReceiving()) {
            //入库首选项配置为不允许超收
            throw new ServiceException(String.format("不允许超收"));
        }

        //超收比例
        BigDecimal _over_rate = new BigDecimal(String.valueOf(1 + preference.getAllowOverReceivingQty() / 100.0));
        //超收量上限
        BigDecimal _total_over = detail.getQty().multiply(_over_rate);
        if (_total_over.compareTo(_total) >= 0) {
            //在入库首选项的超收范围内
            return;
        }

        //超过了入库首选项配置的超收比例
        throw new ServiceException(String.format("超收上限是: %s%",
                _over_rate.multiply(new BigDecimal(100))));
    }

    /**
     * 批量撤销入库组盘
     *
     * @param ids 容器头表标识列表
     * @return true 全部取消成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean cancelByIds(List<Integer> ids) {
        for (Integer id : ids) {
            //如果已生成任务则不允许取消组盘
            if (this.getById(id).getStatus() < QuantityConstant.RECEIPT_CONTAINER_TASK) {
                //根据组盘头表id查询组盘明细表
                LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
                containerDetailLambda.eq(ReceiptContainerDetail::getReceiptContainerId, id);
                List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(containerDetailLambda);

                //查询入库单明细,减去已收数量,更新单据
                for (ReceiptContainerDetail receiptContainerDetail : receiptContainerDetails) {
                    ReceiptDetail receiptDetail = receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId());
                    receiptDetail.setTaskQty(receiptDetail.getTaskQty().subtract(receiptContainerDetail.getQty()));
                    if (!receiptDetailService.updateById(receiptDetail)) {
                        throw new ServiceException("回滚入库明细失败");
                    }

                    containerDetailLambda = Wrappers.lambdaQuery();
                    containerDetailLambda.eq(ReceiptContainerDetail::getReceiptId, receiptContainerDetail.getReceiptId());
                    List<ReceiptContainerDetail> containerDetailList = receiptContainerDetailService.list(containerDetailLambda);

                    //如果是临时容器,取消组盘时删除容器表
                    containerService.removeContainer(receiptContainerDetail.getContainerType(), receiptContainerDetail.getContainerCode());

                    //如果入库组盘没有该入库单的组盘信息,回滚入库单状态
                    if (containerDetailList == null) {
                        ReceiptHeader receiptHeader = new ReceiptHeader();
                        receiptHeader.setId(receiptContainerDetail.getReceiptId());
                        receiptHeader.setFirstStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
                        receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_RECEIVING);
                        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());

                        if (!receiptHeaderService.updateById(receiptHeader)) {
                            throw new ServiceException("回滚头表状态失败");
                        }
                    }

                    if (!receiptContainerDetailService.removeById(receiptContainerDetail.getId())) {
                        throw new ServiceException("删除入库组盘明细表失败,id是" + receiptContainerDetail.getId());
                    }
                }
                //删除入库组盘头表
                if (!this.removeById(id)) {
                    throw new ServiceException("删除入库组盘头表失败");
                }
            } else {
                throw new ServiceException("已生成任务不允许取消");
            }
        }
        return true;
    }

    /**
     * 检查容器状态
     *
     * @param record 容器实列
     * @return 成功 ;可以使用容器
     */
    @Override
    public AjaxResult checkContainer(ReceiptContainerView record) {
        //容器都是单据SN码,每个入库单都是新增容器

        if (com.huaheng.common.utils.StringUtils.isEmpty(record.getContainerCode())) {
            throw new ServiceException("容器不能为空");
        }

        if (taskHeaderService.UncompleteCount(record.getContainerCode()).intValue() > 0) {
            throw new ServiceException("容器已经存在任务,请更换容器");
        }
        return AjaxResult.success("");
    }


    @Override
    public AjaxResult batchSave(List<ReceiptContainerView> list) {
        if (list.size() < 1) {
            throw new ServiceException("收货明细是空!");
        }
        checkContainer(list.get(0));

        AjaxResult ajaxResult = null;
        for (ReceiptContainerView receiptContainerView : list) {
           /* ReceivingDamain receivingDamain=new ReceivingDamain();
            com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(receivingDamain, receiptContainerView);
            receivingDamain.setContainerCode(receiptContainerView.getReceiptContainerCode());*/

            ajaxResult = saveContainer(receiptContainerView);
        }
        return ajaxResult;
    }

    @Override
    public AjaxResult PDAbatchSave(List<ReceiptContainerView> list) {
        if (list.size() < 1) {
            throw new ServiceException("收货明细是空!");
        }
        checkContainer(list.get(0));

        AjaxResult ajaxResult = null;
        for (ReceiptContainerView receiptContainerView : list) {
           /* ReceivingDamain receivingDamain=new ReceivingDamain();
            com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(receivingDamain, receiptContainerView);
            receivingDamain.setContainerCode(receiptContainerView.getReceiptContainerCode());*/

            ajaxResult = PDAsaveContainer(receiptContainerView);
        }
        return ajaxResult;
    }

    /**
     * 移动端校验库位
     *
     * @param record
     * @return
     */
    @Override
    public Boolean mobileCheckLocationCode(ReceiptContainerView record) {
        return checkLocationCode(record.getLocationCode(), record.getContainerCode(), Integer.valueOf(record.getTaskType()));
    }

    /**
     * 检查容器编码合法性
     *
     * @param containerCode 容器编码
     * @param materialCode  物料编码
     * @return 返回容器状态,如果需自动生成容器编码返回1
     */
    @Transactional
    public Integer checkContainer(String containerCode, String materialCode) {
        Material material = materialService.getMaterialByCode(materialCode);
        if (StringUtils.isEmpty(containerCode)) {
            if (material.getAutoGenSerialNum() == 0) {
                return 1;
                //需自动生成容器编码
            } else {
                throw new ServiceException("容器不能为空");
            }
        }
        /**
         * 判断物料是否可以放在当前容器
         */
        if (!material.getIsMix()) {
            LambdaQueryWrapper<InventoryDetail> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(InventoryDetail::getContainerCode, containerCode);
            /* 该容器已存物料数*/
            int inventoryDetailCount = inventoryDetailService.count(queryWrapper);
            LambdaQueryWrapper<InventoryDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(InventoryDetail::getContainerCode, containerCode)
                    .eq(InventoryDetail::getMaterialCode, materialCode);
            /* 该容器存放该物料的记录数*/
            int inventoryDetailMaterialCount = inventoryDetailService.count(lambdaQueryWrapper);
            /* 存放多种物料时返回*/
            if (inventoryDetailCount != inventoryDetailMaterialCount) {
                return 2;
            }
        }
        LambdaQueryWrapper<Container> lambda = Wrappers.lambdaQuery();
        lambda.eq(Container::getCode, containerCode);
        lambda.eq(Container::getWarehouseCode, ShiroUtils.getWarehouseCode());
        Container container = containerService.getOne(lambda);
        if (container == null) {
            throw new ServiceException("该容器编号不存在");
        }
        if (ShiroUtils.getWarehouseCode().equals("KS0001")) {
            if (!ShiroUtils.getCompanyCodeList().contains(container.getCompanyCode())) {
                throw new ServiceException("该操作员没有此容器的操作权限");
            }
            if (StringUtils.isEmpty(container.getUWarehouseCode())) {
                throw new ServiceException("该容器没有关联u8仓库");
            }
        }
        if (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
//            throw new ServiceException("容器已经锁定,不能再组盘");
        }
        //检查该容器编码是否已存任务
        LambdaQueryWrapper<TaskHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(TaskHeader::getContainerCode, containerCode)
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_RELEASE)
                .eq(TaskHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
        if (taskHeaderService.count(lambdaQueryWrapper) > 0) {
            throw new ServiceException("容器已经存在任务,请更换容器");
        }
        //昆山检查该容器或者货架编码是否已存任务
        checkKSContainerORShelf(container);

        if (StringUtils.isEmpty(container.getLocationCode())) {
            return QuantityConstant.TASK_TYPE_WHOLERECEIPT;
        } else if (StringUtils.isNotEmpty(container.getStatus())
                && (container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_EMPTY)
                || container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_SOME))) {
            return QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT;
        } else if (QuantityConstant.STATUS_CONTAINER_FULL.equals(container.getStatus())) {
            throw new ServiceException("该容器已满,请更换容器");
        } else if (StringUtils.isNotEmpty(container.getStatus())
                && container.getStatus().equals(QuantityConstant.STATUS_CONTAINER_LOCK)) {
            //当容器锁住时,同一个用户可以再次组盘
            LambdaQueryWrapper<TaskHeader> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            lambdaQuery.eq(TaskHeader::getInternalTaskType, 100);
            lambdaQuery.eq(TaskHeader::getContainerCode, containerCode);
            if (ShiroUtils.getWarehouseCode().equals("KS0001")) {
                lambdaQuery.eq(TaskHeader::getCreatedBy, ShiroUtils.getUserName());
            }
            lambdaQuery.last("limit 1");
            lambdaQuery.orderByDesc(TaskHeader::getId);
            TaskHeader taskHeader = taskHeaderService.getOne(lambdaQuery);
            if (taskHeader != null) {
                return taskHeader.getTaskType();
            }
            return 3;
        }
        return 0;
    }


    public void checkKSContainerORShelf(Container container) {
        if (StringUtils.isNotEmpty(ShiroUtils.getWarehouseCode())
                && ShiroUtils.getWarehouseCode().equals(QuantityConstant.WAREHOUSE_KS)) {
            LambdaQueryWrapper<TaskHeader> lambdaQueryWrapper1 = Wrappers.lambdaQuery();
            if (container.getZoneCode().equals("AGV")) {
                if (StringUtils.isEmpty(container.getGoodsShelfNo())) {
                    throw new ServiceException("该容器没有绑定货架");
                }
                lambdaQueryWrapper1.eq(TaskHeader::getGoodsShelfNo, container.getGoodsShelfNo());
            } else {
                lambdaQueryWrapper1.eq(TaskHeader::getContainerCode, container.getCode());
            }
            lambdaQueryWrapper1.eq(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD);
            lambdaQueryWrapper1.eq(TaskHeader::getWarehouseCode, ShiroUtils.getWarehouseCode());
            lambdaQueryWrapper1.last("limit 1");
            TaskHeader task = taskHeaderService.getOne(lambdaQueryWrapper1);
            if (task != null) {
                if (task.getCreatedBy().equals(ShiroUtils.getUserName())) {
                } else {
                    throw new ServiceException("容器或货架已经存在任务,请更换容器");
                }
            }
        }
    }

    /**
     * 检查库位是否合法
     *
     * @param locationCode  库位编码
     * @param containerCode 容器编码
     * @param taskType      任务类型
     * @return
     */
    @Transactional
    Boolean checkLocationCode(String locationCode, String containerCode, Integer taskType) {
        //如果选了库位,就要校验库位和已经组盘的库位是否一致,避免重入库
        if (StringUtils.isEmpty(locationCode)) {
            if (QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT == taskType.intValue()) {
                throw new ServiceException("补充入库,必须填写库位");
            } else {
                return true;
            }
        }

        Location location = locationService.getLocationByCode(locationCode, ShiroUtils.getWarehouseCode());
        if (location == null) {
            throw new ServiceException("库位不存在!");
        }
        if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(location.getStatus())) {
//            throw new ServiceException("库位不是空闲状态!");
        }
        if (QuantityConstant.TASK_TYPE_WHOLERECEIPT == taskType.intValue()) {
            if (StringUtils.isNotEmpty(location.getContainerCode())) {
                throw new ServiceException("库位(" + locationCode + ")有容器(" + location.getContainerCode() + "),不能整盘入库!");
            }
        }
        if (QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT == taskType.intValue()) {
            if (StringUtils.isEmpty(location.getContainerCode())) {
                throw new ServiceException("库位(" + locationCode + ")没有容器,不能补充入库!");
            } else if (!location.getContainerCode().equals(containerCode)) {
                throw new ServiceException("库位(" + containerCode + ")对应的容器是:" + location.getContainerCode() + ",请重选库位!");
            }
        }
        return true;
    }

    /**
     * 增加组盘明细记录
     *
     * @param
     * @param receiptDetail 入库详情
     * @param qty           收货数量
     * @param containerCode 容器编码
     */
    @Transactional
    public void receiptContainerDetailAdd(ReceiptContainerHeader receiptContainerHeader, ReceiptDetail receiptDetail, BigDecimal qty, String containerCode, String locationCode) {
        int receiptContainerHeaderId = receiptContainerHeader.getId();
        LambdaQueryWrapper<ReceiptContainerDetail> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeaderId)
                .eq(ReceiptContainerDetail::getReceiptId, receiptDetail.getReceiptId())
                .eq(ReceiptContainerDetail::getReceiptDetailId, receiptDetail.getId())
                .last("Limit 1");
        ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailService.getOne(lambda);
        //查询容器
        LambdaQueryWrapper<Container> containerLambda = Wrappers.lambdaQuery();
        containerLambda.eq(Container::getCode, containerCode)
                .eq(Container::getWarehouseCode, ShiroUtils.getWarehouseCode());
        Container container = containerService.getOne(containerLambda);
        if (receiptContainerDetail == null) {
            //查询入库头表
            LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambda = Wrappers.lambdaQuery();
            receiptHeaderLambda.eq(ReceiptHeader::getId, receiptDetail.getReceiptId())
                    .eq(ReceiptHeader::getCode, receiptDetail.getReceiptCode());
            ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambda);


            receiptContainerDetail = new ReceiptContainerDetail();
            receiptContainerDetail.setReceiptContainerId(receiptContainerHeaderId);
            receiptContainerDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            receiptContainerDetail.setUWarehouseCode(receiptHeader.getUWarehouseCode());
            receiptContainerDetail.setReceiptId(receiptDetail.getReceiptId());
            receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
            receiptContainerDetail.setTaskType(receiptContainerHeader.getTaskType());
            receiptContainerDetail.setReceiptCode(receiptDetail.getReceiptCode());
            receiptContainerDetail.setReceiptType(receiptHeader.getReceiptType());
            receiptContainerDetail.setLocationCode(locationCode);
            receiptContainerDetail.setContainerCode(container.getCode());
            receiptContainerDetail.setContainerType(container.getContainerType());
            receiptContainerDetail.setCompanyCode(receiptDetail.getCompanyCode());
            receiptContainerDetail.setMaterialCode(receiptDetail.getMaterialCode());
            receiptContainerDetail.setMaterialName(receiptDetail.getMaterialName());
            receiptContainerDetail.setMaterialSpec(receiptDetail.getMaterialSpec());
            receiptContainerDetail.setMaterialUnit(receiptDetail.getMaterialUnit());
            receiptContainerDetail.setQty(qty);
            receiptContainerDetail.setSupplierCode(receiptDetail.getSupplierCode());
            receiptContainerDetail.setBatch(receiptDetail.getBatch());
            receiptContainerDetail.setLot(receiptDetail.getLot());
            receiptContainerDetail.setProjectNo(receiptDetail.getProjectNo());
            receiptContainerDetail.setManufactureDate(receiptDetail.getManufactureDate());
            receiptContainerDetail.setExpirationDate(receiptDetail.getExpirationDate());
            receiptContainerDetail.setAgingDate(receiptDetail.getAgingDate());
            receiptContainerDetail.setInventorySts(receiptDetail.getInventorySts());
            receiptContainerDetail.setCreatedBy(ShiroUtils.getLoginName());
            receiptContainerDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            receiptContainerDetail.setSyncMms(0);
            receiptContainerDetail.setZoneCode(container.getZoneCode());
            receiptContainerDetail.setColor(receiptDetail.getColor());
            if (!receiptContainerDetailService.save(receiptContainerDetail)) {
                throw new ServiceException("保存入库组盘详情失败");
            }
        } else {
            receiptContainerDetail.setQty(receiptContainerDetail.getQty().add(qty));
            receiptContainerDetail.setZoneCode(container.getZoneCode());
            if (!receiptContainerDetailService.updateById(receiptContainerDetail)) {
                throw new ServiceException("更新入库组盘详情失败");
            }
        }

    }


    /**
     * 自动生成容器编码
     *
     * @return 容器编码
     */
    @Transactional
    public String createContainer() {
        String code = MessageFormat.format("{0}{1}",
                "LS",
                String.format("%d", Calendar.getInstance().getTimeInMillis()));
        Container container = new Container();
        container.setCode(code);
        container.setWarehouseCode(ShiroUtils.getWarehouseCode());
        container.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        container.setContainerType("LS");
        container.setCreatedBy(ShiroUtils.getLoginName());
        container.setLastUpdated(new Date());
        container.setLastUpdatedBy(ShiroUtils.getLoginName());
        container.setEnable(true);
        container.setStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
        if (!containerService.save(container)) {
            throw new ServiceException("新增容器失败");
        }
        return code;
    }

    @Override
    @Transactional
    public int insertTodayReceiptContainerHeader(ReceiptBill receiptBill, String receiptCode) {
        String receiptContainerCode = receiptBill.getReceiptContainerCode();
        String locationCode = receiptBill.getLocationCode();
        String companyCode = receiptBill.getCompanyCode();
        ReceiptContainerHeader recorder = new ReceiptContainerHeader();
        recorder.setContainerCode(receiptContainerCode);
        recorder.setToLocation(locationCode);
        recorder.setWarehouseCode(ShiroUtils.getWarehouseCode());
        recorder.setCompanyCode(companyCode);
        recorder.setTaskType(QuantityConstant.TASK_TYPE_WHOLERECEIPT);
        recorder.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
        recorder.setCreatedBy(ShiroUtils.getLoginName());
        recorder.setLastUpdatedBy(ShiroUtils.getLoginName());

        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambada = Wrappers.lambdaQuery();
        receiptContainerHeaderLambada.eq(ReceiptContainerHeader::getContainerCode, receiptContainerCode)
                .eq(ReceiptContainerHeader::getCompanyCode, companyCode)
                .eq(ReceiptContainerHeader::getTaskType, String.valueOf(QuantityConstant.TASK_TYPE_WHOLERECEIPT))
                .eq(ReceiptContainerHeader::getStatus, 0);

        ReceiptContainerHeader receiptContainerHeader = getOne(receiptContainerHeaderLambada);
        if (receiptContainerHeader == null) {
            save(recorder);
        } else {
            return receiptContainerHeader.getId();
        }

        locationService.updateStatus(locationCode, QuantityConstant.STATUS_LOCATION_LOCK);
        containerService.updateLocationCodeAndStatus(receiptContainerCode,
                locationCode, QuantityConstant.STATUS_CONTAINER_SOME);
        return recorder.getId();
    }

    /**
     * 取消入库任务
     *
     * @param id 入库组盘头id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult cancelReceipt(Integer id) {
        /**
         * 回滚组盘主表状态
         */
        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
        receiptContainerHeader.setId(id);
        if (!updateById(receiptContainerHeader)) {
            throw new ServiceException("回滚入库组盘头表失败");
        }

        /**
         * 回滚组盘明细状态
         */
        LambdaQueryWrapper<ReceiptContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ReceiptContainerDetail::getReceiptContainerId, id);
        List<ReceiptContainerDetail> containerDetailList = receiptContainerDetailService.list(lambdaQueryWrapper);
        for (ReceiptContainerDetail containerDetail : containerDetailList) {
            containerDetail.setStatus(QuantityConstant.RECEIPT_CONTAINER_BUILD);
        }
        if (containerDetailList != null && containerDetailList.size() > 0 && !receiptContainerDetailService.updateBatchById(containerDetailList)) {
            throw new ServiceException("回滚入库组盘明细失败");
        }

        /**
         * 回滚入库单头表、入库单明细状态
         */
        for (ReceiptContainerDetail containerDetail : containerDetailList) {
            ReceiptHeader receiptHeader = receiptHeaderService.getById(containerDetail.getReceiptId());
            receiptHeader.setLastStatus(QuantityConstant.RECEIPT_HEADER_WAIT);
            ReceiptDetail receiptDetail = receiptDetailService.getById(containerDetail.getReceiptDetailId());
            receiptDetail.setProcessStamp(String.valueOf(QuantityConstant.RECEIPT_HEADER_WAIT));
            if (!receiptHeaderService.updateById(receiptHeader)) {
                throw new ServiceException("回滚入库单头表失败");
            }
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("回滚入库单明细失败");
            }
        }
        return AjaxResult.success("成功");
    }

    @Override
    public int getUnCompleteCombineNumber(String containerCode) {
        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerHeaderLambdaQueryWrapper.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .lt(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        int number = 0;
        List<ReceiptContainerHeader> receiptContainerHeaderList = list(receiptContainerHeaderLambdaQueryWrapper);
        if (receiptContainerHeaderList != null && receiptContainerHeaderList.size() > 0) {
            number = receiptContainerHeaderList.size();
        }
        return number;
    }

    @Override
    public List<ReceiptContainerHeader> getUnCompleteCombineList() {
        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerHeaderLambdaQueryWrapper
                .lt(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_FINISHED);
        List<ReceiptContainerHeader> receiptContainerHeaderList = list(receiptContainerHeaderLambdaQueryWrapper);
        return receiptContainerHeaderList;
    }

    @Override
    public ReceiptContainerHeader selectFirstEntity(ReceiptContainerHeader query) {
        LambdaQueryWrapper<ReceiptContainerHeader> lamb = Wrappers.lambdaQuery();
        lamb.eq(StringUtils.isNotNull(query.getStatus()), ReceiptContainerHeader::getStatus, query.getStatus());
        lamb.eq(StringUtils.isNotEmpty(query.getContainerCode()), ReceiptContainerHeader::getContainerCode, query.getContainerCode());
        lamb.eq(StringUtils.isNotEmpty(query.getWarehouseCode()), ReceiptContainerHeader::getWarehouseCode, query.getWarehouseCode());
        lamb.last("limit 1");
        return this.getOne(lamb);
    }

    @Override
    public List<ReceiptContainerHeader> selectFirstEntityList(ReceiptContainerHeader query) {
        LambdaQueryWrapper<ReceiptContainerHeader> lamb = Wrappers.lambdaQuery();
        lamb.eq(StringUtils.isNotNull(query.getStatus()), ReceiptContainerHeader::getStatus, query.getStatus());
        lamb.eq(StringUtils.isNotEmpty(query.getContainerCode()), ReceiptContainerHeader::getContainerCode, query.getContainerCode());
        lamb.eq(StringUtils.isNotEmpty(query.getWarehouseCode()), ReceiptContainerHeader::getWarehouseCode, query.getWarehouseCode());
        return this.list(lamb);
    }

    @Override
    public AjaxResult clearContainerHeader() {
        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        receiptContainerHeaderLambdaQueryWrapper.eq(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_BUILD);
        List<ReceiptContainerHeader> receiptContainerHeaderList =
                this.list(receiptContainerHeaderLambdaQueryWrapper);
        for (ReceiptContainerHeader receiptContainerHeader : receiptContainerHeaderList) {
            Date date = receiptContainerHeader.getCreated();
            Date now = new Date();
            int fore = date.getDay();
            int day = now.getDay();
            if (fore != day) {
                this.removeById(receiptContainerHeader);
            }
        }

        LambdaQueryWrapper<ReceiptContainerHeader> receiptContainerHeaderLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        receiptContainerHeaderLambdaQueryWrapper1.eq(ReceiptContainerHeader::getStatus,
                QuantityConstant.RECEIPT_CONTAINER_TASK);
        List<ReceiptContainerHeader> receiptContainerHeaderList1 =
                this.list(receiptContainerHeaderLambdaQueryWrapper1);
        for (ReceiptContainerHeader receiptContainerHeader : receiptContainerHeaderList1) {
            String containerCode = receiptContainerHeader.getContainerCode();
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_WHOLERECEIPT,
                            QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT)
                    .eq(TaskHeader::getContainerCode, containerCode);
            List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            if (taskHeaderList.size() == 0) {
                receiptContainerHeader.setStatus(QuantityConstant.RECEIPT_CONTAINER_FINISHED);
                this.updateById(receiptContainerHeader);
            }
        }

        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper.eq(ShipmentContainerHeader::getStatus,
                QuantityConstant.SHIPMENT_CONTAINER_BUILD);
        List<ShipmentContainerHeader> shipmentContainerHeaderList =
                shipmentContainerHeaderService.list(shipmentContainerHeaderLambdaQueryWrapper);
        for (ShipmentContainerHeader shipmentContainerHeader : shipmentContainerHeaderList) {
            Date date = shipmentContainerHeader.getCreated();
            Date now = new Date();
            int fore = date.getDay();
            int day = now.getDay();
            if (fore != day) {
                shipmentContainerHeaderService.removeById(shipmentContainerHeader);
            }
        }

        LambdaQueryWrapper<ShipmentContainerHeader> shipmentContainerHeaderLambdaQueryWrapper1 = Wrappers.lambdaQuery();
        shipmentContainerHeaderLambdaQueryWrapper1.eq(ShipmentContainerHeader::getStatus,
                QuantityConstant.SHIPMENT_CONTAINER_TASK);
        List<ShipmentContainerHeader> shipmentContainerHeaderList1 =
                shipmentContainerHeaderService.list(shipmentContainerHeaderLambdaQueryWrapper1);
        for (ShipmentContainerHeader shipmentContainerHeader : shipmentContainerHeaderList1) {
            String containerCode = shipmentContainerHeader.getContainerCode();
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
                    .in(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_WHOLESHIPMENT,
                            QuantityConstant.TASK_TYPE_SORTINGSHIPMENT)
                    .eq(TaskHeader::getContainerCode, containerCode);
            List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            if (taskHeaderList.size() == 0) {
                shipmentContainerHeader.setStatus(QuantityConstant.SHIPMENT_CONTAINER_FINISHED);
                shipmentContainerHeaderService.updateById(shipmentContainerHeader);
            }
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult quickNoListReceipt(List<ReceiptBill> receiptBills) {
        if (receiptBills == null || receiptBills.size() <= 0) {
            throw new JSONException("没有收货信息");
        }
        boolean flag = true;
        String containerCode = receiptBills.get(0).getReceiptContainerCode();
        String locationCode = receiptBills.get(0).getLocationCode();
        Container container = new Container();
        container.setCode(containerCode);
        container.setWarehouseCode(ShiroUtils.getWarehouseCode());
        container = containerService.selectFirstEntity(container);
        if (container == null) {
            throw new JSONException("不存在这个托盘");
        }
        if (locationCode != null && locationCode.equals(container.getLocationCode())) {
            //查询未完成的入库任务
            List<TaskHeader> tasks = taskHeaderService.selectNofinishReceiptTaskBycontainer(container.getCode());
            TaskHeader task = tasks.get(0);
            if (tasks == null && tasks.size() < 1) {
                throw new JSONException("没有找到托盘对应的任务");
            }
            int receiptHeaderId = receiptHeaderService.createTodayHeader(receiptBills.get(0).getCompanyCode(), container.getUWarehouseCode());
            ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptHeaderId);
            List<Integer> receiptDetailIds = receiptDetailService.insertTodayReceiptDetail(receiptHeaderId,
                    receiptBills, false, receiptBills.get(0).getCompanyCode());
            ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
            receiptContainerHeader.setContainerCode(containerCode);
            receiptContainerHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
            receiptContainerHeader.setTaskType(200);
            receiptContainerHeader.setCompanyCode(receiptBills.get(0).getCompanyCode());
            receiptContainerHeader.setFromLocation(container.getLocationCode());
            receiptContainerHeader.setCreatedBy(ShiroUtils.getLoginName());
            receiptContainerHeader.setStatus(10);
            if (!this.save(receiptContainerHeader)) {
                throw new ServiceException("入库组盘头表保存失败");
            }
            receiptHeader.setFirstStatus(200);
            receiptHeader.setLastStatus(200);
            receiptHeaderService.updateById(receiptHeader);
            task.setAllocationHeadId(receiptContainerHeader.getId());
            taskHeaderService.updateById(task);
            List<Integer> receiptContainerDetailIds = new ArrayList<>();
            receiptDetailIds.forEach(id -> {
                ReceiptDetail receiptDetail = receiptDetailService.getById(id);
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                receiptContainerDetail.setReceiptContainerId(receiptContainerHeader.getId());
                receiptContainerDetail.setReceiptId(receiptHeader.getId());
                receiptContainerDetail.setReceiptDetailId(receiptDetail.getId());
                receiptContainerDetail.setReceiptCode(receiptHeader.getCode());

                Material material = materialService.getMaterialByCode(receiptDetail.getMaterialCode(), ShiroUtils.getWarehouseCode());

                receiptContainerDetail.setMaterialCode(material.getCode());
                receiptContainerDetail.setMaterialName(material.getName());
                receiptContainerDetail.setMaterialSpec(material.getSpec());
                receiptContainerDetail.setQty(receiptDetail.getQty());
                receiptContainerDetail.setCreatedBy(ShiroUtils.getLoginName());
                receiptContainerDetailService.save(receiptContainerDetail);
                receiptContainerDetailIds.add(receiptContainerDetail.getId());
            });


            receiptContainerDetailIds.forEach(id -> {
                ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailService.getById(id);
                TaskDetail taskDetail = new TaskDetail();
                taskDetail.setTaskId(task.getId());
                taskDetail.setTaskType(QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT);
                taskDetail.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_RECEIPT);
                taskDetail.setContainerCode(containerCode);
                taskDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
                taskDetail.setCompanyCode(receiptBills.get(0).getCompanyCode());
                taskDetail.setMaterialCode(receiptContainerDetail.getMaterialCode());
                taskDetail.setMaterialName(receiptContainerDetail.getMaterialName());
                taskDetail.setBillId(receiptHeader.getId());
                taskDetail.setBillCode(receiptHeader.getCode());
                taskDetail.setBillDetailId(receiptContainerDetail.getReceiptDetailId());
                taskDetail.setQty(receiptContainerDetail.getQty());
                taskDetail.setFromLocation(task.getFromLocation());
                taskDetail.setAllocationId(receiptContainerDetail.getId());
                taskDetail.setFromZone("LK");
                taskDetail.setToZone("LK");
                taskDetail.setCompanyCode(task.getCompanyCode());
                taskDetailService.save(taskDetail);
            });
        }
        return AjaxResult.success("收货成功");
    }


    //这个方法主要用agv库的补充入,不校验托盘情况
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult PDAsaveContainer(ReceiptContainerView receivingDamain) {
        String receiptCode = receivingDamain.getReceiptCode();
        String containerCode = receivingDamain.getContainerCode();
        Integer receiptDetailId = receivingDamain.getReceiptDetailId();
        String locationCode = receivingDamain.getLocationCode();
        BigDecimal qty = receivingDamain.getQty();
        String locatingRule = receivingDamain.getLocatingRule();
        ReceiptDetail detail = receiptDetailService.getById(receiptDetailId);

        BigDecimal receiptQty = detail.getQty().subtract(detail.getTaskQty());
        if (receiptQty.compareTo(qty) < 0) {
            return AjaxResult.error("入库明细可用数量" + receiptQty + ",收货数量" + qty + ";不允许超收");
        }

        //检查容器编码合法性

        Container container = containerService.getContainerByCode(containerCode);
        if (StringUtils.isNotNull(container) && StringUtils.isNotEmpty(container.getLocationCode())) {
            locationCode = container.getLocationCode();
        }

        //检查库位编码合法性

        int unCompleteCombineNumber = shipmentContainerHeaderService.getUnCompleteCombineNumber(containerCode);
        if (unCompleteCombineNumber > 0) {
            throw new ServiceException("该托盘已经用于出库组盘");
        }
        /* 新建保存组盘头表记录*/
        //根据容器编码查询组盘表头记录
        LambdaQueryWrapper<ReceiptContainerHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(ReceiptContainerHeader::getContainerCode, containerCode)
                .le(ReceiptContainerHeader::getStatus, QuantityConstant.RECEIPT_CONTAINER_TASK);
        lambda.last("limit 1");
        ReceiptContainerHeader receiptContainerHeader = this.getOne(lambda);

//        ReceiptContainerHeader receiptContainerHeader = new ReceiptContainerHeader();
        //当size大于等于1表示容器已经有组盘了
        if (receiptContainerHeader == null) {
            ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
            // 创建组盘头表
            receiptContainerHeader = createReceiptContainerHeader(receiptContainerHeader, containerCode, locationCode, receiptDetail, 200, locatingRule);
            if (!this.save(receiptContainerHeader)) {
                throw new ServiceException("入库组盘头表保存失败");
            }
        } else {
//            receiptContainerHeader = list.get(0);
            if (receiptContainerHeader.getStatus() > QuantityConstant.RECEIPT_CONTAINER_TASK && receiptContainerHeader.getStatus() < QuantityConstant.RECEIPT_CONTAINER_FINISHED) {
                throw new ServiceException("容器已经生成任务,不能放物料了!");
            }
        }
//        List<ReceiptContainerHeader> receiptContainerHeaders = this.list(lambda);
        /* 更新入库单详情的收货数量*/
        //根据入库单详情id查询入库详情
        ReceiptDetail receiptDetail = receiptDetailService.getById(receiptDetailId);
        receiptDetail.setId(receiptDetailId);
        receiptDetail.setTaskQty(qty.add(receiptDetail.getTaskQty()));
        //更新入库单详情的收货数量
        if (!receiptDetailService.updateById(receiptDetail)) {
            throw new ServiceException("更新入库单详情失败");
        }

        receiptDetail = receiptDetailService.getById(receiptDetailId);
        //save组盘明细
        receiptContainerDetailAdd(receiptContainerHeader, receiptDetail, qty, containerCode, locationCode);
        //如果单据数量等于已收数量,更新入库详情状态和入库单状态
        if (receiptDetail.getQty().compareTo(receiptDetail.getTaskQty()) == 0) {
            if (StringUtils.isNotEmpty(locationCode)) {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_POSITION.toString());
            } else {
                receiptDetail.setProcessStamp(QuantityConstant.RECEIPT_HEADER_RECEIVING.toString());
            }
            if (!receiptDetailService.updateById(receiptDetail)) {
                throw new ServiceException("更新入库详情处理标记失败");
            }

            ReceiptDetail receiptDetail1 = receiptDetailService.queryflow(receiptDetail);
            if (!receiptDetailService.updateById(receiptDetail1)) {
                throw new ServiceException("更新入库详情下一流程失败");
            }
        }
        receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
        return AjaxResult.success("组盘成功");
    }
}