|  |  |  | 
|---|
|  |  |  | @ApiImplicitParam(name = "dimension", value = "维度(profession、region、monitorPoint选一)", required = true, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "sensorKey", value = "传感器key", required = true, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "accountId", value = "账户id", required = true, paramType = "query", dataType = "int"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "timeType", value = "时间类型(month、day、hour选一)", required = true, paramType = "query", dataType = "int"), | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | public ResultBean<List<Map<String, Object>>> getRegionRankingData(HttpServletRequest request){ | 
|---|
|  |  |  | Map<String, Object> parameters = getParametersStartingWith(request, null); | 
|---|
|  |  |  | 
|---|
|  |  |  | return new ResultBean(rtdLayout); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("report_avg_data") | 
|---|
|  |  |  | @ApiOperation(value = "获取监测因子月平均值", notes = "获取监测因子月平均值") | 
|---|
|  |  |  | @ApiImplicitParams(value = { | 
|---|
|  |  |  | @ApiImplicitParam(name = "monitorPoint", value = "监控站id", required = true, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "mac", value = "设备mac", required = false, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "sensorKey", value = "传感器key,如有多个用逗号分隔", required = true, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "time", value = "时间,月份(格式:2018-03)", required = true, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | public ResultBean<List<Map<String, Object>>> getMonitorPointOrDeviceAvgDataBySensorKey(HttpServletRequest request) throws Exception { | 
|---|
|  |  |  | Map<String, Object> parameters = getParametersStartingWith(request, null); | 
|---|
|  |  |  | parameters.put("type", "month"); | 
|---|
|  |  |  | parameters.put("monitorPointId", parameters.remove("monitorPoint")); | 
|---|
|  |  |  | String[] sensorKeys = parameters.remove("sensorKey").toString().split(","); | 
|---|
|  |  |  | parameters.put("sensors", Arrays.asList(sensorKeys)); | 
|---|
|  |  |  | List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (Map<String, Object> map : list) { | 
|---|
|  |  |  | String time = map.get("time").toString(); | 
|---|
|  |  |  | time = time.substring(time.length() - 2); | 
|---|
|  |  |  | map.put("time", Integer.valueOf(time)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new ResultBean<List<Map<String, Object>>>(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Map<String, Object>> selectNextLevelRegion(Map<String, Object> parameters); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Map<String, Object>> getDischargeByRegion(Map<String, Object> parameters); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | Integer getAccountCountByAccountName(String accountName); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Optional<Account> queryAccountByName(String accountName); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Account companyLogin(Map<String, Object> parameters); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | Account account = accountMapper.getByAccountName(accountName); | 
|---|
|  |  |  | return Optional.ofNullable(account); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Account companyLogin(Map<String, Object> parameters) { | 
|---|
|  |  |  | Account account = new Account(); | 
|---|
|  |  |  | account.setIsDelete(Constants.IS_DELETE_FALSE); | 
|---|
|  |  |  | account.setAccountName(parameters.get("account").toString()); | 
|---|
|  |  |  | account = accountMapper.selectOne(account); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(account)) { | 
|---|
|  |  |  | throw new BusinessException("账户不存在,请联系管理员!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!encoder.matches(parameters.get("password").toString(), account.getPassword())) { | 
|---|
|  |  |  | throw new BusinessException("密码错误,请重新输入!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | account.setPassword(parameters.get("password").toString()); | 
|---|
|  |  |  | return account; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | package com.moral.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.time.LocalDate; | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.time.temporal.ChronoUnit; | 
|---|
|  |  |  | import java.time.temporal.TemporalAdjusters; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.Comparator; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.LinkedHashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String regionCode = parameters.get("regionCode").toString(); | 
|---|
|  |  |  | String regionType = "village_code"; | 
|---|
|  |  |  | String regionType = "village"; | 
|---|
|  |  |  | String nextLevel = ""; | 
|---|
|  |  |  | if (regionCode.length() == 6) { | 
|---|
|  |  |  | if (regionCode.endsWith("0000")) { | 
|---|
|  |  |  | regionType = "province_code"; | 
|---|
|  |  |  | regionType = "province"; | 
|---|
|  |  |  | nextLevel = "city"; | 
|---|
|  |  |  | } else if (regionCode.endsWith("00")) { | 
|---|
|  |  |  | regionType = "city_code"; | 
|---|
|  |  |  | regionType = "city"; | 
|---|
|  |  |  | nextLevel = "area"; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | regionType = "area_code"; | 
|---|
|  |  |  | regionType = "area"; | 
|---|
|  |  |  | nextLevel = "town"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (regionCode.endsWith("000")) { | 
|---|
|  |  |  | regionType = "town_code"; | 
|---|
|  |  |  | regionType = "town"; | 
|---|
|  |  |  | nextLevel = "village"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | parameters.put("regionType", regionType); | 
|---|
|  |  |  | parameters.put("nextLevel", nextLevel); | 
|---|
|  |  |  | parameters.put("start", LocalDateTime.now().minusMinutes(5)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LocalDate localDate = LocalDate.now(); | 
|---|
|  |  |  | Object timeType = parameters.get("timeType"); | 
|---|
|  |  |  | String table = "history"; | 
|---|
|  |  |  | String column = "value"; | 
|---|
|  |  |  | if ("month".equals(timeType)) { | 
|---|
|  |  |  | if (1 != localDate.getDayOfMonth()) { | 
|---|
|  |  |  | table = "history_minutely"; | 
|---|
|  |  |  | column = "json"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | parameters.put("start", localDate.with(TemporalAdjusters.firstDayOfMonth())); | 
|---|
|  |  |  | } else if ("day".equals(timeType)) { | 
|---|
|  |  |  | parameters.put("start", localDate); | 
|---|
|  |  |  | } else if ("hour".equals(timeType)) { | 
|---|
|  |  |  | parameters.put("start", LocalDateTime.now().truncatedTo(ChronoUnit.HOURS)); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | parameters.put("start", LocalDateTime.now().minusMinutes(5)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | parameters.put("table", table); | 
|---|
|  |  |  | parameters.put("column", column); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String dimension = ""; | 
|---|
|  |  |  | if ("discharge".equals(parameters.get("dimension"))) { | 
|---|
|  |  |  | dimension = "discharge"; | 
|---|
|  |  |  | parameters.put("dimension","monitorPoint"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | accountService.setOrgIdsByAccount(parameters); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Map<String, Object>> result = historyMapper.getRegionRankingData(parameters); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if ("profession".equals(parameters.get("dimension"))) { | 
|---|
|  |  |  | List<Profession> professions = professionMapper.selectAll(); | 
|---|
|  |  |  | for (Map<String, Object> map : result) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (map.get("code").equals(region.get("code"))) { | 
|---|
|  |  |  | map.put("name", region.get("name")); | 
|---|
|  |  |  | regions.remove(region); | 
|---|
|  |  |  | map.remove("code"); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | result.add(map); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if ("discharge".equals(dimension)) { | 
|---|
|  |  |  | parameters.put("year", LocalDate.now().getYear()); | 
|---|
|  |  |  | List<Map<String, Object>> discharges = historyMapper.getDischargeByRegion(parameters); | 
|---|
|  |  |  | for (Map<String, Object> map : result) { | 
|---|
|  |  |  | for (Map<String, Object> discharge : discharges) { | 
|---|
|  |  |  | if (map.get("name").equals(discharge.get("name"))) { | 
|---|
|  |  |  | map.put("value", discharge.get("value")); | 
|---|
|  |  |  | discharges.remove(discharge); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Collections.sort(result, new Comparator<Map<String, Object>>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public int compare(Map<String, Object> o1, Map<String, Object> o2) { | 
|---|
|  |  |  | return Double.compare(((Double) o2.get("avg"))- Double.valueOf((String) o2.get("value")), | 
|---|
|  |  |  | ((Double) o1.get("avg")) - Double.valueOf((String) o1.get("value"))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if (IS_DELETE_FALSE.equals(organization.getIsDelete())) { | 
|---|
|  |  |  | List<OrganizationRelation> organizationRelations = organizationRelationMapper.select(relation); | 
|---|
|  |  |  | for (OrganizationRelation organizationRelation : organizationRelations) { | 
|---|
|  |  |  | Set<Integer> organizationIds = getChildOrganizationIds(organizationRelation.getParentId()); | 
|---|
|  |  |  | Set<Integer> organizationIds = getChildOrganizationIds(organizationRelation.getChildId()); | 
|---|
|  |  |  | orgIds.addAll(organizationIds); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <select id="getRegionRankingData" resultType="java.util.Map"> | 
|---|
|  |  |  | SELECT | 
|---|
|  |  |  | AVG( h.`value` -> '$.${sensorKey}[0]' ) avg , | 
|---|
|  |  |  | AVG( h.${column} -> '$.${sensorKey}[0]' ) avg , | 
|---|
|  |  |  | <if test="dimension == 'profession'"> | 
|---|
|  |  |  | p.`name` | 
|---|
|  |  |  | </if> | 
|---|
|  |  |  | 
|---|
|  |  |  | profession p , | 
|---|
|  |  |  | </if> | 
|---|
|  |  |  | device d, | 
|---|
|  |  |  | history h, | 
|---|
|  |  |  | ${table} h, | 
|---|
|  |  |  | monitor_point mp | 
|---|
|  |  |  | WHERE | 
|---|
|  |  |  | d.mac = h.mac | 
|---|
|  |  |  | 
|---|
|  |  |  | AND mp.is_delete = '0' | 
|---|
|  |  |  | AND d.monitor_point_id = mp.id | 
|---|
|  |  |  | AND h.time >= #{start} | 
|---|
|  |  |  | AND mp.${regionType} = #{regionCode} | 
|---|
|  |  |  | AND mp.${regionType}_code = #{regionCode} | 
|---|
|  |  |  | <if test="dimension == 'profession'"> | 
|---|
|  |  |  | AND d.profession_id = p.id | 
|---|
|  |  |  | </if> | 
|---|
|  |  |  | 
|---|
|  |  |  | FROM | 
|---|
|  |  |  | ${nextLevel} | 
|---|
|  |  |  | WHERE | 
|---|
|  |  |  | ${regionType} = #{regionCode} | 
|---|
|  |  |  | ${regionType}_code = #{regionCode} | 
|---|
|  |  |  | </select> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <select id="getDischargeByRegion" resultType="java.util.Map"> | 
|---|
|  |  |  | SELECT | 
|---|
|  |  |  | mp.`name`, | 
|---|
|  |  |  | dis.`value` -> '$.e1' 'value' | 
|---|
|  |  |  | FROM | 
|---|
|  |  |  | monitor_point mp, | 
|---|
|  |  |  | discharge dis | 
|---|
|  |  |  | WHERE | 
|---|
|  |  |  | dis.monitor_point_id = mp.id | 
|---|
|  |  |  | AND mp.${regionType}_code = #{regionCode} | 
|---|
|  |  |  | AND dis.`year` = #{year} | 
|---|
|  |  |  | </select> | 
|---|
|  |  |  | </mapper> | 
|---|