QisdaBindService.java 34.9 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
package com.myproject.webapp.controller.webService;

import com.myproject.bean.qisda.AppendInfo;
import com.myproject.bean.update.Barcode;
import com.myproject.bean.update.StoragePos;
import com.myproject.bean.update.qisda.OutInfo;
import com.myproject.bean.update.qisda.OutItem;
import com.myproject.dao.mongo.IBarcodeDao;
import com.myproject.dao.mongo.IStoragePosDao;
import com.myproject.dao.mongo.qisda.IOutInfoDao;
import com.myproject.dao.mongo.qisda.IOutItemDao;
import com.myproject.webapp.controller.qisda.util.OutInfoCache;
import com.myproject.webapp.controller.qisda.util.SoseqCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created by sunke on 2020/4/26.
 */
@Repository
public class QisdaBindService {


    protected final transient Logger log = LogManager.getLogger(getClass());

    @Autowired
    private IOutItemDao outItemDao;

    @Autowired
    private IStoragePosDao storagePosDao;

    @Autowired
    private IBarcodeDao barcodeDao;

    @Autowired
    private SoseqCache soseqCache;

    @Autowired
    private OutInfoCache outInfoCache;

    /**
     * 真实绑定工单(只对分盘需求单进行绑定)
     */
    public void realBindOutInfo(OutInfo outInfo){
        //只对分盘需求单进行绑定(分盘料,未关闭,未绑定OK,未发料完成)
        if(!outInfo.isClosed() && !outInfo.isRealBindOk() && !outInfo.isSendEnd()){
            log.info("需求单["+outInfo+"]开始进行真实绑定");
            for (OutItem outItem : outInfo.getOutItems()) {
                firstBindCutReel(outItem);
                secondBindCutReel(outItem, null);
                realBindNoCutReel(outItem);
                bindSamePnFromOtherSlotForFirstAction(outItem,new ArrayList<String>());
            }
            soseqCache.checkRealBindStatus(outInfo.getSoseq());
        }
    }

    /**
     * 预绑定分盘料
     */
    public synchronized void preBindCutOutInfoList(Collection<OutInfo> outInfoList){
        List<String> soList = new ArrayList<>();
        for (OutInfo outInfo : outInfoList) {
            if(outInfo.isReelCutAction()){
                soList.add(outInfo.getSo());
                //分盘需求单进行预绑定
                log.info("需求单["+outInfo+"]开始进行预绑定");
                for (OutItem outItem : outInfo.getOutItems()) {
                    firstBindCutReel(outItem);
                    preBindReel(outItem);
                }
            }else{
                log.info("需求单["+outInfo+"]不需要进行预绑定");
            }
        }

        for (OutInfo outInfo : outInfoList) {
            if(outInfo.isReelCutAction()){
                //首盘和分盘进行缺料反馈
                log.info("分盘需求单["+outInfo+"]开始进行二次分盘绑定");
                for (OutItem outItem : outInfo.getOutItems()) {
                    if(outItem.isReelCutAction()){//分盘
                        secondBindCutReel(outItem, soList);
                    }
                }
            }
        }
    }


    /******************************** 以下是 Private 方法*******************************************/

    /**
     * 预绑定非分盘料,此方法只会在收到分盘需求单只执行一次
     */
    private void preBindReel(OutItem outItem){
        if(!outItem.isCutMaterial()){
            log.info("预绑定非分盘料:So=["+outItem.getSo()+"]hSerial=["+outItem.getSourceName()+"]pn=["+outItem.getPn()+"]当前预绑定数量["+outItem.getLockQty()+"/"+outItem.getQty()+"]");
            while (true){
                StoragePos pos = storagePosDao.findNoBindMinQty(outItem.getPn(), outItem.getFacility());
                if(pos == null){
                    log.info("\t预绑定"+outItem.getSlotlocation() + "的pn=["+outItem.getPn()+"]facility = "+outItem.getFacility()+"时无库存,跳出预绑定");
                    break;
                }else{
                    int dbQty = pos.getBarcode().getAmount();
                    int lockQty = outItem.getLockQty();
                    int newLockQty = lockQty + dbQty;
                    log.info("\t预绑定["+pos.getBarcode().getBarcode()+"]到hSerial=["+outItem.getSourceName()+"] 数量:" + newLockQty +"/" + outItem.getQty());
                    Barcode barcode = pos.getBarcode();
//                    AppendInfo appendInfo = barcode.getAppendInfo();
//                    appendInfo.sethSerial(outItem.gethSerial());
//                    appendInfo.setRefno(outItem.getRefno());
//                    appendInfo.setSo(outItem.getSo());
//                    appendInfo.setSoseq(outItem.getSoseq());
//                    appendInfo.setPreBindSlot(outItem.getSlotlocation()+"");
//                    barcode.setAppendInfo(appendInfo);
                    barcode.preBindItem(outItem);
                    pos.setBarcode(barcode);
                    storagePosDao.save(pos);
                    outItem.setLockQty(newLockQty);
                    outItemDao.updateLockQty(outItem.getId(),newLockQty, 0);
                    //多出数量
                    int surplusQty = newLockQty - outItem.getQty();
                    if(surplusQty >= 0){
                        //已经满足需求了,剔除同日期的小料盘后直接跳出
                        //已经预绑定的所有料盘中与最大盘日期相同,且与超出数量最接近的料盘剔除掉
                        StoragePos needToUnBindPos = storagePosDao.findSurplusPreBindReel(outItem.getSoseq(), outItem.getSlotlocation(), surplusQty, barcode.getProduceDateStr());
                        if(needToUnBindPos != null){
                            int unbindAmount = needToUnBindPos.getBarcode().getAmount();
                            log.info(outItem.getSourceName() + "找到与最大料盘["+barcode.getBarcode()+"]相同日期["+barcode.getProduceDateStr()+"]与超绑数量["+surplusQty+"]接近的料盘["+needToUnBindPos.getBarcode().getBarcode()+"]数量为["+unbindAmount+"]解除预绑定");
                            storagePosDao.unbindReel(needToUnBindPos);
                            newLockQty = newLockQty - unbindAmount;
                            outItem.setLockQty(newLockQty);
                            outItemDao.updateLockQty(outItem.getId(),newLockQty, 0);
                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 此方法只会在收到分盘需求单只执行一次
     * 分盘需求第一次绑定: 第一轮挑料:
     1.料卷数量等于需求
     2.料卷数量,从小到大去挑选,加总数量小于等于需求
     */
    private void firstBindCutReel(OutItem outItem){
        if(outItem.isCutMaterial()){
            log.info("第一轮绑定分盘料:So=["+outItem.getSo()+"]hSerial=["+outItem.gethSerial()+"]的slot"+outItem.getSlotlocation()+"]pn=["+outItem.getPn()+"]当前绑定数量["+outItem.getLockQty()+"/"+outItem.getQty()+"]");

            //剩余需求数量
            int needNum = outItem.getQty() - outItem.getLockQty();
            while(needNum > 0){
                //分盘料
                StoragePos pos = storagePosDao.findNoBindNoCutMinQty(outItem.getPn(), outItem.getFacility());
                if(pos == null){
                    log.info("\t第一轮绑定分盘料"+outItem.getSlotlocation() + "的pn=["+outItem.getPn()+"]facility = "+outItem.getFacility()+"时无库存,跳出绑定");
                    break;
                }else{
                    Barcode barcode = pos.getBarcode();
                    int totalLockQty = outItem.getLockQty() + barcode.getAmount();
                    if(totalLockQty > outItem.getQty()){
                        log.info("加总数量["+totalLockQty+"]大于需求数量["+outItem.getQty()+"]跳出第一轮绑定");
                        break;
                    }

                    if(!barcode.hasCutInfo()){
                        log.info("\t第一轮绑定分盘料["+barcode.getBarcode()+"]绑定到So=["+outItem.getSo()+"]hSerial=["+outItem.getSourceName()+"] 数量:" + totalLockQty +"/" + outItem.getQty());
                        //没有分盘信息,可以直接绑定
//                        AppendInfo appendInfo = barcode.getAppendInfo();
//                        appendInfo.sethSerial(outItem.gethSerial());
//                        appendInfo.setRefno(outItem.getRefno());
//                        appendInfo.setSo(outItem.getSo());
//                        appendInfo.setSoseq(outItem.getSoseq());
//                        appendInfo.setSlotStr(outItem.getSlotStr());
//                        appendInfo.setBindSlot(outItem.getSlotlocation() + "");
//                        appendInfo.setSlotIndex(outItem.getSlotlocation());
//                        barcode.setAppendInfo(appendInfo);
                        barcode.realBindItem(outItem);
                        pos.setBarcode(barcode);
                        storagePosDao.save(pos);

                        int realLockQty = outItem.getRealLockQty() + barcode.getAmount();
                        outItem.setRealLockQty(realLockQty);
                        outItem.setLockQty(totalLockQty);
                        outItemDao.updateLockQty(outItem.getId(),totalLockQty,realLockQty);
                        soseqCache.udpateTotalPreLockQty(outItem, totalLockQty);
                        soseqCache.updateTotalRealLockQty(outItem, realLockQty);

                        needNum = outItem.getQty() - totalLockQty;
                    }else{
                        log.error("这句不应该出现,第一轮绑定分盘料时查到分盘信息的物料,分盘料["+barcode.getBarcode()+"]分盘信息:" + barcode.getAppendInfo().getCutMap());
                        break;
                    }
                }
            }
        }
    }

    /**
     * 此方法只会在收到分盘需求单只执行一次
     * 1.料卷数量等于余量(需分盘的数量)的总数量
     2.By SO逐一挑选,料卷数量,从小到大去挑选
     * @param outItem
     */
    private void secondBindCutReel(OutItem outItem,List<String> shareCutSoList){
        if(outItem.isCutMaterial()){
            log.info("第二轮绑定分盘料:So=["+outItem.getSo()+"]hSerial=["+outItem.gethSerial()+"]+refno=["+outItem.getRefno()+"]的slot"+outItem.getSlotlocation()+"]pn=["+outItem.getPn()+"]当前预绑定数量["+outItem.getLockQty()+"/"+outItem.getQty()+"]");
            //剩余需求数量
            int needNum = outItem.getQty() - outItem.getLockQty();
            while(needNum > 0){
                //分盘料
                StoragePos pos = null;
                if(shareCutSoList != null){
                    //第二轮应该查找未绑定过或者有分盘信息的物料
                    List<StoragePos> posList = storagePosDao.listPnFacility(outItem.getPn(), outItem.getFacility());
                    for (StoragePos storagePos : posList) {
                        Barcode barcode = storagePos.getBarcode();
                        boolean hasCutInfo = barcode.hasCutInfo();
                        if(barcode.getAmount() > 0  && hasCutInfo){
                            //优先查找同批需求单中有分盘的物料
                            AppendInfo appendInfo = storagePos.getBarcode().getAppendInfo();
                            String bindSo = appendInfo.getSo();
                            if(bindSo != null && shareCutSoList.contains(bindSo)){
                                //同批需求单已经有绑定
                                pos = storagePos;
                                break;
                            }
                        }
                    }

                }

                if(pos == null){
                    //未找到已经绑定过有分盘信息的物料
                    pos = storagePosDao.findNoBindMinQty(outItem.getPn(), outItem.getFacility());
                }

                if(pos == null){
                    log.info("\t第二轮分盘料绑定"+outItem.getSlotlocation() + "的pn=["+outItem.getPn()+"]facility = "+outItem.getFacility()+"时无库存,跳出绑定");
                    break;
                }else{
                    Barcode barcode = pos.getBarcode();
                    int lockQty = outItem.getLockQty();
                    //还有未绑定的需求
                    int reelRemainNum = barcode.cutCount(outItem);
                    if(reelRemainNum > 0){
                        //母盘还有剩余,说明该需求slot已经满足
                        lockQty = outItem.getQty();
                    }else {
                        //母盘正好用完或全部用完也达不到需求量,此母盘绑定slot后,继续寻找其他盘进行绑定
                        //reelRemainNum为0表示正好用完满足需求, reelRemainNum为负值时,表示需求还未满足
                        lockQty = outItem.getQty() + reelRemainNum;
                    }
                    log.info("\t分盘料["+barcode.getBarcode()+"]绑定到So=["+outItem.getSo()+"]hSerial=["+outItem.getSourceName()+"]数量:" + lockQty +"/" + outItem.getQty());
                    if(!barcode.hasCutInfo()){
                        //没有分盘信息,可以直接绑定
                        barcode.realBindItem(outItem);
                        int realLockQty = outItem.getRealLockQty() + barcode.getAmount();
                        outItem.setRealLockQty(realLockQty);

                    }else{

                        AppendInfo appendInfo = barcode.getAppendInfo();
                        appendInfo.setSlotIndex(outItem.getSlotlocation());
                        appendInfo.setSo(outItem.getSo());
                        appendInfo.setSoseq(outItem.getSoseq());
                        barcode.setAppendInfo(appendInfo);
                        log.info("分盘料["+barcode.getBarcode()+"]分盘信息:" + barcode.getAppendInfo().getCutMap());
                    }
                    barcode = barcodeDao.save(barcode);
                    pos.setBarcode(barcode);
                    storagePosDao.save(pos);
                    outItem.setLockQty(lockQty);
                    outItemDao.updateLockQty(outItem.getId(),outItem.getLockQty(), outItem.getRealLockQty());
                    soseqCache.udpateTotalPreLockQty(outItem, outItem.getLockQty());
                    soseqCache.updateTotalRealLockQty(outItem, outItem.getRealLockQty());

                    needNum = outItem.getQty() - lockQty;
                }
            }
        }
    }


    /**
     * 真实绑定非分盘料,此方法只会在首盘需求单状态变为即将执行时(状态为NEW且到达建议出库时间或者必须出库时间或人工提前执行)执行一次
     */
    private void realBindNoCutReel(OutItem outItem){
        if(!outItem.isCutMaterial()){
            //updateRealLockQty(outItem);
            //先从预绑定料盘中进行绑定,如果还有缺料的,从未使用的物料中查找,如果还缺料,从预绑定的物料中查找
            log.info("绑定非分盘料:So=["+outItem.getSo()+"]hSerial=["+outItem.gethSerial()+"]+refno=["+outItem.getRefno()+"]的slotLoction"+outItem.getSlotlocation()+"]pn=["+outItem.getPn()+"]当前绑定数量["+outItem.getRealLockQty()+"/"+outItem.getQty()+"]当前出库/发料数量["+outItem.getOutQty()+"/"+outItem.getSendQty()+"]");

            List<StoragePos> preBindPosList = storagePosDao.findPreBindList(outItem.getSoseq(), outItem.getSlotlocation());
            for (StoragePos pos : preBindPosList) {
                outItem = tryRealBindPos(pos, outItem);
            }
            //所需数量=需求单数量-已发料数量-真实绑定数量
            int needNum = outItem.getQty() - outItem.getSendQty() - outItem.getRealLockQty();
            log.info("将预绑定转为真实绑定结束,所需数量("+needNum+")=需求单数量("+outItem.getQty()+")-已发料数量("+ outItem.getSendQty()+")-真实绑定数量"+ outItem.getRealLockQty() +")");
            if(needNum >= 0){
                log.info("预绑定数量不足,查找未绑定料盘进行真实绑定结束,当前数量:"+outItem.getSendQty()+"+"+ outItem.getRealLockQty() +"/" + outItem.getQty());
                while(needNum >= 0){
                    StoragePos pos = storagePosDao.findNoBindMinQty(outItem.getPn(), outItem.getFacility());
                    OutItem resultOutItem = tryRealBindPos(pos, outItem);
                    if(resultOutItem == null){
                        break;
                    }
                    outItem = resultOutItem;
                    if(outItem.getRealLockQty() > outItem.getQty()){
                        //已经满足需求了,直接跳出
                        break;
                    }
                }
            }

            needNum = outItem.getQty() - outItem.getSendQty() - outItem.getRealLockQty();
            if(needNum >= 0 ){
                log.info("未绑定料盘数量不足,开始抢其他工单的预绑定物料进行真实绑定,当前数量:"+outItem.getSendQty()+"+"+ outItem.getRealLockQty() +"/" + outItem.getQty());
                //抢其他工单的预绑定
                while(needNum >= 0){
                    StoragePos pos = storagePosDao.findOtherPreBindMinQty(outItem.getPn(), outItem.getFacility());
                    OutItem resultOutItem = tryRealBindPos(pos, outItem);
                    if(resultOutItem == null){
                        break;
                    }
                    outItem = resultOutItem;
                    if(outItem.getRealLockQty() + outItem.getSendQty() > outItem.getQty()){
                        //已经满足需求了,直接跳出
                        break;
                    }
                }
            }

            unbindSurplusReel(outItem);

            //首盘,如果此站位一盘也没有,查找是否有同工单不同站位的PN,如果有,抢一盘过来
            log.info("So=["+outItem.getSo()+"]hSerial=["+outItem.getSourceName()+"]pn=["+outItem.getPn()+"]真实绑定结束,当前数量:"+outItem.getSendQty()+"+"+ outItem.getRealLockQty() +"/" + outItem.getQty());
        }
    }

    /**
     * 绑定入库料盘
     */
    public Barcode bindPutInReel(Barcode barcode){
        boolean isCutReelIn = false;
        String bindSoseq = barcode.getAppendInfo().getSoseq();
        String bindSlot = barcode.getAppendInfo().getBindSlot();
        OutItem cutItem = soseqCache.getCutOutItem(bindSoseq, Integer.valueOf(bindSlot));
        if(Strings.isBlank(bindSoseq)){
            //未绑定
            cutItem = findNeedBindItem(barcode);
        }else{
            //料盘分盘过后入库(已经预绑定过)
            isCutReelIn = true;
        }
        if(cutItem != null){
            OutInfo cutOutInfo = soseqCache.getCutActionInfoFromCache(cutItem.getSoseq());
            if(cutOutInfo != null && cutOutInfo.isClosed()){
                log.info("工单["+cutOutInfo.getSo()+"]已关闭,不再绑定料盘["+barcode.getBarcode()+"]");
                barcode = barcode.removeBindInfo();
            }else if(cutItem.realBindLessQty() <= 0){
                log.info("工单["+cutOutInfo.getSo() + "的站位["+bindSlot+"]绑定已满足要求(或线外发过分盘料),不再对["+barcode.getBarcode()+"]进行绑定");
                barcode = barcode.removeBindInfo();
            }else{
                //分盘料
                if(cutItem.isCutMaterial()){
                    if(!isCutReelIn){
                        //不是分完盘入库的,需要先进行预绑定
                        int preBindQty = cutItem.getLockQty() + barcode.getAmount();
                        if(preBindQty > cutItem.getQty()){
                            int reelRemainNum = barcode.cutCount(cutItem);
                            if(reelRemainNum > 0){
                                //母盘还有剩余,说明该需求slot已经满足
                                preBindQty = cutItem.getQty();
                            }else {
                                //母盘正好用完或全部用完也达不到需求量,此母盘绑定slot后,需要继续寻找其他盘进行绑定
                                preBindQty = preBindQty + barcode.getAmount();
                            }
                        }
                        cutItem.setLockQty(preBindQty);
                        soseqCache.udpateTotalPreLockQty(cutItem,preBindQty);


                        log.info("\t分盘料"+barcode.getBarcode()+"["+barcode.getPartNumber()+"]绑定到soseq=["+cutItem.getSoseq()+"]so=["+cutItem.getSo()+"]的["+cutItem.getSlotlocation()+"] 数量:" + preBindQty +"/" + cutItem.getQty());
                    }

                    if(!barcode.hasCutInfo()){
                        //没有分盘信息,可以直接绑定
                        barcode.realBindItem(cutItem);
                        int realBindQty = cutItem.getRealLockQty() + barcode.getAmount();
                        cutItem.setRealLockQty(realBindQty);
                        soseqCache.updateTotalRealLockQty(cutItem,realBindQty);
                    }else{
                        AppendInfo appendInfo = barcode.getAppendInfo();
                        appendInfo.setSlotIndex(cutItem.getSlotlocation());
                        appendInfo.setSo(cutItem.getSo());
                        appendInfo.setSoseq(cutItem.getSoseq());
                        barcode.setAppendInfo(appendInfo);
                        log.info("分盘料["+barcode.getBarcode()+"]分盘信息:" + barcode.getAppendInfo().getCutMap());
                    }
                }else{
                    //绑定
                    barcode.realBindItem(cutItem);
                    int realBindQty = cutItem.getRealLockQty() + barcode.getAmount();
                    cutItem.setRealLockQty(realBindQty);
                    soseqCache.updateTotalRealLockQty(cutItem,realBindQty);
                    log.info(barcode.getBarcode() + "["+barcode.getPartNumber()+"]入库完成,工单so=["+cutItem.getSo()+"] soseq=["+cutItem.getSoseq()+"]slotSeq=["+cutItem.getSlotlocation()+"]绑定数量增加"+barcode.getAmount()+"="+ realBindQty);
                }

                unbindSurplusReel(cutItem);
            }
        }

        barcode = barcodeDao.save(barcode);
        return barcode;
    }

    /**
     * 剔除多余的绑定,最小的绑定与
     */
    public void unbindSurplusReel(OutItem outItem){
        //多出数量
        int surplusQty = /*outItem.getSendQty() + */outItem.getRealLockQty() - outItem.getQty();
        if(surplusQty > 0){
            //已经预绑定的所有料盘中与最大盘日期相同,且与超出数量最接近的料盘剔除掉
            StoragePos maxBindReelPos = storagePosDao.findMaxQtyBindReel(outItem.getSoseq(), outItem.getSlotlocation());
            if(maxBindReelPos != null){
                Barcode maxQtyBindReelBarcode = maxBindReelPos.getBarcode();
                StoragePos needToUnBindPos = storagePosDao.findSurplusBindReel(outItem.getSoseq(), outItem.getSlotlocation(), surplusQty, maxQtyBindReelBarcode.getProduceDateStr());
                if(needToUnBindPos != null){
                    int unbindAmount = needToUnBindPos.getBarcode().getAmount();
                    log.info(outItem.getSourceName() + "找到与最大料盘["+maxQtyBindReelBarcode.getBarcode()+"]相同日期["+maxQtyBindReelBarcode.getProduceDateStr()+"]与超绑数量["+surplusQty+"]接近的料盘["+needToUnBindPos.getBarcode().getBarcode()+"]数量为["+unbindAmount+"]解除绑定");
                    needToUnBindPos = storagePosDao.unbindReel(needToUnBindPos);
                    int newRealLockQty = outItem.getRealLockQty() - unbindAmount;
                    outItem.setRealLockQty(newRealLockQty);
                    //outItem = outItemDao.save(outItem);
                    soseqCache.updateTotalRealLockQty(outItem, newRealLockQty);

                    int newPreBindQty = outItem.getLockQty() - unbindAmount;
                    outItem.setLockQty(newPreBindQty);
                    soseqCache.udpateTotalPreLockQty(outItem, newPreBindQty);

                    //绑定该料盘到其他工单
                    bindPutInReel(needToUnBindPos.getBarcode());

                    surplusQty = outItem.getSendQty() + outItem.getRealLockQty() - outItem.getQty();
                    if(surplusQty > 0){
                        unbindSurplusReel(outItem);
                    }
                }
            }
        }
    }

    /**
     * 入库完成时,查找需要绑定的需求单
     */
    private OutItem findNeedBindItem(Barcode barcode){
        List<String> lessSoseqList = new ArrayList<>();
        List<OutInfo> cacheOutInfoList = outInfoCache.getCachedOutInfos();
        for (OutInfo unEndOutInfo : cacheOutInfoList) {
            if(unEndOutInfo.isReelCutAction()){
                if(!unEndOutInfo.isClosed() && unEndOutInfo.isRealBindLess()){
                    String soseq = unEndOutInfo.getSoseq();
                    if(!lessSoseqList.contains(soseq)){
                        lessSoseqList.add(soseq);
                    }
                }
            }
        }

        String pn = barcode.getPartNumber();
        String facility = barcode.getAppendInfo().getFacility();
        List<OutItem> cutItemList = outItemDao.findCutItemList(lessSoseqList, pn, facility);

        //优先绑定执行过的工单,如果没有,则绑定优先执行的工单
        OutItem needBindItem = null;
        int executeOrderIndex = -1;

        for (OutItem outItem : cutItemList) {
            if(!outItem.isBindOk()){
                OutInfo outInfo = soseqCache.getCutActionInfoFromCache(outItem.getSoseq());
                if(outInfo != null){
                    //真实绑定缺料
                    if(outItem.realBindLessQty() > 0){
                        int orderIndex = lessSoseqList.indexOf(outItem.getSoseq());
                        if(needBindItem == null || orderIndex < executeOrderIndex){
                            needBindItem = outItem;
                            executeOrderIndex = orderIndex;
                        }
                    }
                }
            }
        }

        return needBindItem;
    }



    /**
     * 尝试真实绑定库位
     * @param pos 包含料盘的库位
     * @param outItem 出库需求项
     * @return 如果库位为null,返回null;否则返回更新后的出库需求项
     */
    private OutItem tryRealBindPos(StoragePos pos, OutItem outItem){
        if(pos == null){
            log.info("\t真实绑定"+outItem.getSlotlocation() + "的pn=["+outItem.getPn()+"]facility = "+outItem.getFacility()+"时无库存,跳出绑定");
            return null;
        }else{
            Barcode barcode = pos.getBarcode();
            AppendInfo oldBindAppendInfo = barcode.getAppendInfo();
            String oldBindSoSeq = oldBindAppendInfo.getSoseq();
            int dbQty = barcode.getAmount();
            if(Strings.isNotBlank(oldBindSoSeq)){
                //已经有绑定信息
                String oldBindSlotStr = oldBindAppendInfo.getBindSlot();
                if(Strings.isNotBlank(oldBindSlotStr)){
                    int oldBindSlot = Integer.valueOf(oldBindSlotStr);
                    if(oldBindSlot > 0){
                        log.error("\t真实绑定"+outItem.getSlotlocation() + "的pn=["+outItem.getPn()+"]facility = "+outItem.getFacility()+"时 "+barcode.getBarcode()+" 已被soseq=["+oldBindSoSeq+"_"+oldBindSlot+"]绑定");
                        return null;
                    }
                }
                String oldPreBindSlot = oldBindAppendInfo.getPreBindSlot();
                if(Strings.isNotBlank(oldPreBindSlot)){
                    int oldSlotSeq = Integer.valueOf(oldPreBindSlot);
                    log.info("预绑定oldsoseq=["+oldBindSoSeq+"_"+oldPreBindSlot+"]物料["+barcode.getBarcode()+"],更新其预绑定数量");
                    OutItem oldCutOutItem = soseqCache.getCutOutItem(oldBindSoSeq, oldSlotSeq);
                    int oldTotalPreBindQty = oldCutOutItem.getLockQty();
                    oldTotalPreBindQty = oldTotalPreBindQty - dbQty;
                    soseqCache.udpateTotalPreLockQty(oldCutOutItem, oldTotalPreBindQty);
                }
            }

            int realLockQty = outItem.getRealLockQty();
            int newRealLockQty = realLockQty + dbQty;

            log.info("\t真实绑定["+barcode.getBarcode()+"]到So=["+outItem.getSo()+"]hSerial=["+outItem.getSourceName()+"] 绑定数量:" + newRealLockQty +"/" + outItem.getQty());
            barcode.realBindItem(outItem);
            pos.setBarcode(barcode);
            storagePosDao.save(pos);
            outItem.setRealLockQty(newRealLockQty);
            outItemDao.updateLockQty(outItem.getId(), 0, newRealLockQty);
            soseqCache.updateTotalRealLockQty(outItem, newRealLockQty);

            return outItem;
        }
    }


//    private OutItem updateRealLockQty(OutItem outItem){
//        //查找真实绑定
//        List<StoragePos> bindPosList = storagePosDao.findBindList(outItem.getSoseq(), outItem.getSlotlocation());
//        int realBindQty = 0;
//        for (StoragePos bindPos : bindPosList) {
//            realBindQty = realBindQty + bindPos.getBarcode().getAmount();
//        }
//        outItem.setRealLockQty(realBindQty);
//        int lockQty = outItem.getLockQty();
//        if(lockQty < realBindQty){
//            lockQty = realBindQty;
//            outItem.setLockQty(lockQty);
//        }
//        if(outItem.isCutMaterial()){
//            //分盘料,需要将已绑定未出库的物料也计算进去
//            List<StoragePos> cutReels = storagePosDao.findCutList(outItem.getSo(), outItem.getSlotlocation(), outItem.getSoseq());
//
//            for (StoragePos cutReel : cutReels) {
//                List<Map<String, Object>> cutItems = cutReel.getBarcode().getCutItems();
//                for (Map<String, Object> cutItem : cutItems) {
//                    String so = cutItem.get("so").toString();
//                    String soseqStr = cutItem.get("soseq").toString();
//                    String slotlocation = cutItem.get("slotlocation").toString();
//                    if(outItem.getSo().equals(so) && outItem.getSoseq().equals(soseqStr) &&  outItem.getSlotlocation() == Integer.valueOf(slotlocation)){
//                        log.info("查找到未出库的分盘料信息["+cutItem+"],更新预绑定数量");
//                        String qty = cutItem.get("qty").toString();
//                        lockQty = outItem.getLockQty() + Integer.valueOf(qty);
//                        outItem.setLockQty(lockQty);
//                    }
//                }
//            }
//        }
//        outItem = outItemDao.save(outItem);
//        log.info("更新["+outItem.getSlotlocation()+"]"+outItem.getPn()+"真实绑定数量为["+realBindQty+"/"+outItem.getQty()+"]");
//        return outItem;
//    }

    /**
     * 检查首盘料中不同Slot上相同的PN, 如果缺料,出首盘时要保证每一个Slot上都有料
     */
    private void bindSamePnFromOtherSlotForFirstAction(OutItem outItem, Collection<String> excludeBarcodeList){
        if(outItem.getRealLockQty() == 0){
            //绑定数量为0,说明缺料,查找是否有绑定的本工单的其他Slot相同PN的料
            List<StoragePos> posList = storagePosDao.findBindByPn(outItem.getSoseq(), outItem.getPn());
            if(posList != null && !posList.isEmpty()){
                //站位的绑定的料盘数量,大于2盘才可以抢
                Map<String,Integer> slotReelCountMap = new HashMap<>();
                for (StoragePos storagePos : posList) {
                    //其他工位绑定至少两盘才可以抢
                    Barcode barcode = storagePos.getBarcode();
                    String bindSlot = barcode.getAppendInfo().getBindSlot();

                    if(bindSlot != null){
                        Integer reelCount = slotReelCountMap.get(bindSlot);
                        if(reelCount == null){
                            reelCount = 0;
                        }
                        reelCount = reelCount + 1;
                        slotReelCountMap.put(bindSlot, reelCount);
                    }
                }
                List<String> canRobSlotList = new ArrayList<>();
                for (StoragePos storagePos : posList) {
                    //其他工位绑定至少两盘才可以抢
                    Barcode barcode = storagePos.getBarcode();
                    String bindSlot = barcode.getAppendInfo().getBindSlot();
                    if(bindSlot != null){
                        Integer reelCount = slotReelCountMap.get(bindSlot);
                        if(reelCount >= 2){
                            canRobSlotList.add(bindSlot);
                        }
                    }
                }

                //抢最大的一盘
                StoragePos robPos = null;
                for (StoragePos storagePos : posList) {
                    //其他工位绑定至少两盘才可以抢
                    Barcode barcode = storagePos.getBarcode();
                    String bindSlot = barcode.getAppendInfo().getBindSlot();
                    if(bindSlot != null && canRobSlotList.contains(bindSlot)){
                        if(!excludeBarcodeList.contains(barcode.getBarcode())){
                            //这盘料可以抢
                            if(robPos == null || robPos.getBarcode().getAmount() < barcode.getAmount()){
                                robPos = storagePos;
                            }
                        }
                    }
                }

                if(robPos != null){
                    Barcode barcode = robPos.getBarcode();
                    AppendInfo appendInfo = barcode.getAppendInfo();
                    String hSerial = outItem.gethSerial();
                    int reelQty = barcode.getAmount();
                    String oldSlot = appendInfo.getBindSlot();
                    log.info("首盘需求单["+outItem.getSourceName()+"]缺料,从站位["+oldSlot+"]绑定料盘中抢夺料盘"+barcode.getBarcode()+"["+reelQty+"]进行绑定");
                    OutItem oldItem = outItemDao.findItem(hSerial,Integer.valueOf(oldSlot));
                    if(oldItem != null){
                        //不是预绑定的物料
                        oldItem.setRealLockQty(oldItem.getRealLockQty() - reelQty);
                        outItemDao.updateLockQty(oldItem.getId(), 0, oldItem.getRealLockQty());
                        soseqCache.updateTotalRealLockQty(oldItem, oldItem.getRealLockQty());
                    }

                    appendInfo.setBindSlot(outItem.getSlotlocation() + "");
                    barcode.setAppendInfo(appendInfo);
                    barcodeDao.save(barcode);
                    robPos.setBarcode(barcode);
                    storagePosDao.save(robPos);
                    outItem.setRealLockQty(outItem.getRealLockQty() + reelQty);

                    outItemDao.updateLockQty(outItem.getId(), 0, outItem.getRealLockQty());
                    soseqCache.updateTotalRealLockQty(outItem, outItem.getRealLockQty());
                }

            }
        }
    }


}