TransferTaskService.java 40.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
package com.huaheng.pc.task.taskHeader.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationServiceImpl;
import com.huaheng.common.constant.CodeConstans;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
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.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
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.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 移库任务
 */
@Service
public class TransferTaskService {

    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private InventoryTransactionService inventoryTransactionService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private TaskAssignService taskAssignService;
    @Resource
    private CompanyService companyService;
    @Resource
    private LocationAllocationServiceImpl locationAllocationService;


    /**
     * 创建立库移库任务
     * @param sourceLocationCode 源库位
     * @param desLocationCode 目的库位
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createTransferTask(String sourceLocationCode, String desLocationCode, String warehouseCode) {
        Location sourceLocation = locationService.getLocationByCode(sourceLocationCode, warehouseCode);
        Location desLocation = locationService.getLocationByCode(desLocationCode, warehouseCode);
        Integer preTaskNo = 0;
        if (StringUtils.isNull(sourceLocation)) {
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "未找到");
        }
        if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(sourceLocation.getStatus())) {
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "状态非空闲");
        }
        if (StringUtils.isEmpty(sourceLocation.getContainerCode())) {
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "不存在托盘");
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        if (inventoryHeaderService.getUncompleteReceiptContainer(sourceLocationCode, warehouseCode) > 0) {
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "存在入库组盘,不能移库");
        }
        if (inventoryHeaderService.getUncompleteShipmentContainer(sourceLocationCode, warehouseCode) > 0) {
            return AjaxResult.error("移出(源)库位:" + sourceLocation + "存在出库组盘,不能移库");
        }
        if (StringUtils.isNull(desLocation)) {
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "未找到");
        }
        if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(desLocation.getStatus())) {
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "状态非空闲");
        }
        if (StringUtils.isNotEmpty(desLocation.getContainerCode())) {
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "存在托盘");
        }

        if (taskHeaderService.getUncompleteTaskInNear(desLocation) > 0) {
            return AjaxResult.error("移入(目标)库位:" + desLocationCode + "旁边存在任务,请完成任务以后再分配");
        }
        if(!sourceLocation.getRoadway().equals(desLocation.getRoadway())) {
            return AjaxResult.error("移入(目标)库位和移出(源)库位不在同一个巷道");
        }

        if(sourceLocation.getHigh()>desLocation.getHigh()) {
            return AjaxResult.error("移入(目标)库位和移出(源)库位高度不一样");
        }

//        if(!sourceLocation.getLocationType().equals(desLocation.getLocationType())) {
//            return AjaxResult.error("移入(目标)库位和移出(源)库位库位类型不一样");
//        }

        if(!sourceLocation.getArea().equals(desLocation.getArea())) {
            return AjaxResult.error("移入(目标)库位和移出(源)库位不在同一个区域");
        }

        //创建移库任务前,记录托盘的状态
        Container container = containerService.getContainerByCode(sourceLocation.getContainerCode(),warehouseCode);
        container.setLastStatus(container.getStatus());
        containerService.updateById(container);

        if(sourceLocation.getRowFlag() == QuantityConstant.ROW_OUT) {
            Location location1 = locationService.getNear(sourceLocation);  //内侧库位
            String locationCode = location1.getCode();
            if(StringUtils.isNotEmpty(location1.getContainerCode())) {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, locationCode)
                        .eq(TaskHeader::getWarehouseCode, warehouseCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
                    preTaskNo = taskHeader.getId();
                } else {
                    return AjaxResult.error("移出(源)库位:" + sourceLocationCode + "旁边库位有托盘无法移库");
                }
            } else {
                LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
                taskHeaderLambdaQueryWrapper.eq(TaskHeader::getToLocation, locationCode)
                        .eq(TaskHeader::getWarehouseCode, warehouseCode)
                        .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
                TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
                if(taskHeader != null) {
                    return AjaxResult.error("移出(源)库位:" + sourceLocationCode + "旁边库位有任务无法移库");
                }
            }
        }


        List<Company> warehouseCode1 = companyService.findByWarehouseCode(warehouseCode);
        Company company = warehouseCode1.get(0);
        TaskHeader taskHeader = new TaskHeader();
        taskHeader.setCompanyCode(company.getCode());
        taskHeader.setWarehouseCode(warehouseCode);
        taskHeader.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
        taskHeader.setInternalTaskType(QuantityConstant.TASK_INTENERTYPE_WORK);
        taskHeader.setZoneCode(sourceLocation.getZoneCode());
        taskHeader.setContainerCode(sourceLocation.getContainerCode());
        taskHeader.setRoadway(sourceLocation.getRoadway());
        taskHeader.setFromLocation(sourceLocationCode);
        taskHeader.setToLocation(desLocationCode);
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        taskHeader.setPreTaskNo(preTaskNo);


        LambdaUpdateWrapper<InventoryDetail> detailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        detailLambdaUpdateWrapper.eq(InventoryDetail::getWarehouseCode, warehouseCode);
        detailLambdaUpdateWrapper.eq(InventoryDetail::getLocationCode, sourceLocationCode);
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.list(detailLambdaUpdateWrapper);
        if(inventoryDetailList!=null&&inventoryDetailList.size()>0){
            taskHeader.setCompanyCode(inventoryDetailList.get(0).getCompanyCode());
        }

        if (!taskHeaderService.save(taskHeader)) {
            throw new ServiceException("创建任务失败");
        }
        locationService.updateStatus(sourceLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        locationService.updateStatus(desLocationCode, QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        containerService.updateLocationCodeAndStatus(sourceLocation.getContainerCode(),
                sourceLocationCode, QuantityConstant.STATUS_CONTAINER_LOCK, warehouseCode);


        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(taskHeader.getId());
            taskDetail.setTaskType(taskHeader.getTaskType());
            taskDetail.setInternalTaskType(taskHeader.getInternalTaskType());
            taskDetail.setWarehouseCode(taskHeader.getWarehouseCode());;
            taskDetail.setCompanyCode(inventoryDetail.getCompanyCode());
            taskDetail.setMaterialCode(inventoryDetail.getMaterialCode());
            taskDetail.setMaterialName(inventoryDetail.getMaterialName());
            taskDetail.setMaterialSpec(inventoryDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(inventoryDetail.getMaterialUnit());
            taskDetail.setQty(inventoryDetail.getQty());
            taskDetail.setFromLocation(inventoryDetail.getLocationCode());
            taskDetail.setToLocation(desLocationCode);
            taskDetail.setContainerCode(inventoryDetail.getContainerCode());
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_BUILD);
            taskDetail.setReferenceCode(inventoryDetail.getReferCode());
            taskDetail.setAttribute1(inventoryDetail.getAttribute1());
            taskDetail.setAttribute2(inventoryDetail.getAttribute2());
            taskDetail.setAttribute3(inventoryDetail.getAttribute3());
            taskDetail.setBatch(inventoryDetail.getBatch());
            taskDetail.setLot(inventoryDetail.getLot());
            taskDetail.setProjectNo(inventoryDetail.getProjectNo());
            taskDetail.setManufactureDate(inventoryDetail.getManufactureDate());
            taskDetail.setExpirationDate(inventoryDetail.getExpirationDate());
            taskDetail.setAgingDate(inventoryDetail.getAgingDate());
            taskDetail.setInventorySts(inventoryDetail.getInventorySts());
            taskDetail.setFromInventoryId(inventoryDetail.getId());
            taskDetail.setToInventoryId(inventoryDetail.getId());
            if (!taskDetailService.save(taskDetail) || !inventoryDetailService.updateById(inventoryDetail)) {
                throw new ServiceException("创建任务失败");
            }
        }
        return AjaxResult.success(taskHeader.getId());
    }

    /**
     * 完成移库任务
     * @param taskHeader
     */
    public AjaxResult completeTransferTask(TaskHeader taskHeader) {
        taskHeader.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        List<TaskDetail> taskDetailList = taskDetailService.findByTaskId(taskHeader.getId());
        /* 库存头表id*/
        List<Integer> inventoryHeadIdList = new ArrayList<>();
        String warehouseCode = taskHeader.getWarehouseCode();
        InventoryDetail inventoryDetail = null;

        Container container1 = containerService.getContainerByCode(
                taskHeader.getContainerCode(), warehouseCode);
        container1.setMovementCount(container1.getMovementCount() + 1);
        containerService.updateById(container1);

        for (TaskDetail taskDetail : taskDetailList) {
            taskDetail.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
            inventoryDetail = inventoryDetailService.getById(taskDetail.getToInventoryId());
            inventoryHeadIdList.add(inventoryDetail.getInventoryHeaderId());
            InventoryTransaction inventoryTransaction = new InventoryTransaction();
            inventoryTransaction.setWarehouseCode(taskDetail.getWarehouseCode());
            inventoryTransaction.setCompanyCode(taskDetail.getCompanyCode());
            inventoryTransaction.setLocationCode(taskDetail.getToLocation());
            inventoryTransaction.setContainerCode(taskDetail.getContainerCode());
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFERINTO);
            inventoryTransaction.setMaterialCode(taskDetail.getMaterialCode());
            inventoryTransaction.setMaterialName(taskDetail.getMaterialName());
            inventoryTransaction.setMaterialSpec(taskDetail.getMaterialSpec());
            inventoryTransaction.setMaterialUnit(taskDetail.getMaterialUnit());
            inventoryTransaction.setTaskQty(taskDetail.getQty());
            inventoryTransaction.setInventorySts(taskDetail.getInventorySts());
            inventoryTransaction.setReferCode(taskDetail.getReferenceCode());
            inventoryTransaction.setBatch(taskDetail.getBatch());
            inventoryTransaction.setLot(taskDetail.getLot());
            inventoryTransaction.setProjectNo(taskDetail.getProjectNo());
            inventoryTransaction.setQcCheck(taskDetail.getQcCheck());
            inventoryTransaction.setManufactureDate(taskDetail.getManufactureDate());
            inventoryTransaction.setExpirationDate(taskDetail.getExpirationDate());
            inventoryTransaction.setAgingDate(taskDetail.getAgingDate());
            inventoryTransaction.setAttributeId(String.valueOf(taskDetail.getAttributeId()));
            inventoryTransaction.setAttribute1(taskDetail.getAttribute1());
            inventoryTransaction.setAttribute2(taskDetail.getAttribute2());
            inventoryTransaction.setAttribute3(taskDetail.getAttribute3());
            inventoryTransactionService.save(inventoryTransaction);
            inventoryTransaction.setLocationCode(taskDetail.getFromLocation());
            inventoryTransaction.setTransactionType(QuantityConstant.INVENTORY_TRANSACTION_TRANSFEROUT);
            inventoryTransactionService.save(inventoryTransaction);
        }
        if (taskDetailList.size() != 0 ){
            taskDetailService.saveOrUpdateBatch(taskDetailList);
        }
        if (!taskHeaderService.saveOrUpdate(taskHeader)) {
            throw new ServiceException("任务单据状态更新失败!");
        }
        if(inventoryDetail != null) {
            containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                    taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_SOME, warehouseCode);
        } else {
            Container container = containerService.getContainerByCode(taskHeader.getContainerCode(),taskHeader.getWarehouseCode());
            if(StringUtils.isNotEmpty(container.getLastStatus())&&container.getLastStatus().equalsIgnoreCase(QuantityConstant.STATUS_CONTAINER_MANY)) {
                //恢复托盘组的状态
                containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                        taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_MANY, warehouseCode);
                container = containerService.getContainerByCode(taskHeader.getContainerCode());
                container.setLastStatus(QuantityConstant.EMPTY_STRING);
                containerService.updateById(container);
            }else {
                containerService.updateLocationCodeAndStatus(taskHeader.getContainerCode(),
                        taskHeader.getToLocation(), QuantityConstant.STATUS_CONTAINER_EMPTY, warehouseCode);
            }
        }
        locationService.updateContainerCodeAndStatus(taskHeader.getFromLocation(), "",
                QuantityConstant.STATUS_LOCATION_EMPTY, warehouseCode);
        locationService.updateContainerCodeAndStatus(taskHeader.getToLocation(), taskHeader.getContainerCode(),
                QuantityConstant.STATUS_LOCATION_EMPTY, warehouseCode);
        inventoryHeadIdList = inventoryHeadIdList.stream().distinct().collect(Collectors.toList());
        LambdaUpdateWrapper<InventoryHeader> headerUpdateWrapper = Wrappers.lambdaUpdate();
        headerUpdateWrapper.set(InventoryHeader::getLocationCode, taskHeader.getToLocation())
                .in(InventoryHeader::getId, inventoryHeadIdList);
        LambdaUpdateWrapper<InventoryDetail> detailUpdateWrapper = Wrappers.lambdaUpdate();
        detailUpdateWrapper.set(InventoryDetail::getLocationCode, taskHeader.getToLocation())
                .in(InventoryDetail::getInventoryHeaderId, inventoryHeadIdList);
        if (inventoryHeadIdList.size() != 0) {
            if (!inventoryHeaderService.update(headerUpdateWrapper) ||
                    !inventoryDetailService.update(detailUpdateWrapper)) {
                throw new ServiceException("完成任务失败");
            }
        }
        return AjaxResult.success("完成移库任务成功");
    }

    //判断是否需要移库
    public void doubletransfer(TaskHeader task) {
        if(task.getTaskType() == 300 || task.getTaskType() == 600){
            task.setToLocation(task.getFromLocation());
        }
        //判断任务的库位不为空

        Location location = new Location();
        location.setCode(task.getFromLocation());
        location.setWarehouseCode(task.getWarehouseCode());
        location =locationService.selectFirstEntity(location);

        if(location == null){
            throw new ServiceException("任务的库位错误,系统没有此库位");
        }

        //判断库位是否为为内部库位,并且外部库位锁定
        if(location.getIRow().intValue() == CodeConstans.INTERNELLOCATION.intValue()){
            Location loca = new Location();
            loca.setZoneCode("LK");
            loca.setIRow(CodeConstans.EXTERNALLOCATION);
            loca.setIColumn(location.getIColumn());
            loca.setILayer(location.getILayer());
            loca.setIGrid(location.getIGrid());
            loca.setWarehouseCode(location.getWarehouseCode());
            loca = locationService.selectFirstEntity(loca);

            if(loca == null){
                throw new ServiceException("外侧库位在系统不存在");
            }
            if(loca.getStatus().equals("lock")){
                throw new ServiceException("外侧库位已有任务被锁住");
            }

            if(loca.getStatus().equals("lock") && StringUtils.isNotEmpty(loca.getUserDef1()) && loca.getUserDef1().equals("1")){
                throw new ServiceException(task.getContainerCode() + "此托盘外部库位有进库任务,请等待下发");
            }
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getFromLocation, loca.getCode())
                    .eq(TaskHeader::getWarehouseCode, loca.getWarehouseCode());
            taskHeaderLambdaQueryWrapper.last("limit 1");
            TaskHeader task1 = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
            if(task1 !=null && task1.getFirstStatus() == 1){
                throw new ServiceException("先下发库位号为"+loca.getCode()+"的任务");
            }


            //外部库位绑定了容器时,需要移库任务
            if(loca !=null && StringUtils.isNotEmpty(loca.getContainerCode()) && loca.getStatus().equals("empty")){
                //查看库位类型
                LambdaQueryWrapper<LocationType> lambda = Wrappers.lambdaQuery();
                lambda.eq(LocationType::getCode, loca.getLocationType())
                        .eq(LocationType::getWarehouseCode, loca.getWarehouseCode());
                LocationType locationType=locationTypeService.getOne(lambda);
                if(locationType ==null){
                    throw new ServiceException("该库位的库位类型不存在");
                }


                //查找不可移库的空库位
                List<String> locationss=new ArrayList<>();
                //查出外侧有货,内测无货的库位,将其排除
                List<String> locationlist =locationService.selectlistEmpty(loca.getWarehouseCode());
                if(!locationlist.isEmpty()){
                    locationss.addAll(locationlist);
                }
                //查有任务的库位
                List<Location> locationList =new ArrayList<>();
                List<Location> locations = locationService.selectCode(loca.getWarehouseCode());
                if(locations!=null && locations.size()>0) {
                    //筛选内部库位
                    for(Location item :locations){
                        if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                            locationList.add(item);
                        }
                    }
                    //筛选外部库位
                    if(locationList!=null && locationList.size()>0) {
                        for (Location item : locationList) {
                            Location condition = new Location();
                            condition.setIRow(2);
                            condition.setIColumn(item.getIColumn());
                            condition.setIGrid(item.getIGrid());
                            condition.setILayer(item.getILayer());
                            condition.setZoneCode("LK");
                            condition.setWarehouseCode(location.getWarehouseCode());
                            condition = locationService.selectFirstEntity(condition);
                            locationss.add(condition.getCode());
                        }
                    }
                }

                Container container =containerService.getContainerByCode(loca.getContainerCode());
//                Location loc = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()), Double.valueOf(locationType.getWidth()),
//                        Double.valueOf(container.getUserDef1()), null, ShiroUtils.getWarehouseCode(), locationss, container);
                List<String> list = Lists.newArrayList("1");
                LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(LocationType::getZoneCode, "LK")
                        .eq(LocationType::getWarehouseCode, location.getWarehouseCode());
                List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);
                List<String> locationTypeCodeList = locationTypeList.stream().
                        map(t -> t.getCode()).collect(Collectors.toList());
                //立库area为5
                String loc = locationAllocationService.allocation(1, locationTypeCodeList, loca.getHigh(), "5",list,location.getWarehouseCode(), loca.getContainerCode(), null);
                List<Integer> taskIds = new ArrayList<>();
                if(loc !=null){
                   // this.createTransferTask2(loca.getCode(),task.getCompanyCode());
                    AjaxResult ajaxResult = createTransferTask(loca.getCode(), loc, location.getWarehouseCode());
                    if(!ajaxResult.hasErr()){
                        String taskId = ajaxResult.getData().toString();
                        taskIds.add(Integer.valueOf(taskId));
                    }else {
                        throw new ServiceException("创建移库任务失败");
                    }
//                    TaskHeader tas = new TaskHeader();
//                    tas.setZoneCode("LK");
//                    tas.setStatus(1);
//                    tas.setFromLocation(loca.getCode());
//                    tas.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
//                    tas = selectFirstEntity(tas);
//                    if(tas !=null){
//                        taskIds.add(tas.getId());
//                    }
                }else {
                    //移库任务
                    List<String> locatinCodes = new ArrayList<>();
                    locatinCodes.add(loca.getCode());
                    taskIds = this.createOutPutTask(locatinCodes);
                }
                if(!taskIds.isEmpty()){
                    TaskHeader task2 =taskHeaderService.getById(taskIds.get(0));

                    AjaxResult ajaxResult=taskAssignService.wcsTaskAssign(task2);
                    if(ajaxResult.hasErr()){
                        throw new ServiceException(ajaxResult.getMsg());
                    }
                    task2.setStatus(10);
                    taskHeaderService.updateById(task2);
                }
            }
        }

    }


    public TaskHeader selectFirstEntity(TaskHeader tas){
        LambdaQueryWrapper<TaskHeader> lambda = Wrappers.lambdaQuery();
        lambda.eq(StringUtils.isNotEmpty(tas.getContainerCode()),TaskHeader::getContainerCode, tas.getContainerCode())
                .eq(StringUtils.isNotNull(tas.getZoneCode()) && StringUtils.isNotEmpty(tas.getZoneCode()),TaskHeader::getZoneCode, tas.getZoneCode())
                .eq(StringUtils.isNotNull(tas.getStatus()),TaskHeader::getStatus, tas.getStatus())
                .eq(StringUtils.isNotNull(tas.getFromLocation()) && StringUtils.isNotEmpty(tas.getFromLocation()),TaskHeader::getFromLocation, tas.getFromLocation())
                .eq(StringUtils.isNotNull(tas.getTaskType()) ,TaskHeader::getTaskType, tas.getTaskType());
        lambda.last("limit 1");
        return taskHeaderService.getOne(lambda);
    }
    public List<Integer> createOutPutTask(List<String> locations){
        List<Integer> taskIds  = new ArrayList<>();
        for(String code: locations){
            Location temp = new Location();
            temp.setCode(code);
            temp.setWarehouseCode(ShiroUtils.getWarehouseCode());
            Location loc = locationService.selectFirstEntity(temp);

            if(loc.getStatus().equals("lock")){
                throw new ServiceException("该库位已锁定,有其他任务");
            }

            Container container =containerService.getContainerByCode(loc.getContainerCode());


            if(loc.getZoneCode().equals("LK") || loc.getZoneCode().equals("XC")){
                if(StringUtils.isEmpty(loc.getContainerCode())){
                    throw new ServiceException("该库位没有托盘");
                }

                TaskHeader task =new TaskHeader();
                task.setContainerCode(loc.getContainerCode());
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
                task.setStatus(10);
                task =this.selectFirstEntity(task);
                if(task!=null){
                    throw new ServiceException("该容器已有任务");
                }
                TaskHeader task1 =new TaskHeader();
                task1.setContainerCode(loc.getContainerCode());
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
                task1.setStatus(1);
                task1 =this.selectFirstEntity(task1);
                if(task1!=null){
                    throw new ServiceException("该容器已有任务");
                }
            }

            if(loc.getLocationType().equals("DM") || loc.getLocationType().equals("XN")){
                continue;
            }
            if(!loc.getStatus().equals("empty")){
                throw new ServiceException(code+"状态非空闲,操作失败");
            }
            if(StringUtils.isEmpty(loc.getContainerCode())){
                throw new ServiceException(code+"没有托盘,操作失败");
            }
            TaskHeader task = new TaskHeader();
            task.setWarehouseCode(ShiroUtils.getWarehouseCode());

            task.setCompanyCode("kshhhanjieweId");
            //这里默认一个0
            task.setPriority(0);
            task.setZoneCode(loc.getZoneCode());
            task.setTaskType(900);
            task.setPort("P1100");
            task.setZoneCode(loc.getZoneCode());
            task.setRoadway(loc.getRoadway());
            task.setContainerCode(loc.getContainerCode());
            task.setStatus(1);
            task.setFromLocation(loc.getCode());
            task.setToLocation(loc.getCode());
            task.setCreated(new Date());
            task.setCreatedBy(ShiroUtils.getLoginName());
            task.setCreated(new Date());
            taskHeaderService.save(task);
            taskIds.add(task.getId());
            //更新库位状态
            loc.setStatus("lock");
            locationService.updateById(loc);
        }
        return taskIds;
    }
    @Transactional
    public AjaxResult createTransferTask2(String sourceLocation, String companyCode) {
        Location temp1 = new Location();
        temp1.setCode(sourceLocation);
        Location location = locationService.selectFirstEntity(temp1);
        if(location==null){
            return AjaxResult.error("源库位:"+sourceLocation+"未找到");
        }
        if(!location.getStatus().equals("empty")){
            return AjaxResult.error("源库位:"+sourceLocation+"状态非空闲");
        }
        if(StringUtils.isEmpty(location.getContainerCode())){
            return AjaxResult.error("源库位:"+sourceLocation+"不存在托盘");
        }
        if(!location.getZoneCode().equals("LK") && !location.getZoneCode().equals("XC")){
            return AjaxResult.error("该库位:"+sourceLocation+"不是立库库位");
        }

        if(location.getZoneCode().equals("LK") || location.getZoneCode().equals("XC")){
            TaskHeader task =new TaskHeader();
            task.setContainerCode(location.getContainerCode());
            task.setStatus(10);
            task =this.selectFirstEntity(task);
            if(task!=null){
                return AjaxResult.error(location.getContainerCode()+"该容器已有任务");
            }
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        int count1 = inventoryHeaderService.getUncompleteReceiptContainer(sourceLocation,ShiroUtils.getWarehouseCode());
        if(count1>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在入库组盘,不能移库");
        }
        int count2 = inventoryHeaderService.getUncompleteShipmentContainer(sourceLocation,ShiroUtils.getWarehouseCode());
        if(count2>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在出库组盘,不能移库");
        }

        //判断是否为内测库位
        if(location.getIRow().intValue() == CodeConstans.INTERNELLOCATION.intValue()) {
            Location loca = new Location();
            loca.setIRow(CodeConstans.EXTERNALLOCATION);
            loca.setIColumn(location.getIColumn());
            loca.setILayer(location.getILayer());
            loca.setIGrid(location.getIGrid());
            loca.setWarehouseCode(location.getWarehouseCode());
            loca = locationService.selectFirstEntity(loca);

            if (loca == null) {
                throw new ServiceException("外侧库位在系统不存在");
            }
            //外部库位绑定了容器时,需要移库任务
            if (loca != null && StringUtils.isNotEmpty(loca.getContainerCode()) && loca.getStatus().equals("empty")) {
                //查看库位类型
                LocationType locationTypee = new LocationType();
                locationTypee.setWarehouseCode(loca.getWarehouseCode());
                locationTypee.setCode(loca.getLocationType());
                locationTypee = locationTypeService.getLocationTypeByDoamin(locationTypee);

                if (locationTypee == null) {
                    throw new ServiceException("该库位的库位类型不存在");
                }

                Container containerl = containerService.getContainerByCode(loca.getContainerCode(),loca.getWarehouseCode());

                Location local = locationTypeService.checkType(Double.valueOf(locationTypee.getLength()),Double.valueOf(locationTypee.getWidth()),
                        Double.valueOf(containerl.getUserDef1()),Double.valueOf(locationTypee.getMaxWeight()), ShiroUtils.getWarehouseCode(),loca.getCode(),containerl);


                List<String> locationss=new ArrayList<>();
                List<String> locationlist =locationService.selectlistEmpty(loca.getWarehouseCode());
                if(!locationlist.isEmpty()){
                    locationss.addAll(locationlist);
                }

                //如果此库位为外部库位,则查找所有内部库位是否有任务,且其内部库位是否是绑定状态
                List<Location> locationList =new ArrayList<>();
                List<Location> locations = locationService.selectCode(loca.getWarehouseCode());
                if(locations!=null && locations.size()>0) {

                    //筛选内部库位
                    for(Location item :locations){
                        if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                            locationList.add(item);
                        }
                    }

                    //筛选外部库位
                    if(locationList!=null && locationList.size()>0) {
                        for (Location item : locationList) {
                            Location condition = new Location();
                            condition.setIRow(2);
                            condition.setIColumn(item.getIColumn());
                            condition.setIGrid(item.getIGrid());
                            condition.setILayer(item.getILayer());
                            condition.setZoneCode("LK");
                            condition = locationService.selectFirstEntity(condition);
                            locationss.add(condition.getCode());
                        }
                    }
                }


                if(!locationss.isEmpty()) {
                    local = locationTypeService.checkTypeV1(Double.valueOf(locationTypee.getLength()),Double.valueOf(locationTypee.getWidth()),
                            Double.valueOf(containerl.getUserDef1()),Double.valueOf(locationTypee.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationss,containerl);
                }
                if(local == null){
                    throw new ServiceException("没有空库位");
                }

                TaskHeader task = new TaskHeader();
                task.setWarehouseCode(ShiroUtils.getWarehouseCode());
                task.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
                //这里默认一个0
                task.setPriority(0);
                task.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
                //对移库来说,这个没啥用
                task.setPort(null);
                task.setRoadway(loca.getRoadway());
                task.setZoneCode(loca.getZoneCode());
                task.setContainerCode(loca.getContainerCode());
                task.setStatus(1);
                task.setCreated(new Date());
                task.setFromLocation(loca.getCode());
                task.setToLocation(local.getCode());
                task.setCreated(new Date());
                task.setCreatedBy(ShiroUtils.getLoginName());
                taskHeaderService.save(task);
                //更新货位状态为预定
                local.setStatus("lock");
                local.setUserDef1("1");
                loca.setStatus("lock");
                locationService.updateById(loca);
                locationService.updateById(local);
            }
        }


        Container container =containerService.getContainerByCode(location.getContainerCode(),location.getWarehouseCode());

        LocationType locationType =new LocationType();
        locationType.setWarehouseCode(location.getWarehouseCode());
        locationType.setCode(location.getLocationType());
        locationType = locationTypeService.getLocationTypeByDoamin(locationType);
        //查找可移入的库位
        //通过限制条件查找库位类型列表
        Location locat = new Location();
        if(container.getContainerType().equals(QuantityConstant.CONTAINER_TYPEC)){
            locat = locationService.getLocationOfRule();
        }else {
            locat = locationTypeService.checkType(Double.valueOf(locationType.getLength()), Double.valueOf(locationType.getWidth()),
                    Double.valueOf(container.getUserDef1()), Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(), location.getCode(), container);

            List<String> locationsss=new ArrayList<>();
            List<String> locationlists =locationService.selectlistEmpty(locat.getWarehouseCode());
            if(!locationlists.isEmpty()){
                locationsss.addAll(locationlists);
            }
            //如果此库位为外部库位,则查找所有内部库位是否有任务,且其内部库位是否是绑定状态
            List<Location> locationList =new ArrayList<>();
            List<Location> locations = locationService.selectCode(locat.getWarehouseCode());
            if(locations!=null && locations.size()>0) {

                //筛选内部库位
                for(Location item :locations){
                    if(item.getIRow().intValue()== CodeConstans.INTERNELLOCATION.intValue()){
                        locationList.add(item);
                    }
                }

                //筛选外部库位
                if(locationList!=null && locationList.size()>0) {
                    for (Location item : locationList) {
                        Location condition = new Location();
                        condition.setIRow(2);
                        condition.setIColumn(item.getIColumn());
                        condition.setIGrid(item.getIGrid());
                        condition.setILayer(item.getILayer());
                        condition.setZoneCode("LK");
                        condition = locationService.selectFirstEntity(condition);
                        locationsss.add(condition.getCode());
                    }

                    locat = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()),Double.valueOf(locationType.getWidth()),
                            Double.valueOf(container.getUserDef1()),Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationsss,container);
                }
            }


            if(!locationsss.isEmpty()) {
                locat = locationTypeService.checkTypeV1(Double.valueOf(locationType.getLength()),Double.valueOf(locationType.getWidth()),
                        Double.valueOf(container.getUserDef1()),Double.valueOf(locationType.getMaxWeight()), ShiroUtils.getWarehouseCode(),locationsss,container);
            }
        }


        if(locat == null){
            throw new ServiceException("没有空库位");
        }

        TaskHeader task = new TaskHeader();
        task.setWarehouseCode(ShiroUtils.getWarehouseCode());
        task.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        //这里默认一个0
        task.setPriority(0);
        task.setTaskType(QuantityConstant.TASK_TYPE_TRANSFER);
        //对移库来说,这个没啥用
        task.setPort(null);
        task.setRoadway(location.getRoadway());
        task.setZoneCode(location.getZoneCode());
        task.setContainerCode(location.getContainerCode());
        task.setStatus(1);
        task.setFromLocation(sourceLocation);
        task.setToLocation(locat.getCode());
        task.setCreated(new Date());
        task.setCreatedBy(ShiroUtils.getLoginName());
        taskHeaderService.save(task);
        //更新货位状态为预定
        location.setStatus("lock");
        locat.setStatus("lock");
        locat.setUserDef1("0");
        locationService.updateById(location);
        locationService.updateById(locat);
        return AjaxResult.success(task);
//        return AjaxResult.success("");
    }

}