<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.sql.*,gbase.db.*,beitian.onlinechat.*,java.util.*" %>
<%
	
	String message="用户名重复，注册失败，请重新注册！";
	String register_flag="false";
	String username=request.getParameter("username")==null?"":request.getParameter("username").trim();
	String name=request.getParameter("name")==null?"":request.getParameter("name").trim();
	String password=request.getParameter("password")==null?"":request.getParameter("password");
	String email=request.getParameter("email")==null?"":request.getParameter("email").trim();
	String net_url=request.getParameter("net_url")==null?"":request.getParameter("net_url").trim();
	String url=request.getParameter("return_url")==null?"index.jsp":request.getParameter("return_url").trim();
	String conname=request.getParameter("conname")==null?"":request.getParameter("conname").trim();
        String tel=request.getParameter("tel")==null?"":request.getParameter("tel").trim();
        String content=request.getParameter("content")==null?"":request.getParameter("content").trim();
	
	String sql = " select * from enter_user where username=?";
        DBOperation theDBOperation = new DBOperation();
        DBConnection dbConnection = new DBConnection();
        Connection conn=dbConnection.getConnection();
        PreparedStatement stmt=conn.prepareStatement(sql);
        stmt.setString(1,username);
        ResultSet rs1=stmt.executeQuery();
        
        
        
      try {
        if(!rs1.next())
        {
        sql = " insert into enter_user(username,name,password,email,net_url,conname,tel,content) values(?,?,?,?,?,?,?,?)";
        stmt=conn.prepareStatement(sql);
        stmt.setString(1,username);
        stmt.setString(2,name);
        stmt.setString(3,password);
        stmt.setString(4,email);
        stmt.setString(5,net_url);
        stmt.setString(6,conname);
        stmt.setString(7,tel);
        stmt.setString(8,content);
        int i=stmt.executeUpdate();
        
        if(i>0)
          {
          String sql1 = " insert into enter_user_user(username,name,password,enter_user_id,email) values(?,?,?,?,?)";
          stmt=conn.prepareStatement(sql1);
          stmt.setString(1,username);
          stmt.setString(2,"系统管理员");
          stmt.setString(3,password);
          stmt.setString(4,username);
          stmt.setString(5,email);
          int j=stmt.executeUpdate();
          int id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          if(j>0){
          String sql11[]=new String[7];
          sql11[0]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','你好,请问我有什么可以帮助您的吗？')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[1]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','你好，请问您的联系电话是多少？')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[2]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','等等，我帮您查一下')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[3]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','好的，欢迎您下次再来')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[4]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','您的邮箱是什么')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[5]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','好的，处理完后我们再联系您')";
          id=beitian.util.KeyGenerator.getInstance().getNextKey("user_language");
          sql11[6]="insert into user_language(id,enter_user_id,username,content) values('"+id+"','"+username+"','"+username+"','谢谢了，请问还有什么为您服务的吗')";
          theDBOperation.executeSqls(sql11);
          java.util.Date usertime=Calendar.getInstance().getTime();
          AdminUser theAdminUser = new AdminUser(username, name, usertime);
          ChatManager.onlinechatAdminUserMap.put(username, theAdminUser);
          message = "注册成功,您的帐号是"+username+",系统管理员工号是"+username+",请进行下一步设置！";
          register_flag="true";
          }
          }
        else
          {message = "用户名重复，注册失败，请重新注册！";
          register_flag="false";}
      }
      }
      catch (Exception ex) {
        message = "用户名重复，注册失败，请重新注册！";
        register_flag="false";
      }
      finally{
      if(rs1!=null)rs1.close();
      if(stmt!=null && (!conn.isClosed()))stmt.close();
      if(conn!=null && (!conn.isClosed()))conn.close();
      }
	
%>
<script language="javascript">
	function process ( message,register_flag,url){
		alert(message);
		if(register_flag=='false')
		self.location = "register.jsp?register_flag="+register_flag;
		else
		self.location = "register1.jsp?username=<%=username%>&name=<%=name%>";
	}
</script>
<html>
	<head>
		<title>用户注册</title>
	</head>
	<body onload="javascript: process('<%=message%>','<%=register_flag%>','<%=url%>');">
	</body>
</html>