| | |
| | | using 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) |
| | |
| | | |
| | | public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target, int start) |
| | | { |
| | | Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => |
| | | /*Parallel.ForEach(viewDatas, (vd, ParallelLoopState) => |
| | | { |
| | | try |
| | | { |
| | |
| | | |
| | | 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) |
| | |
| | | { |
| | | 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 = "元数据出错!"; |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | } |