OrderConfirmApiService.java 41.2 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
package com.huaheng.api.SSP.service;

import com.huaheng.api.SSP.domain.*;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.config.zone.domain.SSPdetail;
import com.huaheng.pc.config.zone.domain.SSPheader;
import com.huaheng.pc.config.zone.mapper.SSPdetailMapperAuto;
import com.huaheng.pc.config.zone.mapper.SSPheaderMapperAuto;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.general.company.domain.WarehouseCompany;
import com.huaheng.pc.general.company.mapper.WarehouseCompanyMapperAuto;
import com.huaheng.pc.general.company.service.ICompanyService;
import com.huaheng.pc.general.location.domain.Location;
import com.huaheng.pc.general.location.service.ILocationService;
import com.huaheng.pc.inventory.inventory.domain.Inventory;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.IInventoryTransactionService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.IReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.IReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import com.huaheng.pc.task.task.domain.Task;
import com.huaheng.pc.task.task.service.ITaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.ITaskDetailService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 钱柜出入库单回传接口Service
 * 1.此Service的作用是钱柜完成系统下发的出入库单后进行回传,回传逻辑是先存进钱柜的临时表中,存进钱柜临时表不按出入库单区分,无论数量是否溢出是否有误,但若单据非第
 * 一次回传至临时表,则在主表中判断该订单是否已经完成。并根据单据增加或减少数量。而后进行库存的修改,并在库存临时表中增加库存修改记录。
 */
@Service
@Transactional
public class OrderConfirmApiService {

    @Resource
    private WarehouseCompanyMapperAuto warehouseCompanyMapperAuto;

    @Autowired
    IReceiptDetailService iReceiptDetailService;

    @Autowired
    IReceiptHeaderService iReceiptHeaderService;

    @Autowired
    IShipmentDetailService iShipmentDetailService;

    @Autowired
    IShipmentHeaderService iShipmentHeaderService;

    @Resource
    private SSPheaderMapperAuto ssPheaderMapperAuto;

    @Resource
    private SSPdetailMapperAuto ssPdetailMapperAuto;

    @Autowired
    ILocationService iLocationService;

    @Autowired
    IInventoryService iInventoryService;

    @Autowired
    ICompanyService iCompanyService;

    @Autowired
    IInventoryTransactionService iInventoryTransactionService;

    @Autowired
    ITaskService taskService;

    @Autowired
    ITaskDetailService taskDetailService;




    //订单回传
    public AjaxResult ocfm(ConfirmModel cfm) {
        ConfirmHeaderModel headerModel = cfm.getConfirmHeaderModel();
        List<ConfirmTypeModel> typeModels =  cfm.getOrderItems();
        if (headerModel == null||typeModels == null){
            return AjaxResult.error("订单头表为空或订单明细表为空!");
        }if (headerModel.getCubeCode()==null||headerModel.getCubeCode()==""){
            return AjaxResult.error("钱柜编码不能为空!");
        }if (headerModel.getId()==null||headerModel.getId()==""){
            return AjaxResult.error("钱柜系统主表标识不能为空!");
        }if (headerModel.getOrderHeaderId()<1){
            return AjaxResult.error("订单头表标识不能为空!");
        }if (headerModel.getOrderCode()==null||headerModel.getOrderCode()==""){
            return AjaxResult.error("订单编码不能为空!");
        }if (!headerModel.getOrderType().equals("receipt")&&!headerModel.getOrderType().equals("shipment")){
            return AjaxResult.error("订单类型错误!");
        }if (headerModel.getfDatetime()==null||headerModel.getcDatetime()==""){
            return AjaxResult.error("完成的时间不能为空!");
        }if (headerModel.getcDatetime()==null||headerModel.getcDatetime()==""){
            return AjaxResult.error("交互的当前时间不能为空!");
        }
        String wc = headerModel.getWarehouseCode();
        if (wc==null||wc==""){
            return AjaxResult.error("warehouseCode不能为空!!");
        }

        WarehouseCompany warehouseCompany = new WarehouseCompany();
        warehouseCompany.setWarehouseCode(wc);
        WarehouseCompany list = warehouseCompanyMapperAuto.selectFirstEntity(warehouseCompany);
        if (list == null) {
            return AjaxResult.error("系统中没有该仓库:" + warehouseCompany.toString() + "  信息,请先录入仓库信息!");
        }
        SSPheader ssPheader = new SSPheader();
        ssPheader.setWarehouseCode(headerModel.getWarehouseCode());
        ssPheader.setCubeCode(headerModel.getCubeCode());
        ssPheader.sethID(headerModel.getId());
        ssPheader.setOrderHeaderId(headerModel.getOrderHeaderId());
        ssPheader.setOrderCode(headerModel.getOrderCode());
        ssPheader.setOrderType(headerModel.getOrderType());
        ssPheader.setfDatetime(headerModel.getfDatetime());
        ssPheader.setcDatetime(headerModel.getcDatetime());
        if (ssPheader.getOrderType().equals("receipt")){
            return receipt(ssPheader,typeModels);
        }else {
            return shipment(ssPheader,typeModels);
        }
    }

    @Transactional
    public AjaxResult receipt(SSPheader chm,List<ConfirmTypeModel> ctmlist){
        short firstStatus = 200;
        short lastStatus = 800;
        for (ConfirmTypeModel ctm : ctmlist){
            if (ctm.getItemId()==null||ctm.getItemId()==""){
                return AjaxResult.error("钱柜系统明细标识不能为空!");
            }if (ctm.getOrderDetailId()<1){
                return AjaxResult.error("订单明细表标识不能为空!");
            }if (ctm.getCompanyCode()==null||ctm.getCompanyCode()==""){
                return AjaxResult.error("货主编码不能为空!");
            }if (ctm.getContainerCode()==null||ctm.getContainerCode()==""){
                return AjaxResult.error("容器编码不能为空!");
            }if (ctm.getItemCode()==null||ctm.getItemCode()==""){
                return AjaxResult.error("商品编码不能为空!");
            }if (ctm.getItemBarcode()==null||ctm.getItemBarcode()=="") {
                return AjaxResult.error("商品条码不能为空!");
            }
            SSPheader sh = new SSPheader();
            sh.sethID(chm.gethID());
            sh.setOrderHeaderId(chm.getOrderHeaderId());
            sh.setOrderType(chm.getOrderType());
            //组合入库子单
            SSPdetail sd = new SSPdetail();
            sd.setItemId(ctm.getItemId());
            sd.setOrderDetailId(ctm.getOrderDetailId());
            sd.setOrderHeaderId(chm.getOrderHeaderId());
            sd.setCompanyCode(ctm.getCompanyCode());
            sd.setContainerCode(ctm.getContainerCode());
            sd.setItemCode(ctm.getItemCode());
            sd.setItemName(ctm.getItemName());
            sd.setItemBarcode(ctm.getItemBarcode());
            sd.setItemState("good");
            sd.setQty(ctm.getQty());
            sd.setItemUnit(ctm.getItemUnit());
            sd.setOrderType("receipt");
            String type="receipt";
//            判断系统中是否已有该主单,若无则新增
//            try {

                if (ssPheaderMapperAuto.selectListEntityByEqual(sh).size()<1){
                    //给临时表新增该入库单据
                    ssPheaderMapperAuto.insert(chm);
                    ssPdetailMapperAuto.insert(sd);

                    ReceiptDetail receiptDetail = iReceiptDetailService.selectEntityById(ctm.getOrderDetailId());
                    Integer result = receiptDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                    //判断到货数量是否小于总数量,小于则做加法,因为此次是首次加入,库存数量为0
                    if (result==1||result==0){
                        receiptDetail.setQtyCompleted(receiptDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                        Integer rs = receiptDetail.getQtyCompleted().compareTo(receiptDetail.getQty());//到货数量加原库存比对总数量
                        if (rs==0){//等于
                            receiptDetail.setStatus(lastStatus);
                            iReceiptDetailService.updateByModel(receiptDetail);
                            StockTask(sd,chm,type);
                        }
                        if (rs==-1){//小于
                            receiptDetail.setStatus(firstStatus);
                            iReceiptDetailService.updateByModel(receiptDetail);
                            StockTask(sd,chm,type);
                        }
                        if (rs==1){//大于
                            return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量溢出,请填写正确的收货数量");
                        }
                    }else {
                        return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量大于了系统数量,请填写正确的收货数量");
                    }

                }else {
                    ReceiptHeader rh = new ReceiptHeader();
                    rh.setId(sh.getOrderHeaderId());
                    //判断主单是否已经完成
                    if (iReceiptHeaderService.selectFirstEntity(rh).getLastStatus() == lastStatus){
                        return AjaxResult.setResultCS(0,"该入库主单已经完成,无需再次回传");
                    }


                    SSPdetail sd1 = new SSPdetail();
                    sd1.setOrderDetailId(ctm.getOrderDetailId());
                    sd1.setOrderHeaderId(chm.getOrderHeaderId());
                    //判断系统中是否已有该子单
                    if (ssPdetailMapperAuto.selectListEntityByEqual(sd1).size() < 1) {
                        ssPdetailMapperAuto.insert(sd);



                        ReceiptDetail receiptDetail = iReceiptDetailService.selectEntityById(sd.getOrderDetailId());
                        if(receiptDetail==null){
                            throw new ServiceException("单据已删除");
                        }
                        Integer result = receiptDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                        //判断到货数量是否小于总数量,小于则做加法,因为此次是首次加入,库存数量为0
                        if (result==1||result==0){
                            receiptDetail.setQtyCompleted(receiptDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                            Integer rs = receiptDetail.getQtyCompleted().compareTo(receiptDetail.getQty());//到货数量加原库存比对总数量
                            if (rs==0){//等于
                                receiptDetail.setStatus(lastStatus);
                                iReceiptDetailService.updateByModel(receiptDetail);
                                StockTask(sd,chm,type);
                            }
                            if (rs==-1){//小于
                                receiptDetail.setStatus(firstStatus);
                                iReceiptDetailService.updateByModel(receiptDetail);
                                StockTask(sd,chm,type);
                            }
                            if (rs==1){//大于
                                return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量溢出,请填写正确的收货数量");
                            }
                        }else {
                            return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量大于了系统数量,请填写正确的收货数量");
                        }


                    } else {
                        sd1.setItemId(ctm.getItemId());
                        sd1.setOrderType("receipt");
                        //如果有,再判断该子单是否是多次回传,以及是否是入库子单
                        if (ssPdetailMapperAuto.selectListEntityByEqual(sd1).size() < 1) {
                            ssPdetailMapperAuto.insert(sd);
                        }

                        ReceiptDetail receiptDetail = iReceiptDetailService.selectEntityById(sd.getOrderDetailId());
                        if(receiptDetail==null){
                            throw new ServiceException("单据已删除");
                        }
                        //判断该子单是否已经完成
                        if (receiptDetail.getQty()==receiptDetail.getQtyCompleted()){
                            continue;
                        }
                        Integer result = receiptDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                        //判断到货数量是否小于总数量,小于则做加法,因为此次是非首次加入
                        if (result==1||result==0){
                            receiptDetail.setQtyCompleted(receiptDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                            Integer rs = receiptDetail.getQtyCompleted().compareTo(receiptDetail.getQty());//到货数量加原库存比对总数量
                            if (rs==0){//等于
                                receiptDetail.setStatus(lastStatus);
                                iReceiptDetailService.updateByModel(receiptDetail);
                                StockTask(sd,chm,type);
                            }
                            if (rs==-1){//小于
                                receiptDetail.setStatus(firstStatus);
                                iReceiptDetailService.updateByModel(receiptDetail);
                                StockTask(sd,chm,type);
                            }
                            if (rs==1){//大于
                                return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量溢出,请填写正确的收货数量");
                            }
                        }else {
                            return AjaxResult.error("ID为: "+receiptDetail.getId().toString()+"  的入库子单收货数量大于了系统数量,请填写正确的收货数量");
                        }
                    }
                }
//            }catch (Exception e){
//                throw new ServiceException("入库主单或子单新增失败!");
//            }
        }
        ReceiptDetail detail = new ReceiptDetail();
        detail.setReceiptId(chm.getOrderHeaderId());
        List<ReceiptDetail> receiptDetailList = iReceiptDetailService.selectListEntityByEqual(detail);
        int i = 0;
        for (ReceiptDetail rs:receiptDetailList){
            if (rs.getStatus()==null){
                i++;
            }
            if (rs.getStatus()!=lastStatus){
                i++;
            }
        }
        if (i==0){
            ReceiptHeader receiptHeader = new ReceiptHeader();
            receiptHeader.setId(chm.getOrderHeaderId());
            receiptHeader.setFirstStatus(lastStatus);
            receiptHeader.setLastStatus(lastStatus);
            int res  = iReceiptHeaderService.updateByModel(receiptHeader);
            if (res<1){
                throw new ServiceException("修改状态失败!");
            }
        }



        return AjaxResult.setResultCS(0,"入库单回传成功");
    }

    @Transactional
    public AjaxResult shipment(SSPheader chm,List<ConfirmTypeModel> ctmlist){
        short firstStatus = 200;
        short lastStatus = 800;
        for (ConfirmTypeModel ctm : ctmlist){
            if (ctm.getItemId()==null||ctm.getItemId()==""){
                return AjaxResult.error("钱柜系统明细标识不能为空!");
            }if (ctm.getOrderDetailId()<1){
                return AjaxResult.error("订单明细表标识不能为空!");
            }if (ctm.getCompanyCode()==null||ctm.getCompanyCode()==""){
                return AjaxResult.error("货主编码不能为空!");
            }if (ctm.getContainerCode()==null||ctm.getContainerCode()==""){
                return AjaxResult.error("容器编码不能为空!");
            }if (ctm.getItemCode()==null||ctm.getItemCode()==""){
                return AjaxResult.error("商品编码不能为空!");
            }if (ctm.getItemBarcode()==null||ctm.getItemBarcode()=="") {
                return AjaxResult.error("商品条码不能为空!");
            }
            SSPheader sh = new SSPheader();
            sh.sethID(chm.gethID());
            sh.setOrderHeaderId(chm.getOrderHeaderId());
            sh.setOrderType(chm.getOrderType());
            //组合入库子单
            SSPdetail sd = new SSPdetail();
            sd.setItemId(ctm.getItemId());
            sd.setOrderDetailId(ctm.getOrderDetailId());
            sd.setOrderHeaderId(chm.getOrderHeaderId());
            sd.setCompanyCode(ctm.getCompanyCode());
            sd.setContainerCode(ctm.getContainerCode());
            sd.setItemCode(ctm.getItemCode());
            sd.setItemName(ctm.getItemName());
            sd.setItemBarcode(ctm.getItemBarcode());
            sd.setItemState("good");
            sd.setQty(ctm.getQty());
            sd.setItemUnit(ctm.getItemUnit());
            sd.setOrderType("shipment");
            String type="shipment";
            //判断系统中是否已有该主单
//            try {
                if (ssPheaderMapperAuto.selectListEntityByEqual(sh).size()<1){
                    ssPheaderMapperAuto.insert(chm);
                    ssPdetailMapperAuto.insert(sd);

                    ShipmentDetail shipmentDetail = iShipmentDetailService.selectEntityById(ctm.getOrderDetailId());
                    if(shipmentDetail==null){
                        throw new ServiceException("单据已删除");
                    }
                    Integer result = shipmentDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                    //判断到货数量是否小于总数量,小于则做加法,因为此次是首次加入,库存数量为0
                    if (result==1||result==0){
                        shipmentDetail.setQtyCompleted(shipmentDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                        Integer rs = shipmentDetail.getQtyCompleted().compareTo(shipmentDetail.getQty());//到货数量加原库存比对总数量
                        if (rs==0){//等于
                            shipmentDetail.setStatus(lastStatus);
                            iShipmentDetailService.updateByModel(shipmentDetail);
                            Boolean resu = StockTask(sd,chm,type);
                            if (resu == false){
                                throw new ServiceException("库存中没有该容器!");
                            }
                        }
                        if (rs==-1){//小于
                            shipmentDetail.setStatus(firstStatus);
                            iShipmentDetailService.updateByModel(shipmentDetail);
                            Boolean resu = StockTask(sd,chm,type);
                            if (resu == false){
                                throw new ServiceException("库存中没有该容器!");
                            }
                        }
                        if (rs==1){//大于
                            return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量溢出,请填写正确的出货数量");
                        }
                    }else {
                        return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量大于了系统数量,请填写正确的出货数量");
                    }

                }else {
                    ShipmentHeader rh = new ShipmentHeader();
                    rh.setId(sh.getOrderHeaderId());
                    //判断主单是否已经完成
                    if (iShipmentHeaderService.selectFirstEntity(rh).getLastStatus() == 800){
                        return AjaxResult.setResultCS(0,"该出库主单已经完成,无需再次回传");
                    }

                    SSPdetail sd1 = new SSPdetail();
                    sd1.setOrderDetailId(ctm.getOrderDetailId());
                    sd1.setOrderHeaderId(chm.getOrderHeaderId());
                    //判断系统中是否已有该子单
                    if (ssPdetailMapperAuto.selectListEntityByEqual(sd1).size()<1){
                        ssPdetailMapperAuto.insert(sd);

                        ShipmentDetail shipmentDetail = iShipmentDetailService.selectEntityById(sd.getOrderDetailId());
                        if(shipmentDetail==null){
                            throw new ServiceException("单据已删除");
                        }
                        Integer result = shipmentDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                        //判断到货数量是否小于总数量,小于则做加法,因为此次是首次加入,库存数量为0
                        if (result==1||result==0){
                            shipmentDetail.setQtyCompleted(shipmentDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                            Integer rs = shipmentDetail.getQtyCompleted().compareTo(shipmentDetail.getQty());//到货数量加原库存比对总数量
                            if (rs==0){//等于
                                shipmentDetail.setStatus(lastStatus);
                                iShipmentDetailService.updateByModel(shipmentDetail);
                                Boolean resu = StockTask(sd,chm,type);
                                if (resu == false){
                                    throw new ServiceException("库存中没有该容器!");
                                }
                            }
                            if (rs==-1){//小于
                                shipmentDetail.setStatus(firstStatus);
                                iShipmentDetailService.updateByModel(shipmentDetail);
                                Boolean resu = StockTask(sd,chm,type);
                                if (resu == false){
                                    throw new ServiceException("库存中没有该容器!");
                                }
                            }
                            if (rs==1){//大于
                                return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量溢出,请填写正确的出货数量");
                            }
                        }else {
                            return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量大于了系统数量,请填写正确的出货数量");
                        }

                    }else {
                        sd1.setItemId(ctm.getItemId());
                        sd1.setOrderType("shipment");
                        //如果有,再判断该子单是否是多次回传,以及是否是出库子单
                        if (ssPdetailMapperAuto.selectListEntityByEqual(sd1).size()<1) {
                            ssPdetailMapperAuto.insert(sd);

                            ShipmentDetail shipmentDetail = iShipmentDetailService.selectEntityById(sd.getOrderDetailId());

                            //判断该子单是否已经完成
                            if (shipmentDetail.getQty()==shipmentDetail.getQtyCompleted()){
                                continue;
                            }
                            Integer result = shipmentDetail.getQty().compareTo(BigDecimal.valueOf(sd.getQty()));//总数量比对完成数量
                            //判断到货数量是否小于总数量,小于则做加法,因为此次是非首次加入
                            if (result==1||result==0){
                                shipmentDetail.setQtyCompleted(shipmentDetail.getQtyCompleted().add(BigDecimal.valueOf(sd.getQty())));
                                Integer rs = shipmentDetail.getQtyCompleted().compareTo(shipmentDetail.getQty());//到货数量加原库存比对总数量
                                if (rs==0){//等于
                                    shipmentDetail.setStatus(lastStatus);
                                    iShipmentDetailService.updateByModel(shipmentDetail);
                                    Boolean resu = StockTask(sd,chm,type);
                                    if (resu == false){
                                        throw new ServiceException("库存中没有该容器!");
                                    }
                                }
                                if (rs==-1){//小于
                                    shipmentDetail.setStatus(firstStatus);
                                    iShipmentDetailService.updateByModel(shipmentDetail);
                                    Boolean resu = StockTask(sd,chm,type);
                                    if (resu == false){
                                        throw new ServiceException("库存中没有该容器!");
                                    }
                                }
                                if (rs==1){//大于
                                    return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量溢出,请填写正确的出货数量");
                                }
                            }else {
                                return AjaxResult.error("ID为: "+shipmentDetail.getId().toString()+"  的出库子单收货数量大于了系统数量,请填写正确的出货数量");
                            }
                        }
                    }
                }
//            }catch (Exception e){
//                throw new ServiceException("出库主单或出库子单新增失败!");
//            }
        }

        ShipmentDetail detail = new ShipmentDetail();
        detail.setShipmentId(chm.getOrderHeaderId());
        List<ShipmentDetail> shipmentDetailList = iShipmentDetailService.selectListEntityByEqual(detail);
        int i = 0;
        for (ShipmentDetail rs:shipmentDetailList){
            if (rs.getStatus()==null){
                i++;
            }
            if (rs.getStatus()!=lastStatus){
                i++;
            }
        }
        if (i==0){
            ShipmentHeader shipmentHeader = new ShipmentHeader();
            shipmentHeader.setId(chm.getOrderHeaderId());
            shipmentHeader.setFirstStatus(800);
            shipmentHeader.setLastStatus(800);
            int res  = iShipmentHeaderService.updateByModel(shipmentHeader);
            if (res<1){
                throw new ServiceException("修改状态失败!");
            }
        }
        return AjaxResult.setResultCS(0,"出库单回传成功");
    }


    /**
     *
     * 钱柜生成任务
     *
     *
     */
    @Transactional
    public boolean StockTask(SSPdetail ssPdetail,SSPheader ssPheader,String type){
        Boolean result=true;
        //这里根据传过来的仓库code查到仓库ID
        WarehouseCompany warehouseCompany = new WarehouseCompany();
        warehouseCompany.setCompanyCode(ssPdetail.getCompanyCode());
        warehouseCompany.setWarehouseCode(ssPheader.getWarehouseCode());
        warehouseCompany=iCompanyService.selcetIWCBycode(warehouseCompany);

        //获取库位
        String containerCode=ssPdetail.getContainerCode();
        Location location=new Location();
        location.setWarehouseCode(warehouseCompany.getWarehouseCode());
        location.setContainerCode(containerCode);
        location=iLocationService.selectFirstEntity(location);

        //创建任务
        Task task=new Task();
        task.setWarehouseId(warehouseCompany.getWarehouseId());
        task.setWarehouseCode(warehouseCompany.getWarehouseCode());
        task.setCompanyId(warehouseCompany.getCompanyId());
        task.setCompanyCode(warehouseCompany.getCompanyCode());
        if(type.equals("receipt")) {
            task.setType((short) 200);
        }else {
            task.setType((short)400);
        }
        task.setContainerCode(containerCode);
        task.setRoadway(location.getRoadway());
        task.setSourceLocation(location.getCode());
        task.setDestinationLocation(location.getCode());
        task.setFirstStatus((short)1);
        task.setLastStatus((short)1);
        task.setZoneCode("QG");
        task.setCreatedBy(ShiroUtils.getLoginName());
//        task.setBeginTime(new Date());
//        task.setEndTime(new Date());
        task.setUpdateBy(ShiroUtils.getLoginName());
        try {
            taskService.insert(task);
        }catch (Exception e){
            throw new ServiceException("任务创建失败");
        }

        //获得库存
        Inventory inventory=new Inventory();
        inventory.setMaterialCode(ssPdetail.getItemCode());
        inventory.setWarehouseCode(warehouseCompany.getWarehouseCode());
        inventory.setContainerCode(containerCode);
        inventory=iInventoryService.selectFirstEntity(inventory);

        //创建子任务
        TaskDetail taskDetail=new TaskDetail();
        taskDetail.setTaskId(task.getId());
        taskDetail.setWarehouseId(warehouseCompany.getWarehouseId());
        taskDetail.setWarehouseCode(warehouseCompany.getWarehouseCode());
        taskDetail.setCompanyId(warehouseCompany.getCompanyId());
        taskDetail.setCompanyCode(warehouseCompany.getCompanyCode());
        taskDetail.setBillCode(ssPheader.getOrderCode());
        taskDetail.setBillId(ssPheader.getOrderHeaderId());
        taskDetail.setBillDetailId(ssPdetail.getOrderDetailId());
        taskDetail.setMaterialCode(ssPdetail.getItemCode());
        taskDetail.setMaterialName(ssPdetail.getItemName());
        taskDetail.setContainerCode(containerCode);
        if(inventory!=null){
            taskDetail.setInventoryId(inventory.getId());
        }
        taskDetail.setSourceLocation(location.getCode());
        taskDetail.setDestinationLocation(location.getCode());
        taskDetail.setQty(new BigDecimal(ssPdetail.getQty()));
        taskDetail.setStatus((short)1);
        taskDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        try {
            taskDetailService.insert(taskDetail);
        }catch (Exception e){
            throw new ServiceException("子任务创建失败");
        }
        Integer[] ids=new Integer[1];
        ids[0]=task.getId();
        try {
            taskService.completeTaskByWMS(ids);
        } catch (Exception e) {
            throw new ServiceException("完成任务失败");
        }
        return result;
    }
//    /**
//     * 钱柜入库库存修改类
//     * @param
//     * @return
//     */
//    public boolean StockReceipt(SSPdetail ssPdetail,SSPheader ssPheader){
//        //这里根据传过来的仓库code查到仓库ID
//        WarehouseCompany warehouseCompany = new WarehouseCompany();
//        warehouseCompany.setCompanyCode(ssPdetail.getCompanyCode());
//        warehouseCompany.setWarehouseCode(ssPheader.getWarehouseCode());
//        warehouseCompany=iCompanyService.selcetIWCBycode(warehouseCompany);
//
//        Inventory it = new Inventory();
//        it.setContainerCode(ssPdetail.getContainerCode());
//        it.setReceiptDetailId(ssPdetail.getOrderDetailId());
//        //根据传过来的容器编码和子单ID查询库存中是否已给该子单分配库位和库存
//        Inventory inventory = iInventoryService.selectFirstEntity(it);
//        //组合库存记录实体
//        InventoryTransaction inventoryTransaction = new InventoryTransaction();
//        inventoryTransaction.setWarehouseId(warehouseCompany.getWarehouseId());
//        inventoryTransaction.setWarehouseCode(warehouseCompany.getWarehouseCode());
//        inventoryTransaction.setType("receipt");
//        inventoryTransaction.setMaterialName(ssPdetail.getItemName());
//        inventoryTransaction.setMaterialCode(ssPdetail.getItemCode());
//        inventoryTransaction.setBillId(ssPheader.getOrderHeaderId());
//        inventoryTransaction.setBillCode(ssPheader.getOrderCode());
//        inventoryTransaction.setBillDetailId(ssPdetail.getOrderDetailId());
//        inventoryTransaction.setStatus("good");
//        inventoryTransaction.setCreatedBy(ShiroUtils.getLoginName());
//        inventoryTransaction.setLastUpdatedBy(ShiroUtils.getLoginName());
//        inventoryTransaction.setZoneCode(ssPheader.getCubeCode());
//        if (inventory != null){//已给该子单分配库位,由于没有任务锁定,则增加数量即可
//            BigDecimal qty = inventory.getQty();
//            BigDecimal qty1 = inventory.getQty().add(BigDecimal.valueOf(ssPdetail.getQty()));
//            inventory.setQty(qty1);
//            inventory.setLastUpdatedBy(ShiroUtils.getLoginName());
//            if (iInventoryService.updateByModel(inventory) > 0){
//                inventoryTransaction.setLocationCode(inventory.getLocationCode());
//                inventoryTransaction.setContainerCode(inventory.getContainerCode());
//                inventoryTransaction.setCompanyId(inventory.getCompanyId());
//                inventoryTransaction.setCompanyCode(inventory.getCompanyCode());
//                inventoryTransaction.setQty(qty);
//                inventoryTransaction.setTaskQty(BigDecimal.valueOf(ssPdetail.getQty()));
//                try{
//                    iInventoryTransactionService.insert(inventoryTransaction);
//                }catch (Exception e){
//                    throw new ServiceException("入库库存记录新增失败!");
//                }
//                return true;
//            }
//        }else {//未给该子单分配库位,需在库位里找一个库位分配容器和子单
//            Location location = new Location();
//            location.setContainerCode(ssPdetail.getContainerCode());
//            Location location1 = iLocationService.selectFirstEntity(location);
//            if (location1 != null){//库位里有该容器
//                it.setWarehouseId(warehouseCompany.getWarehouseId());
//                it.setWarehouseCode(ssPheader.getWarehouseCode());
//                it.setLocationId(location1.getId());
//                it.setLocationCode(location1.getCode());
//                it.setCompanyId(warehouseCompany.getCompanyId());
//                it.setCompanyCode(warehouseCompany.getCompanyCode());
//                it.setMaterialCode(ssPdetail.getItemCode());
//                it.setReceiptId(ssPheader.getOrderHeaderId());
//                it.setReceiptCode(ssPheader.getOrderCode());
//                it.setReceiptDetailId(ssPdetail.getOrderDetailId());
//                it.setStatus(ssPdetail.getItemState());
//                it.setQty(BigDecimal.valueOf(ssPdetail.getQty()));
//                it.setCreatedBy(ShiroUtils.getLoginName());
//                if (iInventoryService.insert(it)>0){
//                    inventoryTransaction.setLocationCode(location1.getCode());
//                    inventoryTransaction.setContainerCode(location1.getContainerCode());
//                    inventoryTransaction.setCompanyId(warehouseCompany.getCompanyId());
//                    inventoryTransaction.setCompanyCode(warehouseCompany.getCompanyCode());
//                    inventoryTransaction.setTaskQty(BigDecimal.valueOf(ssPdetail.getQty()));
//                    try{
//                        iInventoryTransactionService.insert(inventoryTransaction);
//                    }catch (Exception e){
//                        throw new ServiceException("入库库存记录新增失败!");
//                    }
//                    return true;
//                }
//            }else {//库位里没有该容器,将在库位中找一个最底层没有放货的容器
//                Location location0 = iLocationService.selectSSPLocation();
//
//                it.setWarehouseId(warehouseCompany.getWarehouseId());
//                it.setWarehouseCode(ssPheader.getWarehouseCode());
//                it.setLocationId(location0.getId());
//                it.setLocationCode(location0.getCode());
//                it.setCompanyId(warehouseCompany.getCompanyId());
//                it.setCompanyCode(warehouseCompany.getCompanyCode());
//                it.setMaterialCode(ssPdetail.getItemCode());
//                it.setReceiptId(ssPheader.getOrderHeaderId());
//                it.setReceiptCode(ssPheader.getOrderCode());
//                it.setReceiptDetailId(ssPdetail.getOrderDetailId());
//                it.setStatus(ssPdetail.getItemState());
//                it.setQty(BigDecimal.valueOf(ssPdetail.getQty()));
//                it.setCreatedBy(ShiroUtils.getLoginName());
//                if (iInventoryService.insert(it)>0){
//                    iLocationService.updateContainerCode(location0.getCode(),ssPdetail.getContainerCode());
//                    inventoryTransaction.setTaskQty(BigDecimal.valueOf(ssPdetail.getQty()));
//                    inventoryTransaction.setLocationCode(location0.getCode());
//                    inventoryTransaction.setContainerCode(location0.getContainerCode());
//                    inventoryTransaction.setCompanyId(warehouseCompany.getCompanyId());
//                    inventoryTransaction.setCompanyCode(warehouseCompany.getCompanyCode());
//                    try{
//                        iInventoryTransactionService.insert(inventoryTransaction);
//                    }catch (Exception e){
//                        throw new ServiceException("入库库存记录新增失败!");
//                    }
//                    return true;
//                }
//            }
//        }
//         throw new ServiceException("入库库存修改失败!");
//    }
//
//    /**
//     * 钱柜出库库存修改类
//     * @param
//     * @return
//     */
//    public boolean StockShipment(SSPdetail ssPdetail,SSPheader ssPheader) {
//        Inventory it = new Inventory();
//        it.setContainerCode(ssPdetail.getContainerCode());
//        it.setMaterialCode(ssPdetail.getItemCode());
//        //根据传过来的容器编码和子单ID查询库存中是否已给该子单分配库位和库存
//        Inventory inventory = iInventoryService.selectFirstEntity(it);
//        if (inventory != null) {//已给该子单分配库位,由于没有任务锁定,则减少数量即可
//            BigDecimal qty = inventory.getQty();
//            BigDecimal qty1 = inventory.getQty().subtract(BigDecimal.valueOf(ssPdetail.getQty()));
//            inventory.setQty(qty1);
//            inventory.setLastUpdatedBy(ShiroUtils.getLoginName());
//            if (iInventoryService.updateByModel(inventory) > 0) {
//                //组合库存记录实体
//                InventoryTransaction inventoryTransaction = new InventoryTransaction();
//                inventoryTransaction.setWarehouseId(inventory.getWarehouseId());
//                inventoryTransaction.setWarehouseCode(inventory.getWarehouseCode());
//                inventoryTransaction.setLocationId(inventory.getLocationId());
//                inventoryTransaction.setLocationCode(inventory.getLocationCode());
//                inventoryTransaction.setContainerCode(inventory.getContainerCode());
//                inventoryTransaction.setType("shipment");
//                inventoryTransaction.setCompanyId(inventory.getCompanyId());
//                inventoryTransaction.setCompanyCode(inventory.getCompanyCode());
//                inventoryTransaction.setMaterialName(ssPdetail.getItemName());
//                inventoryTransaction.setMaterialCode(ssPdetail.getItemCode());
//                inventoryTransaction.setBillId(ssPheader.getOrderHeaderId());
//                inventoryTransaction.setBillCode(ssPheader.getOrderCode());
//                inventoryTransaction.setBillDetailId(ssPdetail.getOrderDetailId());
//                inventoryTransaction.setStatus("good");
//                inventoryTransaction.setLastUpdatedBy(ShiroUtils.getLoginName());
//                inventoryTransaction.setCreatedBy(ShiroUtils.getLoginName());
//                inventoryTransaction.setQty(qty);
//                inventoryTransaction.setTaskQty(BigDecimal.valueOf(ssPdetail.getQty()).multiply(new BigDecimal(-1)));
//                try{
//                    iInventoryTransactionService.insert(inventoryTransaction);
//                }catch (Exception e){
//                    throw new ServiceException("入库库存记录新增失败!");
//                }
//                return true;
//            }
//        } else {
//            return false;
//        }
//        return false;
//    }
}