TaskHeaderServiceImpl.java 41.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
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.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.DataUtils;
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.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.warehouse.domain.Warehouse;
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.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.service.ReceiptContainerHeaderService;
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.shipmentContainerDetail.domain.ShipmentContainerDetail;
import com.huaheng.pc.shipment.shipmentContainerDetail.service.ShipmentContainerDetailService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.ShipmentTaskCreateModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.mapper.TaskHeaderMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class TaskHeaderServiceImpl extends ServiceImpl<TaskHeaderMapper, TaskHeader> implements TaskHeaderService {

    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Autowired
    private ShipmentContainerDetailService shipmentContainerDetailService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private InventoryDetailService inventoryDetailService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private TaskHeaderService taskHeaderService;

    @Autowired
    private InventoryHeaderService inventoryHeaderService;

    @Autowired
    private InventoryTransactionService  inventoryTransactionService;

    @Autowired
    private ReceiptHeaderService receiptHeaderService;

    @Resource
    private ReceiptDetailService receiptDetailService;

    @Autowired
    private ReceiptContainerDetailService receiptContainerDetailService;


    @Autowired
    private ContainerService containerService;


    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;




    @Resource
    private TaskHeaderMapper taskHeaderMapper;

    /**
     * 生成出库任务
     *
     * @param shipmentTaskCreateModel
     * @return
     */
    @Override
    @Transactional
    public AjaxResult createTaskFromShipmentContainers(ShipmentTaskCreateModel shipmentTaskCreateModel) {
        Integer shipmentContainerHeaderId = shipmentTaskCreateModel.getShipmentContainerHeaderIds();
        //获取表头
        ShipmentContainerHeader shipmentContainerHeader = shipmentContainerHeaderService.getById(shipmentContainerHeaderId);
        if (shipmentContainerHeader == null) {
            return AjaxResult.error("出库货箱" + shipmentContainerHeaderId + "未找到,操作中止");
        }
        if (shipmentContainerHeader.getStatus() > 9) {
            return AjaxResult.error("出库货箱" + shipmentContainerHeader.getContainerCode() + "已经生成任务,请不要重复生成,操作中止");
        }
        //获取所有子货箱
        LambdaQueryWrapper<ShipmentContainerDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentContainerDetail::getShippingContainerId, shipmentContainerHeader.getId());
        List<ShipmentContainerDetail> shipmentContainerDetails = shipmentContainerDetailService.list(lambdaQueryWrapper);
        if (shipmentContainerDetails == null || shipmentContainerDetails.size() == 0) {
            return AjaxResult.error("货箱" + shipmentContainerHeader.getContainerCode() + "没有子任务,操作中止");
        }
        //检测库位
        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getCode, shipmentContainerHeader.getLocationCode())
                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getDeleted, false);
        Location location = locationService.getOne(locationLambdaQueryWrapper);
        if (location == null) {
            return AjaxResult.error("库位禁用或不存在!");
        }
        //创建任务头
        TaskHeader task = new TaskHeader();
        //分拣出库
        task.setTaskType(400);
        task.setToLocation(shipmentContainerHeader.getLocationCode());
        //判断是否整出任务,钱柜和AGV不能整出
        if (shipmentContainerHeader.getStatus().intValue() == 300) {
            //表示整出优先
            //判断当前子货箱所有数量是否等于该托盘对应的所有库存的数量,
            //这里必须与库存的在库数量对比,后期可能存在一个配盘在执行任务,后一个配盘又在配这个的情况(这个时候不能整出)
            // 如果相等,则说明这个货箱包含了所有的数量,则可以整出,否则,创建拣选任务;
            //查询所有库存
            InventoryDetail inventoryCondition = new InventoryDetail();
            LambdaQueryWrapper<InventoryDetail> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryDetailLambdaQueryWrapper.eq(InventoryDetail::getLocationCode, shipmentContainerHeader.getLocationCode())
                    .eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
            List<InventoryDetail> inventories = inventoryDetailService.list(inventoryDetailLambdaQueryWrapper);
            BigDecimal inventoryTotal = new BigDecimal("0");
            for (InventoryDetail item : inventories) {
                inventoryTotal = inventoryTotal.add(item.getQty());
            }
            BigDecimal containerTotal = new BigDecimal("0");
            for (ShipmentContainerDetail item : shipmentContainerDetails) {
                containerTotal = containerTotal.add(item.getQty());
            }
            if (inventoryTotal.compareTo(containerTotal) == 0) {
                task.setTaskType(300);//整盘出库
                task.setToLocation("");
            }
        }
        task.setWarehouseCode(shipmentContainerHeader.getWarehouseCode());
        task.setCompanyCode(shipmentContainerHeader.getCompanyCode());
        task.setInternalTaskType(null);
        task.setAssignedUser(ShiroUtils.getLoginName());
        task.setConfirmedBy(ShiroUtils.getLoginName());
        task.setStatus(1);
        task.setContainerCode(shipmentContainerHeader.getContainerCode());
        task.setCreatedBy(ShiroUtils.getLoginName());
        task.setCreated(new Date());
        task.setLastUpdatedBy(ShiroUtils.getLoginName());
        task.setLastUpdated(null);
        this.save(task);
        //遍历子货箱创建子任务
        for (ShipmentContainerDetail shipmentContainerDetail : shipmentContainerDetails) {
            TaskDetail taskDetail = new TaskDetail();
            taskDetail.setTaskId(task.getId());
            taskDetail.setWarehouseCode(task.getWarehouseCode());
            taskDetail.setCompanyCode(task.getCompanyCode());
            taskDetail.setTaskType(2);
            taskDetail.setBillCode(shipmentContainerDetail.getShipmentCode());
            taskDetail.setBillCode(shipmentContainerDetail.getShipmentCode());
            taskDetail.setBillDetailId(shipmentContainerDetail.getShipmentDetailId());
            taskDetail.setMaterialCode(shipmentContainerDetail.getMaterialCode());
            taskDetail.setMaterialName(shipmentContainerDetail.getMaterialName());
            taskDetail.setMaterialSpec(shipmentContainerDetail.getMaterialSpec());
            taskDetail.setMaterialUnit(shipmentContainerDetail.getMaterialUnit());
            taskDetail.setFromInventoryId(shipmentContainerDetail.getInventoryId());
            taskDetail.setQty(shipmentContainerDetail.getQty());
            taskDetail.setContainerCode(task.getContainerCode());
            taskDetail.setFromLocation(task.getFromLocation());
            taskDetail.setToLocation(task.getToLocation());
            taskDetail.setStatus(1);
            taskDetail.setTaskType(2);
            taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
            taskDetail.setLastUpdated(null);
            taskDetailService.save(taskDetail);
        }
        //更新货位状态
        ShipmentContainerHeader record = new ShipmentContainerHeader();
        record.setId(shipmentContainerHeaderId);
        record.setStatus(10);
        record.setStatus(task.getTaskType());//实际出库类型
        shipmentContainerHeaderService.saveOrUpdate(record);
        return AjaxResult.success(task.getId());

    }

    /**
     *
     * 执行任务
     * */
    @Override
    public AjaxResult<TaskHeader> sendTaskToWcs(Integer[] taskIds) {
        TaskHeader task = null;
        for (Integer taskId : taskIds) {
            task = taskHeaderService.getById(taskId);
            if (task.getStatus() > 9) {
                return AjaxResult.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
            }
            //修改任务头表
            task.setStatus(10);
            task.setStartPickDateTime(new Date());   //生成时间
            task.setLastUpdated(new Date());
            task.setLastUpdatedBy(ShiroUtils.getLoginName());
            LambdaUpdateWrapper<TaskHeader> HeaderUpdateWrapper = Wrappers.lambdaUpdate();
            HeaderUpdateWrapper.eq(TaskHeader::getId, taskId);
            taskHeaderService.update(task, HeaderUpdateWrapper);
            //修改任务明细状态
            TaskDetail record = new TaskDetail();
            record.setStatus(10);
            record.setLastUpdated(new Date());
            record.setLastUpdatedBy(ShiroUtils.getLoginName());
            LambdaUpdateWrapper<TaskDetail> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
            lambdaUpdateWrapper.eq(TaskDetail::getTaskId, task.getId());
            taskDetailService.update(record, lambdaUpdateWrapper);
//            if (task.getInternalTaskType().equals("100"))
//            {
//                List<Map<String, Object>> maps = taskDetailService.selectListMapByEqual("billId, billDetailId", condition);
//                for (Map<String, Object> item : maps){
//                    Integer billDetailId = DataUtils.getInteger(item.get("billDetailId"));
//                    receiptHeaderService.updateDetailStatus(billDetailId, (short)300);
//                }
//                maps.stream().map(X -> X.get("billId")).distinct().forEach(X -> receiptHeaderService.receiptStatusUpdate(DataUtils.getInteger(X), (short)300));
//            }
//            //盘点单执行
//            if(task.getType() == 700){
//                CyclecountDetail cyclecountDetai = cyclecountDetailService.selectEntityById(
//                        task.getAllocationHeadId());
//                cyclecountDetai.setStatus(10);
//                cyclecountDetailService.updateByModel(cyclecountDetai);
//            }
////            //如果是单排人工库,那么出入库都是先完成,在下发AGV任务
////            if (task.getType().intValue() == 300 || task.getType().intValue() == 600)   {
////                if (task.getSourceLocation().startsWith("L03") == false )   {
////                    taskAgvService.createTaskAgv(task);
////                }
////            }
////            else    {
////                taskAgvService.createTaskAgv(task);
////            }
//            //任务类型是出库,那就完成任务在叫agv
//            if (task.getType().intValue() == 100 || task.getType().intValue() == 500)   {
//                taskAgvService.createTaskAgv(task);
//            }
        }
        return AjaxResult.success("下发任务成功", task);
    }

    @Override
    public AjaxResult completeTaskByWMS(Integer[] taskIds) throws Exception {
        for (int taskId : taskIds) {
            TaskHeader task = taskHeaderService.getById(taskId);
            if (task == null) {
                throw new ServiceException("任务" + taskId + "未找到,执行中止");
            }
            //如果已完成则不管
            if (task.getStatus() == 100) {
                throw new ServiceException("任务(" + taskId + ")任务已经是完成的!");
            }
            //如果没有库位不能完成
            if (StringUtils.isEmpty(task.getToLocation())) {
                throw new ServiceException("任务" + taskId + "没有库位,执行中止");
            }
            this.completeTask(task);
        }

        return AjaxResult.success("完成任务成功!");
    }

    /**
     * 完成任务
     *
     * @param task
     * @throws Exception
     */
    public void completeTask(TaskHeader task) throws Exception {
        //区分任务类型
        if (task.getInternalTaskType() == 100 || task.getInternalTaskType() == 200) {
            //入库任务
            completeReceiptTask(task);
        }
        if (task.getInternalTaskType() == 300 || task.getInternalTaskType() == 400) {
            //出库任务
//            completeShipmentTask(task);
        }
        //...其他任务类型暂时不处理    // 700 盘点   800 移库  900 出库查看
        if (task.getInternalTaskType() == 700 || task.getInternalTaskType() == 900) {
//            completeCycleCountOrSeeOutTask(task);
        }
        if (task.getInternalTaskType() == 800) {
//            //移库
//            completeTransferTask(task);
        }
        if (task.getInternalTaskType() == 500) {
//            //空托盘入库
//            completeEmptyIn(task);
        }
        if (task.getInternalTaskType() == 600) {
//            //空托盘出库
//            completeEmptyOut(task);
        }
    }

    @Override
    @Transactional
    public AjaxResult completeReceiptTask(TaskHeader task) throws Exception {
        List<Map<String, Object>> taskReceiptContainerDetail =taskHeaderMapper.getReceiptTask(task.getId());
        if (taskReceiptContainerDetail.size()<1){
            return AjaxResult.success("未找到对应任务的入库单号!!!");
        }
        for (Map<String, Object> map : taskReceiptContainerDetail) {
            //将未完成的任务数量更新到库存表
            if (DataUtils.getInteger(map.get("status")) < 100) {
                LambdaQueryWrapper<InventoryDetail> inventory =Wrappers.lambdaQuery();
                inventory.eq(InventoryDetail::getWarehouseCode,ShiroUtils.getWarehouseCode())
                         .eq(InventoryDetail::getLocationCode,task.getToLocation())
                         .eq(InventoryDetail::getReceiptDetailId,DataUtils.getString(map.get("receiptDetailId")))
                         .eq(InventoryDetail::getContainerCode,DataUtils.getString(map.get("containerCode")));
                         InventoryDetail detail=inventoryDetailService.getOne(inventory);
                if (detail == null)
                {
                    //添加库存单
                    InventoryHeader header = new InventoryHeader();
                    header.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));//仓库
                    header.setCompanyCode(task.getCompanyCode());//货主
                    header.setContainerCode(DataUtils.getString(map.get("containerCode")));//容器号
                    header.setLocationCode(task.getToLocation());
                    header.setTotalQty(DataUtils.getInteger(map.get("totalQty")));//总数量
                    header.setLocking(1);
                    header.setEnable(1);
                    header.setCreatedBy(ShiroUtils.getLoginName());
                    header.setCreated(new Date());
                    header.setLastUpdated(new Date());
                    if (!inventoryHeaderService.save(header))
                        throw new ServiceException("添加库存单失败");

                    //库存明细添加
                    detail = new InventoryDetail();
                    detail.setInventoryHeaderId(header.getId());//库存头ID
                    detail.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));//仓库
                    detail.setCompanyCode(task.getCompanyCode());//货主
                    detail.setLocationCode(task.getToLocation());//库位号
                    detail.setContainerCode(DataUtils.getString(map.get("containerCode")));//容器号
                    detail.setMaterialCode(DataUtils.getString(map.get("materialCode")));//物料号
                    detail.setMaterialName(DataUtils.getString(map.get("materialName")));//物料名称
                    detail.setMaterialSpec(DataUtils.getString(map.get("materialSpec")));//物料规格
                    detail.setReceiptCode(DataUtils.getString(map.get("receiptCode")));//入库单编码
                    detail.setReceiptDetailId(DataUtils.getInteger(map.get("receiptDetailId")));//入库单明细ID
                    detail.setBatch(DataUtils.getString(map.get("batch")));//批次
                    detail.setLot(DataUtils.getString(map.get("lot")));//批号
                    detail.setInventorySts(DataUtils.getString(map.get("inventorySts")));//库存状态
                    detail.setManufactureDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("manufactureDate").toString()));//生产日期
                    detail.setExpirationDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("expirationDate").toString()));//失效日期
                    detail.setQty(DataUtils.getBigDecimal(map.get("qty")));//数量
                    detail.setTaskQty(DataUtils.getBigDecimal(map.get("qty")));
                    detail.setCreatedBy(ShiroUtils.getLoginName());//创建人
                    detail.setLastUpdatedBy(ShiroUtils.getLoginName());//创建时间
                    if (!inventoryDetailService.save(detail))
                        throw new ServiceException("添加库存明细失败");
                }
                else
                {
                    detail.setQty(detail.getQty().add(DataUtils.getBigDecimal(map.get("qty"))));
                    detail.setLastUpdatedBy(ShiroUtils.getLoginName());
                    LambdaUpdateWrapper<InventoryDetail> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
                    lambdaUpdateWrapper.eq(InventoryDetail::getId,DataUtils.getInteger(map.get("receiptDetailId")));
                    if (!inventoryDetailService.update(detail, lambdaUpdateWrapper)){
                        throw new ServiceException("更新入库单明细失败");
                    }
                }
                //记录库存交易记录
                InventoryTransaction inventoryTransaction = new InventoryTransaction();
                inventoryTransaction.setTransactionType(10);
                inventoryTransaction.setWarehouseCode(DataUtils.getString(map.get("warehouseCode")));
                inventoryTransaction.setCompanyCode(task.getCompanyCode());
                inventoryTransaction.setLocationCode(task.getToLocation());
                inventoryTransaction.setContainerCode(DataUtils.getString(map.get("containerCode")));
                inventoryTransaction.setMaterialCode(DataUtils.getString(map.get("materialCode")));
                inventoryTransaction.setMaterialName(DataUtils.getString(map.get("materialName")));
                inventoryTransaction.setBillCode(DataUtils.getString(map.get("receiptCode")));
                inventoryTransaction.setBillDetailId(DataUtils.getInteger(map.get("receiptDetailId")));
                inventoryTransaction.setBatch(DataUtils.getString(map.get("batch")));
                inventoryTransaction.setLot(DataUtils.getString(map.get("lot")));
                inventoryTransaction.setManufactureDate(DataUtils.getDateTime(map.get("manufactureDate")));
                inventoryTransaction.setExpirationDate(DataUtils.getDateTime(map.get("expirationDate")));
                inventoryTransaction.setInventorySts(DataUtils.getString((map.get("inventorySts"))));
                inventoryTransaction.setTaskQty(DataUtils.getInteger(map.get("qty")));
                inventoryTransaction.setCreated(new Date());
                inventoryTransaction.setCreatedBy(ShiroUtils.getLoginName());
                if (!inventoryTransactionService.save(inventoryTransaction))
                    throw new ServiceException("新增库存记录失败");
//                //修改任务明细的状态为完成
//                TaskDetail taskDetail = new TaskDetail();
//                taskDetail.setStatus(100);
//                taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
//                taskDetail.setAgingDate(new Date());   //入库时间
//                LambdaUpdateWrapper<TaskDetail> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
//                lambdaUpdateWrapper.eq(TaskDetail::getTaskId, DataUtils.getInteger(map.get("taskDetailId")));
//                taskDetailService.update(taskDetail, lambdaUpdateWrapper);
//                //修改入库单的状态
//                ReceiptHeader receiptHeader =new ReceiptHeader();
//                receiptHeader.setFirstStatus(100);
//                receiptHeader.setLastStatus(100);
//                LambdaUpdateWrapper<ReceiptHeader> receiptHeaderLambdaUpdateWrapper = Wrappers.lambdaUpdate();
//                receiptHeaderLambdaUpdateWrapper.eq(ReceiptHeader::getId, DataUtils.getInteger(map.get("receiptId")));
//                receiptHeaderService.update(receiptHeader, receiptHeaderLambdaUpdateWrapper);

        //修改任务主表状态,因为立库任务表单头只对应一个货箱,表单详情的任务会同时完成
                task.setStatus(100);
                task.setLastUpdatedBy(ShiroUtils.getLoginName());
                task.setLastUpdated(new Date());
                LambdaUpdateWrapper<TaskHeader> taskHeaderLambdaUpdateWrapper = Wrappers.lambdaUpdate();
                taskHeaderLambdaUpdateWrapper.eq(TaskHeader::getId, task.getId());
                if (!taskHeaderService.update(task, taskHeaderLambdaUpdateWrapper))
                    throw new ServiceException("更新任务主表失败");
        //修改库位状态和对应的容器
                Location location =new Location();
                location.setContainerCode(task.getContainerCode());
                location.setStatus("empty");
                LambdaUpdateWrapper<Location> locationLambdaUpdateWrapper = Wrappers.lambdaUpdate();
                locationLambdaUpdateWrapper.eq(Location::getCode, task.getToLocation());
                if (!locationService.update(location, locationLambdaUpdateWrapper))
                    throw new ServiceException("更新库位失败");
        //修改容器状态和对应的库位
                Container container =new Container();
                container.setLocationCode(task.getToLocation());
                container.setStatus("some");
                LambdaUpdateWrapper<Container> containerLambdaUpdateWrapper = Wrappers.lambdaUpdate();
                containerLambdaUpdateWrapper.eq(Container::getCode,task.getContainerCode());
                if (!containerService.update(container, containerLambdaUpdateWrapper))
                    throw new ServiceException("更新容器失败");

        //修改组盘表状态为20
                ReceiptContainerDetail receiptContainerDetail = new ReceiptContainerDetail();
                receiptContainerDetail.setStatus(20);
                LambdaUpdateWrapper<ReceiptContainerDetail> receiptContainerDetailLambdaUpdateWrapper = Wrappers.lambdaUpdate();
                receiptContainerDetailLambdaUpdateWrapper.eq(ReceiptContainerDetail::getReceiptId,DataUtils.getInteger(map.get("receiptId")));
                receiptContainerDetailService.update(receiptContainerDetail, receiptContainerDetailLambdaUpdateWrapper);
                if (! receiptContainerDetailService.update(receiptContainerDetail, receiptContainerDetailLambdaUpdateWrapper))
                    throw new ServiceException("更新组盘状态失败");
        //修改入库明细
                ReceiptDetail receiptDetail = receiptDetailService.queryflow(receiptDetailService.getById(receiptContainerDetail.getReceiptDetailId()));
                if (!receiptDetailService.updateById(receiptDetail)){
                    throw new ServiceException("更新状态失败");
                }
                receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
           }
        }
        return AjaxResult.success("完成入库任务");
    }

    @Override
    public AjaxResult createTransferTask(String sourceLocation, String destinationLocation) {
        return null;
    }

    @Override
    public AjaxResult createCheckOutTask(String[] ids) {
        return null;
    }

    @Override
    public AjaxResult completeCycleCountOrSeeOutTask(TaskHeader taskHeader) {
        return null;
    }

//    @Override
//    public List<Map<String, Object>> getReceiptTask(Integer taskId) {
//        return taskHeaderMapper.getReceiptTask(taskId) ;
//    }

    /**
     * 立库移库
     * */
    @Transactional
    public AjaxResult createTransferTask(String sourceLocation, String destinationLocation, String companyCode) {
       /* Location temp1 = new Location();
        temp1.setCode(sourceLocation);
        temp1.setWarehouseId(ShiroUtils.getWarehouseId());
        Location loc1 = locationService.selectFirstEntity(temp1);
        if(loc1==null){
            return AjaxResult.error("源库位:"+sourceLocation+"未找到");
        }
        if(!loc1.getStatus().equals("empty")){
            return AjaxResult.error("源库位:"+sourceLocation+"状态非空闲");
        }
        if(StringUtils.isEmpty(loc1.getContainerCode())){
            return AjaxResult.error("源库位:"+sourceLocation+"不存在托盘");
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        int count1 = inventoryService.getUncompleteReceiptContainer(sourceLocation,ShiroUtils.getWarehouseId());
        if(count1>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在入库组盘,不能移库");
        }
        int count2 = inventoryService.getUncompleteShipmentContainer(sourceLocation,ShiroUtils.getWarehouseId());
        if(count2>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在出库组盘,不能移库");
        }

        Location temp2 = new Location();
        temp2.setWarehouseId(ShiroUtils.getWarehouseId());
        temp2.setCode(destinationLocation);
        Location loc2 = locationService.selectFirstEntity(temp2);
        if(loc2 == null){
            return  AjaxResult.error("目标库位:"+destinationLocation+"未找到");
        }
        if(!loc2.getStatus().equals("empty")){
            return AjaxResult.error("目标库位:"+destinationLocation+"状态非空闲");
        }
        if(StringUtils.isNotEmpty(loc2.getContainerCode())){
            return AjaxResult.error("目标库位:"+destinationLocation+"已存在托盘");
        }
        int count3 = inventoryService.getUncompleteReceiptContainer(destinationLocation,ShiroUtils.getWarehouseId());
        if(count3>0){
            return AjaxResult.error("目标库位:"+sourceLocation+"存在入库组盘,不能移库");
        }
        //hack:这里暂时写死第五层不能往其他四层移库
        if(loc1.getLayer()==5){
            if(loc2.getLayer()<4){
                return AjaxResult.error("高货位不能往底货位移库");
            }
        }
        Task task = new Task();
        task.setWarehouseId(ShiroUtils.getWarehouseId());
        task.setWarehouseCode(ShiroUtils.getWarehouseCode());
        task.setCompanyId(ShiroUtils.getCompanyIdList().get(0));//获取第一个货主
        task.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        //这里默认一个0
        task.setPriority(0);
        task.setType((short)800);
        //对移库来说,这个没啥用
        task.setStation(null);
        task.setContainerCode(loc1.getContainerCode());
        task.setFirstStatus((short)1);
        task.setLastStatus((short)1);
        task.setBeginTime(new Date());
        task.setSourceLocation(sourceLocation);
        task.setDestinationLocation(destinationLocation);
        task.setCreated(new Date());
        task.setCreatedBy(ShiroUtils.getLoginName());
        insert(task);
        //更新货位状态为预定
        loc1.setStatus("lock");
        loc2.setStatus("lock");
        locationService.updateByModel(loc1);
        locationService.updateByModel(loc2);
        return AjaxResult.success(task.getId());*/
       return null;
    }

    /**
     * 创建上架任务
     * @param ids
     * @return
     */
    @Override
    public AjaxResult createReceiptTask(List<Integer> ids) {
        for (Integer id : ids)  {
            ReceiptContainerHeader receiptContainerHeader = receiptContainerHeaderService.getById(id);
            if (receiptContainerHeader == null)
                throw new ServiceException("任务不存在!");
            if (receiptContainerHeader.getWarehouseCode() != ShiroUtils.getWarehouseCode())
                throw new ServiceException("任务不在当前仓库!");
            LambdaQueryWrapper<ReceiptContainerDetail> containerDetailLambda = Wrappers.lambdaQuery();
            containerDetailLambda.eq(ReceiptContainerDetail::getReceiptId, id);
            List<ReceiptContainerDetail> list = receiptContainerDetailService.list(containerDetailLambda);
            if (list.size() < 1)
                throw new ServiceException("没有组盘明细,请先组盘!");
            if (receiptContainerHeader.getStatus() == 0) {
                if (receiptContainerHeader.getStatus().intValue() < 10) {
                    receiptContainerHeader.setStatus((short)10);
                    receiptContainerHeaderService.updateById(receiptContainerHeader);
                }
                //添加任务主表
                TaskHeader task = new TaskHeader();
                task.setWarehouseCode(receiptContainerHeader.getWarehouseCode());
                task.setCompanyCode(receiptContainerHeader.getCompanyCode());
                task.setFromLocation(receiptContainerHeader.getFromLocation());
                task.setTaskType(Integer.valueOf(receiptContainerHeader.getTaskType()));
                task.setFromLocation(receiptContainerHeader.getFromLocation());
                task.setToLocation(receiptContainerHeader.getToLocation());
                task.setContainerCode(receiptContainerHeader.getContainerCode());
                task.setCreated(new Date());
                task.setCreatedBy(ShiroUtils.getLoginName());
                this.save(task);
                //添加任务明细表
                for(ReceiptContainerDetail item : list) {
                    TaskDetail taskDetail = new TaskDetail();
                    taskDetail.setTaskId(task.getId());
                    taskDetail.setWarehouseCode(task.getWarehouseCode());
                    taskDetail.setCompanyCode(task.getCompanyCode());
                    taskDetail.setCompanyCode(task.getCompanyCode());
                    taskDetail.setMaterialCode(item.getMaterialCode());
                    taskDetail.setMaterialName(item.getMaterialName());
                    taskDetail.setBillCode(item.getReceiptCode());
                    taskDetail.setBillDetailId(item.getReceiptDetailId());
                    taskDetail.setBillCode(item.getReceiptCode());
                    taskDetail.setQty(BigDecimal.valueOf(item.getQty()));
                    taskDetail.setContainerCode(task.getContainerCode());
                    taskDetail.setFromLocation(task.getFromLocation());
                    taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
                    taskDetail.setBatch(item.getBatch());
                    taskDetailService.save(taskDetail);

                    ReceiptDetail receiptDetail = receiptDetailService.getById(item.getReceiptDetailId());
                    if ("300".equals(receiptDetail.getProcessStamp())){
                        ReceiptDetail detail = receiptDetailService.queryflow(receiptDetail);
                        if (!receiptDetailService.updateById(detail)){
                            throw new ServiceException("更新入库单详情失败");
                        }
                        receiptDetailService.updateReceiptHeaderLastStatus(receiptDetail.getReceiptId());
                    }
                }


            }
        }
        return  AjaxResult.success("生成任务成功!");
    }

    /**
     *
     * 完成出库任务
     * */
    @Override
    public void completeShipmentTask(TaskHeader task) {
        //获取所有子任务
        TaskDetail condition = new TaskDetail();
        condition.setTaskId(task.getId());
        LambdaQueryWrapper<TaskDetail> lambdaQueryWrapper = Wrappers.lambdaQuery(condition);
        List<TaskDetail> taskDetails = taskDetailService.list(lambdaQueryWrapper);
        List<Integer> shipmentHeadIds = new ArrayList<>();
//        for(TaskDetail taskDetail : taskDetails){
//            if(taskDetail.getStatus()<20){
//                //获取出库子货箱
//                ShipmentContainerDetail shipmentContainerDetail = shipmentContainerDetailService.selectEntityById(taskDetail.getAllocationId());
//                //取出子单据
//                ShipmentDetail shipmentDetail = shipmentDetailService.selectEntityById(taskDetail.getBillDetailId());
////                //取出主单据
////                ShipmentHeader shipmentHeader = shipmentHeaderService.selectEntityById(shipmentContainerDetail.getShipmentHeaderId());
//                //暂存id,为更新单据状态准备
//                shipmentHeadIds.add(taskDetail.getBillId());
//                //获取对应库存记录
//                Inventory inventory = inventoryService.selectEntityById(taskDetail.getInventoryId());
//                if (inventory == null)  {
//                    throw new ServiceException("任务明细对应的库存ID【" + taskDetail.getInventoryId().toString() + "】不存在!");
//                }
//                BigDecimal orignalQty = inventory.getQty();
//                //扣减库存
//                inventory.setTaskQty(inventory.getTaskQty().subtract(taskDetail.getQty()));
//                inventory.setQty(inventory.getQty().subtract(taskDetail.getQty()));
//                if(inventory.getQty().compareTo(new BigDecimal("0"))==0 && inventory.getTaskQty().compareTo(new BigDecimal("0"))==0){
//                    //如果库存没有了,就删除这个库存
//                    inventoryService.deleteById(inventory.getId());
//                }else {
//                    //否则更新这个库存
//                    inventoryService.updateByModel(inventory);
//                }
//                //设置子任务状态为已执行
//                taskDetail.setStatus((short)100);
//                taskDetail.setEndTime(new Date());   //完成时间
//                taskDetailService.updateByModel(taskDetail);
//                //记录库存交易记录
//                InventoryTransaction inventoryTransaction = new InventoryTransaction();
//                inventoryTransaction.setWarehouseId(task.getWarehouseId());
//                inventoryTransaction.setWarehouseCode(task.getWarehouseCode());
//                inventoryTransaction.setCompanyId(shipmentDetail.getCompanyId());
//                inventoryTransaction.setCompanyCode(shipmentDetail.getCompanyCode());
//                inventoryTransaction.setLocationId(inventory.getLocationId());
//                inventoryTransaction.setLocationCode(inventory.getLocationCode());
//                inventoryTransaction.setContainerCode(inventory.getContainerCode());
//                inventoryTransaction.setSourceCode(shipmentDetail.getSourceCode());
//                inventoryTransaction.setSourceLine(shipmentDetail.getSourceLine());
//                inventoryTransaction.setType("shipment");
//                inventoryTransaction.setCompanyId(task.getCompanyId());
//                inventoryTransaction.setMaterialId(shipmentDetail.getMaterialId());
//                inventoryTransaction.setMaterialCode(shipmentDetail.getMaterialCode());
//                //inventory.setMaterialName(DataUtils.getString(taskDetail.getMaterialName()));//物料名称
//                inventoryTransaction.setBillId(taskDetail.getBillId());
//                inventoryTransaction.setBillCode(taskDetail.getBillCode());
//                inventoryTransaction.setBillDetailId(shipmentDetail.getId());
//                inventoryTransaction.setBatch(shipmentDetail.getBatch());
//                inventoryTransaction.setLot(shipmentDetail.getLot());
//                inventoryTransaction.setProject(shipmentDetail.getProject());
//                inventoryTransaction.setManufactureDate(shipmentDetail.getManufactureDate());
//                inventoryTransaction.setExpirationDate(shipmentDetail.getExpirationDate());
//                inventoryTransaction.setStatus(inventory.getStatus());
//                //这里取反,更符合出库的语义,同时方便对记录进行统计
//                inventoryTransaction.setQty(orignalQty);
//                inventoryTransaction.setTaskQty(taskDetail.getQty().negate());
////                inventoryTransaction.setCostPrice(shipmentDetail.);
//                inventoryTransaction.setCreated(null);
//                inventoryTransaction.setCreatedBy(ShiroUtils.getLoginName());
//                inventoryTransaction.setLastUpdated(null);
//                inventoryTransaction.setLastUpdatedBy(ShiroUtils.getLoginName());
//                inventoryTransactionService.insert(inventoryTransaction);
////                //更新单据状态
////                shipmentHeaderService.updateShipmentStatus(shipmentHeader.getId());
//            }
//        }
//        //设置主任务为已执行
//        task.setFirstStatus((short)100);
//        task.setLastStatus((short)100);
//        task.setLastUpdatedBy(ShiroUtils.getLoginName());
//        task.setEndTime(new Date());
//        taskService.updateByModel(task);
//        //将库位状态改为空闲,如果是整出的对应的容器也清空
//        Location locationRecord = new Location();
//        locationRecord.setStatus("empty");
//        if(task.getType()==300) {
//            locationRecord.setContainerCode("");
//            locationRecord.setContainerId(null);
//        }
//        Location locationCondition = new Location();
//        locationCondition.setCode(task.getSourceLocation());
//        locationCondition.setWarehouseId(ShiroUtils.getWarehouseId());
//        locationService.updateByCondition(locationRecord, locationCondition);
//        //如果是整出,删掉这个库位上的这个托盘,否则更改托盘状态
//        Container containerRecord = new Container();
//        if(task.getType()==300) {
//            containerService.updateLocationCodeAndStatus(task.getContainerCode(),"","empty");
//        }
//        else    {
//            //查询是否存在关联的库存,入如果没有就修改容器状态为empty
//            Inventory inventoryCondition = new Inventory();
//            inventoryCondition.setLocationCode(task.getSourceLocation());
//            inventoryCondition.setWarehouseId(ShiroUtils.getWarehouseId());
//            Map<String, Object> map = inventoryService.selectFirstMap("id", inventoryCondition);
//            if (map == null) {
//                //如果没有库存就设置为empty
//                containerService.updateStatus(task.getContainerCode(), "empty");
//            }
//        }
//        //设置出库货箱状态为拣货任务完成
//        shipmentContainerHeaderService.resetStatusShipmentContainer(task.getAllocationHeadId(),(short)20);
//        //最后更新单据状态
//        shipmentHeadIds.stream().distinct().forEach(t->shipmentHeaderService.updateShipmentStatus(t));

    }


}