using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.DAL.LocalFile
|
{
|
/// <summary>
|
/// 带Id的文件辅助类
|
/// </summary>
|
public class FileIdHelper<T> where T : Entity.BaseEntity
|
{
|
#region Query
|
|
/// <summary>
|
/// 查询全部
|
/// </summary>
|
public static List<T> QueryAll(string path)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
return list;
|
}
|
|
/// <summary>
|
/// 根据Id查询
|
/// </summary>
|
public static T QueryById(string path, long Id)
|
{
|
var list = QueryAll(path);
|
if (list == null || list.Count < 1)
|
return default;
|
return list.Find(t => t.Id == Id);
|
}
|
|
/// <summary>
|
/// 根据Id集合查询
|
/// </summary>
|
public static IEnumerable<T> QueryByIds(string path, IEnumerable<long> Ids)
|
{
|
if (Ids == null || Ids.Count() < 1)
|
return default;
|
var list = QueryAll(path);
|
return list?.Where(x => Ids.Contains(x.Id));
|
}
|
|
#endregion
|
|
#region Insert
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public static long Insert(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null)
|
list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t.Id;
|
}
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public static long Insert(string path, IEnumerable<T> srcList, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = new List<T>();
|
if (srcList != null && srcList.Count() > 0)
|
{
|
list.AddRange(srcList);
|
}
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t.Id;
|
}
|
|
/// <summary>
|
/// 插入并返回
|
/// </summary>
|
public static T InsertR(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null)
|
list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t;
|
}
|
|
/// <summary>
|
/// 插入并返回
|
/// </summary>
|
public static T InsertR(string path, IEnumerable<T> srcList, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = new List<T>();
|
if (srcList != null && srcList.Count() > 0)
|
{
|
list.AddRange(srcList);
|
}
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public static bool Inserts(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var srcList = FileHelper.GetFileObject<List<T>>(fileName);
|
if (srcList == null)
|
srcList = new List<T>();
|
var list_ids = list.ToList();
|
list_ids.ForEach(x => x.Id = SnowflakeIdHelper.NextId());
|
srcList.AddRange(list_ids);
|
FileHelper.SaveObjectFile(fileName, srcList);
|
return true;
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
public static bool Inserts(string path, IEnumerable<T> srcList, IEnumerable<T> newList)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (newList == null || newList.Count() < 1)
|
return default;
|
var list = new List<T>();
|
if (srcList != null && srcList.Count() > 0)
|
list.AddRange(srcList);
|
var newList1 = newList.ToList();
|
newList1.ForEach(x => x.Id = SnowflakeIdHelper.NextId());
|
list.AddRange(newList1);
|
var fileName = FileHelper.GetFilePath<T>(path);
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 批量插入并返回
|
/// </summary>
|
public static List<T> InsertRs(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var srcList = FileHelper.GetFileObject<List<T>>(fileName);
|
if (srcList == null)
|
srcList = new List<T>();
|
var newList = list.ToList();
|
newList.ForEach(x => x.Id = SnowflakeIdHelper.NextId());
|
srcList.AddRange(newList);
|
FileHelper.SaveObjectFile(fileName, srcList);
|
return newList.ToList();
|
}
|
|
/// <summary>
|
/// 批量插入并返回
|
/// </summary>
|
public static List<T> InsertRs(string path, IEnumerable<T> srcList, IEnumerable<T> newList)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (newList == null || newList.Count() < 1)
|
return default;
|
var list = new List<T>();
|
if (srcList != null && srcList.Count() > 0)
|
list.AddRange(srcList);
|
var newList1 = newList.ToList();
|
newList1.ForEach(x => x.Id = SnowflakeIdHelper.NextId());
|
list.AddRange(newList1);
|
var fileName = FileHelper.GetFilePath<T>(path);
|
FileHelper.SaveObjectFile(fileName, list);
|
return newList1.ToList();
|
}
|
|
#endregion
|
|
#region Update
|
/// <summary>
|
/// 更新
|
/// </summary>
|
public static bool Update(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null || list.Count < 1)
|
return default;
|
var src = list.Find(x => x.Id == t.Id);
|
if (src == null)
|
return default;
|
t.Map(src);
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 批量更新
|
/// </summary>
|
public static bool Updates(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var srcList = FileHelper.GetFileObject<List<T>>(fileName);
|
if (srcList == null || srcList.Count < 1)
|
return default;
|
foreach (var t in list)
|
{
|
var src = srcList.Find(x => x.Id == t.Id);
|
if (src == null)
|
return default;
|
t.Map(src);
|
}
|
FileHelper.SaveObjectFile(fileName, srcList);
|
return true;
|
}
|
#endregion
|
|
#region Cover
|
/// <summary>
|
/// 覆盖
|
/// </summary>
|
public static long Cover(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
if (t.Id < 1)
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t.Id;
|
}
|
|
/// <summary>
|
/// 覆盖并返回
|
/// </summary>
|
public static T CoverR(string path, T t)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (t == null)
|
return default;
|
if (t.Id < 1)
|
t.Id = SnowflakeIdHelper.NextId();
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = new List<T>();
|
list.Add(t);
|
FileHelper.SaveObjectFile(fileName, list);
|
return t;
|
}
|
|
/// <summary>
|
/// 批量覆盖
|
/// </summary>
|
public static bool Covers(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
foreach (var t in list)
|
{
|
if (t.Id < 1)
|
t.Id = SnowflakeIdHelper.NextId();
|
}
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 批量覆盖并返回
|
/// </summary>
|
public static List<T> CoversR(string path, IEnumerable<T> list)
|
{
|
if (string.IsNullOrEmpty(path))
|
return default;
|
if (list == null || list.Count() < 1)
|
return default;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
foreach (var t in list)
|
{
|
if (t.Id < 1)
|
t.Id = SnowflakeIdHelper.NextId();
|
}
|
FileHelper.SaveObjectFile(fileName, list);
|
return list.ToList();
|
}
|
#endregion
|
|
#region Delete
|
/// <summary>
|
/// 根据Id删除
|
/// </summary>
|
public static bool DeleteById(string path, long id)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null || list.Count < 1)
|
return false;
|
var item = list.Find(t => t.Id == id);
|
if (item == null)
|
return false;
|
list.Remove(item);
|
if (list.Count < 1)
|
File.Delete(fileName);
|
else
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 根据Id集合删除
|
/// </summary>
|
public static bool DeleteByIds(string path, IEnumerable<long> ids)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
if (ids == null || ids.Count() < 1)
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
var list = FileHelper.GetFileObject<List<T>>(fileName);
|
if (list == null || list.Count < 1)
|
return false;
|
list.RemoveAll(x => ids.Contains(x.Id));
|
if (list.Count < 1)
|
File.Delete(fileName);
|
else
|
FileHelper.SaveObjectFile(fileName, list);
|
return true;
|
}
|
|
/// <summary>
|
/// 删除全部
|
/// </summary>
|
public static bool DeleteAll(string path)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
if (File.Exists(fileName))
|
File.Delete(fileName);
|
return true;
|
}
|
|
#endregion
|
|
#region Exist
|
|
/// <summary>
|
/// 是否存在
|
/// </summary>
|
public static bool IsExist(string path)
|
{
|
if (string.IsNullOrEmpty(path))
|
return false;
|
var fileName = FileHelper.GetFilePath<T>(path);
|
if (!File.Exists(fileName))
|
return false;
|
var all = QueryAll(path);
|
if (all == null || all.Count < 1)
|
return false;
|
return true;
|
}
|
|
#endregion
|
}
|
}
|