@Override
  public Map<String, Object> getDefaultFormData(String businessFormTemplateId) {
    Map<String, Object> formData = new HashMap<String, Object>();
    List<BusinessBaseTemplate> list =
        businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId);
    for (BusinessBaseTemplate baseTemplate : list) {
      List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId());
      String businessBaseId = baseTemplate.getBusinessBaseId();
      BusinessBase businessBase = businessBaseDao.getById(businessBaseId);
      String baseFormId = businessBase.getBaseFormId();
      BaseForm baseForm = baseFormDao.getById(baseFormId);
      // 主表单
      if (businessBase.getIsDetail() != null && businessBase.getIsDetail().intValue() == 0) {
        Map<String, Object> map = new HashMap<String, Object>();
        for (TemplateField templateField : fieldList) {
          FormField formField = formFieldDao.getById(templateField.getFieldId());
          String defaultValue = templateField.getDefaultValue();
          if (!StringUtil.isNullOrEmpty(defaultValue)) {
            if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_DATAFIELD) {
              try {
                Date v = DateUtils.parseDate(defaultValue, new String[] {"yyyy-MM-dd"});
                map.put(formField.getFieldCode().toLowerCase(), v);
              } catch (ParseException e) {
                log.error(
                    this.getClass().getSimpleName()
                        + " e==> getDefaultFormData  ,ParseException + businessFormTemplateId:["
                        + businessFormTemplateId
                        + "]",
                    e);
              }
            } else {
              map.put(formField.getFieldCode().toLowerCase(), defaultValue);
            }
            // 列表组件 需要设置rawText值
            if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_GRID
                || formField.getViewType().intValue() == FormMetaValue.FORMFIELD_TREE) {
              map.put(
                  formField.getFieldCode().toLowerCase() + "_rawtext",
                  formFieldService.getRawTextValue(defaultValue, formField));
            }
          }
        }
        formData.put(baseForm.getBaseFormCode().toLowerCase(), map);
      } else {

      }
    }
    return formData;
  }
 private int countFormData(
     BusinessBaseTemplate baseTemplate, Map<String, String> formData, String flowRequestId) {
   String businessBaseId = baseTemplate.getBusinessBaseId();
   BusinessBase businessBase = businessBaseDao.getById(businessBaseId);
   String baseFormId = businessBase.getBaseFormId();
   BaseForm baseForm = baseFormDao.getById(baseFormId);
   StringBuffer selectSql = new StringBuffer();
   Map<String, Object> selectParam = new HashMap<String, Object>();
   selectSql
       .append("select count(0) from ")
       .append(baseForm.getBaseFormCode())
       .append(" where 1=1 and id =#{id}");
   String id = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_id");
   if (StringUtil.isNullOrEmpty(id)) {
     return 0;
   } else {
     selectParam.put("id", id);
     return this.businessBaseTemplateDao.countByDataSql(selectSql.toString(), selectParam);
   }
 }
  @Override
  public Map<String, Object> getFormData(String businessFormtemplateId, String flowRequestId) {
    Map<String, Object> resultMap = new HashMap<String, Object>();
    List<BusinessBaseTemplate> list =
        businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormtemplateId);
    for (BusinessBaseTemplate baseTemplate : list) {
      StringBuffer selectSql = new StringBuffer();
      Map<String, Object> selectParam = new HashMap<String, Object>();
      String businessBaseId = baseTemplate.getBusinessBaseId();
      String baseFormId = businessBaseDao.getById(businessBaseId).getBaseFormId();
      BaseForm baseForm = baseFormDao.getById(baseFormId);
      String baseFormCode = baseForm.getBaseFormCode().toLowerCase();
      selectSql.append("select id ").append("\"id\",");
      // 需要设置rawText值的字段
      List<FormField> needRawTextList = new ArrayList<FormField>();
      // 自定义组件list
      List<TemplateFieldDto> cusFomFieldList = new ArrayList<TemplateFieldDto>();

      List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId());
      for (TemplateField templateField : fieldList) {
        if (templateField != null
            && templateField.getIsShow() != null
            && templateField.getIsShow() == 1) {
          FormField formField = formFieldDao.getById(templateField.getFieldId());
          selectSql
              .append(formField.getFieldCode() + "  \"")
              .append(formField.getFieldCode().toLowerCase() + "\",");
          // 列表组件
          if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_GRID
              || formField.getViewType().intValue() == FormMetaValue.FORMFIELD_TREE) {
            needRawTextList.add(formField);
          }
          if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_CUS_FIELD) {
            TemplateFieldDto d = new TemplateFieldDto();
            d.setTemplateField(templateField);
            d.setFormField(formField);
            cusFomFieldList.add(d);
          }
        }
      }
      String sql =
          selectSql.substring(0, selectSql.length() - 1)
              + " from "
              + baseForm.getBaseFormCode().toLowerCase()
              + " where FLOW_REQUEST_ID = #{flowRequestId}";
      selectParam.put("flowRequestId", flowRequestId);

      BusinessBase businessBase = businessBaseDao.getById(baseTemplate.getBusinessBaseId());
      // 主表单
      if (businessBase.getIsDetail() != null && businessBase.getIsDetail() == 0) {
        // 从数据库查询表单值
        Map<String, Object> map = businessBaseTemplateDao.getMainDataBySql(sql, selectParam);
        // 设置需要rawText的值
        for (FormField formField : needRawTextList) {
          Object value = map.get(formField.getFieldCode().toLowerCase());
          if (!StringUtil.isNullOrEmpty(value)) {
            map.put(
                formField.getFieldCode().toLowerCase() + "_rawtext",
                formFieldService.getRawTextValue(value.toString(), formField));
          }
        }
        // 自定义组件设置值
        for (TemplateFieldDto dto : cusFomFieldList) {
          Object value = map.get(dto.getFormField().getFieldCode().toLowerCase());
          if (!StringUtil.isNullOrEmpty(value)) {
            sqlDatasourceService.setCusFieldValueToMap(dto, map, value);
          }
        }
        resultMap.put(baseFormCode, map);
      } else if (businessBase.getIsDetail() != null && businessBase.getIsDetail() == 1) {
        List<Map<String, Object>> resultList =
            businessBaseTemplateDao.getSubDataBySql(sql, selectParam);
        // 设置需要rawText的值
        for (Map<String, Object> subList : resultList) {
          for (FormField formField : needRawTextList) {
            Object value = subList.get(formField.getFieldCode().toLowerCase());
            if (!StringUtil.isNullOrEmpty(value)) {
              subList.put(
                  formField.getFieldCode().toLowerCase() + "_rawtext",
                  formFieldService.getRawTextValue(value.toString(), formField));
            }
          }
        }
        if (resultList != null) {
          resultMap.put(baseFormCode, resultList);
        } else {
          resultMap.put(baseFormCode, new ArrayList<Map<String, String>>());
        }
      } else {
        resultMap.put(baseFormCode, new HashMap<String, String>());
      }
    }
    return resultMap;
  }
  @Override
  public String buildByTemplateIdAndSubix(
      String businessFormTemplateId,
      String baseSubix,
      String mainSubix,
      String detailSubix,
      String htmlSubix) {
    StringBuffer bld = new StringBuffer();
    List<businessBaseVo> businessBaseFormList = new ArrayList<businessBaseVo>();
    BusinessFormTemplate businsessFormTemplate =
        businessFormTemplateDao.getById(businessFormTemplateId);
    String htmlTemplatePath = businsessFormTemplate.getHtmlElementPath() + htmlSubix;
    List<BusinessBaseTemplate> list =
        businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId);
    for (BusinessBaseTemplate baseTemplate : list) {
      String businessBaseId = baseTemplate.getBusinessBaseId();
      BusinessBase businessBase = this.businessBaseDao.getById(businessBaseId);
      Integer isDetail = businessBase.getIsDetail();
      String templatePath = baseTemplate.getBusinessBaseTemplate();
      if (isDetail != null && isDetail.intValue() == 0) {
        templatePath = templatePath + mainSubix;
      } else {
        templatePath = templatePath + detailSubix;
      }
      BaseForm baseForm = this.baseFormDao.getById(businessBase.getBaseFormId());
      baseForm.setBaseFormCode(baseForm.getBaseFormCode().toLowerCase());
      List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId());
      List<TemplateFieldDto> templateFieldList = new ArrayList<TemplateFieldDto>();
      for (TemplateField templateField : fieldList) {
        if (htmlSubix.equalsIgnoreCase(".read")) {
          templateField.setIsEdit(0);
          templateField.setIsRequire(0);
        }
        if (templateField != null
            && templateField.getIsShow() != null
            && templateField.getIsShow() == 1) {
          if (isDetail != null && isDetail.intValue() == 0) {
            templateFieldList.add(
                templateFieldService.bulidMainField(templateField, htmlTemplatePath));
          } else {
            templateFieldList.add(
                templateFieldService.buildDetailField(templateField, htmlTemplatePath));
          }
        }
      }
      Map<String, Object> param = new HashMap<String, Object>();
      param.put("templateFieldList", templateFieldList);
      param.put("baseTemplate", baseTemplate);
      param.put("baseForm", baseForm);
      try {
        Template template = formTemplateService.getTemplate(templatePath);
        StringWriter writer = new StringWriter();
        template.process(param, writer);
        businessBaseVo vo = new businessBaseVo();
        vo.setBusinessBaseHtml(writer.toString());
        vo.setBusinessBaseTemplate(baseTemplate);
        businessBaseFormList.add(vo);
      } catch (IOException e) {
        log.error(
            this.getClass().getSimpleName()
                + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:["
                + businessFormTemplateId
                + "]",
            e);
        throw new FormException("form-209");
      } catch (TemplateException e) {
        log.error(
            this.getClass().getSimpleName()
                + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:["
                + businessFormTemplateId
                + "]",
            e);
        throw new FormException("form-210");
      }
    }

    try {
      String baseTemplatePath =
          businessFormTemplateDao.getById(businessFormTemplateId).getTempatePath() + baseSubix;
      Map<String, Object> param = new HashMap<String, Object>();
      param.put("businsessFormTemplate", businsessFormTemplate);
      param.put("businessBaseFormList", businessBaseFormList);
      Template template = formTemplateService.getTemplate(baseTemplatePath);
      StringWriter writer = new StringWriter();
      template.process(param, writer);
      bld.append(writer.toString());
    } catch (IOException e) {
      log.error(
          this.getClass().getSimpleName()
              + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:["
              + businessFormTemplateId
              + "]",
          e);
      throw new FormException("form-209");
    } catch (TemplateException e) {
      log.error(
          this.getClass().getSimpleName()
              + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:["
              + businessFormTemplateId
              + "]",
          e);
      throw new FormException("form-210");
    }
    String resultForm = bld.toString();
    return resultForm;
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private void updateFormData(
      BusinessBaseTemplate baseTemplate, Map<String, String> formData, String flowRequestId) {
    String businessBaseId = baseTemplate.getBusinessBaseId();
    BusinessBase businessBase = businessBaseDao.getById(businessBaseId);
    String baseFormId = businessBase.getBaseFormId();
    BaseForm baseForm = baseFormDao.getById(baseFormId);
    List<TemplateFieldDto> fieldDtoList = new ArrayList<TemplateFieldDto>();
    StringBuffer insertSql = new StringBuffer();
    StringBuffer paramSql = new StringBuffer();
    Map<String, Object> insertParam = new HashMap<String, Object>();
    insertSql.append(" insert into ").append(baseForm.getBaseFormCode()).append("(id,");
    paramSql.append(" values (#{id},");
    insertSql.append("FLOW_REQUEST_ID,");
    paramSql.append("#{flowRequestId},");
    insertParam.put("flowRequestId", flowRequestId);

    StringBuffer updateSql = new StringBuffer();
    Map<String, Object> updateParam = new HashMap<String, Object>();
    updateParam.put("flowRequestId", flowRequestId);
    updateSql.append("update ").append(baseForm.getBaseFormCode()).append(" set ");

    List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId());
    for (TemplateField templateField : fieldList) {
      if (templateField != null
          && templateField.getIsShow() != null
          && templateField.getIsShow() == 1) {
        FormField formField = formFieldDao.getById(templateField.getFieldId());
        TemplateFieldDto dto = new TemplateFieldDto();
        dto.setFormField(formField);
        dto.setTemplateField(templateField);
        fieldDtoList.add(dto);
        updateSql.append(formField.getFieldCode());
        updateSql.append(" =  #{" + formField.getFieldCode().toLowerCase() + "},");
        insertSql.append(formField.getFieldCode()).append(",");
        paramSql.append("#{" + formField.getFieldCode().toLowerCase() + "},");
      }
    }
    String sql_update =
        updateSql.substring(0, updateSql.length() - 1)
            + " where id = #{id} and FLOW_REQUEST_ID = #{flowRequestId} ";
    String sql_insert =
        insertSql.substring(0, insertSql.length() - 1)
            + ") "
            + paramSql.substring(0, paramSql.length() - 1)
            + ")";

    if (businessBase.getIsDetail() != null && businessBase.getIsDetail().intValue() == 0) {
      String id = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_id");
      updateParam.put("id", id);
      List<Map<String, String>> formFileList = new ArrayList<Map<String, String>>();
      Map<String, Object> validValueMap = new HashMap<String, Object>();
      for (TemplateFieldDto dto : fieldDtoList) {
        TemplateField templateField = dto.getTemplateField();
        if (templateField != null
            && templateField.getIsShow() != null
            && templateField.getIsShow() == 1) {
          FormField formField = dto.getFormField();
          Object value =
              formData.get(
                  baseForm.getBaseFormCode().toLowerCase()
                      + "_"
                      + formField.getFieldCode().toLowerCase());
          if (value == null) value = "";
          if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_UPLOAD
              && formField.getDatasourceType().intValue() == 2) {
            Object fileValue =
                formData.get(
                    baseForm.getBaseFormCode().toLowerCase()
                        + "_"
                        + formField.getFieldCode().toLowerCase()
                        + "_file");
            fileValue = (fileValue == null ? "" : fileValue.toString());
            if (fileValue.toString().endsWith(","))
              fileValue = fileValue.toString().substring(0, fileValue.toString().length() - 1);
            Map<String, String> map = new HashMap<String, String>();
            if (StringUtil.isNullOrEmpty(value)) {
              value = UUIDGenerator.getUUID();
            }
            map.put("attachId", value.toString());
            map.put("fileList", fileValue.toString());
            formFileList.add(map);
          }
          if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_DATAFIELD) {
            if (!StringUtil.isNullOrEmpty(value)) {
              try {
                value = DateUtils.parseDate(value.toString(), new String[] {"yyyy-MM-dd"});
              } catch (ParseException e) {
                log.error(
                    this.getClass().getSimpleName()
                        + " e==> updateFormData  ,ParseException + businessFormtemplateId:["
                        + baseTemplate.getBusinessBaseTemplate()
                        + "]",
                    e);
                value = "";
              }
            } else {
              value = "";
            }
          }
          dto.setValue(value);
          validValueMap.put(formField.getFieldCode().toLowerCase(), value);
          updateParam.put(formField.getFieldCode().toLowerCase(), value);
        }
      }
      // 验证表单正确性
      for (TemplateFieldDto dto : fieldDtoList) {
        templateFieldService.checkFormFieldValue(dto, validValueMap);
      }

      // 主表更新
      businessBaseTemplateDao.updateDataBySql(sql_update, updateParam);
      // 更新附件信息
      for (Map<String, String> formFile : formFileList) {
        String attachId = formFile.get("attachId");
        String[] fileList = formFile.get("fileList").split(",");
        formFileService.updateFileList(attachId, fileList);
      }
    } else {
      String detailDataIds =
          formData.get(baseForm.getBaseFormCode().toLowerCase() + "_DETAIL_DATAIDS");
      String[] detailDataIdArr = detailDataIds.split(",");
      Integer maxValue = baseTemplate.getMaxValue();
      if (maxValue != null && maxValue.intValue() > 0 && (detailDataIdArr.length > maxValue)) {
        throw new FormException("form-212", "表单数据超出范围");
      }
      // 需要更新的明细idList 也就是删除时候的not in
      List<String> updateIdList = new ArrayList<String>();
      // 新增加的明细参数
      List<Map<String, Object>> insertParamList = new ArrayList<Map<String, Object>>();
      for (int i = 0; i < detailDataIdArr.length; i++) {
        String idIndex = detailDataIdArr[i];
        if (StringUtil.isNullOrEmpty(idIndex)) continue;
        Object id = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_id_" + idIndex);

        Map<String, Object> validValueMap = new HashMap<String, Object>();
        for (TemplateFieldDto dto : fieldDtoList) {
          TemplateField templateField = dto.getTemplateField();
          if (templateField != null
              && templateField.getIsShow() != null
              && templateField.getIsShow() == 1) {
            FormField formField = dto.getFormField();
            Object value =
                formData.get(
                    baseForm.getBaseFormCode().toLowerCase()
                        + "_"
                        + formField.getFieldCode().toLowerCase()
                        + "_"
                        + idIndex);
            if (value == null) value = "";
            if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_DATAFIELD) {
              if (!StringUtil.isNullOrEmpty(value)) {
                try {
                  value = DateUtils.parseDate(value.toString(), new String[] {"yyyy-MM-dd"});
                } catch (ParseException e) {
                  log.error(
                      this.getClass().getSimpleName()
                          + " e==> updateFormData  ,ParseException + businessFormtemplateId:["
                          + baseTemplate.getBusinessBaseTemplate()
                          + "]",
                      e);
                  value = "";
                }
              } else {
                value = null;
              }
            }
            dto.setValue(value);
            validValueMap.put(formField.getFieldCode().toLowerCase(), value);
            updateParam.put(formField.getFieldCode().toLowerCase(), value);
            insertParam.put(formField.getFieldCode().toLowerCase(), value);
          }
        }
        // 验证表单正确性
        for (TemplateFieldDto dto : fieldDtoList) {
          templateFieldService.checkFormFieldValue(dto, validValueMap);
        }
        if (StringUtil.isNullOrEmpty(id)) {
          // 表示新增明细
          id = UUIDGenerator.getUUID();
          insertParam.put("id", id);
          HashMap<String, Object> insert = new HashMap<String, Object>();
          insert.putAll(insertParam);
          insertParamList.add((HashMap) insert.clone());

        } else {
          updateIdList.add(String.valueOf(id));
          updateParam.put("id", id);
          businessBaseTemplateDao.updateDataBySql(sql_update, updateParam);
        }
      }

      // 删除not in update idList
      businessBaseTemplateDao.deleteDetailData(
          baseForm.getBaseFormCode(), flowRequestId, updateIdList);
      // 新增明细
      for (Map<String, Object> insert : insertParamList) {
        businessBaseTemplateDao.insertDataBySql(sql_insert, insert);
      }
    }
  }
 @Override
 public String buildByTemplateIdAndShowOrder(String businessFormTemplateId, int start, int end) {
   StringBuffer bld = new StringBuffer();
   List<businessBaseVo> businessBaseFormList = new ArrayList<businessBaseVo>();
   BusinessFormTemplate businsessFormTemplate =
       businessFormTemplateDao.getById(businessFormTemplateId);
   String htmlTemplatePath = businsessFormTemplate.getHtmlElementPath();
   List<BusinessBaseTemplate> list =
       businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId);
   for (BusinessBaseTemplate baseTemplate : list) {
     if (baseTemplate.getShowOrder() > end || baseTemplate.getShowOrder() < start) continue;
     String templatePath = baseTemplate.getBusinessBaseTemplate();
     String businessBaseId = baseTemplate.getBusinessBaseId();
     BusinessBase businessBase = this.businessBaseDao.getById(businessBaseId);
     Integer isDetail = businessBase.getIsDetail();
     BaseForm baseForm = this.baseFormDao.getById(businessBase.getBaseFormId());
     baseForm.setBaseFormCode(baseForm.getBaseFormCode().toLowerCase());
     List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId());
     List<TemplateFieldDto> templateFieldList = new ArrayList<TemplateFieldDto>();
     for (TemplateField templateField : fieldList) {
       if (templateField != null
           && templateField.getIsShow() != null
           && templateField.getIsShow() == 1) {
         if (isDetail != null && isDetail.intValue() == 0) {
           templateFieldList.add(
               templateFieldService.bulidMainField(templateField, htmlTemplatePath));
         } else {
           templateFieldList.add(
               templateFieldService.buildDetailField(templateField, htmlTemplatePath));
         }
       }
     }
     Map<String, Object> param = new HashMap<String, Object>();
     param.put("templateFieldList", templateFieldList);
     param.put("baseTemplate", baseTemplate);
     param.put("baseForm", baseForm);
     try {
       Template template = formTemplateService.getTemplate(templatePath);
       StringWriter writer = new StringWriter();
       template.process(param, writer);
       bld.append(writer.toString());
     } catch (IOException e) {
       log.error(
           this.getClass().getSimpleName()
               + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:["
               + businessFormTemplateId
               + "]",
           e);
       throw new FormException("form-209");
     } catch (TemplateException e) {
       log.error(
           this.getClass().getSimpleName()
               + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:["
               + businessFormTemplateId
               + "]",
           e);
       throw new FormException("form-210");
     }
   }
   return bld.toString();
 }