diff --git a/renren-admin/src/main/java/io/renren/common/controller/CensusControllerV2.java b/renren-admin/src/main/java/io/renren/common/controller/CensusControllerV2.java index b667d4a6..aba43f78 100644 --- a/renren-admin/src/main/java/io/renren/common/controller/CensusControllerV2.java +++ b/renren-admin/src/main/java/io/renren/common/controller/CensusControllerV2.java @@ -2,11 +2,14 @@ package io.renren.common.controller; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import io.renren.common.annotation.LogOperation; import io.renren.common.utils.Result; import io.renren.modules.processForm.service.TAbilityApplicationService; import io.renren.modules.resource.service.ResourceService; import io.renren.modules.resourceBrowse.service.ResourceBrowseService; +import io.renren.modules.sys.dto.SysDeptDTO; import io.renren.modules.sys.service.SysDeptService; import io.renren.modules.sys.service.SysUserService; import io.swagger.annotations.Api; @@ -138,10 +141,23 @@ public class CensusControllerV2 { @GetMapping("/districtResourceRank") @ApiOperation("应用资源区县市排名") - @LogOperation("应用资源数量统计") + @LogOperation("应用资源区县市排名") public Result>> districtResourceRank() { + List> result = resourceRank(3); + return new Result().ok(result); + } + + @GetMapping("/cityResourceRank") + @ApiOperation("应用资源委办局排名") + @LogOperation("应用资源委办局排名") + public Result>> cityResourceRank() { + List> result = resourceRank(2); + return new Result().ok(result); + } + + private List> resourceRank(Integer type) { List> result = Collections.synchronizedList(new ArrayList<>()); - List> district = jdbcTemplate.queryForList("SELECT * FROM sys_dept WHERE type = 2"); + List> district = jdbcTemplate.queryForList("SELECT * FROM sys_dept WHERE type = " + type); List> listMap = district.stream().map(index -> { Map re = new LinkedHashMap<>(); @@ -149,22 +165,37 @@ public class CensusControllerV2 { logger.info(sql); Long count = jdbcTemplate.queryForObject(sql, Long.class); if (!"0".equals(index.get("pid").toString())) { // 有上级部门 - re.put(index.get("pid").toString(), count); + Optional sysDeptDTO = + Optional.ofNullable(sysDeptService.get(Long.valueOf(index.get("pid").toString()))); + if (sysDeptDTO.isPresent() && sysDeptDTO.get().getType() != null && sysDeptDTO.get().getType() >= 2) { + re.put(index.get("id").toString(), count); + } else { + re.put(index.get("pid").toString(), count); + } } else { re.put(index.get("id").toString(), count); } return re; - }).distinct().collect(Collectors.toList()); + }).collect(Collectors.toList()); Map>> i = - listMap.stream().collect(Collectors.groupingBy(m -> m.keySet().stream().findFirst().toString())); + listMap.stream().collect(Collectors.groupingBy(m -> m.keySet().stream().findFirst().get())); result = i.keySet().stream().map(index -> { Map re = new LinkedHashMap<>(); - re.put("deptId", index); + re.put("deptName", sysDeptService.get(Long.valueOf(index)).getName()); re.put("count", i.get(index).stream().mapToLong(index_ -> index_.values().stream().mapToLong(count_ -> count_).sum()).sum()); return re; }).collect(Collectors.toList()); - - return new Result().ok(result); + result.sort(Comparator.comparing(x -> { + ObjectMapper mapper = new ObjectMapper(); + try { + Map m = mapper.readValue(mapper.writeValueAsString(x), Map.class); + return Long.valueOf(m.get("count").toString()); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + ).reversed()); + return result; } }