<%@ page contentType="text/html;charset=GBK" %>
<%@ include file="../checkLogin.jsp" %>
<%@ page import="gbase.db.*,java.util.*,gbase.init.*,gbase.util.*,java.text.*,java.io.*,org.apache.log4j.MDC"%>
<%
	SeqIdGenerater theSeqIdGenerater=new SeqIdGenerater();
	ConvertSql theConvertSql=new ConvertSql();
	DBOperation theDBOperation = new DBOperation();
        ResultSet rs=null;
	String message="操作失败";
	String action = request.getParameter("action")==null?"":request.getParameter("action");
	String url="web_info_list.jsp";
	
	String web_id=request.getParameter("web_id")==null?"":request.getParameter("web_id");
	String web_account=request.getParameter("web_account")==null?"":request.getParameter("web_account");
        String web_name=request.getParameter("web_name")==null?"":request.getParameter("web_name");
        String web_url=request.getParameter("web_url")==null?"":request.getParameter("web_url").trim();
        String web_type=request.getParameter("web_type")==null?"":request.getParameter("web_type");
        String web_keywords=request.getParameter("web_keywords")==null?"":request.getParameter("web_keywords");
        String web_description=request.getParameter("web_description")==null?"":request.getParameter("web_description");
        String web_user_type=request.getParameter("web_user_type")==null?"":request.getParameter("web_user_type");
        String web_enterprise_name=request.getParameter("web_enterprise_name")==null?"":request.getParameter("web_enterprise_name");
        String web_enterprise_address=request.getParameter("web_enterprise_address")==null?"":request.getParameter("web_enterprise_address");
        String web_contacter_name=request.getParameter("web_contacter_name")==null?"":request.getParameter("web_contacter_name");
        String web_contacter_telno=request.getParameter("web_contacter_telno")==null?"":request.getParameter("web_contacter_telno");
        String web_contacter_email=request.getParameter("web_contacter_email")==null?"":request.getParameter("web_contacter_email");
        String web_contacter_qq=request.getParameter("web_contacter_qq")==null?"":request.getParameter("web_contacter_qq");
        String web_jive_flag=request.getParameter("web_jive_flag")==null?"":request.getParameter("web_jive_flag");
	String web_jive_desc=request.getParameter("web_jive_desc")==null?"":request.getParameter("web_jive_desc");
	String real_url=request.getParameter("real_url")==null?"":request.getParameter("real_url");
	if(real_url.equals(""))real_url="/images/logo.jpg";
        String password=request.getParameter("password")==null?"":request.getParameter("password");
	
	SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = Calendar.getInstance().getTime();
        String create_date = timeformat.format(date).toString();
	
	String sql="";
	int url_update_flag=1;
	if(!web_url.equals(""))
	{
        sql = "select web_account from web_info  where web_url='"+web_url+"' and web_account!='"+web_account+"'";
        rs =theDBOperation.getResultSet(sql);
        if(rs.next())
          url_update_flag=0;
        }
   if(web_url.equals("") || url_update_flag==1){
   
	String sqls[]=new String[2];
	if(action.equals("update"))
	{ 
	  sqls[0]="update web_info set web_url='"+web_url+"',web_name='"+web_name+"',web_type='"+web_type+"',web_keywords='"+web_keywords+"',web_description='"+web_description+"',web_user_type='"+web_user_type+"',web_enterprise_name='"+web_enterprise_name+"',web_enterprise_address='"+web_enterprise_address+"',web_contacter_telno='"+web_contacter_telno+"',web_contacter_email='"+web_contacter_email+"',web_contacter_qq='"+web_contacter_qq+"',web_jive_flag='"+web_jive_flag+"',web_jive_desc='"+web_jive_desc+"',real_url='"+real_url+"'  where web_account='"+web_account+"'";
	  sqls[1]="update web_user set password='"+password+"' where user_account='admin' and web_account='"+web_account+"'";
	}
	else if(action.equals("delete"))
	 {
	  sqls[0]="delete from web_info  where web_account='"+web_account+"'";
	  sqls[1]="delete from web_user  where web_account='"+web_account+"'";
	 } 
	else
	  {
	  int seqId=theSeqIdGenerater.generatorKey("web_info");
	  web_id=Integer.toString(seqId);
	  int user_id=theSeqIdGenerater.generatorKey("web_user");
	  sqls[0]="insert into web_info(web_id,web_account,web_url,web_name,web_type,web_keywords,web_description,web_user_type,web_enterprise_name,web_enterprise_address,web_contacter_telno,web_contacter_email,web_contacter_qq,create_date,web_jive_flag,web_jive_desc,real_url) values("+seqId+",'"+web_account+"','"+web_url+"','"+web_name+"','"+web_type+"','"+web_keywords+"','"+web_description+"','"+web_user_type+"','"+web_enterprise_name+"','"+web_enterprise_address+"','"+web_contacter_telno+"','"+web_contacter_email+"','"+web_contacter_qq+"','"+create_date+"','"+web_jive_flag+"','"+web_jive_desc+"','"+real_url+"')";
	  sqls[1]="insert into web_user(user_id,web_account,user_account,password,user_name,telno,address,duty,bir_add)  values("+user_id+",'"+web_account+"','admin','"+password+"','超级管理员','','','','')";
	  }
	
        try {
        int i=theDBOperation.executeSqls(sqls);
        if(i>0)
          {
          WebConfig theWebConfig=WebConfig.getInstance();
          String root_path=theWebConfig.getManagerWebDir();
          if(!action.equals("delete")){
          WebInfo theWebInfo=new WebInfo();
          theWebInfo.setWeb_id(web_id);
          theWebInfo.setWeb_account(web_account);
          theWebInfo.setWeb_name(web_name);
          theWebInfo.setWeb_url(web_url);
          theWebInfo.setWeb_type(web_type);
          theWebInfo.setWeb_keywords(web_keywords);
          theWebInfo.setWeb_description(web_description);
          theWebInfo.setWeb_user_type(web_user_type);
          theWebInfo.setWeb_enterprise_name(web_enterprise_name);
          theWebInfo.setWeb_enterprise_address(web_enterprise_address);
          theWebInfo.setWeb_contacter_name(web_contacter_name);
          theWebInfo.setWeb_contacter_telno(web_contacter_telno);
          theWebInfo.setWeb_contacter_email(web_contacter_email);
          theWebInfo.setWeb_contacter_qq(web_contacter_qq);
          theWebInfo.setWeb_jive_flag(web_jive_flag);
          theWebInfo.setWeb_jive_desc(web_jive_desc);
          theWebInfo.setReal_url(real_url);
          theWebConfig.setWebInfo(theWebInfo);
          
          if(!action.equals("update"))
          {
          String sdir=root_path+"/"+web_account;
          File dir=new File(sdir);
          if (!dir.exists()){
           dir.mkdir();
          }
          File upload_dir=new File(sdir+"/upload");
          if (!upload_dir.exists()){
           upload_dir.mkdir();
          }
          
          
          //预设方案导入
          String sys_project_sql="select * from sys_web_project";
          rs =theDBOperation.getResultSet(sys_project_sql);
          while(rs.next())
           {
            String project_sql="insert into web_project(id,title,content,flag,web_account,project_desc) values("+rs.getString("id")+",'"+rs.getString("title")+"','"+theConvertSql.convertSqlValue(rs.getString("content"))+"',"+rs.getString("flag")+",'"+web_account+"','"+theConvertSql.convertSqlValue(rs.getString("project_desc"))+"')";
            theDBOperation.executeSql(project_sql);
           }
          
          //预设风格导入
          String sys_css_sql="select * from sys_web_css";
          rs =theDBOperation.getResultSet(sys_css_sql);
          while(rs.next())
           {
            String css_sql="insert into web_css(id,web_project_id,title,content,flag,web_account) values("+rs.getString("id")+","+rs.getString("web_project_id")+",'"+rs.getString("title")+"','"+theConvertSql.convertSqlValue(rs.getString("content"))+"',"+rs.getString("flag")+",'"+web_account+"')";
            theDBOperation.executeSql(css_sql);
           }
          //复制风格文件
          Copys theCopys=new Copys();   
          theCopys.copys(root_path+"/css",sdir+"/css");
          String from_file=root_path+"/"+web_account+"/css/css.css";
          String to_file=root_path+"/"+web_account+"/"+web_account+"/css/css.css";
          theCopys.copy(from_file,to_file);   
          
          /*
          //预设标签导入
          String sys_label_sql="select * from sys_web_label";
          rs =theDBOperation.getResultSet(sys_label_sql);
          while(rs.next())
           {
            String label_sql="insert into web_label(id,title,content,filename,title_desc,test_desc,web_account) values("+rs.getString("id")+",'"+rs.getString("title")+"','"+theConvertSql.convertSqlValue(rs.getString("content"))+"','"+rs.getString("filename")+"','','','"+web_account+"')";
            theDBOperation.executeSql(label_sql);
           }
          //复制标签文件
          theCopys.copys(root_path+"/label",sdir+"/label");
          */ 
          //预设模板导入
          String sys_template_sql="select * from sys_web_template";
          rs =theDBOperation.getResultSet(sys_template_sql);
          while(rs.next())
           {
            String template_sql="insert into web_template(id,web_project_id,web_template_type_id,title,title_desc,test_desc,content,flag,web_account) values("+rs.getString("id")+","+rs.getString("web_project_id")+","+rs.getString("web_template_type_id")+",'"+rs.getString("title")+"','"+theConvertSql.convertSqlValue(rs.getString("title_desc"))+"','"+theConvertSql.convertSqlValue(rs.getString("test_desc"))+"','"+theConvertSql.convertSqlValue(rs.getString("content"))+"',"+rs.getString("flag")+",'"+web_account+"')";
            theDBOperation.executeSql(template_sql);
           }
           
          //复制模板文件
          theCopys.copys(root_path+"/template",sdir+"/template");
          
          //基础图片  
          theCopys.copys(root_path+"/images",sdir+"/images");   
          
          //基础jive  
          theCopys.copys(root_path+"/managerweb/jive",sdir+"/jive");  
          
          //web-inf 
          theCopys.copys(root_path+"/web_create/WEB-INF",sdir+"/WEB-INF");   
          }
          int web_url_int=web_url.indexOf("http://");
          if(web_url_int>=0)
          web_url=web_url.substring(7,web_url.length());
          ContentToFile theContentToFile=new ContentToFile();
          if(action.equals("update"))
	  {
	   theContentToFile.geneVhostFile(web_account,web_url,"update");
    	  }
    	  else
    	  {
    	   theContentToFile.geneVhostFile(web_account,web_account+(String)MDC.get("web_url"),"add");
	   theContentToFile.geneVhostFile(web_account,web_url,"add");
    	  }
    	  
    	  //首页  
    	WebInfo webInfo=theWebConfig.getWebInfo(web_account);
        String web_title=webInfo.getWeb_name();
        String httpUrl="";
        
        httpUrl=cms_url+web_account+"/template/index.jsp?web_account="+web_account+"&web_title="+web_title;
        String htmlFilePath="";
        String htmlFileName="index.html";
        HtmlGenerater theHtmlGenerater=new HtmlGenerater();
        theHtmlGenerater.geneHtmlFile(web_account,httpUrl,htmlFilePath,htmlFileName); 
    	
    	//rss电子地图
        web_title=webInfo.getWeb_name()+"rss电子地图";
        httpUrl=cms_url+web_account+"/template/rssmap.jsp?web_account="+web_account+"&web_title="+web_title;
        htmlFilePath="rssmap";
        htmlFileName="index.html";
        theHtmlGenerater.geneHtmlFile(web_account,httpUrl,htmlFilePath,htmlFileName); 
        
        //友情链接页
        web_title=webInfo.getWeb_name()+"友情链接页";
        httpUrl=cms_url+web_account+"/template/friend_link.jsp?web_account="+web_account+"&web_title="+web_title;
        htmlFilePath="friend_link";
        htmlFileName="index.html";
        theHtmlGenerater.geneHtmlFile(web_account,httpUrl,htmlFilePath,htmlFileName);     
    	  
          }
          else if(action.equals("delete")){
           theWebConfig.removeWebInfo(web_account);
           String delete_sql[]=new String[14];
           delete_sql[0]="delete from web_info where web_account='"+web_account+"'";
           delete_sql[1]="delete from web_user where web_account='"+web_account+"'";
           delete_sql[2]="delete from bar_menu where web_account='"+web_account+"'";
           delete_sql[3]="delete from web_special where web_account='"+web_account+"'";
           delete_sql[4]="delete from web_project where web_account='"+web_account+"'";
           delete_sql[5]="delete from web_css where web_account='"+web_account+"'";
           delete_sql[6]="delete from web_label where web_account='"+web_account+"'";
           delete_sql[7]="delete from web_template where web_account='"+web_account+"'";
           delete_sql[8]="delete from web_titleinfo where web_account='"+web_account+"'";
           delete_sql[9]="delete from web_imageinfo where web_account='"+web_account+"'";
           delete_sql[10]="delete from web_link where web_account='"+web_account+"'";
           delete_sql[11]="delete from web_timer_task where web_account='"+web_account+"'";
           delete_sql[12]="delete from web_collection where web_account='"+web_account+"'";
           delete_sql[13]="delete from web_project where web_account='"+web_account+"'";
           theDBOperation.executeSqls(delete_sql);
           String sdir=root_path+"/"+web_account;
           File dir=new File(sdir);
           Copys.delFolder(sdir);
          }
          message = "操作成功!";
          }
        else
          message = "操作失败!";
      }
      catch (Exception ex) {
        message = "操作失败!";
        //ex.printStackTrace();
      }
    }
    else
    {
     message="域名"+web_url+"已经被别人使用了，如果域名"+web_url+"是你的，请把域名"+web_url+"解析到111.111.111.111并发邮件到xiantianyou@163.com，请管理员进行确认。";
    }		
%>
<script language="javascript">
	function process ( message,url){
		alert(message);
		self.location = url;
	}
</script>
<html>
	<head>
		<title></title>
	</head>
	<body onload="javascript: process('<%=message%>','<%=url%>');">
	</body>
</html>