| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.time.Instant;
|
| | | import java.time.LocalDate;
|
| | | import java.time.LocalDateTime;
|
| | | import java.time.ZoneId;
|
| | | import java.time.ZoneOffset;
|
| | | import java.time.temporal.ChronoUnit;
|
| | | import java.time.temporal.TemporalAdjusters;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.Calendar;
|
| | | import java.util.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | |
| | |
|
| | | import com.alibaba.fastjson.JSON;
|
| | | import com.alibaba.fastjson.JSONObject;
|
| | | import com.googlecode.aviator.AviatorEvaluator;
|
| | | import com.googlecode.aviator.Expression;
|
| | | import com.moral.common.util.ValidateUtil;
|
| | | import com.moral.entity.Device;
|
| | | import com.moral.entity.Profession;
|
| | | import com.moral.entity.Sensor;
|
| | | import com.moral.mapper.DeviceMapper;
|
| | | import com.moral.mapper.HistoryMapper;
|
| | | import com.moral.mapper.ProfessionMapper;
|
| | | import com.moral.mapper.MonitorPointMapper;
|
| | | import com.moral.mapper.OrganizationRelationMapper;
|
| | | import com.moral.mapper.SensorMapper;
|
| | | import com.moral.mapper.SensorUnitMapper;
|
| | | import com.moral.service.AccountService;
|
| | | import com.moral.service.HistoryService;
|
| | | import com.moral.service.ProfessionService;
|
| | | import com.moral.util.AQICalculation;
|
| | |
|
| | | @Service
|
| | | public class HistoryServiceImpl implements HistoryService {
|
| | |
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | @Resource
|
| | | private ProfessionMapper professionMapper;
|
| | | private ProfessionService professionService;
|
| | | |
| | | @Resource
|
| | | private OrganizationRelationMapper organizationRelationMapper;
|
| | | |
| | | @Resource
|
| | | private MonitorPointMapper monitorPointMapper;
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getAllSensorAverageByDevice(Map<String, Object> parameters) throws Exception {
|
| | |
| | | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
|
| | |
|
| | | String regionCode = parameters.get("regionCode").toString();
|
| | | parameters.put("provinceCode", regionCode.substring(0,2).concat("0000"));
|
| | | String regionType = "village";
|
| | | String nextLevel = "";
|
| | | if (regionCode.length() == 6) {
|
| | |
| | | String column = "value";
|
| | | if ("month".equals(timeType)) {
|
| | | if (1 != localDate.getDayOfMonth()) {
|
| | | table = "history_minutely";
|
| | | table = "history_daily";
|
| | | column = "json";
|
| | | }
|
| | | parameters.put("start", localDate.with(TemporalAdjusters.firstDayOfMonth()));
|
| | |
| | | }
|
| | | }
|
| | | if ("profession".equals(parameters.get("dimension"))) {
|
| | | List<Profession> professions = professionMapper.selectAll();
|
| | | List<Profession> professions = professionService.getProfessiontList(parameters);
|
| | | for (Map<String, Object> map : result) {
|
| | | for (Profession profession : professions) {
|
| | | if (map.get("name").equals(profession.getName())) {
|
| | |
| | | map = JSON.parseObject(values.get(0).get("value").toString());
|
| | | }
|
| | | Device device = deviceMapper.selectByPrimaryKey(Integer.valueOf((String) parameters.get("deviceId")));
|
| | | Map<String, Object> sensorUnits = getSensorUnitByDeviceMac(parameters);
|
| | | for (Map<String, Object> sensor : sensors) {
|
| | | Object sensorKey = sensor.get("sensor_key");
|
| | | sensor.put("name", sensor.get("description"));
|
| | | sensor.put("state", device.getState());
|
| | | if (!ObjectUtils.isEmpty(map)) {
|
| | | sensor.put("value", map.remove(sensor.get("sensor_key")));
|
| | | }
|
| | | sensor.put("state", device.getState());
|
| | | sensor.put("name", sensor.get("description"));
|
| | | if (sensorUnits.containsKey(sensorKey)) {
|
| | | Map<String, Object> sensorUnit = (Map<String, Object>) sensorUnits.get(sensorKey);
|
| | | sensor.put("unit", sensorUnit.get("name"));
|
| | | if (sensor.containsKey("value")) {
|
| | | Object value = sensor.get("value");
|
| | | String rules = sensorUnit.get("rules").toString();
|
| | | rules = rules.replace("d", "").replace("{0}", "value");
|
| | | Expression expression = AviatorEvaluator.compile(rules);
|
| | | Map<String, Object> env = new HashMap<String, Object>();
|
| | | env.put("value", Double.valueOf(value.toString()));
|
| | | value = expression.execute(env);
|
| | | sensor.put("value",String.format("%.2f", value));
|
| | | }
|
| | | }
|
| | | }
|
| | | return sensors;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getSensorDataBySensorKey(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("sensorKey"), "param.is.null");
|
| | | Object sensorKey = parameters.get("sensorKey");
|
| | | ValidateUtil.notNull(sensorKey, "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("mac"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("size"), "param.is.null");
|
| | | parameters.put("size", Integer.valueOf(parameters.remove("size").toString()));
|
| | | List<Map<String, Object>> values = historyMapper.getValueByMacAndSize(parameters);
|
| | | Map<String, Object> sensorUnits = getSensorUnitByDeviceMac(parameters);
|
| | | for (Map<String, Object> value : values) {
|
| | | JSONObject json = JSON.parseObject(value.remove("value").toString());
|
| | | value.put("value", json.get(parameters.get("sensorKey")));
|
| | | Object sensorKeyValue = json.get(sensorKey);
|
| | | if (sensorUnits.containsKey(sensorKey)) {
|
| | | Map<String, Object> sensorUnit = (Map<String, Object>) sensorUnits.get(sensorKey);
|
| | | String rules = sensorUnit.get("rules").toString();
|
| | | rules = rules.replace("d", "").replace("{0}", "value");
|
| | | Expression expression = AviatorEvaluator.compile(rules);
|
| | | Map<String, Object> env = new HashMap<String, Object>();
|
| | | env.put("value", Double.valueOf(sensorKeyValue.toString()));
|
| | | sensorKeyValue = expression.execute(env);
|
| | | sensorKeyValue = String.format("%.2f", sensorKeyValue);
|
| | | }
|
| | | value.put("value", sensorKeyValue);
|
| | | }
|
| | | return values;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getActualDataByRegion(Map<String, Object> parameters) {
|
| | | Object sensorKey = parameters.get("sensorKey");
|
| | | ValidateUtil.notNull(sensorKey, "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("interval"), "param.is.null");
|
| | | Integer interval = Integer.valueOf(parameters.get("interval").toString());
|
| | | LocalDateTime start, end;
|
| | | if (!parameters.containsKey("time")) {
|
| | | end = LocalDateTime.now();
|
| | | int second = end.getSecond() / interval * interval;
|
| | | end = end.truncatedTo(ChronoUnit.MINUTES).plusSeconds(second);
|
| | | start = end.minusSeconds(interval);
|
| | | } else {
|
| | | //start = LocalDateTime.ofEpochSecond(Long.valueOf(parameters.get("time").toString()) / 1000, 0, ZoneOffset.ofHours(8));
|
| | | start = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(parameters.get("time").toString())),ZoneId.of("+8"));
|
| | | end = start.plusSeconds(interval);
|
| | | }
|
| | | parameters.put("start", start);
|
| | | parameters.put("end", end);
|
| | | parameters.put("sensorKeys", Arrays.asList(((String) sensorKey).split(",")));
|
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | Map<String, Object> result = historyMapper.getActualDataByRegion(parameters);
|
| | | if (ObjectUtils.isEmpty(result)) {
|
| | | result = new HashMap<String, Object>();
|
| | | }
|
| | | result.put("time", end.toInstant(ZoneOffset.of("+8")).toEpochMilli());
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Resource
|
| | | private SensorUnitMapper sensorUnitMapper;
|
| | | @Override
|
| | | public Map<String, Object> getSensorUnitByDeviceMac(Map<String, Object> parameters){
|
| | | Map<String, Object> resultMap = new HashMap<String, Object>();
|
| | | List<Map<String, Object>> list = sensorUnitMapper.getSensorUnitByDeviceMac(parameters);
|
| | | for (Map<String, Object> map : list) {
|
| | | resultMap.put(map.remove("sensor_key").toString(), map);
|
| | | }
|
| | | return resultMap;
|
| | | |
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> gitHourlyAQIByMacAndTimeslot(Map<String, Object> parameters) {
|
| | | String mac = parameters.get("mac").toString();
|
| | | //获取日期
|
| | | LocalDate localDate = LocalDate.now();
|
| | | Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
|
| | | int endHour = c.get(Calendar.HOUR_OF_DAY);
|
| | | String endTime = localDate+" "+endHour+":00:00";
|
| | | |
| | | String startTime;
|
| | | if(endHour == 0) {
|
| | | LocalDate startDate = localDate.minusDays(1);
|
| | | startTime = startDate+" "+"23:00:00";
|
| | | }else {
|
| | | int startHour = endHour-1;
|
| | | startTime = localDate+" "+startHour+":00:00";
|
| | | }
|
| | | Map<String, Object> map = historyMapper.getAVGValueByMacAndTimeslot(mac, startTime, endTime);
|
| | | System.out.println(map);
|
| | | Map<String, Object> returnMap = new HashMap<>();
|
| | | if (map.isEmpty()) {
|
| | | returnMap.put("AQI", "N/V");
|
| | | } else {
|
| | | Map<String, Double> AQIMap = new HashMap<>();
|
| | | for (Map.Entry<String, Object> entry : map.entrySet()) {
|
| | | String key = entry.getKey();
|
| | | Double value = Double.parseDouble(entry.getValue().toString());
|
| | | AQIMap.put(key, value);
|
| | | }
|
| | | returnMap = AQICalculation.hourlyAQI(AQIMap);
|
| | | }
|
| | | |
| | | return returnMap;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getMointorPointRankByOrganizationId(Map<String, Object> parameters) {
|
| | | //获取组织id
|
| | | String organizationId = parameters.get("organization_id").toString();
|
| | | //声明一个集合存放要查询站点数据的组织id
|
| | | List<Object> organizationIdList = new ArrayList<>();
|
| | | organizationIdList.add(organizationId);
|
| | | //声明一个集合存放父组织id
|
| | | List<Object> parentIdList = new ArrayList<>();
|
| | | parentIdList.add(organizationId);
|
| | | |
| | | //进行循环
|
| | | for(int i=0;i<10;i++) {
|
| | | if(!parentIdList.isEmpty()) {
|
| | | //或去该组织下的子组织
|
| | | List<Map<String, Object>> childIdList = organizationRelationMapper.getChildIdByParentId(parentIdList);
|
| | | //清空父组织id集合
|
| | | parentIdList.clear();
|
| | | //将子组织id放入到集合中
|
| | | for (Map<String, Object> map : childIdList) {
|
| | | organizationIdList.add(map.get("child_id"));
|
| | | parentIdList.add(map.get("child_id"));
|
| | | }
|
| | | }else {
|
| | | break;
|
| | | }
|
| | | }
|
| | | System.out.println(organizationIdList);
|
| | | |
| | | //根据组织id获取站点id
|
| | | List<Map<String, Object>> monitorPointIdListMap = monitorPointMapper.getIdByOrganizationId(organizationIdList);
|
| | | //将List<Map<String, Object>>装换为List<Object>
|
| | | List<Object> monitorPointIdList = new ArrayList<>();
|
| | | for (Map<String, Object> map : monitorPointIdListMap) {
|
| | | monitorPointIdList.add(map.get("id"));
|
| | | }
|
| | | |
| | | //通过站点id获取设备因子的排名
|
| | | //获取系统当前时间
|
| | | Calendar rightNow = Calendar.getInstance();
|
| | | //获取五分钟前的时间
|
| | | Calendar beforeTime = Calendar.getInstance();
|
| | | beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
|
| | | Date beforeD = beforeTime.getTime();
|
| | | Date end = rightNow.getTime();
|
| | | String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(end);
|
| | | String before5Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforeD); // 前五分钟时间
|
| | | |
| | | String sensor = parameters.get("sensor").toString();
|
| | | List<Map<String, Object>> result = historyMapper.getAVGSensorRankByMonitorPointIdList(sensor, monitorPointIdList, before5Time, endTime);
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> gitHourlyAQIByMonitorPointIdAndTimeslot(Map<String, Object> parameters) {
|
| | | String monitor_point_id = parameters.get("monitor_point_id").toString();
|
| | | //获取日期
|
| | | LocalDate localDate = LocalDate.now();
|
| | | Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
|
| | | int endHour = c.get(Calendar.HOUR_OF_DAY);
|
| | | String endTime = localDate+" "+endHour+":00:00";
|
| | | |
| | | String startTime;
|
| | | if(endHour == 0) {
|
| | | LocalDate startDate = localDate.minusDays(1);
|
| | | startTime = startDate+" "+"23:00:00";
|
| | | }else {
|
| | | int startHour = endHour-1;
|
| | | startTime = localDate+" "+startHour+":00:00";
|
| | | }
|
| | | Map<String, Object> map = historyMapper.getMonitorPointAVGValueByMonitorPointIdAndTimeslot(monitor_point_id, startTime, endTime);
|
| | | System.out.println(map);
|
| | | Map<String, Object> returnMap = new HashMap<>();
|
| | | if (map.isEmpty()) {
|
| | | returnMap.put("AQI", "N/V");
|
| | | } else {
|
| | | Map<String, Double> AQIMap = new HashMap<>();
|
| | | for (Map.Entry<String, Object> entry : map.entrySet()) {
|
| | | String key = entry.getKey();
|
| | | Double value = Double.parseDouble(entry.getValue().toString());
|
| | | AQIMap.put(key, value);
|
| | | }
|
| | | returnMap = AQICalculation.hourlyAQI(AQIMap);
|
| | | }
|
| | | |
| | | return returnMap;
|
| | | }
|
| | | }
|