using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using SysCore;

public partial class Root_InstallSQL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        new RootControl().CheckRootLogin();
        if (!Page.IsPostBack)
        {
            InstallSQL();
        }
    }
    public string InstallSQL()
    {
        Server.ScriptTimeout = 600;//600秒防超时
        string txt_filename = Server.MapPath("sql_files.txt");
        StreamReader sr = File.OpenText(txt_filename);
        string sql_filename;
        Response.Write("<div style='color:White;font-size:9pt'>正在升级脚本:</div>");
        string db_name = Request["db_name"].ToString();
        SqlUtil su = new SqlUtil();
        su.SetDatabaseName(db_name);

        while ((sql_filename = sr.ReadLine()) != null)
        {
            if (!sql_filename.Trim().Equals(""))
            {
                string msg = "<div style='color:Yellow;font-size:9pt'>" + sql_filename + "</div>";
                Response.Write(msg);
                Response.Flush();
                StreamReader sr2 = File.OpenText(Server.MapPath("../../Install/Sql/" + sql_filename.Trim()));
                string sql_all = "";
                sql_all = sr2.ReadToEnd();
                string[] arr_sql = sql_all.Split(new string[] { "/**/GO" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string sql in arr_sql)
                {
                    if (!sql.Trim().Equals(""))
                    {
                        try
                        {
                            Response.Flush();
                            su.ExecuteNonQuery(sql, null);
                        }
                        catch (Exception err)
                        {
                            if (err.Message.IndexOf("附近有语法错误") > 0 || err.Message.IndexOf("语句") > 0)
                            {
                                //Response.Write("<b style='color:blue'>" + sql_filename + "</b><br/><br/>");
                                //Response.Write(err.Message + "<br/><br/>");
                                Response.Write("<div style='color:Red;font-size:9pt'>" + err.Message + "</div>");
                            }
                        }
                    }
                }
                sr2.Close();
            }
        }
        sr.Close();
        string db_id = db_name.Replace("xplat", "");
        string tip = "";
        if (Request["create_db"].ToString().Equals("0"))
        {
            tip = "已对[" + db_id + "]账套完成升级操作！";
            Response.Write("<div style='color:White;font-size:9pt'>" + tip + "</div>");
            Response.Flush();
            Response.Write("<script>if(confirm('" + tip + "需要立即返回吗?')){parent.location='Account_List.aspx';}</script>");
        }
        else if (Request["create_db"].ToString().Equals("1"))
        {
            try
            {
                string account_name = Request["account_name"];
                //设置账套名称
                string sql = "update @db_name..System_Config set cf_value='" + account_name + "' where cf_code='account_name';";
                sql = sql.Replace("@db_name", db_name);
                su.ExecuteNonQuery(sql, null);
            }
            catch
            {
            }
            tip = "已成功创建新账套[" + db_id + "]！\n默认登录用户名：manager 密码：manager";
            Response.Write("<div style='color:White;font-size:9pt'>" + tip + "</div>");
            Response.Flush();
            Response.Write("<script>if(confirm('" + tip + "需要立即返回吗?')){parent.location='Account_List.aspx';}</script>");
        }

        return "1";
    }
}