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

namespace VSoft.Prams
{
    public class Itrycn_Db
    {
        public static string SQLConnStr_SysDb = "<app>\\SysDb\\SysConfig.dat";
        public static string SQLConnStr = "<app>\\UserDb\\Softs.dat|";
        public static string SQLConnStr_Logs = "<app>\\UserDb\\logs.dat|";
        public static DataProvider.DataProviderType dataType = 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;
            }
        }
        /// <summary>
        /// 写入设置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetSetting(string _SQLConnStr, string name, string value)
        {
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(_SQLConnStr) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Setting");
                mySQL.AddField("sName", name);
                mySQL.AddField("sValue", value);
                if (db.ExecuteNonQuery(mySQL.GetUpdateSQL() + " where sName=@sName", mySQL) == 0)
                {
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
                }
            }
            db.Free();
        }
        /// <summary>
        /// 写入设置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetSetting(string name, string value)
        {
            SetSetting(SQLConnStr, name, value);
        }
        /// <summary>
        /// 读取设置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="def_value"></param>
        /// <returns></returns>
        public static string GetSetting(string _SQLConnStr, string name, string def_value)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            string _value = def_value;
            if (db.ConnDb(_SQLConnStr) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Setting");
                mySQL.AddField("sName", name);
                DataSet ds = db.ReadData("select * from Setting where sName=@sName", mySQL);
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    _value = reader["sValue"].ToString();
                }
            }
            db.Free();
            return _value;
        }
        /// <summary>
        /// 读取设置
        /// </summary>
        /// <param name="name"></param>
        /// <param name="def_value"></param>
        /// <returns></returns>
        public static string GetSetting(string name, string def_value)
        {
            return GetSetting(SQLConnStr, name, def_value);
        }
        public static int GetColumnCount(IDbInterface db, int ColumnId)
        {
            var ds = db.ReadData("select count(*) from Columns where parentId=" + ColumnId);
            var icount = ds.Tables[0].Rows[0][0].ToInt();
            ds.Dispose();
            return icount;
        }
        public static int GetSoftCount(IDbInterface db, int ColumnId)
        {
            var ds = db.ReadData("select count(*) from Softs where ColumnId=" + ColumnId);
            var icount = ds.Tables[0].Rows[0][0].ToInt();
            ds.Dispose();
            return icount;
        }
        public static void CreateTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 软件表
                RyQuickSQL mySQL = new RyQuickSQL("Softs");
                mySQL.AddField("Name", "");
                mySQL.AddField("CmdId", "");
                mySQL.AddField("SoftType", 0);//0表示运行文件,1表示执行快速命令,2表示执行脚本
                mySQL.AddField("Path", "");
                mySQL.AddField("RunPram", "");
                mySQL.AddField("StartPath", "");//起始路径
                mySQL.AddField("IconPath","");//图标路径
                mySQL.AddField("ClickCount",0);//点击量
                mySQL.AddField("SetJson", "");//设置
                mySQL.AddField("Des", "");//备注
                mySQL.AddField("PinYin", "");//拼音
                mySQL.AddField("ColumnId", 0);//栏目ID
                mySQL.AddField("sortindex", 0);
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 栏目表
                mySQL.Clear();
                mySQL.TableName = "Columns";
                mySQL.AddField("Name", "");
                mySQL.AddField("sortindex", 0);
                mySQL.AddField("depth", 0);//深度,0表示栏目,1表示分类
                mySQL.AddField("parentId", 0);
                mySQL.AddField("Des", "");//备注
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                //
                #region 设置表
                mySQL.Clear();
                mySQL.TableName = "Setting";
                mySQL.AddField("sName", "");
                mySQL.AddField("sValue", "");
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        public static void CreateSysTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                //
                #region 设置表
                RyQuickSQL mySQL = new RyQuickSQL("Setting");
                mySQL.AddField("sName", "");
                mySQL.AddField("sValue", "");
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        public static void CreateTable_Logs(DataProvider.DataProviderType dataType)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(SQLConnStr_Logs) == 1)
            {
                #region 日志表
                RyQuickSQL mySQL = new RyQuickSQL("Logs");
                mySQL.AddField("LogId", "");
                mySQL.AddField("LogStr", "");
                mySQL.AddField("TaskId", 1);
                mySQL.AddField("TaskGUID", "");
                mySQL.AddField("LogResult", "");
                mySQL.AddField("CreateId", 1);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                //
                #region 设置表
                mySQL.Clear();
                mySQL.TableName = "Setting";
                mySQL.AddField("sName", "");
                mySQL.AddField("sValue", "");
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        /// <summary>
        /// 创建数据库
        /// </summary>
        public static void CreateTable()
        {
            CreateTable(dataType, SQLConnStr_SysDb);
            CreateTable(dataType, SQLConnStr);
            CreateTable_Logs(dataType);
            //CreateSysTable(dataType, SQLConnStr_SysDb);
        }
    }
}
