/**
   * 根据公司ID,获得对外合同申请的合同类别 取公司对应的合同类型 如公司为空,则取所有
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  private String calExterContractApplyType(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String xml = null;
    String company_id = JUtil.convertNull(request.getParameter("company_id"));
    String defalut_type = JUtil.convertNull(request.getParameter("defalut_type"));
    xml = "<?xml version='1.0' encoding='gbk'?>\n";
    org.jdom.Element eleRoot = new org.jdom.Element("root");
    org.jdom.Element defalutType = new org.jdom.Element("defalut_type");
    defalutType.setAttribute("defalut_type", defalut_type);
    eleRoot.addContent(defalutType);
    org.jdom.Element eleRows = new org.jdom.Element("contract_types");
    eleRoot.addContent(eleRows);
    org.jdom.Element eleRow = null;

    try {
      List<ExterContractType> lsType = ExterContractType.getContractTypeByCompany(company_id);
      for (ExterContractType type : lsType) {
        eleRow = new org.jdom.Element("contract_type");
        eleRow.setAttribute("con_type_id", type.getCon_type_id());
        eleRow.setAttribute("con_type_name", type.getCon_type_name());
        eleRows.addContent(eleRow);
      }
      org.jdom.output.XMLOutputter xmlout = new org.jdom.output.XMLOutputter();
      xml += xmlout.outputString(eleRoot);
      JLog.getLogger().debug("xml=\n" + xml);
      return xml.trim();
    } catch (Exception e) {
      JLog.getLogger().error("", e);
    }
    return "";
  }
  /**
   * 判断是否需要必须录入合同金额
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  private String calNeedInputMoney(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String xml = null;
    String company_id = JUtil.convertNull(request.getParameter("company_id"));
    String exter_contract_type = JUtil.convertNull(request.getParameter("exter_contract_type"));
    String need_con_money = "0";
    xml = "<?xml version='1.0' encoding='gbk'?>\n";
    org.jdom.Element eleRoot = new org.jdom.Element("root");
    org.jdom.Element eleNeed = new org.jdom.Element("need_con_money");

    try {
      List<ExterContractType> lsType = ExterContractType.getContractTypeByCompany(company_id);
      for (ExterContractType type : lsType) {
        if (type.getCon_type_id().equals(exter_contract_type)) {
          if (type.getNeed_con_money() == 1) {
            need_con_money = "1";
          }
          break;
        }
      }
      eleNeed.setAttribute("need_con_money", need_con_money);
      eleRoot.addContent(eleNeed);
      org.jdom.output.XMLOutputter xmlout = new org.jdom.output.XMLOutputter();
      xml += xmlout.outputString(eleRoot);
      JLog.getLogger().debug("xml=\n" + xml);
      return xml.trim();
    } catch (Exception e) {
      JLog.getLogger().error("", e);
    }
    return "";
  }
  /**
   * 获得对外合同申请的默认审批人
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  private String calDefaultExterContractApprover(
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    String xml = null;
    String company_id = JUtil.convertNull(request.getParameter("company_id"));
    String con_type = JUtil.convertNull(request.getParameter("con_type"));
    String con_money_str = JUtil.convertNull(request.getParameter("con_money"));
    String apply_user_id = JUtil.convertNull(request.getParameter("apply_user_id"));
    Double con_money = null;
    if (con_money_str.length() > 0) {
      try {
        con_money = Double.parseDouble(con_money_str);
      } catch (Exception e) {
      }
    }

    xml = "<?xml version='1.0' encoding='gbk'?>\n";
    org.jdom.Element eleRoot = new org.jdom.Element("root");
    org.jdom.Element eleRows = new org.jdom.Element("users");
    eleRoot.addContent(eleRows);
    org.jdom.Element eleRow = null;

    try {
      Map<String, JUser> mapUser = null;
      if (company_id.length() > 0 && con_type.length() > 0) {
        boolean need_con_money = false;
        List<ExterContractType> lsType = ExterContractType.getContractTypeByCompany(company_id);
        for (ExterContractType type : lsType) {
          if (type.getCon_type_id().equals(con_type)) {
            if (type.getNeed_con_money() == 1) {
              need_con_money = true;
            }
            break;
          }
        }
        if (!need_con_money || con_money != null) {
          mapUser =
              ExterContractApply.getDefaultApprover(
                  company_id, con_type, con_money == null ? 0 : con_money, apply_user_id);
        }
      }
      if (mapUser != null) {
        for (String userId : mapUser.keySet()) {
          JUser user = mapUser.get(userId);
          if (user == null) continue;
          eleRow = new org.jdom.Element("user");
          eleRow.setAttribute("user_id", user.getUserId());
          eleRow.setAttribute("user_name", user.getUserName());
          eleRows.addContent(eleRow);
        }
      }
      org.jdom.output.XMLOutputter xmlout = new org.jdom.output.XMLOutputter();
      xml += xmlout.outputString(eleRoot);
      JLog.getLogger().debug("xml=\n" + xml);
      return xml.trim();
    } catch (Exception e) {
      JLog.getLogger().error("", e);
    }
    return "";
  }
  /**
   * 获得国内合作渠道申请的默认审批人
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  private String calDefaultGnOrgApprover(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String xml = null;
    String company_id = JUtil.convertNull(request.getParameter("company_id"));
    String dep_id = JUtil.convertNull(request.getParameter("dep_id"));
    String contract_template = JUtil.convertNull(request.getParameter("contract_template"));
    String hasDomain_name = JUtil.convertNull(request.getParameter("hasDomain_name"));
    xml = "<?xml version='1.0' encoding='gbk'?>\n";
    org.jdom.Element eleRoot = new org.jdom.Element("root");
    org.jdom.Element eleRows = new org.jdom.Element("users");
    eleRoot.addContent(eleRows);
    org.jdom.Element eleRow = null;
    Connection con = null;
    try {
      con = com.gemway.igo.JDatabase.getJDatabase().getConnection();

      Map<String, JUser> mapUser =
          GnOrgApply.getDefaultApprover(con, company_id, dep_id, contract_template, hasDomain_name);
      if (mapUser != null) {
        for (String userId : mapUser.keySet()) {
          JUser user = mapUser.get(userId);
          if (user == null) continue;

          eleRow = new org.jdom.Element("user");
          eleRow.setAttribute("user_id", user.getUserId());
          eleRow.setAttribute("user_name", user.getUserName());
          eleRows.addContent(eleRow);
        }
      }
      org.jdom.output.XMLOutputter xmlout = new org.jdom.output.XMLOutputter();
      xml += xmlout.outputString(eleRoot);
      // JLog.getLogger().debug("xml=\n" + xml);
      return xml.trim();
    } catch (Exception e) {
      JLog.getLogger().error("", e);
    } finally {
      if (con != null) con.close();
    }
    return "";
  }
  /**
   * 根据地区代码或学校关键字得到学校
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  private String checkRepeatGnOrg(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String xml = "<?xml version='1.0' encoding='gbk'?>\n";
    org.jdom.Element eleRoot = new org.jdom.Element("root");

    CNHttpServletRequest _req = new CNHttpServletRequest(request);
    String company_id_kwd = JUtil.convertNull(_req.getParameter("company_id_kwd"));
    // String dep_id_kwd = JUtil.convertNull(_req.getParameter("dep_id_kwd"));
    String org_id_kwd = JUtil.convertNull(_req.getParameter("org_id_kwd"));
    String org_name_kwd =
        JUtil.convertNull(
            java.net.URLDecoder.decode(request.getParameter("org_name_kwd"), "utf-8"));
    // String org_name_kwd = JUtil.convertNull(_req.getParameter("org_name_kwd"));
    if (org_name_kwd.length() == 0) throw new Exception("机构名称为空!");
    if (org_id_kwd.length() == 0) org_id_kwd = "-";
    com.gemway.igo.page.Page pageObj = new com.gemway.igo.page.PageOracle();
    java.util.List<com.gemway.igo.page.PageHashMap<String, Object>> lsDate = null;
    pageObj.setRequest(_req);
    org.jdom.Element eleRows = null;

    pageObj.setSelectPartSql(
        "company_id, a.org_id, org_no, org_name, shinyway_contracter, org_type");
    pageObj.setFromPartSql("t_gn_org_company a, t_gn_org b");
    pageObj.addWherePartSql(" a.org_id = b.org_id ");
    pageObj.addWherePartSql(" a.org_id != '" + org_id_kwd + "'");
    pageObj.getPageInfo().setSize(21);
    pageObj.addWherePartSql(" org_name = ? ");
    pageObj.addWhereParam(org_name_kwd);
    pageObj.setOrderByPartSql(" created desc ");
    try {
      lsDate = pageObj.getPageData();
    } catch (Exception e) {
      e.printStackTrace();
    }

    for (int i = 0; i < lsDate.size(); i++) {
      com.gemway.igo.page.PageHashMap<String, Object> data = lsDate.get(i);
      eleRows = new org.jdom.Element("org");
      if (data.getString("company_id").equals(company_id_kwd)) {
        // eleRows = new org.jdom.Element("org");
      } else {
        // eleRows = new org.jdom.Element("other_company_org");
      }
      eleRows.setAttribute("company_id", data.getString("company_id"));
      eleRows.setAttribute(
          "company_name", JUnit.getUnit(data.getString("company_id")).getUnit_name());
      eleRows.setAttribute("org_id", data.getString("org_id"));
      eleRows.setAttribute("org_no", data.getString("org_no"));
      eleRows.setAttribute("org_name", data.getString("org_name"));
      eleRows.setAttribute("org_type", GnOrgBean.getOrgTypeNameById(data.getString("org_type")));
      eleRows.setAttribute(
          "shinyway_contracter",
          data.getString("shinyway_contracter") == null
              ? ""
              : JUser.getUserName(data.getString("shinyway_contracter")));

      eleRoot.addContent(eleRows);
    }

    org.jdom.output.XMLOutputter xmlout = new org.jdom.output.XMLOutputter();

    xml += xmlout.outputString(eleRoot);
    JLog.getLogger().debug("xml=\n" + xml);
    return xml.trim();
  }