/**
  * 通过ds来对应field
  *
  * @param ds
  * @return
  */
 private Map<String, Integer> getIndexMap(Dataset ds) {
   Map<String, Integer> indexMap = new HashMap<String, Integer>();
   int count = ds.getFieldSet().getFieldCount();
   for (int i = 0; i < count; i++) {
     Field field = ds.getFieldSet().getField(i);
     String key = field.getId();
     indexMap.put(key, i);
   }
   return indexMap;
 }
  public Dataset createDataset() {
    Dataset ds = new Dataset();
    ds.setId(Angular_Dataset_Name);
    ds.setLazyLoad(true);

    Field field = new Field();
    field.setId(Angular_Column_Type);
    field.setText("参数名称");
    ds.getFieldSet().addField(field);

    field = new Field();
    field.setId(Angular_Column_Start);
    field.setText("开始参数");
    ds.getFieldSet().addField(field);

    field = new Field();
    field.setId(Angular_Column_End);
    field.setText("结束参数");
    ds.getFieldSet().addField(field);

    field = new Field();
    field.setId(Angular_Column_Color);
    field.setText("颜色");
    ds.getFieldSet().addField(field);
    return ds;
  }
  private String aggregateSql(Dataset ds, FieldRelation rel) {
    if (ds.getCurrentKey() == null) return null;
    Dataset refDs = ds.getWidget().getViewModels().getDataset(rel.getRefDataset());
    if (refDs == null) {
      Logger.error("ref ds can not be fount:" + rel.getRefDataset());
      return null;
    }
    if (rel.getWhereField() == null) return null;
    MatchField[] mfs = rel.getMatchFields();
    WhereField where = rel.getWhereField();
    StringBuffer sql = new StringBuffer();
    sql.append("select ");
    for (int i = 0; i < mfs.length; i++) {
      Field f = refDs.getFieldSet().getField(mfs[i].getReadField());
      if (f == null)
        throw new LfwRuntimeException(
            "can not find field in ds:"
                + rel.getRefDataset()
                + ",field:"
                + mfs[i].getReadField()
                + ", in fieldrelation process");
      sql.append("a.").append(f.getField());
      if (i != mfs.length - 1) sql.append(",");
    }
    sql.append(",a.").append(refDs.getFieldSet().getField(where.getKey()).getField());
    sql.append(" from ");
    sql.append(getRefTableName(refDs));
    sql.append(" a where a.")
        .append(refDs.getFieldSet().getField(where.getKey()).getField())
        .append(" in (");

    RowData rowSet = ds.getCurrentRowSet().getCurrentRowData();
    Set<String> inSet = new HashSet<String>();
    if (rowSet != null) {
      Row[] rows = rowSet.getRows();
      int whereIndex = ds.nameToIndex(where.getValue());
      if (whereIndex == -1) return null;
      for (int i = 0; i < rows.length; i++) {
        if (rows[i] instanceof EmptyRow) continue;
        Object wValue = rows[i].getValue(whereIndex);
        String wValueStr = "";
        if (wValue != null) {
          wValueStr = wValue.toString();
          String[] strs = wValueStr.split(",");
          for (int j = 0; j < strs.length; j++) {
            inSet.add(strs[j]);
          }
        }
      }
    }

    if (inSet.size() == 0) return null;

    Iterator<String> it = inSet.iterator();
    while (it.hasNext()) {
      String value = it.next();
      sql.append("'");
      sql.append(value);
      sql.append("'");
      if (it.hasNext()) sql.append(",");
    }
    sql.append(")");
    return sql.toString();
  }
  private void processFormular(Dataset ds) {
    RowData rd = ds.getCurrentRowData();
    if (rd == null) return;
    int rcount = rd.getRowCount();
    List<String> executedFpList = new ArrayList<String>();
    int fieldCount = ds.getFieldSet().getFieldCount();
    for (int i = 0; i < fieldCount; i++) {
      try {
        Field field = ds.getFieldSet().getField(i);
        List<String> fieldFormulars = new ArrayList<String>();
        String formular = field.getEditFormular();
        if (formular != null) fieldFormulars.add(formular);

        formular = field.getLoadFormular();
        if (formular != null) fieldFormulars.add(formular);

        Iterator<String> fit = fieldFormulars.iterator();
        while (fit.hasNext()) {
          formular = fit.next();
          String exp = null;
          if (formular != null) exp = JsURLDecoder.decode(formular, "UTF-8");
          // 如果没有加载公式,继续下一个字段
          if (exp == null) continue;

          if (executedFpList.contains(exp)) continue;
          executedFpList.add(exp);
          String[] expArr = exp.split(";");

          fp.setExpressArray(expArr);
          VarryVO[] varryVOs = fp.getVarryArray();
          if (varryVOs == null || varryVOs.length == 0) continue;
          String[] formularNames = new String[varryVOs.length];

          Map<String, Integer> indexMap = getIndexMap(ds);
          for (int j = 0; j < varryVOs.length; j++) {
            String[] keys = varryVOs[j].getVarry();
            if (keys != null) {
              for (String key : keys) {
                List<Object> valueList = new ArrayList<Object>();
                for (int k = 0; k < rcount; k++) {
                  Row r = rd.getRow(k);
                  //
                  Object value = r.getValue(indexMap.get(key));
                  Field f = ds.getFieldSet().getField(key);
                  if (f != null && value != null) {
                    //										//如果是Double类型,进行一下转换
                    if (StringDataTypeConst.UFDOUBLE.equals(f.getDataType())
                        || StringDataTypeConst.DOUBLE.equals(f.getDataType())
                        || StringDataTypeConst.Decimal.equals(f.getDataType())) {
                      if (!(value instanceof UFDouble)) value = new UFDouble(value.toString());
                    } else if (StringDataTypeConst.INTEGER.equals(f.getDataType())) {
                      if (!(value instanceof Integer)) value = new Integer((String) value);
                    }
                  }
                  valueList.add(value);
                }
                fp.addVariable(key, valueList);
              }
              formularNames[j] = varryVOs[j].getFormulaName();
            }
          }

          Object[][] result = fp.getValueOArray();
          if (result != null) {
            for (int l = 0; l < formularNames.length; l++) {
              int index = ds.nameToIndex(formularNames[l]);
              if (index == -1) {
                LfwLogger.error(
                    "can not find column:" + formularNames[l] + ", ds id:" + ds.getId());
                continue;
              }
              for (int k = 0; k < rcount; k++) {
                Row r = rd.getRow(k);
                r.setValue(index, result[l][k]);
              }
            }
          }
        }
      } catch (Throwable e) {
        Logger.error(e.getMessage(), e);
      }
    }
  }