package com.lf.server.service.sys; import com.lf.server.entity.all.RedisCacheKey; import com.lf.server.entity.all.StaticData; import com.lf.server.entity.ctrl.CoordinateEntity; import com.lf.server.entity.ctrl.CountEntity; import com.lf.server.entity.data.DownloadEntity; import com.lf.server.entity.sys.AttachEntity; import com.lf.server.entity.sys.ReportEntity; import com.lf.server.entity.sys.UserEntity; import com.lf.server.helper.*; import com.lf.server.mapper.sys.ReportMapper; import com.lf.server.service.all.RedisService; import com.lf.server.service.data.DownloadService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.*; import java.util.concurrent.TimeUnit; /** * 报告模板 * @author WWW */ @Service public class ReportService implements ReportMapper { @Value("${sys.turfServer}") private String turfServer; @Resource ReportMapper reportMapper; @Resource AttachService attachService; @Resource PathHelper pathHelper; @Resource DownloadService downloadService; @Resource RedisService redisService; private final static Log log = LogFactory.getLog(ReportService.class); @Override public Integer selectCount(String name, String code) { name = StringHelper.getLikeUpperStr(name); return reportMapper.selectCount(name, code); } @Override public List selectByPage(String name, String code, Integer limit, Integer offset) { name = StringHelper.getLikeUpperStr(name); return reportMapper.selectByPage(name, code, limit, offset); } @Override public List selectAll() { return reportMapper.selectAll(); } @Override public ReportEntity selectById(int id) { return reportMapper.selectById(id); } @Override public Integer insert(ReportEntity entity) { return reportMapper.insert(entity); } @Override public Integer inserts(List list) { return reportMapper.inserts(list); } @Override public Integer delete(int id) { return reportMapper.delete(id); } @Override public Integer deletes(List ids) { return reportMapper.deletes(ids); } @Override public Integer update(ReportEntity entity) { return reportMapper.update(entity); } @Override public Integer updates(List list) { return reportMapper.updates(list); } @Override public List countSizes() { return reportMapper.countSizes(); } @Override public List countSizesByType() { return reportMapper.countSizesByType(); } @Override public List countServices() { return reportMapper.countServices(); } @Override public List countOperates() { return reportMapper.countOperates(); } @Override public List countSizesByPrj() { return reportMapper.countSizesByPrj(); } @Override public List countExplorationPoints() { String key = RedisCacheKey.dataCountKey("countExplorationPoints"); Object obj = redisService.get(key); if (obj instanceof List) { return (List) obj; } List list = reportMapper.countExplorationPoints(); if (null != list && list.size() > 0) { countExplorationArea(list); redisService.put(key, list, StaticData.I2, TimeUnit.MINUTES); } return list; } @Override public List selectExplorationPoints(String code) { return reportMapper.selectExplorationPoints(StringHelper.getRightLike(code)); } /** * 统计钻孔点面积 */ private void countExplorationArea(List list) { for (CountEntity ce : list) { if (0 == ce.getCount()) { continue; } double area = calcPolygonArea(ce.getM3()); ce.setArea(area); } } /** * 计算多边形面积 */ private double calcPolygonArea(String code) { try { String url = turfServer + "/Call/CalcArea?code=" + code; String str = RestHelper.get(url); if (StringHelper.isEmpty(str)) { return 0d; } return Double.parseDouble(str); } catch (Exception ex) { log.error(ex.getMessage(), ex); return 0d; } } /** * 调用JS方法 * */ private double callJsFn(String str) { String staticPath = PathHelper.getStaticPath(); Object obj = JsHelper.callJsFn(staticPath + "js/turf.min.6.5.js", "pointsToPolygon", str); if (null == obj) { return 0d; } return (double) obj; } /** * 创建报告 */ public void createReport(UserEntity ue, ReportEntity re, HttpServletResponse res) throws Exception { AttachEntity ae = attachService.selectByGuid(re.getGuid()); if (null == ae) { return; } String source = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); String targetName = StringHelper.YMDHMS2_FORMAT.format(new Date()) + FileHelper.getExtension(ae.getName()); String target = pathHelper.getDownloadFullPath() + File.separator + targetName; File sourceFile = new File(source); if (!sourceFile.exists() || sourceFile.isDirectory()) { return; } generateReport(source, target, re); File targetFile = new File(target); if (!targetFile.exists() || sourceFile.isDirectory()) { return; } DownloadEntity de = getDownloadEntity(ue, target); int rows = downloadService.insert(de); if (rows > 0) { WebHelper.download(target, targetName, res); } } /** * 生成报告 */ private void generateReport(String source, String target, ReportEntity re) { if (StaticData.S1.equals(re.getType())) { switch (re.getCode()) { case "countOperates": createCountOperatesWord(source, target); break; case "countSizes": createCountSizesWord(source, target); break; case "countServices": createCountServicesWord(source, target); break; case "countExplorationPoints": createCountExplorationPointsWord(source, target); break; case "countSizesByType": createCountSizesByTypeWord(source, target); break; case "countSizesByPrj": createCountSizesByPrjWord(source, target); break; default: break; } } else { switch (re.getCode()) { case "countOperates": createCountOperatesExcel(source, target); break; case "countSizes": createCountSizesExcel(source, target); break; case "countServices": createCountServicesExcel(source, target); break; case "countExplorationPoints": createCountExplorationPointsExcel(source, target); break; case "countSizesByType": createCountSizesByTypeExcel(source, target); break; case "countSizesByPrj": createCountSizesByPrjExcel(source, target); break; default: break; } } } /** * 创建 用户流量统计 Word */ public void createCountOperatesWord(String source, String target) { List list = countOperates(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allCount += ce.getCount(); String[] strs = new String[]{"" + rows++, ce.getM1(), ce.getM2(), ce.getCount().toString()}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", " ", "" + allCount}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 数据量统计 Word */ public void createCountSizesWord(String source, String target) { List list = countSizes(); if (null == list || list.isEmpty()) { return; } int rows = 1; Double allSize = 0d; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allSize += ce.getSizes(); String[] strs = new String[]{"" + rows++, ce.getM1(), FileHelper.getSizes(ce.getSizes())}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", FileHelper.getSizes(allSize)}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 服务调用量统计 Word */ public void createCountServicesWord(String source, String target) { List list = countServices(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allCount += ce.getCount(); String[] strs = new String[]{"" + rows++, ce.getM1(), ce.getCount().toString()}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", "" + allCount}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 钻孔数据统计 Word */ public void createCountExplorationPointsWord(String source, String target) { List list = countExplorationPoints(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d, allArea = 0d; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allSize += ce.getSizes(); allCount += ce.getCount(); allArea += ce.getArea(); String[] strs = new String[]{"" + rows++, ce.getM1(), "" + ce.getCount(), FileHelper.getSizes(ce.getSizes()), FileHelper.getSquareMeter(ce.getArea())}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", "" + allCount, FileHelper.getSizes(allSize), FileHelper.getSquareMeter(allArea)}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 数据格式统计 Word */ public void createCountSizesByTypeWord(String source, String target) { List list = countSizesByType(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allSize += ce.getSizes(); allCount += ce.getCount(); String[] strs = new String[]{"" + rows++, ce.getM1(), "" + ce.getCount(), FileHelper.getSizes(ce.getSizes())}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", "" + allCount, FileHelper.getSizes(allSize)}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 项目数据统计 Word */ public void createCountSizesByPrjWord(String source, String target) { List list = countSizesByPrj(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d; ArrayList addList = new ArrayList<>(); for (CountEntity ce : list) { allSize += ce.getSizes(); allCount += ce.getCount(); String[] strs = new String[]{"" + rows++, ce.getM1(), "" + ce.getCount(), FileHelper.getSizes(ce.getSizes())}; addList.add(strs); } if (addList.size() > 0) { addList.add(new String[]{"" + rows, "总计", "" + allCount, FileHelper.getSizes(allSize)}); } WordHelper.generateWord(source, target, null, addList); } /** * 创建 用户流量统计 Excel */ public void createCountOperatesExcel(String source, String target) { List list = countOperates(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; for (CountEntity ce : list) { ce.setNo(rows++); allCount += ce.getCount(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setCount((long) allCount); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 创建 数据量统计 Excel */ public void createCountSizesExcel(String source, String target) { List list = countSizes(); if (null == list || list.isEmpty()) { return; } int rows = 1; Double allSize = 0d; for (CountEntity ce : list) { ce.setNo(rows++); ce.setM2(FileHelper.getSizes(ce.getSizes())); allSize += ce.getSizes(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setM2(FileHelper.getSizes(allSize)); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 创建 服务调用量统计 Excel */ public void createCountServicesExcel(String source, String target) { List list = countServices(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; for (CountEntity ce : list) { ce.setNo(rows++); allCount += ce.getCount(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setCount((long) allCount); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 创建 钻孔数据统计 Excel */ public void createCountExplorationPointsExcel(String source, String target) { List list = countExplorationPoints(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d, allArea = 0d; for (CountEntity ce : list) { ce.setNo(rows++); ce.setM2(FileHelper.getSizes(ce.getSizes())); ce.setM3(FileHelper.getSquareMeter(ce.getArea())); allCount += ce.getCount(); allSize += ce.getSizes(); allArea += ce.getArea(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setCount((long) allCount); ce.setM2(FileHelper.getSizes(allSize)); ce.setM3(FileHelper.getSquareMeter(allArea)); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 创建 数据格式统计 Excel */ public void createCountSizesByTypeExcel(String source, String target) { List list = countSizesByType(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d; for (CountEntity ce : list) { ce.setNo(rows++); ce.setM2(FileHelper.getSizes(ce.getSizes())); allCount += ce.getCount(); allSize += ce.getSizes(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setCount((long) allCount); ce.setM2(FileHelper.getSizes(allSize)); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 创建 项目数据统计 Excel */ public void createCountSizesByPrjExcel(String source, String target) { List list = countSizesByPrj(); if (null == list || list.isEmpty()) { return; } int rows = 1, allCount = 0; Double allSize = 0d; for (CountEntity ce : list) { ce.setNo(rows++); ce.setM2(FileHelper.getSizes(ce.getSizes())); allCount += ce.getCount(); allSize += ce.getSizes(); } if (list.size() > 0) { CountEntity ce = new CountEntity(); ce.setNo(rows); ce.setM1("总计"); ce.setCount((long) allCount); ce.setM2(FileHelper.getSizes(allSize)); list.add(ce); } Map> map = new HashMap<>(1); map.put("data", list); ExcelHelper.writeToTemplate(source, target, map); } /** * 获取下载实体类 */ private DownloadEntity getDownloadEntity(UserEntity ue, String file) { DownloadEntity de = new DownloadEntity(); de.setName(FileHelper.getFileName(file)); // 1-Shp文件,2-专题图,3-元数据,4-业务数据,5-管道分析,6-统计报告,7-附件文件,8-瓦片文件 de.setType(6); de.setSizes(FileHelper.sizeToMb(new File(file).length())); de.setDepid(ue.getDepid()); de.setDcount(1); de.setPwd(null); de.setUrl(FileHelper.getRelativePath(file)); de.setDescr("统计报告"); de.setGuid(FileHelper.getFileMd5(file)); de.setCreateUser(ue.getId()); // de.setGeom(null) return de; } }