/**
   * 多个黄单号对应-个主单号计算超重方法
   *
   * @param array
   */
  public void manyFlightToOneFax(List<FiDeliverycost> array, double totalFdWeight) {
    double diffWeight = DoubleUtil.sub(totalFdWeight, array.get(0).getFaxWeight());

    FiDeliverycost fiDeliverycost = array.get(0); // 多票匹配放在第一个黄单记录里面
    fiDeliverycost.setDiffWeight(diffWeight);
    fiDeliverycost.setDiffAmount(
        Math.ceil(DoubleUtil.mul(fiDeliverycost.getDiffWeight(), fiDeliverycost.getPrice())));
    save(fiDeliverycost);

    if (diffWeight > 0.0) {
      // 主单超重处理
      List<OprFaxIn> listMain =
          oprFaxInDao.find(
              "from OprFaxIn oi where oi.faxMainId=?  and oi.status=1   ",
              Long.parseLong(array.get(0).getFaxId()));
      if (listMain.size() > 0) {
        OprFaxIn oprFaxIn = listMain.get(0);
        CusOverweightManager cusMan = null;
        List<CusOverweightManager> cuslist =
            this.cusOverManagerService.findBy("cusId", oprFaxIn.getCusId());
        if (cuslist.size() == 0) {
          List<CusOverweightManager> listM =
              cusOverManagerService.find("from CusOverweightManager cs where cs.cusId is null ");
          if (listM.size() == 0) {
            throw new ServiceException("请在主单超重模块录入主单超重相关信息");
          } else {
            cusMan = listM.get(0);
          }
        } else {
          cusMan = cuslist.get(0);
        }

        if (diffWeight > cusMan.getLowWeight()) { // 写入主单超重表
          OprOverweight oprOverweight = new OprOverweight();
          oprOverweight.setCustomerId(oprFaxIn.getCusId());
          oprOverweight.setCustomerName(oprFaxIn.getCpName());
          oprOverweight.setWeight(diffWeight);
          oprOverweight.setFlightMainNo(array.get(0).getFaxMainNo());
          oprOverweight.setDepartId(array.get(0).getDepartId());
          oprOverweight.setDepartName(array.get(0).getDepartName());
          oprOverweight.setFaxWeight(array.get(0).getFaxWeight());
          oprOverweight.setFlightWeight(totalFdWeight);
          oprOverweight.setAmount(
              Math.ceil(DoubleUtil.mul(diffWeight, cusMan.getOverweightRate())));
          oprOverweight.setRate(cusMan.getOverweightRate());
          oprOverweight.setStatus(1l);
          cusOverWeightService.save(oprOverweight);
        }
      }
    }
  }
  /**
   * 一个黄单号对应多个主单号计算超重方法
   *
   * @param array
   */
  public void oneFlightToManyFax(List<FiDeliverycost> array) {
    FiDeliverycost fiDeliverycost = array.get(0);
    double diffWeight =
        DoubleUtil.sub(fiDeliverycost.getFlightWeight(), fiDeliverycost.getFaxWeight());
    if (diffWeight > 0.0) {
      // 主单超重处理   这里计了多次,在超重处理时,必须得判断一下
      List<Long> list = getPksByIds(fiDeliverycost.getFaxId());
      for (int j = 0; j < list.size(); j++) {
        List<OprFaxIn> listMain =
            oprFaxInDao.find(
                "from OprFaxIn oi where oi.faxMainId=?  and oi.status=1  ", list.get(j));
        OprFaxIn oprFaxIn = listMain.get(0);

        CusOverweightManager cusMan = null;
        List<CusOverweightManager> cuslist =
            this.cusOverManagerService.findBy("cusId", oprFaxIn.getCusId());
        if (cuslist.size() == 0) {
          List<CusOverweightManager> listM =
              cusOverManagerService.find("from CusOverweightManager cs where cs.cusId is null ");
          if (listM.size() == 0) {
            throw new ServiceException("请在主单超重模块录入主单超重相关信息");
          } else {
            cusMan = listM.get(0);
          }
        } else {
          cusMan = cuslist.get(0);
        }

        if (diffWeight > cusMan.getLowWeight()) { // 写入主单超重表
          OprOverweight oprOverweight = new OprOverweight();
          oprOverweight.setCustomerId(oprFaxIn.getCusId());
          oprOverweight.setCustomerName(oprFaxIn.getCpName());
          oprOverweight.setWeight(diffWeight);
          oprOverweight.setFlightMainNo(array.get(0).getFaxMainNo());
          oprOverweight.setDepartId(array.get(0).getDepartId());
          oprOverweight.setDepartName(array.get(0).getDepartName());
          oprOverweight.setFaxWeight(array.get(0).getFaxWeight());
          oprOverweight.setFlightWeight(fiDeliverycost.getFlightWeight());
          oprOverweight.setAmount(
              Math.ceil(DoubleUtil.mul(diffWeight, cusMan.getOverweightRate())));
          oprOverweight.setRate(cusMan.getOverweightRate());
          oprOverweight.setStatus(1l);
          cusOverWeightService.save(oprOverweight);
        }
      }
    }
  }
  // 取消匹配
  @ModuleName(value = "提货成本管理取消匹配", logType = LogType.fi)
  public String qxAudit(List<FiDeliverycost> aa) throws Exception {
    List<Long> fiDelivAll = new ArrayList<Long>(); // 取消匹配的ID集合
    for (FiDeliverycost fd : aa) {
      fiDelivAll.add(fd.getId());
    }

    for (FiDeliverycost fiDeliv : aa) {
      FiDeliverycost fiDeliverycost = fiDeliverycostDao.get(fiDeliv.getId());
      fiDeliverycost.setTs(fiDeliv.getTs());

      if (fiDeliverycost.getStatus() == 1l) {
        throw new ServiceException("已审核数据不能进行取消匹配操作");
      }

      if (fiDeliverycost.getMatStatus() == null || fiDeliverycost.getMatStatus() == 0l) {
        throw new ServiceException("未匹配的数据不能进行取消匹配操作");
      }
      List<Long> list = getPksByIds(fiDeliverycost.getFaxId());
      for (Long faxIdLong : list) {
        OprFaxMain fiMain = oprFaxMainService.get(faxIdLong);
        fiMain.setMatchStatus(0l);
        oprFaxMainService.save(fiMain);

        // 超重删除
        List<OprOverweight> list2 =
            cusOverWeightService.find(
                "from OprOverweight oo where oo.departId=? and oo.flightMainNo=?  and oo.status=1 ",
                fiDeliverycost.getDepartId(),
                fiMain.getFlightMainNo());
        for (OprOverweight oprOverweight : list2) {
          oprOverweight.setStatus(0l);
          cusOverWeightService.save(oprOverweight);
        }
      }

      if (list.size() == 1) { // 保存黄单多对一的ID集合,用来判断是否一次全部取消匹配
        List<FiDeliverycost> lisd =
            find(
                "from FiDeliverycost  fd where fd.departId=? and fd.faxId=? and  fd.id<> ? ",
                fiDeliverycost.getDepartId(),
                fiDeliverycost.getFaxId(),
                fiDeliverycost.getId());
        List<Long> idsLongList = new ArrayList<Long>();
        for (FiDeliverycost fiDt2 : lisd) {
          idsLongList.add(fiDt2.getId());
        }
        if (!fiDelivAll.containsAll(idsLongList)) {
          throw new ServiceException("存在多条黄单记录匹配一条主单号的记录,请全部取消匹配");
        }
      }

      if (fiDeliverycost.getStatus() != null && fiDeliverycost.getStatus() == 1l) {
        throw new ServiceException("数据已审核,不能撤销手工匹配");
      }

      fiDeliverycost.setMatStatus(0l);
      fiDeliverycost.setFaxMainNo(null);
      fiDeliverycost.setFaxPiece(0l);
      fiDeliverycost.setFaxWeight(0.0);
      fiDeliverycost.setDiffAmount(0.0);
      fiDeliverycost.setDiffWeight(0.0);
      fiDeliverycost.setFaxId(null);
      fiDeliverycostDao.save(fiDeliverycost); // 撤销手工匹配
    }
    return null;
  }