using NAudio.Wave;
using ryCommon;
using ryCommonDb;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static ryCommonDb.DataProvider;

namespace TimeClock
{
    /// <summary>
    /// 提醒分类
    /// </summary>
    public class TipType
    {
        /// <summary>
        /// 每隔几分钟
        /// </summary>
        public const int tip_minute = 0;
        /// <summary>
        /// 每隔几小时
        /// </summary>
        public const int tip_hour = 1;
        /// <summary>
        /// 每隔几天
        /// </summary>
        public const int tip_day = 2;
        /// <summary>
        /// 指定时间
        /// </summary>
        public const int tip_thetime = 3;
        /// <summary>
        /// 每隔几周
        /// </summary>
        public const int tip_week = 4;
        /// <summary>
        /// 每隔几月
        /// </summary>
        public const int tip_month = 5;
        /// <summary>
        /// 每隔几年
        /// </summary>
        public const int tip_year = 6;
        /// <summary>
        /// 系统启动时
        /// </summary>
        public const int tip_winstart = 7;
    }
    /// <summary>
    /// 配置类(本系统基于乘黄V1架构)
    /// </summary>
    public class Itrycn_Info
    {

        public static void LoadSetting()
        {
            //ryCommon.Storage Stor = new Storage();
            //Stor.SelectNodeBySet();
            //LowCountTip = Stor.GetAttrValue("LowCountTip", true);
            //low_count = Stor.GetAttrValue("LowCount", 10);
        }
        public static bool FullScreenTip = true;
        public static int AutoCloseMinute = 0;
        /// <summary>
        /// 允许整点报时的时间段,共24位,表示24小时，第0位表示0点是否报时(1表示报时，0表示不报时),以此类推
        /// </summary>
        public static string Hours_On = "";
        /// <summary>
        /// 整点报时开关
        /// </summary>
        public static bool HourlyChime_On = true;
        /// <summary>
        /// 使用明星报时
        /// </summary>
        public static bool UseStarHourlyChime = true;
        /// <summary>
        /// 使用24小时报时，而不是使用12小时制报时
        /// </summary>
        public static bool Use24Hours = true;
        /// <summary>
        /// 是否开启休息提醒
        /// </summary>
        public static bool RestTip_On = false;
        /// <summary>
        /// 休息时间间隔,单位分钟
        /// </summary>
        public static int RestInteval = 50;
        /// <summary>
        /// 休息时间
        /// </summary>
        public static int Rest = 3;
        /// <summary>
        /// 现在是否在休息
        /// </summary>
        public static bool IsRestNowing =false;


        public static bool TipBeforeRest =true;
        public static bool SoundBeforeRest = true;
        /// <summary>
        /// 是否允许延迟休息
        /// </summary>
        public static bool DelayRestOn = true;
        /// <summary>
        /// 最大延迟休息次数
        /// </summary>
        public static int DelayRestCount = 3;
        /// <summary>
        /// 当前延迟次数
        /// </summary>
        public static int DelayRestIndex = 0;
        /// <summary>
        /// 下一次休息时间
        /// </summary>
        public static DateTime NextRestTime2 = DateTime.Now.AddHours(1);
        /// <summary>
        /// 电脑休息剩余秒数
        /// </summary>
        public static Int64 Timer_index = 0;
        /// <summary>
        /// 全屏程序时停止计时
        /// </summary>
        public static bool StopRestByFullScreen = true;
        /// <summary>
        /// 空闲时停止计时
        /// </summary>
        public static bool StopRestByIdle = true;
        /// <summary>
        /// 空闲时间
        /// </summary>
        public static int IdleMinute = 3;
        /// <summary>
        /// 空闲时间超过后置0的分钟数
        /// </summary>
        public static int IdleOverMinute = 10;
        /// <summary>
        /// 空闲时间超过后置0
        /// </summary>
        public static bool IdleOver_On = true;
        /// <summary>
        /// 休息模式,0表示1分钟后可解锁,1表示不可解锁
        /// </summary>
        public static int RestMode =0;
        /// <summary>
        /// 朗读者
        /// </summary>
        public static string Speaker ="";
        /// <summary>
        /// 朗读音量
        /// </summary>
        public static int SpeakerVolume = 0;
        /// <summary>
        /// 朗读速度
        /// </summary>
        public static int SpeakerRate = 0;
        /// <summary>
        /// 休息时是不是播放音乐
        /// </summary>
        public static bool RestMusic_On =false;
        /// <summary>
        /// 音乐类型,0表示文件,1表示文件夹
        /// </summary>
        public static int MusicType = 0;
        /// <summary>
        /// 音乐路径
        /// </summary>
        public static string MusicPath = "";
        /// <summary>
        /// 保存时间进度
        /// </summary>
        public static bool SaveSchedule = true;
        /// <summary>
        /// 当前开始计算的周数
        /// </summary>
        public static DateTime CurrWeekDate =DateTime.Now;
        public static IntPtr TipViewHandle = IntPtr.Zero;
        /// <summary>
        /// 是否计算工作信息,例如闲置和工作时间占比
        /// </summary>
        public static bool CalcWorkInfo = false;
        /// <summary>
        /// 音乐音量
        /// </summary>
        public static int MusicVolume = 0;
        /// <summary>
        /// 休息界面透明度
        /// </summary>
        public static int RestOpacity =90;

        /// <summary>
        /// 现在是否正在停止计时
        /// </summary>
        public static bool IsStopCalcRestNowing = false;
        /// <summary>
        /// 停止计时的原因
        /// </summary>
        public static string IsStopCalcRestReason = "";
        public static ArrayList StickyList = new ArrayList();
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="path"></param>
        public static void PlaySound(string path)
        {
            List<string> list = new List<string>
            {
                 path
            };
            PlaySound(list);
        }
        /// <summary>
        /// 报时
        /// </summary>
        public static void PlayHours(bool IsHour)
        {
            bool canPlay = false;
            int hour = DateTime.Now.Hour;
            if (IsHour)
            {
                if (Itrycn_Info.Hours_On == "") { canPlay = true; }
                else if (Itrycn_Info.Hours_On.Length == 24 && Itrycn_Info.Hours_On.Substring(hour, 1) == "1") { canPlay = true; }
            }
            else { canPlay = true; }
            if (!canPlay) { return; }
            bool have_star_sound = false;
            string path = "";
            if (Itrycn_Info.UseStarHourlyChime)//如果优先使用明星报时
            {
                #region 去数据库查询是否还有明星报时
                DataProvider mydb = new DataProvider();
                IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
                if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
                {
                    DataSet ds = db.ReadData("select * from HourlyChime where TimeStr='" + DateTime.Now.ToString("HHmm") + "' ORDER BY RANDOM() limit 1");
                    if (mydb.HaveData(ds))
                    {
                        DataRow row = ds.Tables[0].Rows[0];
                        path = row["Path"].ToString();
                        have_star_sound = true;
                    }
                }
                db.Free();
                #endregion
            }
            if (have_star_sound)//如果有明星声音文件
            {
                Itrycn_Info.PlaySound(path.Replace("<app>", Application.StartupPath));
            }
            else
            {
                int minute = DateTime.Now.Minute;
                string sound_folder = Itrycn_Info.SysDbFolder + @"\animation\Hours";
                List<string> list_sound = new List<string>();
                if (Itrycn_Info.Use24Hours)
                {
                    #region 24小时报时
                    sound_folder = sound_folder + "\\24hours";
                    list_sound.Add(sound_folder + "\\now.wav");
                    AddIntSounds(hour);
                    list_sound.Add(sound_folder + "\\Point.wav");
                    if (minute == 0) { list_sound.Add(sound_folder + "\\z.wav"); }
                    else
                    {
                        AddIntSounds(minute); list_sound.Add(sound_folder + "\\m.wav");
                    }
                    #endregion
                }
                else
                {
                    #region 12小时报时
                    sound_folder = sound_folder + "\\12hours";
                    list_sound.Add(sound_folder + "\\now.wav");
                    if (hour <= 5) { list_sound.Add(sound_folder + "\\dm.wav"); }
                    else if (hour >= 6 && hour <= 11) { list_sound.Add(sound_folder + "\\am.wav"); }
                    else if (hour >= 12 && hour <= 17) { list_sound.Add(sound_folder + "\\pm.wav"); }
                    else if (hour >= 18 && hour <= 23) { list_sound.Add(sound_folder + "\\em.wav"); }
                    if (hour <= 12)
                    {
                        AddIntSounds(hour);
                    }
                    else
                    {
                        AddIntSounds(hour - 12);
                    }
                    list_sound.Add(sound_folder + "\\Point.wav");
                    if (minute == 0) { list_sound.Add(sound_folder + "\\z.wav"); }
                    else
                    {
                        AddIntSounds(minute); list_sound.Add(sound_folder + "\\m.wav");
                    }
                    #endregion
                }
                Itrycn_Info.PlaySound(list_sound);
                void AddIntSounds(int num) //根据数字添加声音文件
                {
                    if (num < 0 && num > 99) { return; }
                    int ten = num / 10;//取十位数
                    int gewei = num % 10;//取个位数
                    if (ten == 0) { list_sound.Add(sound_folder + "\\" + gewei + ".wav"); }
                    else
                    {
                        if (ten > 1)
                        {
                            list_sound.Add(sound_folder + "\\" + ten + ".wav");
                        }
                        list_sound.Add(sound_folder + "\\10.wav");
                        if (gewei != 0) { list_sound.Add(sound_folder + "\\" + gewei + ".wav"); }
                    }
                }
            }
        }
        /// <summary>
        /// 将报时文件索引到数据库
        /// </summary>
        public static void AddSoundToDb()
        {
            string sound_folder = Itrycn_Info.SysDbFolder + @"\animation\Hours";
            if (!System.IO.Directory.Exists(sound_folder)) { return; }
            string[] folders = System.IO.Directory.GetDirectories(sound_folder);
            if (folders == null) { return; }

            #region 将报时文件索引到数据库
            DataProvider mydb = new DataProvider();
            IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
            if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
            {
                db.ExecuteNonQuery("delete from HourlyChime");
                db.BeginTransaction();
                for (int i = 0; i < folders.Length; i++)
                {
                    string folder_name = System.IO.Path.GetFileName(folders[i]).ToLower();
                    if(folder_name== "12hours" || folder_name == "24hours") { continue; }
                    string[] file_list = System.IO.Directory.GetFiles(folders[i]);
                    if (file_list == null) { continue; }
                    for (int n = 0; n < file_list.Length; n++)
                    {
                        string ext = System.IO.Path.GetExtension(file_list[n]).ToLower();
                        if(ext!=".mp3" && ext != ".wav") { continue; }
                        string filename = System.IO.Path.GetFileNameWithoutExtension(file_list[n]).ToLower();
                        if(filename.Length<=2 && filename.ToInt().IsInRange(0,23))
                        {
                            RyQuickSQL mySQL = new RyQuickSQL("HourlyChime");
                            mySQL.AddField("TimeStr", filename.ToInt().ToString("00")+ "00");
                            mySQL.AddField("Path", file_list[n].Replace(Application.StartupPath,"<app>"));
                            db.ExecuteNonQuery(mySQL.GetInsertSQL(),mySQL);
                        }
                    }
                }
                db.Commit();
            }
            db.Free();
            #endregion
        }
        public static string GetRealPath(string path)
        {
            return RyFiles.GetRealPath(path).Replace("<img>", Itrycn_Info.UserDbFolder + "\\Images").Replace("<sound>", Itrycn_Info.UserDbFolder + "\\Sound");
        }
        public static string GetRelativePath(string path)
        {
            return RyFiles.GetRelativePath(path).Replace(Itrycn_Info.UserDbFolder + "\\Images","<img>").Replace(Itrycn_Info.UserDbFolder + "\\Sound", "<sound>");
        }
        /// <summary>
        /// 系统数据文件夹
        /// </summary>
        public static string SysDbFolder { get; set; } = Application.StartupPath+"\\SysDb";
        /// <summary>
        /// 用户数据文件夹
        /// </summary>
        public static string UserDbFolder { get; set; } = Application.StartupPath + "\\UserDb";
        /// <summary>
        /// 播放音乐列表
        /// </summary>
        /// <param name="list_path"></param>
        public static void PlaySound(List<string> list_path)
        {            
            Thread th = new Thread(Thread_Play);
            th.Start();
            void Thread_Play()
            {
                try
                {
                    API.SoundPlay sound = new API.SoundPlay
                    {
                        Volume = Itrycn_Info.MusicVolume / 100f
                    };
                    for (int i = 0; i < list_path.Count; i++)
                    {
                        string _path = ryCommon.RyFiles.GetRealPath(list_path[i]);
                        if (_path.IndexOf("\\") == -1) //如果只是文件名，则播放默认目录下的
                        {
                            _path = Itrycn_Info.SysDbFolder + @"\animation\sound\" + _path;
                        }
                        if (System.IO.File.Exists(_path))
                        {
                            sound.Play(_path);
                        }
                    }
                }
                catch
                {
                }
            }
        }
        public static void ReadSetting()
        {
            ReadSetting(true);
        }
        public static void ReadSetting(bool UpdateRestDb)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
            string xml = "";
            if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
            {
                DataSet ds = db.ReadData("select * from Setting where sName='Setting'");
                if (mydb.HaveData(ds))
                {
                    DataRow reader = ds.Tables[0].Rows[0];
                    xml = reader["sValue"].ToString();
                }
            }
            db.Free();
            ryCommon.Storage tStor = new Storage(xml);
            tStor.SelectNodeBySet();
            FullScreenTip = tStor.GetAttrValue("FullScreenTip", true);
            if (tStor.GetAttrValue("AutoClose", false))
            {
                AutoCloseMinute = tStor.GetAttrValue("AutoCloseMinute", 0);
            }
            else
            {
                AutoCloseMinute = -1;
            }
            CurrWeekDate = tStor.GetAttrValue("CurrDate", DateTime.Now);
            Hours_On = tStor.GetAttrValue("HourOn", "");
            HourlyChime_On = tStor.GetAttrValue("HourlyChime_On",true);
            UseStarHourlyChime = tStor.GetAttrValue("UseStarHourlyChime", true);
            Use24Hours = tStor.GetAttrValue("Use24Hours", true);
            RestTip_On = tStor.GetAttrValue("RestTip_On", false);
            RestInteval = tStor.GetAttrValue("RestInteval", 50);
            Rest = tStor.GetAttrValue("Rest", 3);
            TipBeforeRest = tStor.GetAttrValue("TipBeforeRest", true);
            SoundBeforeRest = tStor.GetAttrValue("SoundBeforeRest", true);
            DelayRestOn = tStor.GetAttrValue("DelayRestOn", true);
            DelayRestCount = tStor.GetAttrValue("DelayRestCount", 3);
            StopRestByFullScreen = tStor.GetAttrValue("StopRestByFullScreen", true);
            StopRestByIdle = tStor.GetAttrValue("StopRestByIdle", true);
            IdleMinute = tStor.GetAttrValue("IdleMinute", 3);
            IdleOverMinute = tStor.GetAttrValue("IdleOverMinute", 10);
            IdleOver_On= tStor.GetAttrValue("IdleOver_On", true);
            RestMode = tStor.GetAttrValue("RestMode", 0);
            Speaker = tStor.GetAttrValue("Speaker", "");
            SpeakerVolume = tStor.GetAttrValue("SpeakerVolume", 100);
            SpeakerRate = tStor.GetAttrValue("SpeakerRate", 1);
            RestMusic_On = tStor.GetAttrValue("RestMusic_On", false);
            MusicType = tStor.GetAttrValue("MusicType", 0);
            MusicPath = tStor.GetAttrValue("MusicPath", "");
            SaveSchedule = tStor.GetAttrValue("SaveSchedule", true);
            MusicVolume = tStor.GetAttrValue("MusicVolume", 100);
            RestOpacity = tStor.GetAttrValue("RestOpacity", 90);
            if (RestTip_On && UpdateRestDb)
            {
                InitRest();
            }
        }
        /// <summary>
        ///获取指定日期节假日数据sql语句
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetHolidaySQL(DateTime dt)
        {
            #region 设置搜索条件
            string sql_where = "";
            int week = RyDate.GetWeek_index(dt);
            int week_num = (dt.Day / 7) + 1;
            if (dt.Day % 7 == 0) { week_num--; }
            string time1 = dt.Month.ToString("00") + week_num.ToString() + week.ToString();
            sql_where += "(sType=2 and Holiday_Time='" + time1 + "')";
            if (dt.AddDays(7).Month != dt.Month)
            {
                var time2 = dt.Month.ToString("00") + "0" + week.ToString();
                sql_where += " or (sType=2 and Holiday_Time='" + time2 + "')";
            }
            if (dt.Day == (new DateTime(dt.Year, dt.Month, 1)).AddMonths(1).AddDays(-1).Day)
            {
                sql_where += " or (sType=0 and Holiday_Time='" + dt.Month.ToString("00") + "00')";
            }
            sql_where += " or (sType=0 and Holiday_Time='" + dt.Month.ToString("00") + dt.Day.ToString("00") + "')";
            ChinaDate.GetLunarFromDay(dt.Year, dt.Month, dt.Day, out int LunarYear, out int LunarMonth, out int LunarDay, out bool IsLeapMonth);
            if (!IsLeapMonth)
            {
                sql_where += " or (sType=1 and Holiday_Time='" + LunarMonth.ToString("00") + LunarDay.ToString("00") + "')";//农历
            }
            sql_where = "(" + sql_where + ") and (" + dt.Year + ">=startYear and " + dt.Year + "<=endYear)";
            return "select * from Holidays where " + sql_where + " order by Sorts desc";
            #endregion
        }
        /// <summary>
        /// 初始化休息提醒时间
        /// </summary>
        public static void InitRest()
        {
            if (Itrycn_Info.RestTip_On)
            {
                Timer_index = RestInteval * 60;
                DelayRestIndex = 0;
                Itrycn_Info.IsRestNowing = false;                
            }
        }
        public static DateTime soft_startTime = DateTime.Now;
    }
    public class Itrycn_Db
    {
        /// <summary>
        /// 分组图标
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Bitmap GetGroupImg(string path)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream sm = assembly.GetManifestResourceStream("TimeClock.images.group." + path + ".png");
            return new Bitmap(sm);
        }
        /// <summary>
        /// 获取皮肤图标
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Bitmap GetSkinImg(string path)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream sm = assembly.GetManifestResourceStream("TimeClock.images.skin." + path + ".png");
            return new Bitmap(sm);
        }
        public static string SQLConnStr = "<app>\\UserDb\\TimeTip.dat|";
        public static string Logs_SQLConnStr = "<app>\\UserDb\\logs.dat|";
        public static string WorkInfo_ConnStr = "<app>\\UserDb\\WorkInfo.dat|";
        public static string Holidays_ConnStr = "<app>\\SysDb\\Holidays.dat|";
        public static List<RestInfo> list_rest = new List<RestInfo>();
        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;
            }
        }
        public static void SetSetting(string name,string value)
        {
            DataProvider mydb = new DataProvider();
            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();
        }
        public static string GetSetting(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;
        }
        public static DateTime GetNextTipTime(DataRow reader)
        {
            return GetNextTipTime(reader,DateTime.Now);
        }
        /// <summary>
        /// 获取该月的第几个星期几
        /// </summary>
        /// <param name="month_start">该月第一天日期</param>
        /// <param name="weektype">为false表示第几个周几,为true表示第几个星期的周几</param>
        /// <param name="weekindex">第几个周几或第几个星期的周几,范围为1-4或-1</param>
        /// <param name="week">周几，范围为1-7，表示周一到周日</param>
        /// <param name="OK">表示是否返回成功,成功则返回true,否则为false</param>
        /// <returns>返回具体日期</returns>
        private static DateTime GetWeekIndexTime(DateTime month_start,bool weektype, int weekindex,int week,out bool OK)
        {
            OK = false;
            DateTime dt;
            var month_start_week = RyDate.GetWeek_index(month_start);
            if (!weektype)
            {
                #region 方式1，第几个星期几
                if (weekindex > 0)
                {
                    if (month_start_week <= week)
                    {
                        dt = month_start.AddDays((weekindex - 1) * 7 + week - month_start_week);
                    }
                    else { dt = month_start.AddDays(weekindex * 7 + week - month_start_week); }
                }
                else //最后一个星期几
                {
                    var month_end = month_start.AddMonths(1).AddDays(-1);
                    var month_end_week = RyDate.GetWeek_index(month_end);
                    if (month_end_week >= week)
                    {
                        dt = month_end.AddDays(week - month_end_week);
                    }
                    else
                    {
                        dt = month_end.AddDays(week - month_end_week - 7);
                    }
                }
                //如果算出的结果在同一个月,则表示计算正确
                if (RyDate.InSameMonth(dt, month_start)) { OK = true; }
                #endregion
            }
            else
            {
                #region 方式2,第几个星期的周几
                if (weekindex > 0)
                {
                    var month_start_weekstart = RyDate.GetWeekInfo(month_start).startDate;
                    dt = month_start_weekstart.AddDays(week - 1 + (weekindex - 1) * 7);
                }
                else
                {
                    var month_end = RyDate.GetWeekInfo(month_start.AddMonths(1).AddDays(-1)).startDate;
                    dt = month_end.AddDays(week - 1);
                }
                OK = true;
                #endregion
            }
            return dt.Date.AddHours(month_start.Hour).AddMinutes(month_start.Minute) ;
        }
        public static DateTime GetNextTipTime(DataRow reader,DateTime dt_Now)
        {
            DateTime StartTime = reader["StartTime"].ToDateTime();
            DateTime dt = dt_Now;
            int interval = reader["Interval"].ToInt();
            int value = 0;
            switch (reader["modtype"].ToInt())
            {
                case TipType.tip_minute://每隔几分钟
                    value = (dt_Now - StartTime).TotalMinutes.ToInt();
                    if (value < 0) { return StartTime; }
                    dt = StartTime.AddMinutes(value - value % interval);
                    if (dt <= dt_Now)
                    { return dt.AddMinutes(interval); }
                    else
                    { return dt; }
                case TipType.tip_hour://每隔几小时
                    value = (dt_Now - StartTime).TotalHours.ToInt();
                    if (value < 0) { return StartTime; }
                    dt = StartTime.AddHours(value - value % interval);
                    if (dt <= dt_Now)
                    { return dt.AddHours(interval); }
                    else
                    { return dt; }
                case TipType.tip_day://每隔天
                    value = (dt_Now.Date - StartTime.Date).TotalDays.ToInt();
                    if (value < 0) { return StartTime; }//如果开始时间在未来，则直接取开始时间
                    dt = StartTime.AddDays(value - (value % interval));
                    if (dt <= dt_Now)
                    { return dt.AddDays(interval); }
                    else
                    { return dt; }
                case TipType.tip_week://每隔周
                    #region 每隔周
                    int week_index = ryCommon.RyDate.GetWeek_index(dt_Now);//获取当前是周几
                    int StartTime_week_index = ryCommon.RyDate.GetWeek_index(StartTime);
                    ryCommon.Storage tStor = new Storage(reader["SetXML"].ToString());
                    tStor.SelectNodeBySet();
                    string week_str = tStor.GetAttrValue("week");//周一到周日,如果有提醒就为1，没有则为0，比如周四提醒,则为0001000
                    if (week_str.Length != 7) { return dt_Now.AddDays(-100); }
                    value = ryCommon.RyDate.GetWeekCount(StartTime, dt_Now);
                    if (value < 0) { value = 0; }
                    dt = ryCommon.RyDate.GetWeekInfo(StartTime).startDate.AddDays((value - (value % interval)) * 7);
                    if (ryCommon.RyDate.InSameWeek(dt_Now, dt))//如果在同一个星期
                    {
                        DateTime nextTime = getNextDay(dt_Now);
                        if (ryCommon.RyDate.InSameDay(nextTime, dt_Now))
                        {
                            if (StartTime.TimeOfDay <= dt_Now.TimeOfDay)//时间大于现在，则当天提醒,否则循环到下一个提醒周期提醒
                            {
                                nextTime = getNextDay(dt_Now.AddDays(1));
                            }
                        }
                        return nextTime.Date.AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                    }
                    else
                    {
                        if (dt < ryCommon.RyDate.GetWeekInfo(dt_Now).startDate)
                        {
                            dt = dt.AddDays(7 * interval);
                        }
                        DateTime nextTime = DateTime.Now.AddDays(-100);
                        if (StartTime > dt_Now)
                        {
                            nextTime = getNextDay(StartTime);
                        }
                        else
                        {
                            nextTime = getNextDay(dt);
                        }
                        return nextTime.Date.AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                    }
                    DateTime getNextDay(DateTime dt_info)
                    {
                        int _week_index = ryCommon.RyDate.GetWeek_index(dt_info);
                        int pos = week_str.IndexOf("1", _week_index - 1);
                        if (pos >= 0)
                        {
                            return dt_info.AddDays(pos - _week_index + 1);
                        }
                        else
                        {
                            pos = week_str.IndexOf("1");
                            if (pos >= 0)
                            {
                                return dt_info.AddDays(pos - _week_index + 1).AddDays(7 * interval);
                            }
                        }
                        return dt_Now.AddDays(-100);
                    }
                #endregion
                case TipType.tip_month:
                    #region 每隔月
                    ryCommon.Storage tStor2 = new Storage(reader["SetXML"].ToString());
                    tStor2.SelectNodeBySet();
                    var month_mode = tStor2.GetAttrValue("month_mode", 0);
                    value = ryCommon.RyDate.GetMonthCount(StartTime, dt_Now);
                    if (value < 0) { value = 0; }
                    dt = RyDate.GetMonthStart(StartTime).AddMonths(value - (value % interval)).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                    if (month_mode == 0) //按日
                    {
                        #region 每月几号的提醒
                        int day = tStor2.GetAttrValue("day", 0);
                        bool revday = tStor2.GetAttrValue("revday", false);//是否倒数
                        if (revday)
                        { dt = dt.AddMonths(1).AddDays(-day - 1); }
                        else
                        {
                            dt = dt.AddDays(day);
                        }
                        if (dt <= dt_Now)
                        {
                            if (revday)
                            {
                                dt = RyDate.GetMonthStart(dt).AddMonths(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                return dt.AddMonths(1).AddDays(-day - 1);
                            }
                            else
                            {
                                return dt.AddMonths(interval);
                            }
                        }
                        else
                        {
                            if (dt < StartTime)
                            {
                                return dt.AddMonths(interval);
                            }
                            return dt;
                        } 
                        #endregion
                    }
                    else //按周
                    {
                        var week_m = tStor2.GetAttrValue("week_m", 1); //表示星期,值为1-7，表示周一到周日
                        if (!week_m.IsInRange(1, 7)) { week_m = 1; }
                        var weekindex_m = tStor2.GetAttrValue("weekindex_m", 1);//第几个星期,值为1-4或-1，表示第1-4个星期，或最后一个星期
                        if (!weekindex_m.IsInRange(1, 4) && weekindex_m != -1) { weekindex_m = 1; }
                        var weektype_m = tStor2.GetAttrValue("weektype_m", false);
                        var month_start = RyDate.GetMonthStart(dt).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                        dt= GetWeekIndexTime(month_start, weektype_m, weekindex_m, week_m,out bool OK);
                        if(OK)
                        {
                            if (dt <= dt_Now)
                            {
                               dt= GetWeekIndexTime(month_start.AddMonths(interval), weektype_m, weekindex_m, week_m, out _);
                            }
                            else
                            {
                                if (dt < StartTime)
                                {
                                    dt= GetWeekIndexTime(month_start.AddMonths(interval), weektype_m, weekindex_m, week_m, out _);
                                }
                                return dt;
                            }
                        }
                        else
                        {
                            dt = GetWeekIndexTime(month_start.AddMonths(interval), weektype_m, weekindex_m, week_m, out OK);
                        }
                        return dt;
                    }
                #endregion
                case TipType.tip_year:
                    #region 每隔年
                    ryCommon.Storage tStor3 = new Storage(reader["SetXML"].ToString());
                    tStor3.SelectNodeBySet();
                    int IsLunar = tStor3.GetAttrValue("IsLunar", 0);
                    int Month_y = tStor3.GetAttrValue("Month", 0);
                    int day_y = tStor3.GetAttrValue("day", 0);
                    bool revday_y = tStor3.GetAttrValue("revday", false);//是否倒数
                    if (IsLunar == 0) //阳历
                    {
                        #region 阳历提醒
                        value = dt_Now.Year - StartTime.Year;
                        if (value < 0) { value = 0; }
                        dt = RyDate.GetMonthStart(StartTime).AddYears(value - (value % interval)).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                        dt = dt.AddMonths(-dt.Month + 1 + Month_y);
                        int icount = 0;
                        while (true)
                        {
                            icount++;
                            if (day_y < dt.AddMonths(1).AddDays(-1).Day) //如果天数小于当月天数，则计算，否则使用下个循环年
                            {
                                if (revday_y)
                                { dt = dt.AddMonths(1).AddDays(-day_y - 1); }
                                else
                                {
                                    dt = dt.AddDays(day_y);
                                }
                                if (dt < dt_Now)
                                {
                                    if (revday_y)
                                    {
                                        dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                        dt= dt.AddMonths(1).AddDays(-day_y - 1);
                                    }
                                    else
                                    {
                                        dt= dt.AddYears(interval);
                                    }
                                }
                                else
                                {
                                    return dt;
                                }
                                break;
                            }
                            else
                            {
                                if (icount >= 5) { return "2100-1-1".ToDateTime(); }//超过重试次数,则直接返回最大提醒值
                                if (revday_y)
                                {
                                    dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                    dt = dt.AddMonths(1).AddDays(-day_y - 1);
                                }
                                else
                                {
                                    dt = dt.AddYears(interval);
                                }
                            }
                        }
                        if (dt <= dt_Now)
                        {
                            if (revday_y)
                            {
                                dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                dt = dt.AddMonths(1).AddDays(-day_y - 1);
                            }
                            else
                            {
                                dt = dt.AddYears(interval);
                            }
                            return dt;
                        }
                        else
                        {
                            if (dt < StartTime)
                            {
                                if (revday_y)
                                {
                                    dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                    dt = dt.AddMonths(1).AddDays(-day_y - 1);
                                }
                                else
                                {
                                    dt = dt.AddYears(interval);
                                }
                                return dt;
                            }
                            return dt;
                        } 
                        #endregion
                    }
                    else //农历
                    {
                        #region 农历提醒
                        ChinaDate.GetLunarFromDay(StartTime.Year, StartTime.Month, StartTime.Day, out int LunarYear, out int LunarMonth, out int LunarDay, out bool IsLeapMonth);
                        ChinaDate.GetLunarFromDay(dt_Now.Year, dt_Now.Month, dt_Now.Day, out int LunarYear_Now, out int LunarMonth_Now, out int LunarDay_Now, out bool IsLeapMonth_Now);
                        value = LunarYear_Now - LunarYear;
                        if (value < 0) { value = 0; }
                        DateTime StartTime_lunar = new DateTime(LunarYear, LunarMonth, LunarDay, StartTime.Hour, StartTime.Minute,0);
                        dt = RyDate.GetMonthStart(StartTime_lunar).AddYears(value - (value % interval)).AddHours(StartTime_lunar.Hour).AddMinutes(StartTime_lunar.Minute);
                        dt = dt.AddMonths(-dt.Month + 1 + Month_y);
                        int icount = 0;
                        while (true)
                        {
                            icount++;
                            int days= ChinaDate.GetLunarMonthDays(dt.Year, dt.Month, false);
                            if (day_y < days) //如果天数小于当月天数，则计算，否则使用下个循环年
                            {
                                bool yl_date = false;
                                int ayear;int amonth; int aday;
                                if (revday_y)
                                {
                                    yl_date = ChinaDate.GetDayFromLunar(dt.Year, dt.Month, days - day_y,false,out ayear,out amonth,out aday);
                                }
                                else
                                {
                                    yl_date = ChinaDate.GetDayFromLunar(dt.Year, dt.Month, day_y+1, false, out ayear, out amonth, out aday);
                                }
                                if (yl_date)
                                {
                                    DateTime dt_2 = new DateTime(ayear, amonth, aday, dt.Hour, dt.Minute, dt.Second);
                                    if (dt_2 < dt_Now)
                                    {
                                        if (revday_y)
                                        {
                                            dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                            dt = dt.AddMonths(1).AddDays(-day_y - 1);
                                        }
                                        else
                                        {
                                            dt = dt.AddYears(interval);
                                        }
                                    }
                                    else
                                    {
                                        return dt_2;
                                    }
                                }
                                else { return new DateTime(2100, 1, 1); }
                            }
                            else
                            {
                                if (icount >= 5) { return new DateTime(2100, 1, 1); }//超过重试次数,则直接返回最大提醒值
                                if (revday_y)
                                {
                                    dt = RyDate.GetMonthStart(dt).AddYears(interval).AddHours(StartTime.Hour).AddMinutes(StartTime.Minute);
                                    dt = dt.AddMonths(1).AddDays(-day_y - 1);
                                }
                                else
                                {
                                    dt = dt.AddYears(interval);
                                }
                            }
                        }
                        #endregion
                    }
                #endregion
                case TipType.tip_thetime:
                    return reader["RunTime"].ToDateTime();
                case TipType.tip_winstart:
                    DateTime dt_3= Itrycn_Info.soft_startTime.AddMinutes(interval);
                    dt_3 = dt_3.Date.AddHours(dt_3.Hour).AddMinutes(dt_3.Minute+1);
                    return dt_3;
                default:
                    return StartTime;
            }
        }
        /// <summary>
        /// 下次提醒时间，如果有例外免打扰,则显示排除后的时间。
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static DateTime GetNextTipTime2(DataRow reader,DateTime dt_now)
        {
            var nexttiptime = GetNextTipTime(reader,dt_now);
            var ExceptXML = reader["ExceptXML"].ToString();
            if (ExceptXML == "") {return nexttiptime; }
            var isTemp = reader["TempTip"].ToInt();
            int iindex = 0;
            while (iindex<=7)
            {
                iindex++;
                if (!CanTip(nexttiptime, isTemp, reader["ExceptXML"].ToString()))
                {
                    nexttiptime = GetNextTipTime(reader, nexttiptime);
                    continue;
                }
                break;
            }
            return nexttiptime;
        }
        private static string[] week_day = { "日", "一", "二", "三", "四", "五", "六", "日" };
        public static string GetNextTipTimeStr(DateTime dt, bool convert_day)
        {
            if(dt<DateTime.Now)
            {
                return "已执行完毕";
            }
            string dt_str = dt.ToString("yyyy-MM-dd dddd HH:mm");
            if (dt.Date == DateTime.Now.Date) { dt_str = "今天 " + dt.ToString("HH:mm"); }
            else if (dt.Date == DateTime.Now.Date.AddDays(1)) { dt_str = "明天 " + dt.ToString("HH:mm"); }
            else if (dt.Date == DateTime.Now.Date.AddDays(2)) { dt_str = "后天 " + dt.ToString("HH:mm"); }
            else if (dt.Date == DateTime.Now.Date.AddDays(-1)) { dt_str = "昨天 " + dt.ToString("HH:mm"); }
            else if (dt.Date == DateTime.Now.Date.AddDays(-2)) { dt_str = "前天 " + dt.ToString("HH:mm"); }
            else
            {
                var weekinfo1 = ryCommon.RyDate.GetWeekInfo(dt);
                var weekinfo2 = ryCommon.RyDate.GetWeekInfo(DateTime.Now);
                if (weekinfo1.startDate == weekinfo2.startDate)
                {
                    dt_str = "本周" + week_day[dt.DayOfWeek.ToInt()] + " " + dt.ToString("HH:mm");
                }
                else if (weekinfo1.startDate == weekinfo2.startDate.AddDays(7))
                {
                    dt_str = "下周" + week_day[dt.DayOfWeek.ToInt()] + " " + dt.ToString("HH:mm");
                }
            }
            var timespan = dt - DateTime.Now;
            if (convert_day)
            {
                var TotalDays = timespan.TotalDays.ToInt();
                if (TotalDays >= 2)
                {
                    dt_str += "(还有" + TotalDays + "天)";
                }
                else if (TotalDays <= -2)
                {
                    dt_str += "(已过去" + -TotalDays + "天)";
                }
            }
            var TotalMinutes = timespan.TotalMinutes.ToInt();
            if (TotalMinutes < 60 && TotalMinutes >= 0)
            {
                if (TotalMinutes < 1)
                {
                    dt_str += "(不足一分钟)";
                }
                else
                {
                    dt_str += "(还有" + TotalMinutes + "分钟)";
                }
            }
            else if (TotalMinutes < 600)
            {
                var Hour = TotalMinutes / 60;
                var Minute = TotalMinutes % 60;
                if (TotalMinutes < 0 && TotalMinutes >= -600)
                {
                    if (Minute == 0)
                    { dt_str += "(已过去" + -Hour + "小时)"; }
                    else
                    {
                        dt_str += "(已过去" + -Hour + "小时" + -Minute + "分钟)";
                    }
                }
                else if (TotalMinutes > 0)
                {
                    if (Minute == 0)
                    { dt_str += "(还有" + Hour + "小时)"; }
                    else
                    {
                        dt_str += "(还有" + Hour + "小时" + Minute + "分钟)";
                    }
                }
            }
            return dt_str;
        }
        /// <summary>
        /// 例外免打扰
        /// </summary>
        /// <param name="dt_now"></param>
        /// <param name="isTemp"></param>
        /// <param name="ExceptXML"></param>
        /// <returns></returns>
        public static bool CanTip(DateTime dt_now, int isTemp, string ExceptXML)
        {
            #region 例外免打扰
            bool canTip = true;
            if (ExceptXML != "" && isTemp != 1)
            {
                ryCommon.Storage myExcept_XML = new Storage(ExceptXML);
                myExcept_XML.SelectNodeBySet();
                int ExceptionType = myExcept_XML.GetAttrValue("ExceptionType", 0);//为1表示只执行的时间段，为0则表示不执行的时间段
                int iCount = myExcept_XML.GetAttrValue("ExceptionCount", 0);
                canTip = ExceptionType == 0;
                for (int m = 0; m < iCount; m++)
                {
                    int iType = myExcept_XML.GetAttrValue("ExType" + (m + 1).ToString(), 1);
                    DateTime start = myExcept_XML.GetAttrValue("ExStart" + (m + 1).ToString(), DateTime.Now);
                    DateTime end = myExcept_XML.GetAttrValue("ExEnd" + (m + 1).ToString(), DateTime.Now).AddMinutes(1);
                    if (iType == 0)//每天
                    {
                        #region 每天
                        if (start.TimeOfDay <= end.TimeOfDay)
                        {
                            if (dt_now.TimeOfDay >= start.TimeOfDay && dt_now.TimeOfDay <= end.TimeOfDay)
                            {
                                canTip = ExceptionType == 1;
                            }
                        }
                        else
                        {
                            if (dt_now.TimeOfDay >= start.TimeOfDay || dt_now.TimeOfDay <= end.TimeOfDay)
                            {
                                canTip = ExceptionType == 1;
                            }
                        }
                        #endregion
                    }
                    else if (iType == 2)//星期
                    {
                        #region 星期
                        int week_index = RyDate.GetWeek_index(dt_now);
                        int start_week = myExcept_XML.GetAttrValue("ExStartWeek" + (m + 1).ToString(), 1);
                        int end_week = myExcept_XML.GetAttrValue("ExEndWeek" + (m + 1).ToString(), 1);
                        if (start_week <= end_week)
                        {
                            if (week_index >= start_week && week_index <= end_week)
                            {
                                canTip = ExceptionType == 1;
                            }
                        }
                        else
                        {
                            if (week_index >= start_week || week_index <= end_week)
                            {
                                canTip = ExceptionType == 1;
                            }
                        }
                        #endregion
                    }
                    else //具体时间
                    {
                        if (dt_now >= start && dt_now <= end)
                        {
                            canTip = ExceptionType == 1;
                        }
                    }
                    if (canTip == (ExceptionType == 1)) { break; }

                }
            }
            return canTip;
            #endregion
        }
        public static void CreateTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 定时提醒表
                RyQuickSQL mySQL = new RyQuickSQL("Dingshi");
                mySQL.AddField("MsgText", "");
                mySQL.AddField("MsgTitle", "");
                mySQL.AddField("modtype", 1);
                mySQL.AddField("Interval",1);
                mySQL.AddField("RunTime",DateTime.Now);
                mySQL.AddField("RunTimeStr", "");
                mySQL.AddField("Enabled", 1);
                mySQL.AddField("GroupKey","");
                mySQL.AddField("NoShow", 0);//不允许展示
                mySQL.AddField("Clac_TipTime",0);//是否需要计算提醒时间
                mySQL.AddField("Clac_Time", DateTime.Now);//计算的时间
                mySQL.AddField("NextTipTime", DateTime.Now);
                mySQL.AddField("NextTipAddMinute", 0);//推迟多少分钟提醒
                mySQL.AddField("EarlierMinute", 0);//提前多少分钟提醒
                mySQL.AddField("EarlierTipTime", DateTime.Now);//提前提醒的时间点
                mySQL.AddField("TempTip", 0);//是否是临时提醒,临时提醒不显示在列表里，而且提醒过后即删除
                mySQL.AddField("TipClose", 0);//该提醒是否已经结束,结束的提醒将不会提醒
                mySQL.AddField("StartTime", DateTime.Now);
                mySQL.AddField("ExceptXML", "");//例外
                mySQL.AddField("CustomShow", "");//自定义显示
                mySQL.AddField("SetXML", "");
                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
                #region 整点报时表
                mySQL.Clear();
                mySQL.TableName = "HourlyChime";
                mySQL.AddField("TimeStr", "");//时间
                mySQL.AddField("Path", "");//路径
                db.CreateDb(mySQL);
                #endregion
                #region 待办表
                mySQL.Clear();
                mySQL.TableName = "ToDo";
                mySQL.AddField("name", "");//名称
                mySQL.AddField("Sorts", 0);//优先级
                mySQL.AddField("des", "");//备注
                mySQL.AddField("des_rtf", "");//备注
                mySQL.AddField("startTime", 0L);
                mySQL.AddField("endTime", 0L);
                mySQL.AddField("state", 0);//0表示未处理,100表示处理完成,1表示处理中,-1表示待办无需再处理
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime",DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 待办日志表
                mySQL.Clear();
                mySQL.TableName = "ToDo_logs";
                mySQL.AddField("log_text", "");//日志内容
                mySQL.AddField("log_type", "");//日志类型
                mySQL.AddField("todo_id", 0);//待办id
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
                #region 便笺表
                mySQL.Clear();
                mySQL.TableName = "Sticky";
                mySQL.AddField("Name", "");//便笺标题
                mySQL.AddField("Title", "");//便笺标题
                mySQL.AddField("RichContent", "");//便笺内容的富文本格式
                mySQL.AddField("Content", "");//便笺内容的txt格式
                mySQL.AddField("DeskShow", 1);//是否桌面显示
                mySQL.AddField("Setting", "");
                mySQL.AddField("editTime", DateTime.Now);
                mySQL.AddField("addTime", DateTime.Now);
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        /// <summary>
        /// 添加待办日志
        /// </summary>
        /// <param name="todo_id"></param>
        /// <param name="log_type"></param>
        /// <param name="log_text"></param>
        public static void AddTodo_Log(int todo_id,string log_type, string log_text)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(SQLConnStr) == 1)
            {
                AddTodo_Log(db, todo_id, log_type, log_text);
            }
        }
        /// <summary>
        /// 添加待办日志
        /// </summary>
        /// <param name="todo_id"></param>
        /// <param name="log_type"></param>
        /// <param name="log_text"></param>
        public static void AddTodo_Log(IDbInterface db,int todo_id, string log_type, string log_text)
        {
            RyQuickSQL mySQL = new RyQuickSQL("ToDo_logs");
            mySQL.AddField("log_text", log_text);//日志内容
            mySQL.AddField("log_type", log_type);//日志类型
            mySQL.AddField("todo_id", todo_id);//待办id
            mySQL.AddField("addTime", DateTime.Now);
            db.ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
        }
        public static void CreateHolidaysTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 节假日表
                RyQuickSQL mySQL = new RyQuickSQL("Holidays");
                mySQL.AddField("Name", "");//节日名
                mySQL.AddField("startYear", 0);//开始年份
                mySQL.AddField("endYear", 1);//结束年份
                mySQL.AddField("sType", 1);//节日类型,0表示公历节日,1表示农历节日,2表示某月第n个星期几的节日
                mySQL.AddField("Holiday_Time","");//节日时间
                mySQL.AddField("Des","");//节日描述
                mySQL.AddField("Sorts", 0);//数值越高，排名越靠前
                mySQL.AddField("NoShowRL", 0);//是否不显示在日历选项中
                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 CreateLogsTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 定时提醒表
                RyQuickSQL mySQL = new RyQuickSQL("Logs");
                mySQL.AddField("LogsText", "");
                mySQL.AddField("LogsType", 0);//0表示提醒记录,1表示休息记录
                mySQL.AddField("TipId", 1);//提醒id
                mySQL.AddField("RestTime", 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();
        }
        public static void CreateWorkInfoTable(DataProvider.DataProviderType dataType, string ConnStr)
        {
            DataProvider mydb = new DataProvider();
            IDbInterface db = CreateDataProvider(dataType);
            if (db.ConnDb(ConnStr) == 1)
            {
                #region 工作信息表
                RyQuickSQL mySQL = new RyQuickSQL("WorkInfo");
                mySQL.AddField("StartTime", DateTime.Now);//开始计算时间
                mySQL.AddField("EndTime", DateTime.Now);//结束计算时间
                mySQL.AddField("stype", 1);//类别,0表示无操作,1表示在用电脑,2表示休息
                mySQL.AddField("totalSecond", 1);//历时多少秒
                db.CreateDb(mySQL);
                #endregion
                //
                #region 设置表
                mySQL.Clear();
                mySQL.TableName = "Setting";
                mySQL.AddField("sName", "");
                mySQL.AddField("sValue", "");
                db.CreateDb(mySQL);
                #endregion
            }
            db.Free();
        }
        /// <summary>
        /// 添加快速提醒。
        /// </summary>
        /// <param name="MsgTitle"></param>
        /// <param name="MsgText"></param>
        /// <param name="add_minute"></param>
        public static void AddTip(string MsgTitle,string MsgText, DateTime dt,int isTemp,string RunTimeStr)
        {
            int modtype =3;
            DateTime RunTime = dt.Date.AddHours(dt.Hour).AddMinutes(dt.Minute);
            string runtime_str = dt.ToString("yyyy年MM月dd日") + " " + dt.ToString("HH:mm"); 
            if(isTemp==1)
            {
                runtime_str = RunTimeStr;
            }
            RyQuickSQL mySQL = new RyQuickSQL("Dingshi");
            mySQL.AddField("MsgTitle", MsgTitle);
            mySQL.AddField("MsgText", MsgText);
            mySQL.AddField("modtype", modtype);
            mySQL.AddField("Interval",1);
            mySQL.AddField("RunTime", RunTime);
            mySQL.AddField("StartTime", RunTime);
            mySQL.AddField("RunTimeStr", runtime_str);
            mySQL.AddField("Clac_TipTime", 1);
            mySQL.AddField("NextTipAddMinute", 0);
            mySQL.AddField("EarlierMinute", 0);
            mySQL.AddField("TempTip", isTemp);
            mySQL.AddField("TipClose", 0);
            mySQL.AddField("NextTipTime", RunTime);
            ryCommon.Storage tStor = new Storage();
            tStor.SelectNodeBySet();
            tStor.SetAttrValue("ActionId", "NoAction");
            tStor.SetAttrValue("File", "");
            tStor.SetAttrValue("Pram","");
            mySQL.AddField("SetXML", tStor.XMLText);
            mySQL.AddField("Enabled",1);
            mySQL.AddField("NoShow", 0);
            DataProvider mydb = new DataProvider();
            IDbInterface db = TimeClock.Itrycn_Db.CreateDataProvider(TimeClock.Itrycn_Db.dataType);
            if (db.ConnDb(TimeClock.Itrycn_Db.SQLConnStr) == 1)
            {
                db.ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
            }
        }
        public static void AddTip(string MsgTitle, string MsgText, DateTime dt)
        {
            AddTip(MsgTitle,MsgText,dt,0,"");
        }
        public static void CreateTable()
        {
            CreateTable(dataType, SQLConnStr);
            CreateLogsTable(dataType, Logs_SQLConnStr);
            CreateWorkInfoTable(dataType, WorkInfo_ConnStr);
            CreateHolidaysTable(dataType, Holidays_ConnStr);
        }
    }
    public class StateInfo
    {
        public int id = 0;
        public string Text = "";
        public override string ToString()
        {
            return Text;
        }
    }
}
