| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | 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.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | |
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.util.ObjectUtils;
|
| | |
|
| | | import com.alibaba.fastjson.JSON;
|
| | | import com.alibaba.fastjson.JSONObject;
|
| | | 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.SensorMapper;
|
| | | import com.moral.service.AccountService;
|
| | | import com.moral.service.HistoryService;
|
| | | import com.moral.service.ProfessionService;
|
| | |
|
| | | @Service
|
| | | public class HistoryServiceImpl implements HistoryService {
|
| | |
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | @Resource
|
| | | private ProfessionMapper professionMapper;
|
| | | private ProfessionService professionService;
|
| | |
|
| | | @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()));
|
| | |
| | | accountService.setOrgIdsByAccount(parameters);
|
| | |
|
| | | List<Map<String, Object>> result = historyMapper.getRegionRankingData(parameters);
|
| | | |
| | | for (Map<String, Object> map : result) {
|
| | | if (ObjectUtils.isEmpty(map.get("avg"))) {
|
| | | map.put("avg", 0);
|
| | | }
|
| | | }
|
| | | 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())) {
|
| | | professions.remove(profession);
|
| | | if (ObjectUtils.isEmpty(map.get("avg"))) {
|
| | | map.put("avg", 0);
|
| | | }
|
| | | // if (ObjectUtils.isEmpty(map.get("avg"))) {
|
| | | // map.put("avg", 0);
|
| | | // }
|
| | | break;
|
| | | }
|
| | | }
|
| | |
| | | for (Map<String, Object> map : result) {
|
| | | if (regions.contains(map.get("name"))) {
|
| | | regions.remove(map.get("name"));
|
| | | if (ObjectUtils.isEmpty(map.get("avg"))) {
|
| | | map.put("avg", 0);
|
| | | }
|
| | | // if (ObjectUtils.isEmpty(map.get("avg"))) {
|
| | | // map.put("avg", 0);
|
| | | // }
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | }
|
| | | return result;
|
| | | }
|
| | | |
| | | @Override
|
| | | public List<Map<String, Object>> getSensorsDataByDevice(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("deviceVersionId"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("mac"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("deviceId"), "param.is.null");
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(parameters);
|
| | | parameters.put("size", 1);
|
| | | List<Map<String, Object>> values = historyMapper.getValueByMacAndSize(parameters);
|
| | | JSONObject map = null;
|
| | | if (!ObjectUtils.isEmpty(values)) {
|
| | | map = JSON.parseObject(values.get(0).get("value").toString());
|
| | | }
|
| | | Device device = deviceMapper.selectByPrimaryKey(Integer.valueOf((String) parameters.get("deviceId")));
|
| | | for (Map<String, Object> sensor : sensors) {
|
| | | if (!ObjectUtils.isEmpty(map)) {
|
| | | sensor.put("value", map.remove(sensor.get("sensor_key")));
|
| | | }
|
| | | sensor.put("state", device.getState());
|
| | | sensor.put("name", sensor.get("description"));
|
| | | }
|
| | | return sensors;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getSensorDataBySensorKey(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("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);
|
| | | for (Map<String, Object> value : values) {
|
| | | JSONObject json = JSON.parseObject(value.remove("value").toString());
|
| | | value.put("value", json.get(parameters.get("sensorKey")));
|
| | | }
|
| | | 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;
|
| | | }
|
| | |
|
| | | }
|