using ryCommonDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static ryCommonDb.DataProvider;

namespace 开发辅助工具.Manager
{
    public class Itrycn_Db
    {
        public static bool IsCloseConfirm = true;
        public static string Sys_SQLConn = "<app>\\SysDb\\Tools.dat|";
        public static string User_SQLConn = "<app>\\UserDb\\MyDb.dat|";
        public static ryCommonDb.DataProvider.DataProviderType dataType = ryCommonDb.DataProvider.DataProviderType.SQLiteDataProvider;
        public static IDbInterface CreateDataProvider(DataProviderType dataProviderType)
        {
            switch (dataProviderType)
            {
                //case DataProviderType.OdbcDataProvider:
                //    return new OdbcDataProvider();
                //case DataProviderType.OleDbDataProvider:
                //    return new OleDbDataProvider();
                //case DataProviderType.OracleDataProvider:
                //    return new OracleDataProvider();
                case DataProviderType.SQLiteDataProvider:
                    return new SQLiteDataProvider();
                default:
                    return null;
            }
        }
        public static void DeleteFiles(string dir, string filter)
        {
            var files = System.IO.Directory.GetFiles(dir, filter);
            for (int i = 0; i < files.Length; i++)
            {
                ryCommon.RyFiles.DeleteFile(files[i]);
            }
            ryCommon.RyFiles.DeleteFile(dir + "\\" + filter);
            var dirs = System.IO.Directory.GetDirectories(dir);
            for (int i = 0; i < dirs.Length; i++)
            {
                DeleteFiles(dirs[i], filter);
            }
        }
        public static void CreateSysTable()
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(Sys_SQLConn) == 1)
            {
                #region 工具表
                RyQuickSQL mySQL = new RyQuickSQL("Tools");
                mySQL.AddField("Name", "");
                mySQL.AddField("Des", "");
                mySQL.AddField("Keys", "");
                mySQL.AddField("Addr", "");
                mySQL.AddField("PY", "");
                mySQL.AddField("Sort", 0);
                mySQL.AddField("addTime",DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                //
            }
        }
        public static void CreateUserTable()
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(User_SQLConn) == 1)
            {
                #region Url表
                RyQuickSQL mySQL = new RyQuickSQL("Urls");
                mySQL.AddField("Name", "");
                mySQL.AddField("Url", "");
                mySQL.AddField("Cookie", "");
                mySQL.AddField("Postdata", "");
                mySQL.AddField("Method", "");
                mySQL.AddField("Header", "");
                mySQL.AddField("Referer", "");
                mySQL.AddField("ContentType", "");
                mySQL.AddField("Encoding", "");
                mySQL.AddField("ClickCount", 0);//点击次数
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 启动表
                mySQL.Clear();
                mySQL.TableName = "Starts"; ;
                mySQL.AddField("Addr", "");
                mySQL.AddField("Name", "");
                mySQL.AddField("ClickCount", 0);//点击次数
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 变量表
                mySQL.Clear();
                mySQL.TableName = "VarInfo"; ;
                mySQL.AddField("VarName", "");
                mySQL.AddField("VarValue", "");
                mySQL.AddField("Des", "");//备注
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 设置表
                mySQL.Clear();
                mySQL.TableName = "Settings"; ;
                mySQL.AddField("Name", "");
                mySQL.AddField("Value", "");//值
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
            }
        }
    }
}
