package com.lf.server.service.show; import com.lf.server.entity.all.StaticData; import com.lf.server.entity.ctrl.DownloadTileEntity; import com.lf.server.entity.ctrl.ShpRecordEntity; import com.lf.server.entity.data.DownloadEntity; import com.lf.server.entity.data.MetaFileEntity; import com.lf.server.entity.data.PublishEntity; import com.lf.server.entity.sys.UserEntity; import com.lf.server.helper.*; import com.lf.server.service.data.DownloadService; import com.lf.server.service.sys.DownlogService; import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.model.ZipParameters; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 查询服务类 * @author WWW */ @Service public class InquiryService { @Resource PathHelper pathHelper; @Resource DownloadService downloadService; /** * 读取Shp第一条记录的WKT */ public ShpRecordEntity readShpFirstRecord(List list) { String fileName = null; for (MetaFileEntity mf : list) { if (mf.getName().toLowerCase().contains(".shp")) { fileName = mf.getPath(); break; } } if (StringHelper.isEmpty(fileName)) { return null; } ShpRecordEntity sr = ShpHelper.readShpFirstRecord(fileName); FileHelper.deleteFiles(list); return sr; } /** * 打包瓦片 */ public String zipTiles(DownloadTileEntity dt, PublishEntity pub, UserEntity ue) { if (!isTilePathExist(pub)) { return null; } List list = findTiles(dt, pub); if (list.isEmpty()) { return null; } String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".zip"; ZipFile zip = Zip4jHelper.createZipFile(zipFile, dt.getPwd()); ZipParameters params = Zip4jHelper.getZipParams(true); addFiles(zip, params, list, pub.getPath() + File.separator); String dbPwd = Md5Helper.reverse(Md5Helper.generate(dt.getPwd())); DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd); int rows = downloadService.insert(de); return rows > 0 ? de.getGuid() : null; } /** * 瓦片路径是否存在 */ private boolean isTilePathExist(PublishEntity pub) { String tilePath = pathHelper.getConfig().getTilePath().replace("2d\\tiles", "") + File.separator + pub.getPath(); File f = new File(tilePath); if (!f.exists() || !f.isDirectory()) { return false; } pub.setPath(tilePath); return true; } /** * 查找瓦片 */ private List findTiles(DownloadTileEntity dt, PublishEntity pub) { List list = new ArrayList<>(); for (int i = 0; i < StaticData.I23; i++) { findTilesByZoom(dt, pub, i, list); } return list; } /** * 根据层次查找瓦片 */ private void findTilesByZoom(DownloadTileEntity dt, PublishEntity pub, int zoom, List list) { File f = new File(pub.getPath() + File.separator + zoom); if (!f.exists() || !f.isDirectory()) { return; } int x = 0; int y = 0; // 关键算法 String pngPath = String.format("%s\\%d\\%d.png", pub.getPath(), x, y); File pngFile = new File(pngPath); if (pngFile.exists()) { list.add(pngFile); } } /** * 添加文件至压缩包 */ private void addFiles(ZipFile zip, ZipParameters params, List list, String basePath) { for (File f : list) { try { params.setFileNameInZip(f.getPath().replace(basePath, "")); zip.addStream(new FileInputStream(f), params); } catch (Exception e) { // } } } /** * 获取下载实体类 */ 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(8); 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; } }