<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.sql.*" %>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.database.*" %>
<%@ page import="net.sz600.system.SzProperties" %>
<%@ page import="net.sz600.system.SzLevelNumber" %>
<%@ page import="net.sz600.system.SzPageHelp" %>
<%@ include file="/sessionInfo.jsp"%>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
</head>
<%
    //获取提交的参数
    String sType = SzConvert.toZeroStr(request.getParameter("type"));
    String sOPERATOR = SzConvert.toSpaceStr(request.getParameter("OPERATOR"));
	String sID = SzConvert.strWriteData(request.getParameter("txtID"));
	String sName = SzConvert.strWriteData(request.getParameter("txtName"));
    String sAddress = SzConvert.strWriteData(request.getParameter("txtAddress"));
    String sCode = SzConvert.strWriteData(request.getParameter("txtCode"));

	//mysql连接配置参考：
	
    String sDriver = "org.gjt.mm.mysql.Driver";
    String sUrl = "jdbc:mysql://localhost:3306/SPDB_CUSTOMER?useUnicode=true&characterEncoding=latin1";
    String sUser = "root";
    String sPassword = "";

    //sql server连接配置参考：
	/*
    String sDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String sUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SZ_PUFA";
    String sUser = "sa";
    String sPassword = "";
	*/
    Connection conn = null;
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;
	StringBuffer sb = new StringBuffer(" select custom.cust_id, cust_cn_name, org_no, office_addr,linker_type_cd,linker_name");
	sb.append(" from custom,custom_linker");
	sb.append(" where ");
	
	if (sOPERATOR.equals("query")) {
		sb.append(" custom.cust_id = custom_linker.cust_id and (linker_type_cd='01' or linker_type_cd='03')");
		//查询
		if (!sID.equals("")) {
			sb.append(" AND custom.cust_id like '%");
			sb.append(new String(sID.getBytes(), "latin1"));
			sb.append("%'");
		}
		if (!sName.equals("")) {
			sb.append(" AND custom.cust_cn_name like '%");
			sb.append(new String(sName.getBytes(), "latin1"));
			sb.append("%'");
		}
		if (!sAddress.equals("")) {
			sb.append(" AND custom.office_addr like '%");
			sb.append(new String(sAddress.getBytes(), "latin1"));
			sb.append("%'");
		}
		if (!sCode.equals("")) {
			sb.append(" AND custom.org_no like '%");
			sb.append(new String(sCode.getBytes(), "latin1"));
			sb.append("%'");
		}		
	} else {
		sb.append(" 1<>1 ");
	}
	sb.append(" order by custom.cust_id, linker_type_cd");
	String sql = sb.toString();
	sb = null;
    try {
    	Class.forName(sDriver);
    	conn = DriverManager.getConnection(sUrl, sUser, sPassword);
		stmt=conn.createStatement();
//		stmt.executeQuery("set names 'GBK'");
//    	pstmt = conn.prepareStatement(sql);
    	rs = stmt.executeQuery(sql);    	
    } catch(Exception e) { 
		out.println("连接客户信息数据库有问题<p>");
		out.println("URL: " + sUrl + "<BR>");
		out.println("User: " + sUser + "<BR>");
//		out.println("Password: ********<BR>");
		if (pstmt != null) {
			pstmt.close();
			pstmt = null;
		}
		if (conn != null) {
			conn.close();
			conn = null;
		} 
		return;
    }
    int i = 0;
    

%>
<!-----上面的工具栏------>
<%@ include file="/share/title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <input type="hidden" name="type" value="<%=sType%>">
  <tr>
    <td>
客户名称:<input type="text" name="txtName" class="txtClass1" value="<%=sName %>"> 
客户号:<input type="text" name="txtID" class="txtClass80" value="<%=sID %>"><br>     
住&nbsp;所&nbsp;在:<input type="text" name="txtAddress" class="txtClass1" value="<%=sAddress %>">
组织机构代码:<input type="text" name="txtCode" class="txtClass100" value="<%=sCode %>">            
     <%=SzPageHelp.button("重置","javascript:Reset();") %>
     <%=SzPageHelp.button("查询","javascript:FormSubmit(formBar, 'index.jsp?OPERATOR=query');") %>
     <%=SzPageHelp.button("导入选中","javascript:FormSubmitConfirm(formList, 'control.jsp?OPERATOR=import', '已存在的信息将覆盖,是否继续?');") %>
    </td>
  </tr>
  </form>
</table>

<!-----信息列表----->
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formList" method="post">
  <input type="hidden" name="type" value="<%=sType%>">
  <tr class="trClassTitle">
    <td width="5"><input type="checkbox" name="chkCheckAll" OnClick="javascript:CheckAll(formList, chkCheckAll, 'chkId');"></td>
	<td>客户号</td>
  	<td>客户名称</td>
  	<td>组织机构代码</td>
  	<td>住所在</td>
  	<td>法定代表人</td>
  	<td>客户联系人</td>
  </tr>
<%
  String cust_id = "";
  String cust_cn_name = "";
  String org_no = "";
  String office_addr = "";
  String linker_name= "";
  String linker_name1= "";
  String linker_name3= "";
  String linker_type_cd = "";
   
  String cust_id2 = "";
  String cust_cn_name2 = "";
  String org_no2 = "";
  String office_addr2 = "";
  String linker_name2 = "";  
  String linker_type_cd2 = "";
  i = 0;
  
  while (rs.next()) {
	cust_id = SzConvert.toSpaceStr( rs.getString("cust_id") );
	cust_cn_name = SzConvert.toSpaceStr( rs.getString("cust_cn_name") );
	org_no = SzConvert.toSpaceStr( rs.getString("org_no") );
	office_addr = SzConvert.toSpaceStr( rs.getString("office_addr") );
	linker_type_cd = SzConvert.toSpaceStr( rs.getString("linker_type_cd") );
	linker_name = SzConvert.toSpaceStr( rs.getString("linker_name") );

	if (!cust_id2.equals(cust_id) && !cust_id2.equals("")) {
		String name1=new String(cust_cn_name2.getBytes("latin1"),"GBK");
		String name2=new String(linker_name1.getBytes("latin1"),"GBK");
		String name3=new String(linker_name3.getBytes("latin1"),"GBK");
		String address=new String(office_addr2.getBytes("latin1"),"GBK");
%>
<tr>
    <td width="5"><input type="checkbox" name="chkId" value="<%=i %>" ></td>
  	<td><input type="hidden" name="cust_id<%=i%>" value="<%=cust_id2%>"><%=cust_id2 %>&nbsp;</td>
  	<td><input type="hidden" name="cust_cn_name<%=i%>" value="<%=name1%>"><%=name1 %>&nbsp;</td>
  	<td><input type="hidden" name="org_no<%=i%>" value="<%=org_no2%>"><%=org_no2 %>&nbsp;</td>
  	<td><input type="hidden" name="office_addr<%=i%>" value="<%=address%>"><%=address %>&nbsp;</td>
  	
  	<td><input type="hidden" name="linker_nameA<%=i%>" value="<%=name2%>"><%=name2 %>&nbsp;</td>
  	<td><input type="hidden" name="linker_nameB<%=i%>" value="<%=name3%>"><%=name3 %>&nbsp;</td>
</tr>  	
<%	  
	  i++;
	  linker_name1 = "";
	  linker_name3 = "";
	}
	cust_id2 = cust_id;
	cust_cn_name2 = cust_cn_name;
	org_no2 = org_no;
	office_addr2 = office_addr;
	linker_type_cd2 = linker_type_cd;
	if (linker_type_cd2.equals("01")) {
		linker_name1 = linker_name;
	} else if (linker_type_cd.equals("03")) {
		  linker_name3 = linker_name;
	}	
  }
  try {
	if (rs != null) {
		rs.close();
		rs = null;
	}
	if (pstmt != null) {
		pstmt.close();
		pstmt = null;
	}
	if (conn != null) {
		conn.close();
		conn = null;
	}     	
  } catch(Exception e) { 
  }  
  if (!cust_id2.equals("")) {
		String name1=new String(cust_cn_name2.getBytes("latin1"),"GBK");
		String name2=new String(linker_name1.getBytes("latin1"),"GBK");
		String name3=new String(linker_name3.getBytes("latin1"),"GBK");
		String address=new String(office_addr2.getBytes("latin1"),"GBK");
	%>
	<tr>
    <td width="5"><input type="checkbox" name="chkId" value="<%=i %>" ></td>
  	<td><input type="hidden" name="cust_id<%=i%>" value="<%=cust_id2%>"><%=cust_id2 %>&nbsp;</td>
  	<td><input type="hidden" name="cust_cn_name<%=i%>" value="<%=name1%>"><%=name1%>&nbsp;</td>
  	<td><input type="hidden" name="org_no<%=i%>" value="<%=org_no2%>"><%=org_no2 %>&nbsp;</td>
  	<td><input type="hidden" name="office_addr<%=i%>" value="<%=address%>"><%=address %>&nbsp;</td>
  	
  	<td><input type="hidden" name="linker_nameA<%=i%>" value="<%=name2%>"><%=name2%>&nbsp;</td>
  	<td><input type="hidden" name="linker_nameB<%=i%>" value="<%=name3%>"><%=name3 %>&nbsp;</td>
</tr>  		
<%
  }
%>
  </form>
</table>

<!-----下面的工具栏------>
<table width="100%">
  <form name="formPage" method="post">
  <input type="hidden" name="type" value="<%=sType%>">
  </form>
</table>

<!-----这里包含要提交的内容--->
<form name="formHidden" method="post">
  <input type="hidden" name="type" value="<%=sType%>">
</form>
<script language="javascript" src="../../share/js/commonTool.js"></script>
<script language="javascript">
  function Reset() {
  	formBar.txtID.value = "";
  	formBar.txtName.value = "";
  	formBar.txtAddress.value = "";
  	formBar.txtCode.value = "";
  }
</script>
