| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using Newtonsoft.Json;
- namespace SAGA.Models
- {
- public static class DbHelper
- {
- static DbHelper()
- {
- if (!Directory.Exists(Path))
- {
- Directory.CreateDirectory(Path);
- }
- }
- private static string Path => StaticData.DbDirectory;
- /// <summary>
- /// 清除所有文件
- /// </summary>
- public static void ClearAllData()
- {
- foreach (var file in Directory.GetFiles(Path))
- {
- File.Delete(file);
- }
- }
- /// <summary>
- /// 清除指定的文件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="fileName"></param>
- public static void ClearData<T>(string fileName = "")
- {
- var name = fileName == "" ? GetTableName(typeof(T)) : fileName;
- foreach (var file in Directory.GetFiles(Path))
- {
- if (file.IndexOf(name, StringComparison.Ordinal)>-1)
- File.Delete(file);
- }
- }
- /// <summary>
- /// 根据文件类型或者文件名保存数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="t"></param>
- /// <param name="fileName"></param>
- public static void SaveAsFile<T>(T t, string fileName = "")
- {
- var name = GetTableNamePath<T>(true, fileName);
- var settings = new JsonSerializerSettings();
- settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
- var data = JsonConvert.SerializeObject(t, settings);
- //var data = t.ToBinaryString();
- File.AppendAllText(name, data);
- }
- public static List<T> FindAllByFile<T>()
- {
- var name = GetTableName(typeof(T)) + ".json";
- if (File.Exists(name))
- {
- return JsonConvert.DeserializeObject<List<T>>(File.ReadAllText(name));
- }
- else
- {
- return new List<T>();
- }
- }
- /// <summary>
- /// 根据文件类型或者文件名获取数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public static T FindOneByFile<T>(string tableName = "", bool isCreateTimestamp = true)
- {
- var name = GetTableNamePath<T>(false, tableName, isCreateTimestamp);
- if (File.Exists(name))
- {
- return JsonConvert.DeserializeObject<T>(File.ReadAllText(name));
- // return File.ReadAllText(name).BinaryToObject<T>();
- }
- var type = typeof(T);
- if (type.IsGenericType && type.Name == "List`1")
- {
- var list = (T)Activator.CreateInstance(type);
- return list;
- }
- return default(T);
- }
- /// <summary>
- /// 根据名称查找文件
- /// </summary>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public static string GetFileNamePath(string tableName)
- {
- var file = Directory.GetFiles(Path).FirstOrDefault(f => f.IndexOf(tableName) > -1);
- return file;
- }
- /// <summary>
- /// 根据数据名称生成文件名称
- /// </summary>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public static string CreateFileNamePath(string tableName, bool isCreateTimeState = true)
- {
- var name = System.IO.Path.Combine(Path, tableName + ".json");
- if (isCreateTimeState)
- name = System.IO.Path.Combine(Path, tableName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-new.json");
- var oldFileName = GetFileNamePath(tableName);
- if (File.Exists(oldFileName))
- File.Delete(oldFileName);
- return name;
- }
- /// <summary>
- /// 根据类型或者数据名称获取数据路径
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="isCreate"></param>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public static string GetTableNamePath<T>(bool isCreate, string tableName = "", bool isCreateTimestamp = true)
- {
- var type = typeof(T);
- if (type.IsGenericType && type.Name == "List`1")
- {
- type = type.GetGenericArguments().FirstOrDefault();
- }
- var name = tableName == "" ? GetTableName(type) : tableName;
- if (!isCreateTimestamp) return System.IO.Path.Combine(Path, name + ".json");
- return isCreate ? CreateFileNamePath(name, isCreateTimestamp) : GetFileNamePath(name);
- }
- /// <summary>
- /// 获取拓扑数据的时间戳
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="tableName"></param>
- /// <returns></returns>
- public static string GetTimestamp<T>(string tableName = "")
- {
- Regex reg = new Regex(@"^\w+-(\d+)(-new)?.json$");
- var filePath = GetTableNamePath<T>(false, tableName);
- var fileName = System.IO.Path.GetFileName(filePath);
- if (!string.IsNullOrEmpty(fileName))
- {
- var mathch = reg.Match(fileName);
- if (mathch.Success)
- {
- var timestamp = mathch.Groups[1].Value;
- return FormatDateTime(timestamp);
- }
- }
- return null;
- }
- public static string FormatDateTime(string timeStr)
- {
- Regex dateReg = new Regex(@"^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})");
- //转换成可以直接转换的时间格式
- var timestamp = dateReg.Replace(timeStr, "$1-$2-$3 $4:$5:$6");
- // DateTime no1 = DateTime.ParseExact(timeStr, "yyyyMMddHHmmss", null);
- return timestamp;
- }
- private static string GetTableName(Type type)
- {
- //检测类型是否为泛型
- if (type.GetType().IsGenericType)
- {//取出泛型类型
- var types = type.GetGenericArguments();
- type
- = types.FirstOrDefault();
- }
- var name = type.GetAttribute<TableNameAttribute>()?.Name;
- if (string.IsNullOrEmpty(name))//找不到标记名称取类型名称
- {
- name = type.Name;
- }
- return name;
- }
- }
- public static class RefletcExtend
- {
- public static T GetAttribute<T>(this Type type) where T : Attribute
- {
- return (T)type.GetCustomAttributes(typeof(T), false).FirstOrDefault();
- }
- }
- public class TableNameAttribute : Attribute
- {
- public string Name { get; set; }
- public TableNameAttribute(string name)
- {
- this.Name = name;
- }
- }
- }
|