package com.terra.system.service.show;
|
|
import com.terra.common.entity.all.StaticData;
|
import com.terra.system.entity.ctrl.MarkJsonEntity;
|
import com.terra.system.entity.data.DownloadEntity;
|
import com.terra.system.entity.data.MetaFileEntity;
|
import com.terra.system.entity.show.MarkEntity;
|
import com.terra.system.entity.sys.UserEntity;
|
import com.terra.system.helper.*;
|
import com.terra.system.mapper.show.MarkMapper;
|
import com.terra.system.service.data.DownloadService;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 标绘
|
* @author WWW
|
*/
|
@Service
|
public class MarkService implements MarkMapper {
|
@Resource
|
MarkMapper markMapper;
|
|
@Resource
|
PathHelper pathHelper;
|
|
@Resource
|
DownloadService downloadService;
|
|
@Override
|
public Integer selectCount(Integer uid) {
|
return markMapper.selectCount(uid);
|
}
|
|
@Override
|
public List<MarkEntity> selectByPage(Integer uid, Integer limit, Integer offset) {
|
return markMapper.selectByPage(uid, limit, offset);
|
}
|
|
@Override
|
public List<MarkEntity> selectAll() {
|
return markMapper.selectAll();
|
}
|
|
@Override
|
public MarkEntity selectById(int id) {
|
return markMapper.selectById(id);
|
}
|
|
@Override
|
public Integer insert(MarkEntity entity) {
|
return markMapper.insert(entity);
|
}
|
|
@Override
|
public Integer inserts(List<MarkEntity> list) {
|
return markMapper.inserts(list);
|
}
|
|
@Override
|
public Integer delete(int id) {
|
return markMapper.delete(id);
|
}
|
|
@Override
|
public Integer deletes(List<Integer> ids) {
|
return markMapper.deletes(ids);
|
}
|
|
@Override
|
public Integer update(MarkEntity entity) {
|
return markMapper.update(entity);
|
}
|
|
@Override
|
public Integer updates(List<MarkEntity> list) {
|
return markMapper.updates(list);
|
}
|
|
/**
|
* 下载ShapeFile文件
|
*
|
* @param ue 用户实体
|
* @param list 标绘JSON实体类集合
|
* @return GUID
|
* @throws Exception 异常
|
*/
|
public String downloadShp(UserEntity ue, List<MarkJsonEntity> list) throws Exception {
|
String path = pathHelper.getTempPath();
|
createShapeFiles(ue, list, path);
|
|
File[] files = new File(path).listFiles();
|
if (files == null || files.length == 0) {
|
return null;
|
}
|
|
String zip = getZipPath();
|
ZipHelper.zip(zip, path);
|
FileHelper.deleteDir(path);
|
|
String guid = FileHelper.getFileMd5(zip);
|
DownloadEntity entity = downloadService.selectByGuid(guid);
|
if (entity != null) {
|
return entity.getGuid();
|
}
|
|
DownloadEntity de = getDownloadEntity(ue, zip);
|
int rows = downloadService.insert(de);
|
|
return rows > 0 ? de.getGuid() : null;
|
}
|
|
/**
|
* 创建shp文件
|
*/
|
private String createShapeFiles(UserEntity ue, List<MarkJsonEntity> list, String path) {
|
List<MarkJsonEntity> points = getMarkByType(list, "POINT");
|
if (points.size() > 0) {
|
ShpHelper.createShp(points, path, "POINT");
|
}
|
List<MarkJsonEntity> lines = getMarkByType(list, "LINESTRING");
|
if (lines.size() > 0) {
|
ShpHelper.createShp(lines, path, "LINESTRING");
|
}
|
List<MarkJsonEntity> polygons = getMarkByType(list, "POLYGON");
|
if (polygons.size() > 0) {
|
ShpHelper.createShp(polygons, path, "POLYGON");
|
}
|
|
return path;
|
}
|
|
/**
|
* 获取shp目录
|
*/
|
private String getShpDir(UserEntity ue, String parent) {
|
String path = parent + File.separator + StringHelper.YMDHMS2_FORMAT.format(new Date());
|
|
File file = new File(path);
|
if (!file.exists() && !file.isDirectory()) {
|
file.mkdirs();
|
}
|
|
return path;
|
}
|
|
/**
|
* 获取标绘类型
|
*/
|
private List<MarkJsonEntity> getMarkByType(List<MarkJsonEntity> list, String type) {
|
List<MarkJsonEntity> rs = new ArrayList<>();
|
for (MarkJsonEntity mark : list) {
|
if (StringHelper.isEmpty(mark.getWkt())) {
|
continue;
|
}
|
if (mark.getWkt().contains(type)) {
|
rs.add(mark);
|
}
|
}
|
|
return rs;
|
}
|
|
/**
|
* 获取zip路径
|
*/
|
private String getZipPath() {
|
String path = pathHelper.getDownloadFullPath() + File.separator + StringHelper.YMDHMS2_FORMAT.format(new Date()) + ".zip";
|
|
File file = new File(path);
|
if (file.exists() && !file.isDirectory()) {
|
file.delete();
|
}
|
|
return path;
|
}
|
|
/**
|
* 获取下载实体类
|
*/
|
private DownloadEntity getDownloadEntity(UserEntity ue, String file) throws Exception {
|
DownloadEntity de = new DownloadEntity();
|
de.setName(FileHelper.getFileName(file));
|
// 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件文件,8-瓦片文件
|
de.setType(1);
|
de.setSizes(FileHelper.sizeToMb(new File(file).length()));
|
de.setDepid(ue.getDepid());
|
de.setDcount(0);
|
// de.setPwd(null)
|
de.setUrl(FileHelper.getRelativePath(file));
|
de.setDescr("Shp文件");
|
de.setGuid(FileHelper.getFileMd5(file));
|
de.setCreateUser(ue.getId());
|
// de.setGeom(null)
|
|
return de;
|
}
|
|
/**
|
* 获取下载文件路径
|
*/
|
public String getDownloadFilePath(DownloadEntity de) {
|
return pathHelper.getConfig().getDownloadPath() + File.separator + de.getUrl();
|
}
|
|
/**
|
* 读取ShapeFile文件获取Mark实体类
|
*/
|
public List<MarkJsonEntity> readShpForMarks(List<MetaFileEntity> list) {
|
String fileName = null;
|
for (MetaFileEntity mf : list) {
|
if (mf.getName().toLowerCase().contains(StaticData.SHP)) {
|
fileName = mf.getPath();
|
break;
|
}
|
}
|
if (StringHelper.isEmpty(fileName)) {
|
return null;
|
}
|
|
List<MarkJsonEntity> mjeList = ShpHelper.readShpForMarks(fileName);
|
FileHelper.deleteFiles(list);
|
|
return mjeList;
|
}
|
}
|