管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-08-23 5f6661195e3abab67149e2eaca61ac4bf17f9b1f
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using DataLoader.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
 
namespace DataLoader.CS
{
    public class Importor
    {
        #region 加载
        /// <summary>
        /// 获取文件
        /// </summary>
        public static void GetFiles(ObservableCollection<ViewData> viewDatas, string path)
        {
            GetSubFiles(viewDatas, path);
 
            string[] directories = Directory.GetDirectories(path);
            if (null == directories || directories.Length == 0) return;
 
            foreach (string sub in directories)
            {
                if (IsGdbFile(sub))
                {
                    SetSysMeta(viewDatas, sub, "gdb");
                    continue;
                }
                if (IsOsgbFile(sub))
                {
                    SetSysMeta(viewDatas, sub, "osgb");
                    continue;
                }
 
                GetFiles(viewDatas, sub);
            }
        }
 
        /// <summary>
        /// 设置元数据
        /// </summary>
        private static void SetSysMeta(ObservableCollection<ViewData> viewDatas, string path, string ext)
        {
            ViewData vd = new ViewData();
            vd.ID = viewDatas.Count + 1;
            vd.FilePath = path;
            vd.Ext = "." + ext;
            vd.Status = "准备";
            viewDatas.Add(vd);
 
            SetSysMeta(vd);
 
            DirectoryInfo di = new DirectoryInfo(vd.FilePath);
            vd.Meta.name = di.Name;
            vd.Meta.type = ext;
        }
 
        /// <summary>
        /// 获取子文件
        /// </summary>
        private static void GetSubFiles(ObservableCollection<ViewData> viewDatas, string path)
        {
            string[] files = Directory.GetFiles(path);
            if (null == files || files.Length == 0) return;
 
            foreach (string file in files)
            {
                string ext = Path.GetExtension(file).ToLower();
                if (!StaticData.ALL_EXTENSION.Contains(ext) || IsExcludeFile(file)) continue;
 
                ViewData vd = new ViewData();
                vd.ID = viewDatas.Count + 1;
                vd.FilePath = file;
                vd.Ext = ext;
                vd.Status = "准备";
                viewDatas.Add(vd);
 
                SetSysMeta(vd);
 
                FileInfo fi = new FileInfo(vd.FilePath);
                vd.Meta.name = fi.Name;
                vd.Meta.type = fi.Extension.ToLower().Replace(".", "");
            }
        }
 
        /// <summary>
        /// 设置元数据
        /// </summary>
        private static void SetSysMeta(ViewData vd)
        {
            SysMeta meta = new SysMeta();
            meta.eventid = Guid.NewGuid().ToString();
            meta.metaid = 0;
            meta.verid = 0;
            meta.guid = null;
            meta.path = vd.FilePath; // *
            meta.sizes = GetFileSizes(vd);
            meta.tab = null;
            meta.rows = 0;
            meta.create_user = CommonProp.UserId;
            meta.create_time = DateTime.Now;
            meta.bak = null;
            meta.geom = null;
            meta.layer = null;
            meta.depcode = CommonProp.Depcode;
            meta.dircode = CommonProp.Dircode;
            meta.ismeta = 0;
            meta.sensortype = CommonProp.SensorType;
            meta.acq_time = CommonProp.AcqTime;
 
            vd.Meta = meta;
            vd.Sizes = meta.sizes;
        }
 
        /// <summary>
        /// 获取文件大小
        /// </summary>
        private static double GetFileSizes(ViewData vd)
        {
            if (File.Exists(vd.FilePath))
            {
                if (vd.Ext == StaticData.SHP)
                {
                    List<string> files = GetShpFiles(vd.FilePath);
                    return GetFileSizes(files.ToArray());
                }
                else
                {
                    FileInfo fi = new FileInfo(vd.FilePath);
                    return Tools.SizeToMb(fi.Length);
                }
            }
 
            if (Directory.Exists(vd.FilePath))
            {
                string[] files = Directory.GetFiles(vd.FilePath, "*", SearchOption.AllDirectories);
 
                return GetFileSizes(files);
            }
 
            return 0;
        }
 
        /// <summary>
        /// 获取文件大小
        /// </summary>
        private static double GetFileSizes(string[] files)
        {
            if (null == files || files.Length == 0) return 0;
 
            double sizes = 0;
            foreach (string file in files)
            {
                FileInfo fi = new FileInfo(file);
                sizes += Tools.SizeToMb(fi.Length);
            }
 
            return sizes;
        }
 
        /// <summary>
        /// 获取SHP文件集合
        /// </summary>
        private static List<string> GetShpFiles(string shpPath)
        {
            List<string> files = new List<string>();
            files.Add(shpPath);
 
            foreach (string ext in StaticData.SHP_EXT)
            {
                string file = shpPath.Replace(".shp", ext);
                if (File.Exists(file))
                {
                    files.Add(file);
                }
            }
 
            return files;
        }
 
        /// <summary>
        /// 是否为GDB文件
        /// </summary>
        private static bool IsGdbFile(string path)
        {
            if (!Directory.Exists(path)) return false;
 
            DirectoryInfo info = new DirectoryInfo(path);
            if (!info.Name.ToLower().EndsWith(StaticData.GDB)) return false;
 
            string gdb = Path.Combine(path, StaticData.NGDB);
 
            return File.Exists(gdb);
        }
 
        /// <summary>
        /// 是否为OSGB文件
        /// </summary>
        private static bool IsOsgbFile(string path)
        {
            if (!Directory.Exists(path)) return false;
 
            string meta = Path.Combine(path, "metadata.xml");
            string data = Path.Combine(path, "Data");
 
            return File.Exists(meta) && Directory.Exists(data);
        }
 
        /// <summary>
        /// 是/否为排除文件
        /// </summary>
        private static bool IsExcludeFile(string file)
        {
            bool isExclude = false;
 
            string name = Path.GetFileName(file).ToLower();
            foreach (string ext in StaticData.MAPPER_EXCLUDE_EXT)
            {
                if (name.EndsWith(ext))
                {
                    isExclude = true;
                    break;
                }
            }
 
            return isExclude;
        }
 
        /// <summary>
        /// 获取文件的MD5码
        /// </summary>
        private static string GetFileMD5(string file)
        {
            if ("1" != Tools.GetSetting("GetMD5") || !File.Exists(file)) return null;
 
            return MD5Helper.GetMD5Hash(file);
        }
 
        /// <summary>
        /// 获取多个文件的MD5码
        /// </summary>
        private static string GetFilesMD5(string path)
        {
            if ("1" != Tools.GetSetting("GetMD5") || !Directory.Exists(path)) return null;
 
            string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            if (null == files || files.Length == 0) return null;
 
            List<string> list = new List<string>();
            foreach (string file in files)
            {
                string md5 = MD5Helper.GetMD5Hash(file);
                if (string.IsNullOrEmpty(md5)) list.Add(md5);
            }
 
            if (list.Count == 0) return null;
 
            string str = string.Join(",", list.ToArray());
            byte[] bytes = Encoding.Unicode.GetBytes(str);
 
            return MD5Helper.GetMD5Hash(bytes);
        }
        #endregion
 
        #region 导入
        /// <summary>
        /// 导入文件
        /// </summary>
        public static void ImportFiles(ObservableCollection<ViewData> viewDatas)
        {
            Parallel.ForEach(viewDatas, (vd, ParallelLoopState) =>
            {
                try
                {
                    //
 
                    SysMeta meta = new SysMeta();
                    //
 
                    int id = DBHelper.InsertMeta(meta);
                    vd.Status = id > 0 ? "成功。" : "失败!";
                }
                catch (Exception ex)
                {
                    LogOut.Error(ex.StackTrace);
                    vd.Status = string.Format("失败:{0}!", ex.Message);
                }
            });
        }
 
        /// <summary>
        /// 复制文件
        /// </summary>
        private static void CopyFile(string source, string target)
        {
            if (File.Exists(source))
            {
                File.Copy(source, target, true);
            }
        }
 
        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;
            });*/
        }
 
        public static void ImportFiles2(ObservableCollection<ViewData> viewDatas, string target)
        {
            int start = 1;
            foreach (ViewData vd in viewDatas)
            {
                try
                {
                    vd.Status = "插入数据库...";
                    start = GetSubPath(target, start);
                    SysMeta mf = GetMetaFile(vd, start, "");
 
                    int metaId = 0; // InsertMeta(mf);
                    if (metaId == 0)
                    {
                        vd.Status = "元数据出错!";
                        continue;
                    }
 
                    vd.Status = "复制文件...";
                    CopyFile(vd.FilePath, Path.Combine(target, mf.path));
 
                    vd.Status = "完成。";
                }
                catch (Exception ex)
                {
                    LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                    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;
        }
        #endregion
    }
}