DatabaseSerise.cs 4.4 KB
Newer Older
cdy816's avatar
cdy816 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
//==============================================================
//  Copyright (C) 2020  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2020/2/16 10:10:13.
//  Version 1.0
//  种道洋
//==============================================================

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

namespace Cdy.Tag
{
    public class DatabaseSerise
    {

        #region ... Variables  ...
        /// <summary>
        /// 
        /// </summary>
        public static DatabaseSerise manager = new DatabaseSerise();

        #endregion ...Variables...

        #region ... Events     ...

        #endregion ...Events...

        #region ... Constructor...

        #endregion ...Constructor...

        #region ... Properties ...

        /// <summary>
        /// 
        /// </summary>
        public Database Dbase { get; set; }

        #endregion ...Properties...

        #region ... Methods    ...

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public Database Load(string name)
        {
            Dbase = LoadDatabaseSelf(PathHelper.helper.GetDataPath(name,name + ".db"));
            
            Dbase.RealDatabase = new RealDatabaseSerise().LoadByName(name);
            
            Dbase.HisDatabase = new HisDatabaseSerise().LoadByName(name);

            Dbase.Security = new SecuritySerise().LoadByName(name);

            return Dbase;
        }

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Database LoadRealDatabase(string name)
        {
            Dbase = LoadDatabaseSelf(PathHelper.helper.GetDataPath(name, name + ".db"));
            Dbase.RealDatabase = new RealDatabaseSerise().LoadByName(name);

            Dbase.Security = new SecuritySerise().LoadByName(name);

            return Dbase;
        }

cdy816's avatar
cdy816 已提交
79 80 81 82 83 84 85 86 87 88 89 90
        /// <summary>
        /// 
        /// </summary>
        private Database LoadDatabaseSelf(string path)
        {
            Database doc = new Database();

            if (System.IO.File.Exists(path))
            {
                XElement xe = XElement.Load(path);

                doc.Name = xe.Attribute("Name").Value;
cdy816's avatar
cdy816 已提交
91
                doc.Desc = xe.Attribute("Desc") != null ? xe.Attribute("Desc").Value : string.Empty;
cdy816's avatar
cdy816 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                doc.Version = xe.Attribute("Version").Value;

                if (xe.Element("Setting") != null)
                {
                    doc.Setting = LoadSetting(xe.Element("Setting"));
                }
            }

            return doc;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        private void SaveDatabaseSelf(string path)
        {

            XElement doc = new XElement("RealDatabase");
            doc.SetAttributeValue("Name", Dbase.Name);
cdy816's avatar
cdy816 已提交
113
            doc.SetAttributeValue("Desc", Dbase.Desc);
cdy816's avatar
cdy816 已提交
114 115 116 117 118
            doc.SetAttributeValue("Version", Dbase.Version);
            doc.SetAttributeValue("Auther", "cdy");

            doc.Add(Save(Dbase.Setting));

cdy816's avatar
cdy816 已提交
119 120 121 122 123 124
            var spath = System.IO.Path.GetDirectoryName(path);
            if(!System.IO.Directory.Exists(spath))
            {
                System.IO.Directory.CreateDirectory(spath);
            }

cdy816's avatar
cdy816 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
            doc.Save(path);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xe"></param>
        /// <returns></returns>
        private SettingDoc LoadSetting(XElement xe)
        {
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        private XElement Save(SettingDoc doc)
        {
            XElement xe = new XElement("Setting");
            return xe;
        }


        /// <summary>
        /// 
        /// </summary>
        public void Save()
        {
            SaveDatabaseSelf(PathHelper.helper.GetDataPath(Dbase.Name, Dbase.Name + ".db"));
            new RealDatabaseSerise() { Database = Dbase.RealDatabase }.Save();
            new HisDatabaseSerise() { Database = Dbase.HisDatabase }.Save();
            new SecuritySerise() { Document = Dbase.Security }.Save();
        }

        #endregion ...Methods...

        #region ... Interfaces ...

        #endregion ...Interfaces...
    }
}