/**
   * 功能:预处理数据 作者:宋涛 创建时间:(2001-10-25 16:00:59) 参数:<|> 返回值: 算法:
   *
   * @return nc.vo.pub.rs.MemoryResultSet[]
   */
  private MemoryResultSet[] prepareData(
      MemoryResultSet[] mrs,
      QueryStructVO voQryStruct,
      boolean bHasSum,
      String[][] strObj,
      String[][] strDisplay)
      throws java.sql.SQLException {

    MemoryResultSet[] mrsResults = new MemoryResultSet[2];

    /** 需要计算的数字字段 */
    String[] sValueFlds = getJeFlds(voQryStruct.getIfx());

    //	String[] sObjFlds = strObj[1];
    //	if (!ArrayUtils.isEmpty(strObj[5])) {
    //		for (String column : strObj[5]) {
    //			sObjFlds = (String[]) ArrayUtils.add(sObjFlds, column);
    //		}
    //	}

    /** 查询对象字段 */
    String sObjKey = EfficientPubMethod_NEW.array2string(strObj[1], IEfficientPubMethod.COMMA);
    String sDisplay = EfficientPubMethod_NEW.array2string(strDisplay[1], IEfficientPubMethod.COMMA);
    /** 期初 */
    long qichu = System.currentTimeMillis();
    mrsResults[0] = ComTool.sumResultSet(mrs[0], mrs[1], strObj[1], sValueFlds, JOIN_MODE);
    mrs[0] = null;
    mrs[1] = null;
    Log.getInstance(this.getClass()).debug("####预处理期初处理时间:" + (System.currentTimeMillis() - qichu));

    long benqi = System.currentTimeMillis();
    ResultSumTool sumTool = new ResultSumTool();
    sumTool.setSourceResultSet(mrs[2]);
    mrs[2] = null;
    sumTool.setSumKey(PubMethodVO.getToken(sObjKey + ",zy,clbh," + sDisplay, ","));
    sumTool.setSumValueKey(sValueFlds);
    sumTool.setJoinMode(JOIN_MODE);
    int LJ = 10;
    if (voQryStruct.isNew()) {
      LJ = 3;
    }
    for (int i = 3; i <= LJ; i++) {
      sumTool.addSumResultSet(mrs[i]);
      mrs[i] = null;
    }
    /** 本期 */
    mrsResults[1] = sumTool.execute();
    sumTool = null;
    Log.getInstance(this.getClass()).debug("####预处理本期处理时间:" + (System.currentTimeMillis() - benqi));

    // ComTool.printResultSet(mrsResults[1]);
    long bennianleiji = System.currentTimeMillis();
    if (bHasSum) {
      String[] arryLjValueFlds = getLjValueFlds();
      sumTool = new ResultSumTool();
      sumTool.setSourceResultSet(mrs[mrs.length - 1]);
      mrs[mrs.length - 1] = null;
      sumTool.setSumKey(strObj[1]);
      sumTool.setSumValueKey(arryLjValueFlds);
      sumTool.setJoinMode(JOIN_MODE);
      for (int i = LJ + 1; i < mrs.length - 1; i++) {
        sumTool.addSumResultSet(mrs[i]);
        mrs[i] = null;
      }
      MemoryResultSet mrsLj = sumTool.execute();
      // ComTool.printResultSet(mrsLj);
      /** 连接本年累计和期初 */
      ResultJoinTool rjt = new ResultJoinTool();
      rjt.setSourceResultSet(mrsResults[0]);
      rjt.addJoinResultSet(mrsLj, strObj[1], arryLjValueFlds);
      mrsResults[0] = rjt.execute();
      // ComTool.printResultSet(mrsResults[0]);
      /** 连接本年累计和本期 */
      rjt = new ResultJoinTool();
      rjt.setSourceResultSet(mrsResults[1]);
      rjt.addJoinResultSet(mrsLj, strObj[1], arryLjValueFlds);
      mrsResults[1] = rjt.execute();
      // ComTool.printResultSet(mrsResults[1]);
    }
    sumTool = null;
    Log.getInstance(this.getClass())
        .debug("####预处理本年累计处理时间:" + (System.currentTimeMillis() - bennianleiji));
    return mrsResults;
  }
  /**
   * 通过查询对象、条件和显示字段构造查询语句,返回符合条件的查询结果。
   *
   * <p>创建日期:(2001-5-15)
   *
   * @return StatValueObject[] 查到的VO对象数组
   * @param QueryStructVO voQryStruct 查询结构
   * @exception BusinessException 异常说明。
   */
  public RSTransferVO getQueryResult(QueryStructVO voQryStruct) throws BusinessException {
    try {

      RSTransferVO voResult = new RSTransferVO();
      MemoryResultSet[] mrsResults = null;

      // add by wdg(2007-10-18)
      // 期初暂估冲回数据
      MemoryResultSet[] tempEstimateInitMrsResults = null;
      // 暂估发生数据
      MemoryResultSet[] tempEstimateOccurMrsResults = null;
      for (int i = 0; i < voQryStruct.getVetQryObj().size(); i++) {
        if (nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
            .getStrByID("2006", "UPTcommon-0100172") /*@res "客商"*/
            .equals(voQryStruct.getVetQryObj().get(i).getM_strDisplayName())) {
          voQryStruct.getVetQryObj().get(i).setPk_bdinfo("00010000000000000073");
        }
      }
      if (voQryStruct.getSecsrc() != null) {
        InvocationInfoProxy.getInstance().setUserDataSource(voQryStruct.getSecsrc());
      }
      if (true) {
        ListQueryMetadata queryMetadata =
            new ListQueryMetadata(QueryMetadataUtil.convertQryStruct2Metadata(voQryStruct));
        DateRange dateRange = new DateRange();
        dateRange.setStartDate(voQryStruct.getSumDate()[0]);
        dateRange.setEndDate(voQryStruct.getSumDate()[1]);
        queryMetadata.getConfig().setSumDateRange(dateRange);
        queryMetadata.getConfig().setMonthQuery(voQryStruct.isQuerybyPeriod());
        if (voQryStruct.getSecsrc() == null) {
          BalQryObjLogUtil.log(
              queryMetadata.getQueryObjects(),
              queryMetadata.getConfig().getSystem() == SysEnum.YS ? "20060504" : "20080504");
        }

        voResult = new ListQueryBO().query(queryMetadata);
        mrsResults = voResult.getResultset();
      } else {

        /** 插入单据信息,这是明细查询中的必查项目 */
        Vector<QryObjVO> vetDisplay = voQryStruct.getVetDisplay();
        vetDisplay = insertDjxx(vetDisplay);
        voQryStruct.setVetDisplay(vetDisplay);
        Vector vetObj = voQryStruct.getVetQryObj();
        vetObj = PubMethodVO.mergeVector(vetObj, voQryStruct.getVetGroupBy());
        voQryStruct.setVetQryObj(vetObj);
        voQryStruct.setVetGroupBy(new Vector());
        ICreateSql createTools = getSqlCreater();
        if (voQryStruct.isWanglaiDetail()) {
          createTools = new CreateSql_WL();
        }
        /*设置需要的查询语句*/
        int[] iPos;
        boolean[] bSum;
        if (!voQryStruct.isQuerybyPeriod()) {
          /*日期模式*/
          iPos =
              new int[] {
                0, 1, 2, 3, /*4, 5, 6, 7, 8, 9, 10*/
              };
          bSum =
              new boolean[] {
                false, false, false, false, /* false, false, false, false, false, false, false */
              };
        } else {
          /*期间模式*/
          iPos =
              new int[] {
                0, 1, 2, 3, /* 4, 5, 6, 7, 8, 9, 10, */ 2, 3, /*4, 5, 6, 7, 8, 9, 10*/
              };
          bSum =
              new boolean[] {
                false,
                false,
                false,
                false,
                /*
                false,
                false,
                false,
                false,
                false,
                false,
                false,*/
                true,
                true,
                /*true,
                true,
                true,
                true,
                true,
                true,
                true */ };
        }
        /*-----设置完毕----*/

        NewListQueryDAO dmo = new NewListQueryDAO();
        //        Debug.println("调用dmo类,进行查询");

        // zhongyue_begin for  多单位权限控制 begin
        nc.bs.arap.pub.CreatJoinSQLTool jointool = new nc.bs.arap.pub.CreatJoinSQLTool();
        if (voQryStruct.getSecsrc() != null) {
          InvocationInfoProxy.getInstance().setUserDataSource(voQryStruct.getSecsrc());
        }
        if (jointool.checkLegal(voQryStruct.getCorp())) {
          createTools.setQueryIdx(iPos, bSum);
          createTools.setDateType(voQryStruct);
          createTools.initParam(voQryStruct);
          String[] strSqls = createTools.getSQL();
          mrsResults = dmo.getQueryResult(strSqls);
        } else {
          String[] sPk_corps = voQryStruct.getCorp();
          if (sPk_corps != null) {
            for (int i = 0; i < sPk_corps.length; i++) {
              String[] tables = new PowerCtrlVO(sPk_corps[i]).getTables();
              if (voQryStruct.getPowerCtrlVO() != null) {
                voQryStruct.getPowerCtrlVO().setTables(tables);
              }
              voQryStruct.setCorp(new String[] {sPk_corps[i]});
              if (voQryStruct.getPowerCtrlVO() != null) {
                voQryStruct.getPowerCtrlVO().setPk_corp(new String[] {sPk_corps[i]});
              }
              createTools = getSqlCreater();
              if (voQryStruct.isWanglaiDetail()) {
                createTools = new CreateSql_WL();
              }
              createTools.setQueryIdx(iPos, bSum);
              createTools.setDateType(voQryStruct);
              createTools.initParam(voQryStruct);

              String[] strSqls = createTools.getSQL();
              mrsResults = mergeMultiCorps(mrsResults, dmo.getQueryResult(strSqls));
            }
          }
        }
      }
      String[] sPk_corps = voQryStruct.getCorp();
      if (sPk_corps != null) {
        for (int i = 0; i < sPk_corps.length; i++) {
          // 期初冲回数据要考虑数据权限,否则查出的无权限数据加入原期初结果集会造成数据增多
          if (!voQryStruct.isBlnClearEstimateDataShow()) {
            tempEstimateInitMrsResults =
                mergeMultiCorps(
                    tempEstimateInitMrsResults, getInitTempEstimateOffsetData(voQryStruct));
            tempEstimateOccurMrsResults =
                mergeMultiCorps(tempEstimateOccurMrsResults, getTempEstimateData(voQryStruct));
          }
        }
      }

      // end
      if (voQryStruct.isVouchshow()) {
        PubMethodVO.insertVouchidCol(mrsResults);
      }
      VOCompress.objectReference(mrsResults);
      voResult.setResultset(mrsResults);

      /*删除控制数据权限建立的临时表*/
      // new PubDMO().onDropTemTab(createTools.getPowerHashtable());

      // 如果查询条件为“已两清的暂估单据不显示”(add by wdg:2007-10-11)
      if (!voQryStruct.isBlnClearEstimateDataShow()) {
        mergeTempEstimateData(
            voQryStruct, voResult, tempEstimateInitMrsResults, tempEstimateOccurMrsResults);
      }

      if (!voQryStruct.isRecheck()
          && !(voQryStruct
                  .getStrBillType()
                  .equals(
                      NCLangRes4VoTransl.getNCLangRes()
                          .getStrByID("20060504", "UPP20060504-000228"))
              || voQryStruct
                  .getStrBillType()
                  .equals(
                      NCLangRes4VoTransl.getNCLangRes()
                          .getStrByID("20060504", "UPP20060504-000230")))) {
        Iterator<QryObjVO> iterator = voQryStruct.getVetQryObj().iterator();
        while (iterator.hasNext()) {
          QryObjVO qryObjVO = iterator.next();
          if ("bzbm".equals(qryObjVO.getQryfld())) {
            iterator.remove();
          }
        }
      }

      voResult.setObjs(getJoinTools().getQryObjs(voQryStruct.getVetQryObj()));
      voResult.setDisplays(getJoinTools().getQryObjs(voQryStruct.getVetDisplay()));
      MemoryResultSet sumRs = null;
      MemoryResultSet[] mrs = voResult.getResultset();
      if (isLastPageIndex(voQryStruct, voResult)) {
        sumRs = voResult.getResultset()[voResult.getResultset().length - 1];
        mrs =
            (MemoryResultSet[])
                ArrayUtils.subarray(voResult.getResultset(), 0, voResult.getResultset().length - 1);
      }
      MemoryResultSet[] mrsSource =
          prepareData(
              mrs,
              voQryStruct,
              voQryStruct.isQuerybyPeriod(),
              voResult.getObjs(),
              voResult.getDisplays());
      if (mrsSource != null) {
        int total = 0;
        for (MemoryResultSet ms : mrsSource) {
          if (ms != null && ms.getResultArrayList() != null)
            total += ms.getResultArrayList().size();
        }
        if (total > 20000) {
          throw new BusinessException("记录超过两万条,请分页查或缩小查询范围");
        }
      }
      voResult.setResultset(mrsSource);
      if (sumRs != null) {
        voResult.setResultset((MemoryResultSet[]) ArrayUtils.add(mrsSource, sumRs));
      }
      PubMethodVO.createCodeAndName(voResult.getObjs());
      PubMethodVO.insertCodeAndNameColumns(voResult.getResultset(), voResult.getObjs());
      return voResult;
    } catch (Exception e) {
      Log.getInstance(this.getClass()).error(e.getMessage());
      throw new BusinessShowException(e.getMessage());
    }
  }