管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-01-11 8d18451181c9e3588fdddc1724bd6a7860e9c3b4
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
package com.lf.server.service.data;
 
import com.lf.server.entity.all.BaseEntity;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.CoordEntity;
import com.lf.server.entity.data.DirEntity;
import com.lf.server.entity.data.FmeLogEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.all.BasicMapper;
import com.lf.server.mapper.data.UploadMapper;
import com.lf.server.service.all.BaseQueryService;
import com.lf.server.service.all.BaseUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
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;
 
    @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);
        dataLoader(list);
        excelLoader(list, req);
    }
 
    /**
     * 检查元数据文件
     */
    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.setDepid(ue.getDepid());
            mf.setPath(tempPath + File.separator + mf.getPath());
 
            File f = new File(mf.getPath());
            if (!f.exists()) {
                mf.setRows(-1);
                mf.setMsg("文件不存在");
            }
        }
    }
 
    /**
     * 加载数据
     */
    private void dataLoader(List<MetaFileEntity> list) {
        loadData(list);
        copyFiles(list);
        insertMetas(list);
    }
 
    /**
     * 加载数据
     */
    private void loadData(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (StaticData.SHP.equals(mf.getExtName()) || StaticData.GDB.equals(mf.getExtName())) {
                loadSpatialData(mf);
            }
        }
    }
 
    /**
     * 加载空间数据
     */
    private 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;
        }
 
        List<?> list = null;
        if (StaticData.SHP.equals(mf.getExtName())) {
            list = ShpHelper.readData(clazz, mf.getPath());
        } else {
            GdbHelper.readData(clazz, mf.getPath(), mf.getTab());
        }
        if (null == list || list.isEmpty()) {
            return;
        }
        setCreateInfo(list, mf);
 
        int rows = basicMapper.insertBatch(list);
        if (rows > 0) {
            mf.setTab(tabName);
            mf.setRows(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.getDirid());
                be.setDepid(mf.getDepid());
                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) {
        //
    }
 
    /**
     * 插入元数据
     * @param list
     */
    private void insertMetas(List<MetaFileEntity> list) {
        //
    }
 
    /**
     * Excel入库
     */
    private String excelLoader(List<MetaFileEntity> list, HttpServletRequest req) {
        List<MetaFileEntity> xlsList = getExcelFiles(list);
        if (xlsList.isEmpty()) {
            return "";
        }
 
        String guid = null;
        try {
            MetaFileEntity meta = getExcelMeta(xlsList);
            guid = fmeService.excelLoader(meta, req);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return guid;
    }
 
    /**
     * 获取Excel元数据文件
     */
    private List<MetaFileEntity> getExcelFiles(List<MetaFileEntity> list) {
        List<MetaFileEntity> xlsList = new ArrayList<>();
        for (MetaFileEntity mf : list) {
            boolean isXls = StaticData.XLS.equals(mf.getExtName()) || StaticData.XLSX.equals(mf.getExtName());
            if (mf.getRows() > -1 && isXls) {
                xlsList.add(mf);
            }
        }
 
        return xlsList;
    }
 
    /**
     * 获取Excel的元数据
     */
    private MetaFileEntity getExcelMeta(List<MetaFileEntity> xlsList) {
        List<String> pathList = new ArrayList<>();
        List<String> dirList = new ArrayList<>();
        List<String> pidList = new ArrayList<>();
 
        for (MetaFileEntity mf : xlsList) {
            pathList.add(mf.getPath());
            dirList.add(mf.getDirid() + "");
            pidList.add(mf.getEventid());
        }
 
        MetaFileEntity meta = new MetaFileEntity();
        meta.setPath(StringHelper.join(pathList, ","));
        meta.setEpsgCode(xlsList.get(0).getEpsgCode());
        meta.setName(StringHelper.join(dirList, ","));
        meta.setDepid(xlsList.get(0).getDepid());
        meta.setVerid(xlsList.get(0).getVerid());
        meta.setCreateUser(xlsList.get(0).getCreateUser());
        meta.setEventid(StringHelper.join(pidList, ","));
 
        return meta;
    }
}