管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-08-23 0138ef1182ab12141a59f927acf2965c4533e1f5
修改获取文件、导入文件的业务逻辑
已修改1个文件
183 ■■■■■ 文件已修改
DataLoader/CS/Importor.cs 183 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DataLoader/CS/Importor.cs
@@ -6,7 +6,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
namespace DataLoader.CS
{
@@ -138,7 +137,6 @@
            if (Directory.Exists(vd.FilePath))
            {
                string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories);
                return GetFileSizes(files);
            }
@@ -229,25 +227,76 @@
            return isExclude;
        }
        #endregion
        #region 导入
        /// <summary>
        /// 导入文件
        /// </summary>
        public static void ImportFiles(ObservableCollection<ViewData> viewDatas)
        {
            Parallel.ForEach(viewDatas, (vd, ParallelLoopState) =>
            {
                try
                {
                    vd.Status = "生成MD5...";
                    vd.Meta.guid = GetFilesMD5(vd);
                    if (!string.IsNullOrEmpty(vd.Meta.guid) && DBHelper.IsFileExists(vd.Meta.guid))
                    {
                        vd.Status = "已存在!";
                        return;
                    }
                    vd.Status = "复制文件...";
                    CopyFiles(vd);
                    vd.Status = "数据入库...";
                    int id = DBHelper.InsertMeta(vd.Meta);
                    vd.Status = id > 0 ? "成功。" : "失败!";
                }
                catch (Exception ex)
                {
                    LogOut.Error(ex.StackTrace);
                    vd.Status = string.Format("失败:{0}!", ex.Message);
                }
            });
        }
        /// <summary>
        /// 获取文件的MD5码
        /// 获取文件的MD5
        /// </summary>
        private static string GetFileMD5(string file)
        private static string GetFilesMD5(ViewData vd)
        {
            if ("1" != Tools.GetSetting("GetMD5") || !File.Exists(file)) return null;
            if ("1" != Tools.GetSetting("GetMD5")) return null;
            return MD5Helper.GetMD5Hash(file);
            if (File.Exists(vd.FilePath))
            {
                if (vd.Ext == StaticData.SHP)
                {
                    List<string> files = GetShpFiles(vd.FilePath);
                    return GetFilesMD5(files.ToArray());
                }
                else
                {
                    return MD5Helper.GetMD5Hash(vd.FilePath);
                }
            }
            if (Directory.Exists(vd.FilePath))
            {
                string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories);
                return GetFilesMD5(files);
            }
            return null;
        }
        /// <summary>
        /// 获取多个文件的MD5码
        /// </summary>
        private static string GetFilesMD5(string path)
        private static string GetFilesMD5(string[] files)
        {
            if ("1" != Tools.GetSetting("GetMD5") || !Directory.Exists(path)) return null;
            string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            if (null == files || files.Length == 0) return null;
            List<string> list = new List<string>();
@@ -264,100 +313,18 @@
            return MD5Helper.GetMD5Hash(bytes);
        }
        #endregion
        #region 导入
        /// <summary>
        /// 导入文件
        /// </summary>
        public static void ImportFiles(ObservableCollection<ViewData> 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);
                }
            });
        }
        /// <summary>
        /// 复制文件
        /// </summary>
        private static void CopyFile(string source, string target)
        private static void CopyFiles(ViewData vd)
        {
            if (File.Exists(source))
            {
                File.Copy(source, target, true);
            }
        }
            //mf.path = subPath + "\\" + mf.name;
        public static void ImportFiles(ObservableCollection<ViewData> 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<ViewData> 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 = "失败!";
                }
            }
            //if (File.Exists(source))
            //{
            //    File.Copy(source, target, true);
            //}
        }
        private static int GetSubPath(string target, int start = 1)
@@ -379,26 +346,6 @@
                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;
        }
        #endregion
    }