管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-02-21 e608048cf0ca1456e8c21ee54a97c7550eb7c3db
DataLoader/CS/Importor.cs
@@ -7,12 +7,21 @@
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
@@ -25,13 +34,11 @@
            }
        }
        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 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)
        {
            string sql = "select path from lf.sys_meta_file where guid = @guid limit 1";
            string sql = "select path from lf.sys_meta where guid = @guid limit 1";
            DbParameter dp = new NpgsqlParameter("@guid", guid);
            object obj = Helper.GetScalar(sql, dp);
@@ -41,7 +48,7 @@
        public static bool IsFileExists(string guid)
        {
            string sql = "select count(*) from lf.sys_meta_file where guid = @guid limit 1";
            string sql = "select count(*) from lf.sys_meta where guid = @guid";
            DbParameter dp = new NpgsqlParameter("@guid", guid);
            object obj = Helper.GetScalar(sql, dp);
@@ -51,19 +58,9 @@
        public static int InsertMeta(SysMeta meta)
        {
            // string sql = "INSERT INTO public.data_files(mid, guid, name, ext, path, subs, remark) VALUES (@mid, @guid, @name, @ext, @path, @subs, @remark) returning id";
            List<DbParameter> list = Tool.GetParams<SysMeta>(insertMeta, meta);
            object obj = Helper.GetScalar(insertMeta, list.ToArray());
            return obj == null ? 0 : Convert.ToInt32(obj);
        }
        public static int InsertMetaFile(SysMetaFile metaFile)
        {
            List<DbParameter> list = Tool.GetParams<SysMetaFile>(insertMetaFile, metaFile);
            object obj = Helper.ExecuteNonQuery(insertMetaFile, list.ToArray());
            return obj == null ? 0 : Convert.ToInt32(obj);
        }
@@ -76,6 +73,7 @@
                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);
@@ -89,21 +87,53 @@
            vd.Meta = new SysMeta();
            vd.Meta.name = fi.Name;
            vd.Meta.dirid = 0;
            vd.Meta.depid = 0;
            vd.Meta.dirid = Tool.DirId;
            vd.Meta.depid = 1;
            vd.Meta.verid = 0;
            vd.Meta.type = "file";
            vd.Meta.type = fi.Extension.ToLower().Replace(".", "");
            vd.Meta.sizes = Tool.SizeToMb(fi.Length);
            vd.Meta.cs = null;
            vd.Meta.scale = null;
            vd.Meta.resolution = null;
            vd.Meta.gather = DateTime.Now;
            vd.Meta.batch = null;
            vd.Meta.descr = null;
            vd.Meta.create_user = Tool.UserId;
        }
        public static void ImportFiles(ObservableCollection<ViewData> viewDatas, string target)
        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;
                }
                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)
        {
            int start = 1;
            foreach (ViewData vd in viewDatas)
@@ -119,16 +149,15 @@
                    }
                    vd.Status = "插入数据库...";
                    int metaId = InsertMeta(vd.Meta);
                    start = GetSubPath(target, start);
                    SysMeta mf = GetMetaFile(vd, start, guid);
                    int metaId = InsertMeta(mf);
                    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));
@@ -137,6 +166,7 @@
                }
                catch (Exception ex)
                {
                    LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                    vd.Status = "失败!";
                }
            }
@@ -163,15 +193,21 @@
            }
        }
        private static SysMetaFile GetMetaFile(ViewData vd, int metaId, int subPath, string guid)
        private static SysMeta GetMetaFile(ViewData vd, int subPath, string guid)
        {
            SysMetaFile mf = new SysMetaFile();
            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.metaid = metaId;
            mf.fileid = 0;
            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;