diff --git a/src/main/java/com/sipai/controller/pipeline/PipelineDataController.java b/src/main/java/com/sipai/controller/pipeline/PipelineDataController.java index 2398ce59..1d82ad6f 100644 --- a/src/main/java/com/sipai/controller/pipeline/PipelineDataController.java +++ b/src/main/java/com/sipai/controller/pipeline/PipelineDataController.java @@ -7,6 +7,7 @@ import com.sipai.entity.user.User; import com.sipai.service.pipeline.PipelineDataService; import com.sipai.tools.CommUtil; import net.sf.json.JSONArray; +import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; @@ -16,7 +17,9 @@ import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.math.BigDecimal; import java.util.List; +import java.util.Map; @Controller @RequestMapping("/pipeline/pipelineData") @@ -150,4 +153,100 @@ public class PipelineDataController { model.addAttribute("pipelineData", pipelineData); return "/pipeline/pipelineDataView"; } + + /** + * 获取管道统计数据 + * 返回内容: + * - totalLength: 污水管线总长度(米) + * - totalCount: 污水管网总数量 + * - materialLengthRatio: 污水管网【材质-长度】比例 + * - materialCountRatio: 污水管网【材质-数量】比例 + */ + @RequestMapping("/getStatistics.do") + public ModelAndView getStatistics(HttpServletRequest request, Model model) { + JSONObject result = new JSONObject(); + + try { + // 获取总统计数据 + Map totalStats = pipelineDataService.selectTotalStats(); + + // 总数量 + long totalCount = 0; + double totalLength = 0.0; + + if (totalStats != null) { + Object countObj = totalStats.get("totalCount"); + Object lengthObj = totalStats.get("totalLength"); + + if (countObj != null) { + totalCount = ((Number) countObj).longValue(); + } + if (lengthObj != null) { + totalLength = ((Number) lengthObj).doubleValue(); + } + } + + result.put("totalLength", Math.round(totalLength * 100) / 100.0); // 保留2位小数 + result.put("totalCount", totalCount); + + // 获取按材质分组的统计数据 + List> materialStats = pipelineDataService.selectMaterialStats(); + + // 材质-长度比例 + JSONArray materialLengthRatio = new JSONArray(); + // 材质-数量比例 + JSONArray materialCountRatio = new JSONArray(); + + if (materialStats != null && !materialStats.isEmpty()) { + for (Map stat : materialStats) { + String material = (String) stat.get("material"); + if (material == null || material.isEmpty()) { + material = "未知"; + } + + Object countObj = stat.get("count"); + Object lengthObj = stat.get("totalLength"); + + int count = 0; + double length = 0.0; + + if (countObj != null) { + count = ((Number) countObj).intValue(); + } + if (lengthObj != null) { + length = ((Number) lengthObj).doubleValue(); + } + + // 计算比例 + double lengthRatio = totalLength > 0 ? Math.round(length / totalLength * 10000) / 100.0 : 0; + double countRatio = totalCount > 0 ? Math.round(count * 1.0 / totalCount * 10000) / 100.0 : 0; + + // 材质-长度比例 + JSONObject lengthItem = new JSONObject(); + lengthItem.put("material", material); + lengthItem.put("length", Math.round(length * 100) / 100.0); + lengthItem.put("ratio", lengthRatio); // 百分比 + materialLengthRatio.add(lengthItem); + + // 材质-数量比例 + JSONObject countItem = new JSONObject(); + countItem.put("material", material); + countItem.put("count", count); + countItem.put("ratio", countRatio); // 百分比 + materialCountRatio.add(countItem); + } + } + + result.put("materialLengthRatio", materialLengthRatio); + result.put("materialCountRatio", materialCountRatio); + result.put("success", true); + + } catch (Exception e) { + result.put("success", false); + result.put("message", "获取统计数据失败:" + e.getMessage()); + } + + model.addAttribute("result", result.toString()); + return new ModelAndView("result"); + } } diff --git a/src/main/java/com/sipai/dao/pipeline/PipelineDataDao.java b/src/main/java/com/sipai/dao/pipeline/PipelineDataDao.java index d88f43f7..15dac455 100644 --- a/src/main/java/com/sipai/dao/pipeline/PipelineDataDao.java +++ b/src/main/java/com/sipai/dao/pipeline/PipelineDataDao.java @@ -4,10 +4,27 @@ import com.sipai.dao.base.CommDaoImpl; import com.sipai.entity.pipeline.PipelineData; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Map; + @Repository public class PipelineDataDao extends CommDaoImpl { public PipelineDataDao() { super(); this.setMappernamespace("pipeline.PipelineDataMapper"); } + + /** + * 获取按材质分组的统计数据 + */ + public List> selectMaterialStats() { + return this.getSqlSession().selectList(this.getMappernamespace() + ".selectMaterialStats"); + } + + /** + * 获取总统计数据 + */ + public Map selectTotalStats() { + return this.getSqlSession().selectOne(this.getMappernamespace() + ".selectTotalStats"); + } } diff --git a/src/main/java/com/sipai/mapper/pipeline/PipelineDataMapper.xml b/src/main/java/com/sipai/mapper/pipeline/PipelineDataMapper.xml index f90d3565..e0d0a7c3 100644 --- a/src/main/java/com/sipai/mapper/pipeline/PipelineDataMapper.xml +++ b/src/main/java/com/sipai/mapper/pipeline/PipelineDataMapper.xml @@ -181,4 +181,23 @@ tb_pipeline_data ${where} + + + + + + diff --git a/src/main/java/com/sipai/service/pipeline/PipelineDataService.java b/src/main/java/com/sipai/service/pipeline/PipelineDataService.java index 7e05d3c3..f44b3876 100644 --- a/src/main/java/com/sipai/service/pipeline/PipelineDataService.java +++ b/src/main/java/com/sipai/service/pipeline/PipelineDataService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; +import java.util.Map; @Service public class PipelineDataService implements CommService { @@ -62,4 +63,18 @@ public class PipelineDataService implements CommService { pipelineData.setWhere(wherestr); return pipelineDataDao.deleteByWhere(pipelineData); } + + /** + * 获取按材质分组的统计数据 + */ + public List> selectMaterialStats() { + return pipelineDataDao.selectMaterialStats(); + } + + /** + * 获取总统计数据 + */ + public Map selectTotalStats() { + return pipelineDataDao.selectTotalStats(); + } }