管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-01-01 979ba3f2d4da1a69b9ecc7c4ef1975bc7e110148
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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.Windows.Forms;
 
namespace DataLoader.CS
{
    public class Importor
    {
        private static PostgreHelper _ph = null;
 
        public static PostgreHelper Helper
        {
            get
            {
                if (_ph == null) _ph = new PostgreHelper(DbEnum.langfang);
 
                return _ph;
            }
        }
 
        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)";
 
        public static string GetFilePathByGuid(string guid)
        {
            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)
        {
            // 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);
        }
 
        public static void Import(ObservableCollection<ViewData> viewDatas, string source, string target)
        {
            string[] files = Directory.GetFiles(source);
            for (int i = 0, c = files.Length; i < c; i++)
            {
                ViewData vd = new ViewData();
                vd.ID = i + 1;
                vd.FilePath = files[i];
                vd.Status = "准备";
                viewDatas.Add(vd);
 
                SetSysMeta(vd);
            }
 
            ImportFiles(viewDatas, target);
        }
 
        private static void SetSysMeta(ViewData vd)
        {
            FileInfo fi = new FileInfo(vd.FilePath);
 
            vd.Meta = new SysMeta();
            vd.Meta.name = fi.Name;
            vd.Meta.dirid = 0;
            vd.Meta.depid = 0;
            vd.Meta.verid = 0;
            vd.Meta.type = "file";
            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 = "插入数据库";
                    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);
            }
        }
    }
}