管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-09 effc7aa4c1006fc2874f47f34db3be13384014ea
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
package com.lf.server.service.show;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.all.BasicMapper;
import com.lf.server.mapper.all.GeomBaseMapper;
import com.lf.server.mapper.data.DownloadMapper;
import com.lf.server.service.all.BaseQueryService;
import com.lf.server.service.data.DownloadService;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.util.*;
 
/**
 * 资料馆
 * @author WWW
 */
@Service
public class DataLibService {
    @Autowired
    PathHelper pathHelper;
 
    @Autowired
    DownloadMapper downloadMapper;
 
    @Autowired
    DownloadService downloadService;
 
    @Autowired
    BaseQueryService baseQueryService;
 
    private final static Log log = LogFactory.getLog(DataLibService.class);
 
    /**
     * 查询DB中溢出的单位ID
     */
    public List<Integer> selectDbOverflowDep(UserEntity ue, List<String> entities, String wkt) {
        List<Integer> rs = new ArrayList<>();
        for (String enity : entities) {
            try {
                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
                if (null == baseMapper) {
                    continue;
                }
 
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.select("depid");
                wrapper.gt("depid", 0);
                wrapper.apply(String.format("depid != ALL(fn_rec_array(%d, 'dep'))", ue.getDepid()));
                wrapper.groupBy("depid");
 
                Integer srid = baseQueryService.getSrid(baseMapper);
                if (null != srid) {
                    wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
                }
 
                List<Integer> ids = baseMapper.selectObjs(wrapper);
 
                addDepIds(rs, ids);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
 
        return rs;
    }
 
    /**
     * 添加单位ID
     */
    private void addDepIds(List<Integer> rs, List<Integer> ids) {
        if (null == ids || ids.isEmpty()) {
            return;
        }
 
        for (Integer id : ids) {
            if (!rs.contains(id)) {
                rs.add(id);
            }
        }
    }
 
    /**
     * 创建Zip包
     */
    public String createZipFile(UserEntity ue, List<String> entities, String wkt, String pwd) throws Exception {
        Map<String, List<?>> map = queryData(entities, wkt);
        if (map.size() == 0) {
            return null;
        }
 
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String tempPath = pathHelper.getTempPath(tempName);
        // String filePath = "D:\\LF\\temp\\20221219202706\\2022.gdb"
        String filePath = tempPath + File.separator + tempName + ".gdb";
 
        File file = new File(filePath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
        }
        GdbHelper.createGdb(filePath, map);
 
        String zipName = tempName + ".gdb.zip";
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
 
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams();
        addZipFiles(zip, params, file.listFiles());
 
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
        int rows = downloadMapper.insert(downloadEntity);
 
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
 
    /**
     * 查询数据
     */
    private Map<String, List<?>> queryData(List<String> entities, String wkt) {
        Map<String, List<?>> map = new HashMap<>(5);
        for (String enity : entities) {
            try {
                GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(enity);
                if (null == baseMapper) {
                    continue;
                }
 
                QueryWrapper wrapper = createWrapper(baseMapper, wkt);
                List<?> list = baseMapper.selectList(wrapper);
                if (null == list || list.size() == 0) {
                    continue;
                }
 
                if (!map.containsKey(enity)) {
                    map.put(enity, list);
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
 
        return map;
    }
 
    /**
     * 创建QueryWrapper
     */
    private QueryWrapper createWrapper(GeomBaseMapper baseMapper, String wkt) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("ST_AsText(geom) as geom, *");
 
        Integer srid = baseQueryService.getSrid(baseMapper);
        if (null != srid) {
            wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", wkt, srid));
        }
 
        return wrapper;
    }
 
    /**
     * 添加Zip文件
     */
    private void addZipFiles(ZipFile zip, ZipParameters params, File[] files) {
        if (null == files || files.length == 0) {
            return;
        }
 
        for (File f : files) {
            try {
                zip.addFile(f, params);
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
 
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        // 1-Shp文件,2-专题图,3-元数据,4-业务数据
        de.setType(4);
        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        de.setPwd(pwd);
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("业务数据文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
 
        return de;
    }
 
    /**
     * 打包DB数据
     */
    public String zipDbData(UserEntity ue, String name, String filter, String pwd) throws Exception {
        BasicMapper baseMapper = ClassHelper.getBasicMapper(name);
        if (baseMapper == null) {
            return null;
        }
 
        QueryWrapper wrapper = new QueryWrapper();
        baseQueryService.addFilterWrapper(wrapper, filter);
        if (baseMapper instanceof GeomBaseMapper) {
            wrapper.select("ST_AsText(geom) as geom, *");
        }
 
        List<?> list = baseMapper.selectList(wrapper);
        if (null == list || 0 == list.size()) {
            return null;
        }
 
        Map<String, List<?>> map = new HashMap<>(1);
        map.put(name, list);
 
        return zipData(ue, map, pwd);
    }
 
    /**
     * 打包数据
     */
    private String zipData(UserEntity ue, Map<String, List<?>> map, String pwd) throws Exception {
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String tempPath = pathHelper.getTempPath(tempName);
        String filePath = tempPath + File.separator + tempName + ".gdb";
 
        File file = new File(filePath);
        if (file.exists() && file.isDirectory()) {
            FileHelper.deleteDir(filePath);
        }
        GdbHelper.createGdb(filePath, map);
 
        String zipName = tempName + ".gdb.zip";
        String zipFile = pathHelper.getDownloadFullPath() + File.separator + zipName;
 
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams();
        addZipFiles(zip, params, file.listFiles());
 
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
        int rows = downloadMapper.insert(downloadEntity);
 
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
}