/**
  * 添加转移联单收货标签
  *
  * @param Buldbarcode
  * @return 0
  */
 @Override
 public int saveBuldbarcode(
     IFWUserInfo userInfo, Buldbarcode buldbarcode, SerialnumInfo serialnumInfo) {
   buldbarcode.setCreatedate(PublicMethod.now());
   buldbarcode.setCreateempID(userInfo.getUserID());
   buldbarcode.setModifydate(PublicMethod.now());
   buldbarcode.setModifyempID(userInfo.getUserID());
   serialnumBusiness.updateSerialnum(userInfo, serialnumInfo);
   return buldbarcodeMapper.insert(buldbarcode);
 }
 /**
  * 添加派车单主表
  *
  * @param Tpactionhead
  * @return 0
  */
 @Override
 public void saveTpactionhead(
     IFWUserInfo userInfo,
     Tpactionhead tpactionhead,
     Tpactionline tpactionline,
     SerialnumInfo serialnumInfo) {
   if (tpactionhead != null) {
     if (serialnumInfo != null) {
       serialnumBusiness.updateSerialnum(userInfo, serialnumInfo);
     }
     tpactionhead.setMcreatedate(PublicMethod.now());
     tpactionhead.setMactdate(PublicMethod.now());
     tpactionhead.setMcreateempID(userInfo.getUserID());
     tpactionhead.setMmodifydate(PublicMethod.now());
     tpactionhead.setMmodifyempID(userInfo.getUserID());
     tpactionhead.setMdtype(HConstants.P);
     tpactionheadMapper.insert(tpactionhead);
   }
   if (tpactionline != null) {
     String macttypeID = tpactionline.getMacttypeID();
     Tptrplanline trplanline =
         tptrplanlineBusiness.getTptrplanlineByID(userInfo, tpactionline.getMsguid());
     double prplanqty = 0.00;
     if (trplanline != null) {
       prplanqty = trplanline.getMexecqty();
     }
     if (HConstants.PLAN.equals(macttypeID)) {
       Tptrplanline updateLine = new Tptrplanline();
       updateLine.setMexecqty(tpactionline.getMplanqty() + prplanqty);
       updateLine.setMguid(tpactionline.getMsguid());
       updateLine.setMexecstate(HConstants.ONE);
       tptrplanlineBusiness.updateTptrplaneStatus(userInfo, updateLine);
     }
     tpactionline.setMdtype(HConstants.P);
     tpactionline.setMcreatedate(PublicMethod.now());
     tpactionline.setMcreateempID(userInfo.getUserID());
     tpactionline.setMmodifydate(PublicMethod.now());
     tpactionline.setMmodifyempID(userInfo.getUserID());
     tpactionlineMapper.insert(tpactionline);
   }
 }
  /** 刚添加派车单执行(和经营协议一样) 目前操作4张表:标签表、库存单据主表、库存单据子表、累计库存表 需判断许可证 */
  public void saveSWMBuldbarcodeYLC(
      String type,
      String uuid,
      String bartype,
      IFWUserInfo userInfo,
      Buldbarcode buldbarcode,
      String headguid,
      String lineguid,
      String matID,
      double bqty,
      String whID) {
    try {
      SerialnumInfo serialnumInfo = new SerialnumInfo();
      buldbarcode.setPcount(0);
      buldbarcode.setGuid(uuid);
      serialnumInfo = serialnumBusiness.getSerialnum(userInfo, JConstants.B);
      buldbarcode.setBatchno(PublicMethod.formatSerialnum(serialnumInfo)); // 后台生成
      buldbarcode.setBartype(bartype);
      saveBuldbarcode(userInfo, buldbarcode, serialnumInfo); // 保存标签

      String batchno = buldbarcode.getBatchno();
      SAOrderHead saorderHead = saorderBusiness.queryMSWMSAOrderHeadById(userInfo, headguid);
      /*转移联单:
      转移联单/经营协议(标签)入库时,需根据许可证信息进行校验:
      1.转移联单/经营协议子表中的八位码是否在有效许可证的范围内
      */
      BaseMatEntity baseMatEntity = baseMatBusiness.queryMSWMBaseMatById(userInfo, matID);
      if (JConstants.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 = new String();
        if (JConstants.JAVAANDROID.equals(type)) {
          // 业务类型(经营协议入库(扫码))
          bustype = JConstants.INV13;
        } else if (JConstants.JAVAWEB.equals(type)) {
          // 业务类型(经营协议入库)
          bustype = JConstants.INV12;
        }
        // 关联单据号
        String relcode = saorderHead.getHetongcode();
        // 关联单据类型
        String reltype = JConstants.SA01;
        invtkHead.setBustype(bustype);
        invtkHead.setRelcode(relcode);
        invtkHead.setReltype(reltype);
        invtkHead.setTktype(JConstants.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(JConstants.I);
        //				invcurTune.setBusinessType(JConstants.INV13);
        invcurTune.setBusinessType(bustype);
        invtkLines.add(invtkLine);
        InvcurBusiness.changeInvcur(userInfo, invcurTune, invtkHead, invtkLines);
        // 1插入库存之后修改转移联单標籤表中:是否入库,入库日期,
        Buldbarcode buldbarcode12 = new Buldbarcode();
        buldbarcode12.setIsinv(JConstants.ONE);
        buldbarcode12.setGuid(buldbarcode.getGuid());
        buldbarcode12.setInvdate(PublicMethod.now());
        updateBuldbarcode(userInfo, buldbarcode12);
        // 经营协议入库后,更新许可证中的已接收数量

        //				Double counts13 = invtkLineBusiness.getSumQtyBySguidAndBusType(userInfo,
        //						lineguid, JConstants.INV13);
        Double counts13 = invtkLineBusiness.getSumQtyBySguidAndBusType(userInfo, lineguid, bustype);
        if (counts13 == null) {
          counts13 = 0.00;
        }
        saorderBusiness.updateRecqty(userInfo, lineguid, bqty);
        baseLicBusiness.updateRecQtyByMat(
            userInfo, matID, saorderLine.getDealtypeID(), bqty, new Date());
      }
    } catch (Exception e) {
      e.printStackTrace();
      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());
    }
  }