From 979ba3f2d4da1a69b9ecc7c4ef1975bc7e110148 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期日, 01 一月 2023 20:17:34 +0800
Subject: [PATCH] 1

---
 DataLoader/MainWindow.xaml.cs |    8 +-
 DataLoader/CS/Importor.cs     |  111 +++++++++++++++++++++++++++++++++----
 DataLoader/CS/MD5Helper.cs    |    4 
 3 files changed, 105 insertions(+), 18 deletions(-)

diff --git a/DataLoader/CS/Importor.cs b/DataLoader/CS/Importor.cs
index c91bd8d..f19e15c 100644
--- a/DataLoader/CS/Importor.cs
+++ b/DataLoader/CS/Importor.cs
@@ -7,6 +7,7 @@
 using System.IO;
 using System.Linq;
 using System.Reflection;
+using System.Windows.Forms;
 
 namespace DataLoader.CS
 {
@@ -24,18 +25,28 @@
             }
         }
 
-        private static readonly string insertMeta = "insert into lf.sys_meta(name, dirid, depid, verid, type, sizes, cs, scale, resolution, gather, batch, descr, create_user) values  @name, @dirid, @depid, @verid, @type, @sizes, @cs, @scale, @resolution, @gather, @batch, @descr, @create_user) returning id";
+        private static readonly string insertMeta = "insert into lf.sys_meta (name, dirid, depid, verid, type, sizes, cs, scale, resolution, gather, batch, descr, create_user) values (@name, @dirid, @depid, @verid, @type, @sizes, @cs, @scale, @resolution, @gather, @batch, @descr, @create_user) returning id";
 
-        private static readonly string insertMetaFile = "insert into lf.sys_meta_file(name, metaid, fileid, guid, path, sizes, create_user) values (@name, @metaid, @fileid, @guid, @path, @sizes, @create_user)";
+        private static readonly string insertMetaFile = "insert into lf.sys_meta_file (name, metaid, fileid, guid, path, sizes, create_user) values (@name, @metaid, @fileid, @guid, @path, @sizes, @create_user)";
 
         public static string GetFilePathByGuid(string guid)
         {
-            string sql = "select path from lf.sys_meta_file where guid = @guid limit";
+            string sql = "select path from lf.sys_meta_file where guid = @guid limit 1";
 
             DbParameter dp = new NpgsqlParameter("@guid", guid);
             object obj = Helper.GetScalar(sql, dp);
 
             return obj == null ? null : obj.ToString();
+        }
+
+        public static bool IsFileExists(string guid)
+        {
+            string sql = "select count(*) from lf.sys_meta_file where guid = @guid limit 1";
+
+            DbParameter dp = new NpgsqlParameter("@guid", guid);
+            object obj = Helper.GetScalar(sql, dp);
+
+            return obj != null && Convert.ToInt32(obj) > 0;
         }
 
         public static int InsertMeta(SysMeta meta)
@@ -60,14 +71,6 @@
         public static void Import(ObservableCollection<ViewData> viewDatas, string source, string target)
         {
             string[] files = Directory.GetFiles(source);
-            foreach (string file in files)
-            {
-                ViewData vd = new ViewData();
-                viewDatas.Add(vd);
-
-
-            }
-
             for (int i = 0, c = files.Length; i < c; i++)
             {
                 ViewData vd = new ViewData();
@@ -78,6 +81,8 @@
 
                 SetSysMeta(vd);
             }
+
+            ImportFiles(viewDatas, target);
         }
 
         private static void SetSysMeta(ViewData vd)
@@ -89,7 +94,7 @@
             vd.Meta.dirid = 0;
             vd.Meta.depid = 0;
             vd.Meta.verid = 0;
-            vd.Meta.type = "File";
+            vd.Meta.type = "file";
             vd.Meta.sizes = Tool.SizeToMb(fi.Length);
             vd.Meta.cs = null;
             vd.Meta.scale = null;
@@ -99,5 +104,87 @@
             vd.Meta.descr = null;
             vd.Meta.create_user = Tool.UserId;
         }
+
+        public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target)
+        {
+            int start = 1;
+            foreach (ViewData vd in viewDatas)
+            {
+                try
+                {
+                    vd.Status = "鑾峰彇MD5鐮�";
+                    string guid = MD5Helper.GetMD5Hash(vd.FilePath);
+                    if (IsFileExists(guid))
+                    {
+                        vd.Status = "宸插瓨鍦�";
+                        continue;
+                    }
+
+                    vd.Status = "鎻掑叆鏁版嵁搴�";
+                    int metaId = InsertMeta(vd.Meta);
+                    if (metaId == 0)
+                    {
+                        vd.Status = "鍏冩暟鎹嚭閿�";
+                        continue;
+                    }
+
+                    start = GetSubPath(target, start);
+                    SysMetaFile mf = GetMetaFile(vd, metaId, start, guid);
+                    InsertMetaFile(mf);
+
+                    vd.Status = "澶嶅埗鏂囦欢";
+                    CopyFile(vd.FilePath, Path.Combine(target, mf.path));
+
+                    vd.Status = "瀹屾垚";
+                }
+                catch (Exception ex)
+                {
+                    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 SysMetaFile GetMetaFile(ViewData vd, int metaId, int subPath, string guid)
+        {
+            SysMetaFile mf = new SysMetaFile();
+            mf.name = vd.Meta.name;
+            mf.metaid = metaId;
+            mf.fileid = 0;
+            mf.guid = guid;
+            mf.path = subPath + "\\" + mf.name;
+            mf.sizes = vd.Meta.sizes;
+            mf.create_user = vd.Meta.create_user;
+
+            return mf;
+        }
+
+        private static void CopyFile(string source, string target)
+        {
+            if (File.Exists(source))
+            {
+                File.Copy(source, target, true);
+            }
+        }
     }
 }
diff --git a/DataLoader/CS/MD5Helper.cs b/DataLoader/CS/MD5Helper.cs
index 4738a4b..b51a9e3 100644
--- a/DataLoader/CS/MD5Helper.cs
+++ b/DataLoader/CS/MD5Helper.cs
@@ -15,7 +15,7 @@
         /// </summary>
         /// <param name="pathName">鏂囦欢缁濆璺緞</param>
         /// <returns>MD5鏍¢獙鐮�</returns>
-        public static string getMD5Hash(string pathName)
+        public static string GetMD5Hash(string pathName)
         {
             try
             {
@@ -40,7 +40,7 @@
         /// </summary>
         /// <param name="buffer">寰呭瓧鑺傛暟缁�</param>
         /// <returns>MD5鏍¢獙鐮�</returns>
-        public static string getMD5Hash(byte[] buffer)
+        public static string GetMD5Hash(byte[] buffer)
         {
             MD5CryptoServiceProvider oMD5Hasher = new MD5CryptoServiceProvider();
             try
diff --git a/DataLoader/MainWindow.xaml.cs b/DataLoader/MainWindow.xaml.cs
index 587b2fe..8fb9b4a 100644
--- a/DataLoader/MainWindow.xaml.cs
+++ b/DataLoader/MainWindow.xaml.cs
@@ -19,9 +19,9 @@
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        private string _sourcePath;
+        private string _sourcePath= "D:\\LF\\data\\mdb";
 
-        private string _targetPath;
+        private string _targetPath= "D:\\LF\\upload";
 
         private LoginWin win;
 
@@ -70,8 +70,8 @@
             lvView.DataContext = viewDatas;
             lvView.SetBinding(ListView.ItemsSourceProperty, new Binding());
 
-            String file = "E:\\data\\mpt\\A4mpt20131125.mpt";
-            string md5 = MD5Helper.getMD5Hash(file);
+            //String file = "E:\\data\\mpt\\A4mpt20131125.mpt";
+            //string md5 = MD5Helper.GetMD5Hash(file);
         }
 
         // 鐧诲綍

--
Gitblit v1.9.3