using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Config
{
    /// <summary>
    /// 基于XML的自定义数据操作
    /// </summary>
    public class CustomDataBaseUtil
    {
        public static string CXPATH_BUFFER_FILE_PATH = System.Configuration.ConfigurationManager.AppSettings["BufferFilePath"].ToString();

        public const string GLOBAL_CONFIGUATION_FILE_PATH = "app.config";
        /// <summary>
        /// 缓存表的主键
        /// </summary>
        public const string CXPATH_BUFFER_TABLE_KEY = "Configuration/CommonConfiguration/BufferTableKey";
        /// <summary>
        /// 缓存表的主键2
        /// </summary>
        public const string CXPATH_BUFFER_TABLE_KEY2 = "Configuration/CommonConfiguration/BufferTableKey2";
        /// <summary>
        /// 返回数据文件是否存在
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsDBFileExists(string fileName)
        {
            //"D:\\T99_FileServer\\'" + user + "'.buf"
            #region
            //if (System.IO.File.Exists(Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_FILE_PATH) + @"\" + priUser + ".buf"))
            if (System.IO.File.Exists("D:\\T99_FileServer\\'" + fileName + "'.buf"))
            {
                return true;
            }
            return false;
            #endregion
        }

        /// <summary>
        /// 获取缓存数据
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static System.Data.DataTable GetData(string fileName)
        {
            #region
            if (!IsDBFileExists(fileName))
            {
                return null;
            }

            System.Data.DataTable table = null;
            using (System.Data.DataSet ds = new System.Data.DataSet())
            {
                ds.ReadXml("D:\\T99_FileServer\\'" + fileName + "'.buf", System.Data.XmlReadMode.ReadSchema);
                table = ds.Tables[0].Copy();
            }

            return table;
            #endregion
        }

        /// <summary>
        /// 删除缓存中指定项
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="key"></param>
        public static void DeleteData(string fileName, string[] key)
        {
            #region
            using (System.Data.DataTable table = GetData(fileName))
            {
                if (table != null)
                {
                    System.Data.DataRow row = table.Rows.Find(key);
                    if (row != null)
                    {
                        //移除数据
                        table.Rows.Remove(row);
                        if (table.Rows.Count == 0)
                        {
                            //删除空文件
                            DeleteFile(fileName);
                        }
                        else
                        {
                            //保存文件
                            SaveFile(fileName, table);
                        }
                    }
                    else
                    {
                        //缓存中不存在要删除的数据
                        //throw new Exception(Config.ConfigurationUtil.ReadConfig("缓存中不存在要删除的数据"));
                        //throw new Exception("缓存中不存在要删除的数据");
                        return;
                    }
                }
                else
                {
                    //缓存文件不存在
                    //throw new Exception(Config.ConfigurationUtil.ReadConfig("缓存文件不存在"))
                    //throw new Exception("缓存文件不存在");
                    return;
                }
            }
            #endregion
        }


        /// <summary>
        /// 返回指定项是否存在
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool IsItemExists(string fileName, string[] key)
        {
            #region
            using (System.Data.DataTable table = GetData(fileName))
            {
                if (table != null)
                {
                    System.Data.DataRow row = table.Rows.Find(key);
                    if (row != null)
                    {
                        return true;
                    }
                }
            }

            return false;
            #endregion
        }

        /// <summary>
        /// 保存缓存文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        internal static void SaveFile(string  fileName, System.Data.DataTable data)
        {
            #region
            using (System.Data.DataSet ds = new System.Data.DataSet())
            {
                //添加数据
                ds.Tables.Add(data.Copy());

                //删除缓存文件
                DeleteFile(fileName);

                //保存文件
                ds.WriteXml("D:\\T99_FileServer\\'" + fileName + "'.buf", System.Data.XmlWriteMode.WriteSchema);
            }
            #endregion
        }

        /// <summary>
        /// 添加数据项
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="data"></param>
        /// <param name="parmCount"></param>
        public void AddItem(string fileName, System.Data.DataTable data, int parmCount)
        {
            #region
            for (int i = data.Rows.Count - 1; i >= 0; i--)
            {
                string key1 = data.Rows[i][Common.Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_TABLE_KEY)].ToString();
                if (parmCount == 2)
                {
                    string key2 = data.Rows[i][Common.Config.ConfigurationUtil.ReadConfig(CXPATH_BUFFER_TABLE_KEY2)].ToString();
                    string[] key = { key1, key2 };
                    if (IsItemExists(fileName, key))
                    {
                        data.Rows.RemoveAt(i);
                    }
                }
                else
                {
                    string[] key = { key1 };
                    if (IsItemExists(fileName, key))
                    {
                        data.Rows.RemoveAt(i);
                    }
                }
            }

            using (System.Data.DataTable table = GetData(fileName))
            {
                if (table != null)
                {
                    table.Merge(data, true, System.Data.MissingSchemaAction.Ignore);
                    SaveFile(fileName, table);
                }
                else
                {
                    SaveFile(fileName, data);
                }
            }
            #endregion
        }

        /// <summary>
        /// 删除缓存文件
        /// </summary>
        /// <param name="fileName"></param>
        internal static void DeleteFile(string fileName)
        {
            #region
            if (IsDBFileExists(fileName))
            {
                System.IO.File.Delete("D:\\T99_FileServer\\'" + fileName + "'.buf");
            }
            #endregion
        }
    }
}