管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-01-17 d633d8200bacc4a05996ba367d81e1dc6e60d572
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,13 +25,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";
            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);
@@ -38,9 +37,18 @@
            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)
        {
            // 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());
@@ -48,26 +56,9 @@
            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);
        }
        public static void Import(ObservableCollection<ViewData> viewDatas, string source, string target)
        public static void GetFiles(ObservableCollection<ViewData> viewDatas, string source)
        {
            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();
@@ -86,18 +77,99 @@
            vd.Meta = new SysMeta();
            vd.Meta.name = fi.Name;
            vd.Meta.dirid = 0;
            vd.Meta.depid = 0;
            vd.Meta.dirid = 1;
            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)
        {
            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 = "插入数据库...";
                    start = GetSubPath(target, start);
                    SysMeta mf = GetMetaFile(vd, start, guid);
                    int metaId = InsertMeta(mf);
                    if (metaId == 0)
                    {
                        vd.Status = "元数据出错!";
                        continue;
                    }
                    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 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;
        }
        private static void CopyFile(string source, string target)
        {
            if (File.Exists(source))
            {
                File.Copy(source, target, true);
            }
        }
    }
}