/**
  * 删除标签(转移联单标签)
  *
  * @param IDList
  */
 @Override
 public void delSWMBuldbarcodeInfo(IFWUserInfo userInfo, String[] IDList) {
   try {
     // 只能是未入库的才能删除
     // 如果标签类型是LD01的个数为0的话,修改转移联单字表中的setIsbarcode为0
     for (int i = 0; i < IDList.length; i++) {
       Buldbarcode buldbarcode = getBuldbarcodeByID(userInfo, IDList[i]);
       String isinv = buldbarcode.getIsinv();
       if (HConstants.ONE.equals(isinv)) {
         throw new BusinessException("[当标签的状态为已入库时,不可以删除此标签");
       } else if (HConstants.ZERO.equals(isinv)) {
         String bartype = buldbarcode.getBartype();
         if (HConstants.LD01.equals(bartype)) {
           throw new BusinessException("[当标签的类型为转移联单时,不可以删除此标签");
         } else {
           removeBuldbarcode(userInfo, IDList[i]);
         }
       }
     }
   } catch (Exception e) {
     throw new BusinessException(e.getMessage());
   }
 }
  /**
   * 添加(转移联单,经营协议,手动添加数据)收货标签
   *
   * @param type
   * @param uuid
   * @param bartype
   * @param userInfo
   * @param buldbarcode
   * @param headguid
   * @param lineguid
   * @param matID
   * @param bqty
   * @param whID
   */
  public void saveSWMBuldbarcode(
      String type,
      String uuid,
      String bartype,
      IFWUserInfo userInfo,
      Buldbarcode buldbarcode,
      String headguid,
      String lineguid,
      String matID,
      double bqty,
      String whID) {
    try {
      if (HConstants.JAVAWEB.equals(type)) {
        SerialnumInfo serialnumInfo = new SerialnumInfo();
        buldbarcode.setPcount(0);
        buldbarcode.setGuid(uuid);
        serialnumInfo = serialnumBusiness.getSerialnum(userInfo, HConstants.B);
        buldbarcode.setBatchno(PublicMethod.formatSerialnum(serialnumInfo)); // 后台生成
        buldbarcode.setBartype(bartype);
        saveBuldbarcode(userInfo, buldbarcode, serialnumInfo);
        if (HConstants.LD01.equals(bartype)) {
          buliandanlineBusiness.updateBuliandanlineISbarcode(userInfo, lineguid);
        }
      }
      String batchno = buldbarcode.getBatchno();
      Buliandanhead buliandanhead = buliandanheadBusiness.getBuliandanheadByID(userInfo, headguid);
      SAOrderHead saorderHead = saorderBusiness.queryMSWMSAOrderHeadById(userInfo, headguid);
      /*转移联单:
      转移联单/经营协议(标签)入库时,需根据许可证信息进行校验:
      1.转移联单/经营协议子表中的八位码是否在有效许可证的范围内
      */

      BaseMatEntity baseMatEntity = baseMatBusiness.queryMSWMBaseMatById(userInfo, matID);
      // 只有状态为LD01才做此操作
      if (HConstants.LD01.equals(bartype)) {
        Buliandanline buliandanline =
            buliandanlineBusiness.getBuliandanlineByID(userInfo, lineguid);
        LicenceWrapper licenceWrapper =
            baseLicBusiness.getValidLicenceByMat(
                userInfo, matID, buliandanline.getDealtypeID(), new Date());
        if (licenceWrapper == null) {
          throw new BusinessException(
              "转移联单子表中的"
                  + baseMatEntity.getMatcode()
                  + " "
                  + baseMatEntity.getMatname()
                  + "不在有效许可证的范围内!");
        }

        // 3.生成一笔入库单据,单据类型为 “INV09 转移入库”并更新库存结存表
        /* 库存单据主表start */
        InvtkHead invtkHead = new InvtkHead();
        // 业务类型
        String bustype = HConstants.INV09;
        // 关联单据号
        String relcode = buliandanhead.getLiandancode();
        // 关联单据类型
        String reltype = HConstants.LD01;
        invtkHead.setBustype(bustype);
        invtkHead.setRelcode(relcode);
        invtkHead.setReltype(reltype);
        invtkHead.setTktype(HConstants.I);
        /* 库存单据主表end */
        /* 库存单据子表start */
        List<InvtkLine> invtkLines = new ArrayList<InvtkLine>();
        InvtkLine invtkLine = new InvtkLine();
        // 来源表guid
        invtkLine.setSguid(lineguid);
        invtkLine.setBustype(bustype);
        // 仓库
        invtkLine.setWhID(whID);
        // 物品id
        invtkLine.setMatID(matID);
        // 数量
        String bqty1 = String.valueOf(bqty);
        invtkLine.setQty(bqty1);
        // 单位
        invtkLine.setUnitID(buliandanline.getUnitID());
        invtkLine.setBatchno(batchno);
        // 关联单据号
        invtkLine.setRelcode(relcode);
        // 关联单据类型
        invtkLine.setReltype(reltype);
        // 联系单位id
        String cusID = buliandanhead.getCusID();
        invtkLine.setCusID(cusID);
        /* 库存单据子表end */
        InvcurTune invcurTune = new InvcurTune();
        invcurTune.setIo(HConstants.I);
        invcurTune.setBusinessType(HConstants.INV09);
        invtkLines.add(invtkLine);
        InvcurBusiness.changeInvcur(userInfo, invcurTune, invtkHead, invtkLines);
        // 1插入库存之后修改转移联单標籤表中:是否入库,入库日期,
        Buldbarcode buldbarcode12 = new Buldbarcode();
        buldbarcode12.setIsinv(HConstants.ONE);
        buldbarcode12.setGuid(buldbarcode.getGuid());
        buldbarcode12.setInvdate(PublicMethod.now());
        updateBuldbarcode(userInfo, buldbarcode12);
        // 2通過聯單子表的guid查看聯單的簽收量,在原來的基礎上加加。。。。。。
        String buliandanlineguid = buliandanline.getGuid();
        Buliandanline buliandanline3 =
            buliandanlineBusiness.getBuliandanlineByID(userInfo, buliandanlineguid);
        double recqty = buliandanline3.getRecqty();
        Buliandanline buliandanline2 = new Buliandanline();
        buliandanline2.setRecqty(bqty + recqty);
        buliandanline2.setGuid(buliandanlineguid);
        // buliandanline2.setSaorderlineID(buliandanline.getSaorderlineID());
        buliandanlineBusiness.updateBuliandanline(userInfo, buliandanline2, null);
        // 通过查看(全部联单子表中的簽收量大於0時)修改主表中的状态(已入库)///////////
        Buliandanline chren = new Buliandanline();
        chren.setHeadguid(headguid);
        // 通過轉移聯單主表guid查看轉移聯單子表中的數量
        int countline = buliandanlineBusiness.buliandanlinecountByheadguid(userInfo, chren);
        // 通過轉移聯單主表guid查看全部轉移聯單子表循環取出簽收量,查看簽收量全部大於0的個數
        List<Buliandanline> linelist =
            buliandanlineBusiness.findBuliandanlineByHeadguid(userInfo, chren);
        int countsize = 0;
        for (int i = 0; i < linelist.size(); i++) {
          double countRecqty = linelist.get(i).getRecqty();
          if (countRecqty > 0) {
            countsize++;
          }
        }
        // 比較
        if (countline == countsize) {
          Buliandanhead head = new Buliandanhead();
          head.setGuid(headguid);
          // 已入库
          head.setLiandanstate(HConstants.REC);
          buliandanheadBusiness.updateBuliandanheadStatus(userInfo, head);
        }
        buliandanlineBusiness.updateBuliandanlineISbarcode(userInfo, lineguid);

        saorderBusiness.updateRecqty(
            userInfo,
            buliandanlineBusiness.getBuliandanlineByID(userInfo, lineguid).getSaorderlineID(),
            bqty);
        baseLicBusiness.updateRecQtyByMat(
            userInfo, matID, buliandanline3.getDealtypeID(), bqty, new Date());
      } else if (HConstants.SA01.equals(bartype)) {
        SAOrderLine saorderLine = saorderBusiness.queryMSWMSAOrderLineById(userInfo, lineguid);
        LicenceWrapper licenceWrapper =
            baseLicBusiness.getValidLicenceByMat(
                userInfo, matID, saorderLine.getDealtypeID(), new Date());
        if (licenceWrapper == null) {
          throw new BusinessException(
              "经营协议子表中的"
                  + baseMatEntity.getMatcode()
                  + " "
                  + baseMatEntity.getMatname()
                  + "不在有效许可证的范围内!");
        }

        // 3.生成一笔入库单据,单据类型为 “INV13 转移入库”并更新库存结存表
        /* 库存单据主表start */
        InvtkHead invtkHead = new InvtkHead();
        // 业务类型(经营协议入库(扫码))
        String bustype = HConstants.INV13;
        // 关联单据号
        String relcode = saorderHead.getHetongcode();
        // 关联单据类型
        String reltype = HConstants.SA01;
        invtkHead.setBustype(bustype);
        invtkHead.setRelcode(relcode);
        invtkHead.setReltype(reltype);
        invtkHead.setTktype(HConstants.I);
        /* 库存单据主表end */
        /* 库存单据子表start */
        List<InvtkLine> invtkLines = new ArrayList<InvtkLine>();
        InvtkLine invtkLine = new InvtkLine();
        // 来源表guid
        invtkLine.setSguid(lineguid);
        invtkLine.setBustype(bustype);
        // 仓库
        invtkLine.setWhID(whID);
        // 物品id
        invtkLine.setMatID(matID);
        // 数量
        String bqty1 = String.valueOf(bqty);
        invtkLine.setQty(bqty1);
        // 单位
        invtkLine.setUnitID(saorderLine.getUnitID());
        invtkLine.setBatchno(batchno);
        // 关联单据号
        invtkLine.setRelcode(relcode);
        // 关联单据类型
        invtkLine.setReltype(reltype);
        // 联系单位id
        String cusID = saorderHead.getCusID();
        invtkLine.setCusID(cusID);
        /* 库存单据子表end */
        InvcurTune invcurTune = new InvcurTune();
        invcurTune.setIo(HConstants.I);
        invcurTune.setBusinessType(HConstants.INV13);
        invtkLines.add(invtkLine);
        InvcurBusiness.changeInvcur(userInfo, invcurTune, invtkHead, invtkLines);
        // 1插入库存之后修改转移联单標籤表中:是否入库,入库日期,
        Buldbarcode buldbarcode12 = new Buldbarcode();
        buldbarcode12.setIsinv(HConstants.ONE);
        buldbarcode12.setGuid(buldbarcode.getGuid());
        buldbarcode12.setInvdate(PublicMethod.now());
        updateBuldbarcode(userInfo, buldbarcode12);
        // 经营协议入库后,更新许可证中的已接收数量

        Double counts13 =
            invtkLineBusiness.getSumQtyBySguidAndBusType(userInfo, lineguid, HConstants.INV13);
        if (counts13 == null) {
          counts13 = 0.00;
        }
        saorderBusiness.updateRecqty(userInfo, lineguid, bqty);
        baseLicBusiness.updateRecQtyByMat(
            userInfo, matID, saorderLine.getDealtypeID(), bqty, new Date());
      } else if (HConstants.OT00.equals(bartype)) {
        // 3.生成一笔入库单据,单据类型为 “INV14手工添加数据入库”
        /* 库存单据主表start */
        InvtkHead invtkHead = new InvtkHead();
        // 业务类型(手工添加数据)
        String bustype = HConstants.INV14;
        // 关联单据号
        String relcode = buldbarcode.getReltkcode();
        // 关联单据类型(手动添加数据)
        String reltype = HConstants.OT00;
        invtkHead.setBustype(bustype);
        invtkHead.setRelcode(relcode);
        invtkHead.setReltype(reltype);
        invtkHead.setTktype(HConstants.I);
        /* 库存单据主表end */
        /* 库存单据子表start */
        List<InvtkLine> invtkLines = new ArrayList<InvtkLine>();
        InvtkLine invtkLine = new InvtkLine();
        // 来源表guid
        invtkLine.setSguid(uuid);
        invtkLine.setBustype(bustype);
        // 仓库
        invtkLine.setWhID(whID);
        // 物品id
        invtkLine.setMatID(matID);
        // 数量
        String bqty1 = String.valueOf(bqty);
        invtkLine.setQty(bqty1);
        // 单位
        BaseMatEntity baseEmp = baseMatService.queryMSWMBaseMatById(userInfo, matID);
        invtkLine.setUnitID(baseEmp.getUnit());
        invtkLine.setBatchno(batchno);
        // 关联单据号
        invtkLine.setRelcode(relcode);
        // 关联单据类型
        invtkLine.setReltype(reltype);
        // 联系单位id
        // invtkLine.setCusID(buldbarcode.getBasecustomercusID());
        invtkLine.setCusID(buldbarcode.getReltkinfo());
        /* 库存单据子表end */
        InvcurTune invcurTune = new InvcurTune();
        invcurTune.setIo(HConstants.I);
        invcurTune.setBusinessType(HConstants.INV14);
        invtkLines.add(invtkLine);
        InvcurBusiness.changeInvcur(userInfo, invcurTune, invtkHead, invtkLines);
        // 1插入库存之后修改转移联单標籤表中:是否入库,入库日期,
        Buldbarcode buldbarcode12 = new Buldbarcode();
        buldbarcode12.setIsinv(HConstants.ONE);
        buldbarcode12.setGuid(buldbarcode.getGuid());
        buldbarcode12.setInvdate(PublicMethod.now());
        updateBuldbarcode(userInfo, buldbarcode12);
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    }
  }