using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using System.Linq;
using System.IO;
namespace IStation.DAL.LocalFile
{
///
/// 项目
///
public class Project : IDAL.IProject
{
#region Path
///
/// 查询文件路径
///
private string GetFolderPath()
{
return FileHelper.GetWorkPath();
}
///
/// 查询文件路径
///
private string GetProjectPath(long id)
{
return FileHelper.GetProjectPath(id);
}
#endregion
#region Get
///
/// 查询全部
///
public List GetAll()
{
var path = GetFolderPath();
var all = FileIdHelper.GetAll(path);
var entities = all?.ToList();
return entities;
}
///
/// 根据 Id查询
///
public Entity.Project GetById(long Id)
{
var all = GetAll();
var entities = all?.ToList();
return entities?.Find(t => t.Id == Id);
}
///
/// 根据 Id集合查询
///
public List GetByIds(IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return default;
var all = GetAll();
var entities = all?.Where(x => Ids.Contains(x.Id)).ToList();
return entities;
}
#endregion
#region Insert
///
/// 插入
///
public long Insert(Entity.Project rhs)
{
if (rhs == null)
return default;
var path = GetFolderPath();
return FileIdHelper.Insert(path, rhs);
}
///
/// 批量插入
///
public bool Inserts(IEnumerable list)
{
if (list == null || list.Count() < 1)
return default;
var path = GetFolderPath();
return FileIdHelper.Inserts(path, list);
}
///
/// 插入并返回
///
public Entity.Project InsertR(Entity.Project rhs)
{
if (rhs == null)
return default;
var path = GetFolderPath();
var entities = FileIdHelper.InsertR(path, rhs);
return entities;
}
///
/// 批量插入并返回
///
public List InsertsR(IEnumerable list)
{
if (list == null || list.Count() < 1)
return default;
var path = GetFolderPath();
var entities = FileIdHelper.InsertRs(path, list);
return entities?.Select(x => x.Id).ToList();
}
#endregion
#region Update
///
/// 更新
///
public bool Update(Entity.Project rhs)
{
if (rhs == null)
return default;
var path = GetFolderPath();
return FileIdHelper.Update(path, rhs);
}
///
/// 批量更新
///
public bool Updates(IEnumerable list)
{
if (list == null || list.Count() < 1)
return default;
var path = GetFolderPath();
return FileIdHelper.Updates(path, list);
}
#endregion
#region Delete
///
/// 根据 Id删除
///
public bool DeleteById(long Id)
{
if (Id < 0)
return default;
var path = GetFolderPath();
var bol = FileIdHelper.DeleteById(path, Id);
return bol;
}
///
/// 根据 Id集合删除
///
public bool DeleteByIds(IEnumerable Ids)
{
if (Ids == null || Ids.Count() < 1)
return default;
var path = GetFolderPath();
var bol = FileIdHelper.DeleteByIds(path, Ids);
return bol;
}
///
/// 删除
///
public bool Delete(Entity.Project rhs)
{
if (rhs == null)
return default;
var path = GetFolderPath();
var bol = FileIdHelper.DeleteById(path, rhs.Id);
return bol;
}
///
/// 批量删除
///
public bool Deletes(IEnumerable list)
{
if (list == null || list.Count() < 1)
return default;
var path = GetFolderPath();
var Ids = list.Select(x => x.Id).ToList();
var bol = FileIdHelper.DeleteByIds(path, Ids);
return bol;
}
///
/// 删除全部
///
public bool DeleteAll()
{
var path = GetFolderPath();
var bol = FileIdHelper.DeleteAll(path);
return bol;
}
#endregion
#region Import
///
/// 导入
///
public long Import(string fileName, out string msg)
{
msg = string.Empty;
var newProjectId = SnowflakeIdHelper.NextId();
var newProjectFolderPath = GetProjectPath(newProjectId);
ZipHelper.UnZipFolder(fileName, newProjectFolderPath);
var defaultProject = FileIdHelper.GetLatest(newProjectFolderPath);
if (defaultProject == null)
{
msg = "项目文件缺失";
return default;
}
else
{
FileIdHelper.DeleteAll(newProjectFolderPath);
}
var fileInfo = new FileInfo(fileName);
var entity = new Entity.Project();
entity.Id = newProjectId;
entity.Name = defaultProject.Name;
entity.Version = defaultProject.Version;
entity.TagName = defaultProject.TagName;
entity.Description = defaultProject.Description;
entity.CreateTime = fileInfo.CreationTime;
entity.UpdateTime = fileInfo.LastWriteTime;
var folderPath = GetFolderPath();
FileSrcHelper.Append(folderPath, entity);
return newProjectId;
}
#endregion
#region Export
///
/// 导出
///
public bool Export(long id, string fileName, out string msg)
{
msg = string.Empty;
var project = GetById(id);
var projectPath = GetProjectPath(id);
var result = FileSrcHelper.Append(projectPath, project);
if (!result)
{
msg = "项目文件未生成";
return default;
}
ZipHelper.ZipFolder(projectPath, fileName);
return true;
}
#endregion
}
}