From 994d438b4b06011eb7656357a367392bb30467c5 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期二, 22 八月 2023 17:33:33 +0800 Subject: [PATCH] 处理数据导入流程-1 --- DataLoader/MainWindow.xaml.cs | 73 +++++++++++------- DataLoader/CS/Importor.cs | 118 +++++++++++------------------ DataLoader/MainWindow.xaml | 2 DataLoader/CS/DBHelper.cs | 18 +++- DataLoader/Model/CommonProp.cs | 12 -- 5 files changed, 105 insertions(+), 118 deletions(-) diff --git a/DataLoader/CS/DBHelper.cs b/DataLoader/CS/DBHelper.cs index 13019a2..6d4fcbd 100644 --- a/DataLoader/CS/DBHelper.cs +++ b/DataLoader/CS/DBHelper.cs @@ -14,20 +14,30 @@ public class DBHelper { // id, eventid, metaid, verid, name, type, guid, path, sizes, tab, rows, create_user, create_time, update_user, update_time, bak, geom, layer, depcode, dircode, ismeta, sensortype, acq_time, resolution, gridsize, coor_sys, epsg, h_datum, mata_type, bands, band_type, ct - public static string insertMeta = "insert into lf.sys_meta (eventid, metaid, verid, name, type, guid, path, sizes, tab, rows, create_user, create_time, bak, geom, layer, depcode, dircode, ismeta, sensortype, acq_time, resolution, gridsize, coor_sys, epsg, h_datum, mata_type, bands, band_type, ct) values (@eventid, @metaid, @verid, @name, @type, @guid, @path, @sizes, @tab, @rows, @create_user, now(), @bak, @geom, @layer, @depcode, @dircode, @ismeta, @sensortype, @acq_time, @resolution, @gridsize, @coor_sys, @epsg, @h_datum, @mata_type, @bands, @band_type, @ct) returning id"; + private const string insertMeta = "insert into lf.sys_meta (eventid, metaid, verid, name, type, guid, path, sizes, tab, rows, create_user, create_time, bak, geom, layer, depcode, dircode, ismeta, sensortype, acq_time, resolution, gridsize, coor_sys, epsg, h_datum, mata_type, bands, band_type, ct) values (@eventid, @metaid, @verid, @name, @type, @guid, @path, @sizes, @tab, @rows, @create_user, now(), @bak, @geom, @layer, @depcode, @dircode, @ismeta, @sensortype, @acq_time, @resolution, @gridsize, @coor_sys, @epsg, @h_datum, @mata_type, @bands, @band_type, @ct) returning id"; /// <summary> /// 鎻掑叆鍏冩暟鎹� /// </summary> - public int InsertMeta(List<SysMeta> list) + public static int InsertMeta(SysMeta meta) + { + List<DbParameter> args = Tools.GetParams<SysMeta>(insertMeta, meta); + + return Tools.DBHelper.GetIntScalar(insertMeta, args.ToArray()); + } + + /// <summary> + /// 鎻掑叆鍏冩暟鎹� + /// </summary> + public static int InsertMetas(List<SysMeta> list) { PostgreHelper db = Tools.DBHelper; int count = 0; foreach (SysMeta meta in list) { - List<DbParameter> args = Tools.GetParams<SysMeta>(insertMeta, meta) - ; + List<DbParameter> args = Tools.GetParams<SysMeta>(insertMeta, meta); + int id = db.GetIntScalar(insertMeta, args.ToArray()); if (id > 0) count++; } diff --git a/DataLoader/CS/Importor.cs b/DataLoader/CS/Importor.cs index 6f5251a..0b5fd01 100644 --- a/DataLoader/CS/Importor.cs +++ b/DataLoader/CS/Importor.cs @@ -1,68 +1,68 @@ 锘縰sing DataLoader.Model; -using Npgsql; using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Data.Common; using System.IO; using System.Linq; -using System.Reflection; using System.Threading.Tasks; -using System.Windows.Forms; namespace DataLoader.CS { public class Importor { - private static List<String> Exclusions = new List<string>() { - "jp2.aux.xml", - ".midx", ".strmi", - ".rrd", ".img.aux.xml", ".hdr", ".img.enp", ".img.xml", - ".tfw", ".tif.ovr", ".tif.aux.xml", ".tif.enp", ".tif.xml", ".prj", - ".shx", ".dbf", ".cpg" //, ".prj" - }; - - private static PostgreHelper _ph = null; - - public static PostgreHelper Helper + /// <summary> + /// 鑾峰彇鏂囦欢 + /// </summary> + public static void GetFiles(ObservableCollection<ViewData> viewDatas) { - get + string[] files = Directory.GetFiles(CommonProp.SourcePath); + for (int i = 0, c = files.Length; i < c; i++) { - if (_ph == null) _ph = new PostgreHelper(); + 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); - return _ph; + SetSysMeta(vd); } } - private static readonly string insertMeta = "insert into lf.sys_meta (eventid, metaid, dirid, depid, verid, name, type, guid, path, sizes, tab, rows, create_user, create_time) values (@eventid, @metaid, @dirid, @depid, @verid, @name, @type, @guid, @path, @sizes, @tab, @rows, @create_user, now()) returning id"; - - public static string GetFilePathByGuid(string guid) + /// <summary> + /// 鏁版嵁瀵煎叆 + /// </summary> + public static void ImportFiles(ObservableCollection<ViewData> viewDatas) { - string sql = "select path from lf.sys_meta where guid = @guid limit 1"; + Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => + { + try + { + // - DbParameter dp = new NpgsqlParameter("@guid", guid); - object obj = Helper.GetScalar(sql, dp); + SysMeta meta = new SysMeta(); + // - return obj == null ? null : obj.ToString(); + int id = DBHelper.InsertMeta(meta); + vd.Status = id > 0 ? "鎴愬姛銆�" : "澶辫触锛�"; + } + catch (Exception ex) + { + LogOut.Error(ex.StackTrace); + vd.Status = string.Format("澶辫触锛歿0}锛�", ex.Message); + } + }); } - public static bool IsFileExists(string guid) + /// <summary> + /// 澶嶅埗鏂囦欢 + /// </summary> + private static void CopyFile(string source, string target) { - string sql = "select count(*) from lf.sys_meta where guid = @guid"; - - 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) - { - List<DbParameter> list = Tools.GetParams<SysMeta>(insertMeta, meta); - - object obj = Helper.GetScalar(insertMeta, list.ToArray()); - - return obj == null ? 0 : Convert.ToInt32(obj); + if (File.Exists(source)) + { + File.Copy(source, target, true); + } } public static void GetFiles(ObservableCollection<ViewData> viewDatas, string source) @@ -97,7 +97,7 @@ public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target, int start) { - Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => + /*Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => { try { @@ -118,19 +118,7 @@ vd.Status = "鍑嗗鍏ュ簱"; vd.Meta = mf; - } - catch (Exception ex) - { - LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); - vd.Status = "澶辫触锛�"; - } - }); - - foreach (ViewData vd in viewDatas) - { - int metaId = InsertMeta(vd.Meta); - vd.Status = metaId == 0 ? "鍏冩暟鎹嚭閿欙紒" : "瀹屾垚銆�"; - } + });*/ } public static void ImportFiles2(ObservableCollection<ViewData> viewDatas, string target) @@ -140,19 +128,11 @@ { try { - vd.Status = "鐢熸垚MD5鐮�..."; - string guid = MD5Helper.GetMD5Hash(vd.FilePath); - if (IsFileExists(guid)) - { - vd.Status = "宸插瓨鍦紒"; - continue; - } - vd.Status = "鎻掑叆鏁版嵁搴�..."; start = GetSubPath(target, start); - SysMeta mf = GetMetaFile(vd, start, guid); + SysMeta mf = GetMetaFile(vd, start, ""); - int metaId = InsertMeta(mf); + int metaId = 0; // InsertMeta(mf); if (metaId == 0) { vd.Status = "鍏冩暟鎹嚭閿欙紒"; @@ -211,14 +191,6 @@ 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/MainWindow.xaml b/DataLoader/MainWindow.xaml index d62efb8..1f05831 100644 --- a/DataLoader/MainWindow.xaml +++ b/DataLoader/MainWindow.xaml @@ -1,7 +1,7 @@ 锘�<Window x:Class="DataLoader.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - Height="700" Width="900" Loaded="Window_Loaded" + Height="700" Width="900" Loaded="Window_Loaded" Closing="Window_Closing" Title="鏁版嵁鍏ュ簱 v1.5" WindowStartupLocation="CenterScreen"> <Window.Resources> <Style x:Key="btn" TargetType="Button"> diff --git a/DataLoader/MainWindow.xaml.cs b/DataLoader/MainWindow.xaml.cs index 6610d73..1e1af95 100644 --- a/DataLoader/MainWindow.xaml.cs +++ b/DataLoader/MainWindow.xaml.cs @@ -15,6 +15,8 @@ { public partial class MainWindow : Window { + private bool isBusy; + public ResWin resWin; private LoginWin loginWin; @@ -32,8 +34,6 @@ CommonProp.Owner = this; CommonProp.Init(); - //this.cbMetaType.ItemsSource= GetMetaTypes(); - //this.cbMetaType.SelectedIndex= 0; this.btnLoad.IsEnabled = false; this.btnImport.IsEnabled = false; @@ -59,7 +59,7 @@ return list; } - // 璁剧疆 + // 鐧诲綍 private void Login_MouseLeftButtonDown(object sender, RoutedEventArgs e) { loginWin = new LoginWin(); @@ -67,6 +67,7 @@ loginWin.Show(); } + // 璁剧疆鐧诲綍淇℃伅 public void SetLoginInfo() { if (loginWin != null) loginWin.Close(); @@ -88,6 +89,7 @@ this.resWin.Show(); } + // 璁剧疆璧勬簮淇℃伅 public void SetRes(string dircode, string fullName) { this.Show(); @@ -126,40 +128,41 @@ // 鍔犺浇 private void Load_MouseLeftButtonDown(object sender, RoutedEventArgs e) { - //if (string.IsNullOrEmpty(SourcePath) || !Directory.Exists(SourcePath)) - //{ - // MessageBox.Show("璇烽�夋嫨鏁版嵁鐩綍锛�", "鎻愮ず"); - // return; - //} - //if (string.IsNullOrEmpty(TargetPath) || !Directory.Exists(TargetPath)) - //{ - // MessageBox.Show("璇烽�夋嫨鍏ュ簱鐩綍锛�", "鎻愮ず"); - // return; - //} + if (isBusy) return; + if (string.IsNullOrEmpty(CommonProp.Dircode)) + { + MessageBox.Show("璇烽�夋嫨璧勬簮鐩綍锛�", "鎻愮ず"); + return; + } + if (string.IsNullOrEmpty(CommonProp.SourcePath) || !Directory.Exists(CommonProp.SourcePath)) + { + MessageBox.Show("璇烽�夋嫨鏁版嵁鐩綍鎴栨暟鎹洰褰曚笉瀛樺湪锛�", "鎻愮ず"); + return; + } + if (string.IsNullOrEmpty(CommonProp.TargetPath) || !Directory.Exists(CommonProp.TargetPath)) + { + MessageBox.Show("璇烽�夋嫨鍏ュ簱鐩綍鎴栧叆搴撶洰褰曚笉瀛樺湪锛�", "鎻愮ず"); + return; + } + CommonProp.SensorType = this.tbSensorType.Text.Trim(); + ContentControl cc = this.cbMetaType.SelectedItem as ContentControl; + CommonProp.MetaType = null == cc || "璇烽�夋嫨" == cc.Content.ToString() ? string.Empty : cc.Content.ToString(); + CommonProp.AcqTime = this.dpAcqTime.SelectedDate; - //string[] files = Directory.GetFiles(SourcePath); - //if (null == files || files.Length == 0) - //{ - // MessageBox.Show("鏁版嵁鐩綍涓嬫病鏈夋枃浠讹紒", "鎻愮ず"); - // return; - //} - - //this.viewDatas.Clear(); - //Importor.GetFiles(viewDatas, SourcePath); + this.isBusy = true; + this.viewDatas.Clear(); + Importor.GetFiles(viewDatas); + this.isBusy = false; } - // 瀵煎叆 private void Import_MouseLeftButtonDown(object sender, RoutedEventArgs e) { - if (viewDatas.Count == 0) return; + if (isBusy || viewDatas.Count == 0) return; - //string str = this.tbStart.Text.Trim(); - //int start = 1; - //int.TryParse(str, out start); - + this.isBusy = true; Thread thread = new Thread(new ThreadStart(() => { - //Importor.ImportFiles(viewDatas, TargetPath, start); + Importor.ImportFiles(viewDatas); lvView.Dispatcher.BeginInvoke((ThreadStart)delegate { @@ -167,9 +170,21 @@ view.Refresh(); }); + isBusy = false; MessageBox.Show("鏁版嵁瀵煎叆瀹屾垚锛�", "鎻愮ず"); })); thread.Start(); } + + // 绐楀彛鍏抽棴涓簨浠� + private void Window_Closing(object sender, CancelEventArgs e) + { + if (isBusy) + { + MessageBox.Show("姝e湪瀵煎叆鏁版嵁锛岃绋嶅悗~", "鎻愮ず"); + e.Cancel = true; + return; + } + } } } diff --git a/DataLoader/Model/CommonProp.cs b/DataLoader/Model/CommonProp.cs index 5a38914..314e199 100644 --- a/DataLoader/Model/CommonProp.cs +++ b/DataLoader/Model/CommonProp.cs @@ -76,16 +76,6 @@ public static string Dirname; /// <summary> - /// 浼犳劅鍣ㄧ被鍨� - /// </summary> - public static string sensortype; - - /// <summary> - /// 閲囬泦鏃堕棿 - /// </summary> - public static DateTime acq_time = DateTime.Now; - - /// <summary> /// 鍏冩暟鎹被鍨� /// </summary> public static string MetaType; @@ -108,7 +98,7 @@ /// <summary> /// 閲囬泦鏃堕棿 /// </summary> - public static DateTime AcqTime; + public static DateTime? AcqTime; /*private string _sourcePath; -- Gitblit v1.9.3