管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-19 1ea11926b07d1c0ff58d5d96a356e5810ceda45a
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
package com.lf.server.service.data;
 
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.helper.FileHelper;
import com.lf.server.service.all.BaseUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.util.List;
 
/**
 * 数据入库
 * @author WWW
 */
@Service
public class DataLoaderService extends BaseUploadService {
    @Autowired
    MetaService metaService;
 
    @Autowired
    MetaFileService metaFileService;
 
    /**
     * 插入文件
     */
    public int insertFiles(MetaEntity entity, List<MetaFileEntity> list) {
        int count = 0;
        try {
            String temp = pathHelper.getConfig().getTempPath();
            String root = pathHelper.getUploadFullPath();
            String uploadPath = pathHelper.getConfig().getUploadPath();
 
            for (MetaFileEntity mf : list) {
                String filePath = getFilePath(temp, root, mf);
                if (null == filePath) {
                    continue;
                }
 
                // 元数据
                MetaEntity me = createMetaEntity(entity, mf);
                Integer rows = metaService.insert(me);
                if (rows < 1) {
                    continue;
                }
 
                // 元数据文件
                MetaFileEntity mfe = createMetaFileEntity(entity, mf, filePath);
                rows = metaFileService.insert(mfe);
                if (rows < 1) {
                    continue;
                }
 
                rows = insertDb(mfe, uploadPath);
                if (rows > 0) {
                    count++;
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return count;
    }
 
    /**
     * 获取文件路径
     */
    private String getFilePath(String temp, String root, MetaFileEntity mf) {
        // 移动文件
        File file = new File(temp + File.separator + mf.getPath());
        File newFile = new File(root + File.separator + mf.getGuid());
 
        // 文件类型
        String metaType = getType(mf.getName().toLowerCase());
        if (null == metaType) {
            file.delete();
            return null;
        }
 
        MetaFileEntity old = metaFileService.selectByGuid(mf.getGuid());
 
        String filePath = null;
        if (null == old) {
            filePath = newFile.getPath();
            file.renameTo(newFile);
        } else {
            filePath = old.getPath();
            file.delete();
        }
 
        return filePath;
    }
 
    /**
     * 创建元数据实体
     */
    private MetaEntity createMetaEntity(MetaEntity entity, MetaFileEntity mf) {
        MetaEntity me = new MetaEntity();
        me.setDepid(entity.getDepid());
        me.setDirid(entity.getDirid());
        me.setVerid(entity.getVerid());
        me.setType(getType(entity.getName().toLowerCase()));
        me.setGather(entity.getGather());
        me.setBatch(entity.getBatch());
        me.setDescr(entity.getDescr());
        me.setName(mf.getName());
        me.setSizes(mf.getSizes());
        me.setCreateTime(entity.getCreateTime());
        me.setCreateUser(entity.getCreateUser());
 
        return me;
    }
 
    /**
     * 创建元数据文件实体
     */
    private MetaFileEntity createMetaFileEntity(MetaEntity metaEntity, MetaFileEntity entity, String filePath) {
        MetaFileEntity mfe = new MetaFileEntity();
        mfe.setName(entity.getName());
        mfe.setMetaid(metaEntity.getId());
        mfe.setGuid(entity.getGuid());
        mfe.setSizes(entity.getSizes());
        mfe.setPath(FileHelper.getRelativePath(filePath));
        mfe.setCreateUser(metaEntity.getCreateUser());
        mfe.setCreateTime(metaEntity.getCreateTime());
 
        return mfe;
    }
 
    /**
     * 获取文件类型
     */
    private String getType(String name) {
        if (name.contains(".xls")) {
            return "xls";
        }
        if (name.contains(".mdb")) {
            return "mdb";
        }
        if (name.contains(".shp")) {
            return "shp";
        }
        if (name.contains(".gdb")) {
            return "gdb";
        }
 
        return null;
    }
 
    /**
     * 插入数据库
     */
    private Integer insertDb(MetaFileEntity mfe, String root) {
 
 
        return 0;
    }
}