月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-10 9cc20b867c7f7310626af51f3a79aca3fba84935
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package com.moon.server.service.data;
 
import com.google.common.collect.Lists;
import com.moon.server.entity.all.BaseEntity;
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.data.*;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.helper.*;
import com.moon.server.mapper.all.BasicMapper;
import com.moon.server.mapper.data.UploadMapper;
import com.moon.server.service.all.BaseQueryService;
import com.moon.server.service.all.BaseUploadService;
import com.moon.server.service.sys.DepService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 数据上传服务类
 *
 * @author WWW
 */
@Service
public class UploadService extends BaseUploadService implements UploadMapper {
    @Autowired
    UploadMapper uploadMapper;
 
    @Autowired
    FmeService fmeService;
 
    @Autowired
    MetaService metaService;
 
    @Autowired
    DepService depService;
 
    @Autowired
    DirService dirService;
 
    @Resource
    ReadRasterService readRasterService;
 
    @Override
    public List<CoordEntity> selectCoords(String zoning) {
        return uploadMapper.selectCoords(zoning);
    }
 
    @Override
    public Integer selectCount4Coord(String epsgCode) {
        return uploadMapper.selectCount4Coord(epsgCode);
    }
 
    @Override
    public List<DirEntity> selectProject() {
        return uploadMapper.selectProject();
    }
 
    @Override
    public List<FmeLogEntity> selectFmeLog(String parentid) {
        return uploadMapper.selectFmeLog(parentid);
    }
 
    /**
     * 插入文件
     */
    public void insertFiles(UserEntity ue, List<MetaFileEntity> list, HttpServletRequest req) {
        checkMetaFiles(ue, list);
        readRasterInfo(list);
        loadData(list);
        copyFiles(list);
        insertMetas(list);
    }
 
    /**
     * 检查元数据文件
     */
    private void checkMetaFiles(UserEntity ue, List<MetaFileEntity> list) {
        Timestamp createTime = WebHelper.getCurrentTimestamp();
        String tempPath = pathHelper.getConfig().getTempPath();
 
        for (MetaFileEntity mf : list) {
            mf.setCreateUser(ue.getId());
            mf.setCreateTime(createTime);
            mf.setDepcode(ue.getDepcode());
            mf.setPath(tempPath + File.separator + mf.getPath());
            mf.setMsg(null);
 
            File f = new File(mf.getPath());
            if (!f.exists()) {
                mf.setMsg("文件丢失");
            }
            if (f.exists() && StringHelper.isNull(mf.getGuid())) {
                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
            }
 
            MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), null);
            if (null != old) {
                mf.setMsg("已存在");
            }
        }
    }
 
    /**
     * 读取栅格信息
     */
    private void readRasterInfo(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg()) {
                continue;
            }
 
            if (StaticData.RASTER_EXT.contains("." + mf.getType())) {
                readRasterService.readRasterInfo(mf, mf.getPath());
            }
        }
    }
 
    /**
     * 获取目录编码
     */
    private String getDirCode(MetaFileEntity mf) {
        if (StringHelper.isEmpty(mf.getDircode())) {
            return null;
        }
 
        return StringHelper.getRightLike(mf.getDircode().substring(0, 2));
    }
 
    /**
     * 获取Excel元数据文件
     */
    private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) {
        List<MetaFileEntity> xlsList = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            if (null == mf.getMsg() && isExcel(mf)) {
                xlsList.add(mf);
            }
        }
        if (xlsList.isEmpty()) {
            return xlsList;
        }
 
        String xlsBasePath = getXlsPath(xlsList.get(0).getPath());
        for (int i = 0, c = xlsList.size(); i < c; i++) {
            MetaFileEntity mf = xlsList.get(i);
            String xlsPath = copyXlsFile(xlsBasePath, i, mf);
            mf.setXlsPath(xlsPath);
        }
 
        return xlsList;
    }
 
    /**
     * 是/否为Excel
     */
    private boolean isExcel(MetaFileEntity mf) {
        return StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName());
    }
 
    /**
     * 获取Xls目录
     */
    private String getXlsPath(String filePath) {
        String tempPath = pathHelper.getConfig().getTempPath() + File.separator;
        String subPath = filePath.substring(tempPath.length());
        subPath = tempPath + subPath.substring(0, subPath.indexOf(File.separator)).replace("_zip", "") + "_xls";
 
        File f = new File(subPath);
        if (!f.exists() || !f.isDirectory()) {
            f.mkdirs();
        }
 
        return subPath;
    }
 
    /**
     * 复制Xls文件
     */
    private String copyXlsFile(String xlsBasePath, int i, MetaFileEntity mf) {
        try {
            String xlsPath = xlsBasePath + File.separator + i;
 
            File file = new File(xlsPath);
            if (!file.exists() || !file.isDirectory()) {
                file.mkdirs();
            }
            file = new File(mf.getPath());
 
            File newFile = new File(xlsPath + File.separator + FileHelper.getFileName(file.getPath()));
            FileUtils.copyFile(file, newFile);
 
            return newFile.getPath();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 加载数据
     */
    private void loadData(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg() || StringHelper.isEmpty(mf.getEntity())) {
                continue;
            }
            if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) {
                loadSpatialData(mf);
            }
        }
    }
 
    /**
     * 加载空间数据
     */
    public void loadSpatialData(MetaFileEntity mf) {
        BasicMapper basicMapper = ClassHelper.getBasicMapper(mf.getEntity());
        if (null == basicMapper) {
            return;
        }
 
        String tabName = BaseQueryService.getTabName(basicMapper);
        String className = ClassHelper.getClassName(basicMapper);
        Class clazz = ClassHelper.getEntityClass(className);
        if (null == clazz || null == tabName) {
            return;
        }
 
        File file = new File(mf.getPath());
        if (!file.exists()) {
            mf.setMsg("文件丢失");
            return;
        }
 
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), getDirCode(mf), tabName);
        if (null != old) {
            mf.setMsg("已存在");
            return;
        }
 
        List<?> list;
        if (StaticData.SHP.equals(mf.getExtName())) {
            list = ShpHelper.readData(clazz, mf.getPath(), true);
        } else {
            list = GdbHelper.readData(clazz, mf.getPath(), mf.getTab(), true);
        }
        if (null == list || list.isEmpty()) {
            return;
        }
        mf.setRecords(list.size());
        setCreateInfo(list, mf);
 
        int rows = batchInserts(basicMapper, list);
        if (rows > 0) {
            mf.setEntity(mf.getTab());
            mf.setTab(tabName);
            mf.setRows(rows);
        }
    }
 
    /**
     * 批量插入
     */
    private <T> int batchInserts(BasicMapper basicMapper, List<T> list) {
        int rows = 0;
        //int count = (int) Math.ceil(list.size() / StaticData.D100)
        List<List<T>> subLists = Lists.partition(list, StaticData.I50);
        for (List<T> sub : subLists) {
            try {
                rows += basicMapper.insertBatch(sub);
            } catch (Exception ex) {
                log.error(ex);
            }
        }
 
        return rows;
    }
 
    /**
     * 设置创建信息
     */
    private <T> void setCreateInfo(List<T> list, MetaFileEntity mf) {
        try {
            if (!(list.get(0) instanceof BaseEntity)) {
                return;
            }
 
            for (T t : list) {
                BaseEntity be = (BaseEntity) t;
                be.setEventid(StringHelper.getGuid());
                be.setParentid(mf.getEventid());
                be.setDirid(mf.getDircode());
                be.setDepid(mf.getDepcode());
                be.setVerid(mf.getVerid());
                be.setCreateuser(mf.getCreateUser());
                be.setCreatetime(mf.getCreateTime());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 复制文件
     */
    private void copyFiles(List<MetaFileEntity> list) {
        List<String> gdbList = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg()) {
                continue;
            }
 
            switch (mf.getExtName()) {
                case StaticData.MPT:
                    copyMultiFile(mf, StaticData.MPT_EXT);
                    break;
                case StaticData.JPG:
                    copyMultiFile(mf, StaticData.JPG_EXT);
                    break;
                case StaticData.JP2:
                    copyMultiFile(mf, StaticData.JP2_EXT);
                    break;
                case StaticData.IMG:
                    copyMultiFile(mf, StaticData.IMG_EXT);
                    break;
                case StaticData.TIF:
                    copyMultiFile(mf, StaticData.TIF_EXT);
                    break;
                case StaticData.TIFF:
                    copyMultiFile(mf, StaticData.TIFF_EXT);
                    break;
                case StaticData.SHP:
                    copyMultiFile(mf, StaticData.SHP_EXT);
                    break;
                case StaticData.OSGB:
                    copyFolderFile(mf);
                    break;
                case StaticData.GDB:
                    if (gdbList.contains(mf.getPath())) {
                        String path = findPathByGuid(list, mf);
                        if (null != path) {
                            mf.setPath(path);
                            continue;
                        }
                    }
 
                    gdbList.add(mf.getPath());
                    copyFolderFile(mf);
                    break;
                default:
                    copySingleFile(mf);
                    break;
            }
        }
    }
 
    /**
     * 复制单个文件
     */
    private int copySingleFile(MetaFileEntity mf) {
        File file = new File(mf.getPath());
        if (!file.exists()) {
            mf.setMsg("文件丢失");
            return -1;
        }
 
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null);
        if (null != old) {
            // mf.setMsg("已存在")
            setOldMeta(mf, old);
            file.delete();
            return 0;
        }
 
        String uploadPath = pathHelper.getUploadFullPath();
        String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
        String subPath = FileHelper.getRelativePath(targetPath);
 
        File newFile = new File(targetPath);
        if (newFile.exists()) {
            mf.setPath(subPath);
            file.delete();
            return 0;
        }
 
        file.renameTo(newFile);
        mf.setPath(subPath);
 
        return 1;
    }
 
    /**
     * 设置旧元数据信息
     */
    private void setOldMeta(MetaFileEntity mf, MetaEntity old) {
        mf.setPath(old.getPath());
        mf.setTab(old.getTab());
        mf.setRows(old.getRows());
        mf.setEntity(old.getEventid());
    }
 
    /**
     * 复制多个文件
     */
    private void copyMultiFile(MetaFileEntity mf, List<String> extList) {
        String path = mf.getPath();
        int status = copySingleFile(mf);
        if (status < 1) {
            for (int i = 0, c = extList.size(); i < c; i++) {
                String subPath = path.replace(mf.getExtName(), extList.get(i));
                File file = new File(subPath);
                if (file.exists()) {
                    file.delete();
                }
            }
            return;
        }
 
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (int i = 0, c = extList.size(); i < c; i++) {
            String sourcePath = path.replace(mf.getExtName(), extList.get(i));
            File file = new File(sourcePath);
            if (!file.exists()) {
                continue;
            }
 
            String targetPath = uploadPath + File.separator + mf.getPath().replace(mf.getExtName(), extList.get(i));
            File newFile = new File(targetPath);
            if (newFile.exists()) {
                continue;
            }
 
            file.renameTo(newFile);
        }
    }
 
    /**
     * 复制文件夹文件
     */
    private void copyFolderFile(MetaFileEntity mf) {
        File file = new File(mf.getPath());
        if (!file.exists()) {
            mf.setMsg("文件丢失");
            return;
        }
 
        MetaEntity old = metaService.selectByGuid(mf.getGuid(), null, null);
        if (null != old) {
            // mf.setMsg("已存在")
            setOldMeta(mf, old);
            FileHelper.deleteFiles(file);
            return;
        }
 
        String uploadPath = pathHelper.getUploadFullPath();
        String targetPath = uploadPath + File.separator + mf.getGuid() + mf.getExtName();
        String subPath = FileHelper.getRelativePath(targetPath);
 
        File newFile = new File(targetPath);
        if (newFile.exists() && newFile.isDirectory()) {
            mf.setPath(subPath);
            FileHelper.deleteFiles(file);
            return;
        }
 
        try {
            // newFile.mkdirs()
            FileUtils.moveDirectory(file, newFile);
        } catch (Exception ex) {
            log.error(ex);
        }
        mf.setPath(subPath);
    }
 
    /**
     * 根据GUID查找路径
     */
    private String findPathByGuid(List<MetaFileEntity> list, MetaFileEntity mf) {
        for (MetaFileEntity meta : list) {
            if (meta.getGuid().equals(mf.getGuid()) && !meta.getPath().equals(mf.getPath())) {
                return meta.getPath();
            }
        }
 
        return null;
    }
 
    /**
     * 创建文件链接
     */
    private void createFileLink(String source, String target) {
        String cmd = String.format("cmd /c mklink \"%s\" \"%s\"", target, source);
        WebHelper.exec(cmd);
    }
 
    /**
     * 插入元数据
     */
    private void insertMetas(List<MetaFileEntity> list) {
        int metaId = insertParentMeta(list);
 
        List<String> guids = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg()) {
                continue;
            }
 
            MetaEntity me = createMeta(mf, metaId);
            if (StaticData.NGDB.equals(me.getType())) {
                if (guids.contains(me.getGuid())) {
                    me.setIsmeta((short) -1);
                } else {
                    guids.add(me.getGuid());
                }
            }
 
            metaService.insert(me);
            String err = mf.getRows() < mf.getRecords() ? "(" + (mf.getRecords() - mf.getRows()) + " 条失败)" : "";
            mf.setMsg(me.getId() > 0 ? String.format("成功%s", err) : "失败");
        }
    }
 
    /**
     * 创建元数据
     */
    private MetaEntity createMeta(MetaFileEntity mf, int metaId) {
        MetaEntity me = new MetaEntity();
        me.setMetaid(metaId);
        me.setEventid(mf.getEventid());
        me.setDircode(mf.getDircode());
        me.setDepcode(mf.getDepcode());
        me.setVerid(mf.getVerid());
        me.setName(mf.getName());
        me.setType(mf.getType());
        me.setGuid(mf.getGuid());
        me.setPath(mf.getPath());
        me.setSizes(mf.getSizes());
        if (mf.getRows() > 0) {
            me.setTab(mf.getTab());
        }
        if (!StringHelper.isEmpty(mf.getEntity())) {
            me.setLayer(mf.getEntity());
        }
        me.setRows(mf.getRows());
        me.setCreateUser(mf.getCreateUser());
        me.setCreateTime(mf.getCreateTime());
        // 栅格信息
        me.setGeom(StringHelper.isEmpty(mf.getGeom()) ? "null" : mf.getGeom());
        me.setSensortype(mf.getSensortype());
        me.setAcqTime(mf.getAcqTime());
        me.setResolution(mf.getResolution());
        me.setGridsize(mf.getGridsize());
        me.setCoorSys(mf.getCoorSys());
        me.setEpsg(mf.getEpsg());
        me.sethDatum(mf.gethDatum());
        me.setMataType(mf.getMataType());
        me.setBands(mf.getBands());
        me.setBandType(mf.getBandType());
        me.setCt(mf.getCt());
 
        return me;
    }
 
    /**
     * 插入父元数据
     */
    private int insertParentMeta(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (null != mf.getMsg() || !mf.getIsMeta()) {
                continue;
            }
 
            MetaEntity me = createMeta(mf, 0);
            me.setIsmeta((short) 1);
            metaService.insert(me);
            mf.setMsg(me.getId() > 0 ? "成功" : "失败");
 
            return me.getId();
        }
 
        return 0;
    }
 
    /**
     * Excel入库
     */
    private String excelLoader(List<MetaFileEntity> xlsList, HttpServletRequest req) {
        try {
            MetaFileEntity xlsMeta = getExcelMeta(xlsList);
            if (null != xlsMeta) {
                return fmeService.excelLoader(xlsMeta, req);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return null;
    }
 
    /**
     * 获取Excel的元数据
     */
    private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) {
        if (null == xlsList || xlsList.isEmpty()) {
            return null;
        }
 
        List<String> pathList = new ArrayList<>();
        List<String> dirList = new ArrayList<>();
        List<String> pidList = new ArrayList<>();
 
        for (int i = 0, c = xlsList.size(); i < c; i++) {
            MetaFileEntity mf = xlsList.get(i);
            if ("成功".equals(mf.getMsg()) && null != mf.getXlsPath()) {
                pathList.add(mf.getXlsPath());
                dirList.add(mf.getDircode());
                pidList.add(mf.getEventid());
            }
        }
 
        MetaFileEntity first = xlsList.get(0);
        MetaFileEntity meta = new MetaFileEntity();
        meta.setPath(StringHelper.join(pathList, ","));
        meta.setEpsgCode(first.getEpsgCode());
        meta.setName(StringHelper.join(dirList, ";"));
        meta.setDepcode(first.getDepcode());
        meta.setVerid(first.getVerid());
        meta.setCreateUser(first.getCreateUser());
        meta.setEventid(StringHelper.join(pidList, ";"));
 
        return meta;
    }
}