using DataLoader.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace DataLoader.CS
{
public class Importor
{
///
/// 获取文件
///
public static void GetFiles(ObservableCollection viewDatas)
{
string[] files = Directory.GetFiles(CommonProp.SourcePath);
for (int i = 0, c = files.Length; i < c; i++)
{
ViewData vd = new ViewData();
vd.ID = i + 1;
vd.FilePath = files[i];
vd.Ext = System.IO.Path.GetExtension(files[i]);
vd.Status = "准备";
viewDatas.Add(vd);
SetSysMeta(vd);
}
}
///
/// 数据导入
///
public static void ImportFiles(ObservableCollection viewDatas)
{
Parallel.ForEach(viewDatas, (vd, ParallelLoopState) =>
{
try
{
//
SysMeta meta = new SysMeta();
//
int id = DBHelper.InsertMeta(meta);
vd.Status = id > 0 ? "成功。" : "失败!";
}
catch (Exception ex)
{
LogOut.Error(ex.StackTrace);
vd.Status = string.Format("失败:{0}!", ex.Message);
}
});
}
///
/// 复制文件
///
private static void CopyFile(string source, string target)
{
if (File.Exists(source))
{
File.Copy(source, target, true);
}
}
public static void GetFiles(ObservableCollection viewDatas, string source)
{
string[] files = Directory.GetFiles(source);
for (int i = 0, c = files.Length; i < c; i++)
{
ViewData vd = new ViewData();
vd.ID = i + 1;
vd.FilePath = files[i];
vd.Ext = System.IO.Path.GetExtension(files[i]);
vd.Status = "准备";
viewDatas.Add(vd);
SetSysMeta(vd);
}
}
private static void SetSysMeta(ViewData vd)
{
FileInfo fi = new FileInfo(vd.FilePath);
vd.Meta = new SysMeta();
vd.Meta.name = fi.Name;
//vd.Meta.dirid = Common.DirId;
//vd.Meta.depid = 1;
vd.Meta.verid = 0;
vd.Meta.type = fi.Extension.ToLower().Replace(".", "");
vd.Meta.sizes = Tools.SizeToMb(fi.Length);
vd.Meta.create_user = CommonProp.UserId;
}
public static void ImportFiles(ObservableCollection viewDatas, string target, int start)
{
/*Parallel.ForEach(viewDatas, (vd, ParallelLoopState) =>
{
try
{
vd.Status = "生成MD5码...";
string guid = MD5Helper.GetMD5Hash(vd.FilePath);
if (!Exclusions.Contains(vd.Ext) && IsFileExists(guid))
{
vd.Status = "已存在!";
return;
}
vd.Status = "获取数据目录...";
int subPath = GetSubPath(target, start);
SysMeta mf = GetMetaFile(vd, subPath, guid);
vd.Status = "复制文件...";
CopyFile(vd.FilePath, Path.Combine(target, mf.path));
vd.Status = "准备入库";
vd.Meta = mf;
});*/
}
public static void ImportFiles2(ObservableCollection viewDatas, string target)
{
int start = 1;
foreach (ViewData vd in viewDatas)
{
try
{
vd.Status = "插入数据库...";
start = GetSubPath(target, start);
SysMeta mf = GetMetaFile(vd, start, "");
int metaId = 0; // InsertMeta(mf);
if (metaId == 0)
{
vd.Status = "元数据出错!";
continue;
}
vd.Status = "复制文件...";
CopyFile(vd.FilePath, Path.Combine(target, mf.path));
vd.Status = "完成。";
}
catch (Exception ex)
{
LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
vd.Status = "失败!";
}
}
}
private static int GetSubPath(string target, int start = 1)
{
while (true)
{
string path = Path.Combine(target, start.ToString());
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return start;
}
string[] files = Directory.GetFiles(path);
if (files.Length < 2001)
{
return start;
}
start++;
}
}
private static SysMeta GetMetaFile(ViewData vd, int subPath, string guid)
{
SysMeta mf = new SysMeta();
mf.eventid = Guid.NewGuid().ToString();
mf.metaid = 0;
//mf.dirid = vd.Meta.dirid;
//mf.depid = vd.Meta.depid;
mf.verid = vd.Meta.verid;
mf.name = vd.Meta.name;
mf.type = vd.Meta.type;
mf.guid = guid;
mf.path = subPath + "\\" + mf.name;
mf.sizes = vd.Meta.sizes;
mf.tab = null;
mf.rows = 0;
mf.create_user = vd.Meta.create_user;
return mf;
}
}
}