package com.moon.server.service.show;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.moon.server.entity.ctrl.DownloadReqEntity;
|
import com.moon.server.entity.data.DownloadEntity;
|
import com.moon.server.entity.sys.AttachEntity;
|
import com.moon.server.entity.sys.UserEntity;
|
import com.moon.server.mapper.all.BasicMapper;
|
import com.moon.server.mapper.all.GeomBaseMapper;
|
import com.moon.server.mapper.data.DownloadMapper;
|
import com.moon.server.service.all.BaseQueryService;
|
import com.moon.server.service.data.MetaService;
|
import com.moon.server.helper.*;
|
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
|
MetaService metaService;
|
|
@Autowired
|
DownloadMapper downloadMapper;
|
|
@Autowired
|
BaseQueryService baseQueryService;
|
|
private final static Log log = LogFactory.getLog(DataLibService.class);
|
|
/**
|
* 查询DB中溢出的单位编码
|
*/
|
public List<String> selectDbOverflowDep(UserEntity ue, DownloadReqEntity dr) {
|
if (!StringHelper.isEmpty(dr.getWkt())) {
|
return selectDbOverflowDep4Wkt(ue, dr);
|
}
|
|
return selectDbOverflowDep4Prop(ue, dr);
|
}
|
|
/**
|
* 查询DB中溢出的单位编码-空间查询
|
*/
|
public List<String> selectDbOverflowDep4Wkt(UserEntity ue, DownloadReqEntity dr) {
|
List<String> rs = new ArrayList<>();
|
for (String entity : dr.getEntities()) {
|
GeomBaseMapper<?> baseMapper = ClassHelper.getGeoBaseMapper(entity);
|
if (null == baseMapper) {
|
continue;
|
}
|
|
QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
|
Integer srid = baseQueryService.getSrid(baseMapper);
|
wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid));
|
|
List<String> ids = baseMapper.selectObjs(wrapper);
|
addDepCodes(rs, ids);
|
}
|
|
return rs;
|
}
|
|
/**
|
* 查询DB中溢出的单位编码-属性查询
|
*/
|
public List<String> selectDbOverflowDep4Prop(UserEntity ue, DownloadReqEntity dr) {
|
List<String> rs = new ArrayList<>();
|
BasicMapper<?> baseMapper = ClassHelper.getBasicMapper(dr.getEntities().get(0));
|
if (null == baseMapper) {
|
return rs;
|
}
|
|
QueryWrapper wrapper = getWrapper4DbOverflow(ue, dr);
|
List<String> ids = baseMapper.selectObjs(wrapper);
|
addDepCodes(rs, ids);
|
|
return rs;
|
}
|
|
/**
|
* 查询DB中溢出单位编码的查询包装器
|
*/
|
private <T> QueryWrapper<T> getWrapper4DbOverflow(UserEntity ue, DownloadReqEntity dr) {
|
QueryWrapper<T> wrapper = new QueryWrapper<T>();
|
wrapper.select("depid");
|
wrapper.groupBy("depid");
|
wrapper.apply("depid is not null and depid not like '" + ue.getDepcode() + "%'");
|
|
if (null != dr.getIds() && dr.getIds().size() > 0) {
|
wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ",")));
|
} else {
|
baseQueryService.addFilterWrapper(wrapper, dr.getFilter());
|
}
|
|
String dirs = copeCodes(dr.getDirs(), "dirid");
|
if (!StringHelper.isEmpty(dirs)) {
|
wrapper.apply(dirs);
|
}
|
if (!StringHelper.isEmpty(dr.getDepcode())) {
|
wrapper.likeRight("depid", dr.getDepcode());
|
}
|
|
return wrapper;
|
}
|
|
/**
|
* 添加单位编码
|
*/
|
private void addDepCodes(List<String> rs, List<String> ids) {
|
if (null == ids || ids.isEmpty()) {
|
return;
|
}
|
|
for (String id : ids) {
|
if (StringHelper.isEmpty(id)) {
|
continue;
|
}
|
if (!rs.contains(id)) {
|
rs.add(id);
|
}
|
}
|
}
|
|
/**
|
* 请求DB数据下载
|
*/
|
public String downloadDbReq(UserEntity ue, DownloadReqEntity dr) throws Exception {
|
Map<String, List<?>> dataMap = new HashMap<>(2);
|
Map<String, List<AttachEntity>> annexMap = new HashMap<>(2);
|
queryData(dr, dataMap, annexMap);
|
if (dataMap.size() == 0) {
|
return null;
|
}
|
|
String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
|
String tempPath = pathHelper.getTempPath(tempName);
|
String gdbPath = tempPath + File.separator + "tabs.gdb";
|
|
File gdbFile = new File(gdbPath);
|
if (gdbFile.exists() && gdbFile.isDirectory()) {
|
FileHelper.deleteDir(gdbPath);
|
}
|
GdbHelper.createGdb(gdbPath, dataMap);
|
|
String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".gdb.zip";
|
ZipFile zip = Zip4jHelper.createZipFile(zipFile, dr.getPwd());
|
ZipParameters params = Zip4jHelper.getZipParams(true);
|
zip.addFolder(new File(gdbPath), params);
|
// zip.addFolder(new File(annexPath), params)
|
metaService.addAnnex(zip, params, annexMap);
|
|
String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd()));
|
DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd);
|
int rows = downloadMapper.insert(de);
|
|
return rows > 0 ? de.getGuid() : null;
|
}
|
|
/**
|
* 查询数据+附件
|
*/
|
private void queryData(DownloadReqEntity dr, Map<String, List<?>> dataMap, Map<String, List<AttachEntity>> annexMap) {
|
for (String entity : dr.getEntities()) {
|
try {
|
BasicMapper baseMapper = ClassHelper.getBasicMapper(entity);
|
if (null == baseMapper) {
|
continue;
|
}
|
|
QueryWrapper wrapper = createQueryWrapper(baseMapper, dr);
|
metaService.addData(entity, baseMapper, wrapper, dataMap, annexMap);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
}
|
}
|
|
/**
|
* 创建查询包装器
|
*/
|
private <T> QueryWrapper<T> createQueryWrapper(BasicMapper baseMapper, DownloadReqEntity dr) {
|
QueryWrapper<T> wrapper = new QueryWrapper<T>();
|
|
String dirs = copeCodes(dr.getDirs(), "dirid");
|
if (!StringHelper.isEmpty(dirs)) {
|
wrapper.apply(dirs);
|
}
|
if (!StringHelper.isEmpty(dr.getDepcode())) {
|
// wrapper.apply(String.format("depid like '%s'", StringHelper.getRightLike(dr.getDepcode())))
|
wrapper.likeRight("depid", dr.getDepcode());
|
}
|
if (baseMapper instanceof GeomBaseMapper) {
|
wrapper.select("ST_AsText(geom) as geom, *");
|
if (!StringHelper.isEmpty(dr.getWkt())) {
|
Integer srid = baseQueryService.getSrid((GeomBaseMapper) baseMapper);
|
wrapper.apply(String.format("ST_Intersects(ST_PolygonFromText('%s', %d), geom)", dr.getWkt(), srid));
|
}
|
}
|
if (null != dr.getIds() && dr.getIds().size() > 0) {
|
wrapper.apply(String.format("gid in (%s)", StringHelper.join(dr.getIds(), ",")));
|
} else {
|
baseQueryService.addFilterWrapper(wrapper, dr.getFilter());
|
}
|
|
return wrapper;
|
}
|
|
/**
|
* 创建附件 *
|
*/
|
private void createAnnex(String annexPath, Map<String, List<AttachEntity>> annexMap) {
|
if (annexMap.size() == 0) {
|
return;
|
}
|
|
String uploadPath = pathHelper.getConfig().getUploadPath();
|
for (String key : annexMap.keySet()) {
|
String targetPath = annexPath + File.separator + key;
|
File targetFile = new File(targetPath);
|
if (!targetFile.exists() || !targetFile.isDirectory()) {
|
targetFile.mkdirs();
|
}
|
|
for (AttachEntity ae : annexMap.get(key)) {
|
try {
|
File srcFile = new File(uploadPath + File.separator + ae.getPath());
|
if (!srcFile.exists() || srcFile.isDirectory()) {
|
continue;
|
}
|
|
File destFile = new File(targetPath + File.separator + ae.getName());
|
if (destFile.exists() && !destFile.isDirectory()) {
|
continue;
|
}
|
|
FileHelper.copyFile(srcFile, destFile);
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
}
|
}
|
}
|
|
/**
|
* 获取下载实体类
|
*/
|
private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) {
|
DownloadEntity de = new DownloadEntity();
|
de.setName(FileHelper.getFileName(file));
|
// 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件文件,8-瓦片文件
|
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;
|
}
|
|
/**
|
* 处理编码
|
*/
|
public static String copeCodes(String codes, String field) {
|
if (StringHelper.isEmpty(codes) || StringHelper.isSqlInjection(codes)) {
|
return null;
|
}
|
|
List<String> list = codesAsList(codes);
|
removeDuplicate(list);
|
setRightLike(list, field);
|
|
return "(" + StringHelper.join(list, " or ") + ")";
|
}
|
|
/**
|
* 单位编码转集合
|
*/
|
private static List<String> codesAsList(String codes) {
|
List<String> list = Arrays.asList(codes.split(","));
|
Set set = new HashSet(list);
|
|
List<String> newList = new ArrayList<>(set);
|
Collections.sort(newList);
|
|
return newList;
|
}
|
|
/**
|
* 去除重复
|
*/
|
private static void removeDuplicate(List<String> list) {
|
int i = 0;
|
while (i < list.size()) {
|
int j = findStr(list, i);
|
if (j > -1) {
|
list.remove(j);
|
continue;
|
}
|
|
i++;
|
}
|
}
|
|
/**
|
* 查找字符串
|
*/
|
private static int findStr(List<String> list, int i) {
|
String source = list.get(i);
|
for (int j = i + 1, c = list.size(); j < c; j++) {
|
if (list.get(j).startsWith(source)) {
|
return j;
|
}
|
}
|
|
return -1;
|
}
|
|
/**
|
* 设置 右like
|
*/
|
private static void setRightLike(List<String> list, String field) {
|
for (int i = 0, c = list.size(); i < c; i++) {
|
String str = String.format("%s like '%s%%'", field, list.get(i));
|
list.set(i, str);
|
}
|
}
|
}
|