| | |
| | | package com.moral.service.impl; |
| | | |
| | | import com.moral.mapper.*; |
| | | import io.swagger.models.auth.In; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.NumberFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.Instant; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.Iterator; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | |
| | | import org.apache.commons.collections.CollectionUtils; |
| | | import org.apache.commons.lang3.time.DateUtils; |
| | | import org.apache.jasper.compiler.JspUtil; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | |
| | | import com.moral.common.util.ReportTimeFormat; |
| | | import com.moral.common.util.ResourceUtil; |
| | | import com.moral.common.util.ValidateUtil; |
| | | import com.moral.entity.Area; |
| | | import com.moral.entity.City; |
| | | import com.moral.entity.MonitorPoint; |
| | | import com.moral.entity.Province; |
| | | import com.moral.entity.Sensor; |
| | | import com.moral.entity.charts.DataCondition; |
| | | import com.moral.entity.charts.LineChartCriteria; |
| | | import com.moral.entity.charts.TimePeriod; |
| | | import com.moral.mapper.AlarmDailyMapper; |
| | | import com.moral.mapper.AreaMapper; |
| | | import com.moral.mapper.CityMapper; |
| | | import com.moral.mapper.DeviceMapper; |
| | | import com.moral.mapper.HistoryMapper; |
| | | import com.moral.mapper.HistoryMinutelyMapper; |
| | | import com.moral.mapper.MonitorPointMapper; |
| | | import com.moral.mapper.ProvinceMapper; |
| | | import com.moral.mapper.SensorMapper; |
| | | import com.moral.mapper.ShAreaMapper; |
| | | import com.moral.service.HistoryMinutelyService; |
| | | import com.sun.org.apache.bcel.internal.generic.ANEWARRAY; |
| | | |
| | | import static com.moral.common.bean.Constants.NULL_VALUE; |
| | | import static org.springframework.util.ObjectUtils.isEmpty; |
| | |
| | | |
| | | @Resource |
| | | private AlarmDailyMapper alarmDailyMapper; |
| | | |
| | | @Resource |
| | | private AreaMapper areaMapper; |
| | | |
| | | @Resource |
| | | private ProvinceMapper provinceMapper; |
| | | |
| | | @Resource |
| | | private CityMapper cityMapper; |
| | | |
| | | @Resource |
| | | private MonitorPointMapper monitorPointMapper; |
| | | |
| | | |
| | | @Override |
| | | public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getAreaAvgDataByAreaCode(Map<String, Object> parameters) throws Exception { |
| | | public List<Map<String, Object>> get(Map<String, Object> parameters) throws Exception { |
| | | convertQueryParam(parameters); |
| | | if (!ObjectUtils.isEmpty(parameters.get("compensate"))) { |
| | | parameters.put("timeUnits", "10min"); |
| | | } |
| | | return historyMinutelyMapper.getAreaAvgDataByAreaCode(parameters); |
| | | |
| | | return historyMinutelyMapper.getMultiDeviceSensorData(parameters); |
| | | } |
| | | |
| | | @Override |
| | | public void convertQueryParam(Map<String, Object> parameters) throws ParseException { |
| | | if (!parameters.containsKey("field")) { |
| | | // String type = (String) parameters.get("type"); |
| | | // parameters.putAll(getElementByType(type)); |
| | | ParameterUtils.getElementByType(parameters); |
| | | if (parameters.containsKey("timeUnits")) { |
| | | if ("minutely".equals(parameters.get("timeUnits"))) { |
| | |
| | | String[] timeStr = parameters.get("time").toString().split("-"); |
| | | int year = Integer.valueOf(timeStr[0]); |
| | | int month = Integer.valueOf(timeStr[1]); |
| | | String yearAndMonth; |
| | | if (month < 10) { |
| | | yearAndMonth = "minutely_" + year + "0" + month; |
| | | } else { |
| | | yearAndMonth = "minutely_" + year + month; |
| | | } |
| | | if (year >= 2020) { |
| | | String yearAndMonth; |
| | | if (month < 10) { |
| | | yearAndMonth = "minutely_" + year + "0" + month; |
| | | } else { |
| | | yearAndMonth = "minutely_" + year + month; |
| | | } |
| | | parameters.put("timeUnits", yearAndMonth); |
| | | } |
| | | } |
| | |
| | | } |
| | | parameters.put("timeList", timeList); |
| | | } else if ("day".equals(parameters.get("type"))) { |
| | | String time = parameters.get("time") + "T00:00:00"; |
| | | String time = parameters.get("time") + "T01:00:00"; |
| | | LocalDateTime value = LocalDateTime.parse(time); |
| | | LocalDateTime end = value.plusHours(23); |
| | | parameters.put("timeUnits", "hourly"); |
| | |
| | | return historyMinutelyMapper.getDevicesSensorsAvgDataToExcel(parameters); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> get5MinutesOrHalfHour(Map<String, Object> parameters) throws ParseException { |
| | | if (parameters.get("city") == null) { |
| | | return new ArrayList<Map<String, Object>>(); |
| | | } |
| | | String string = parameters.get("time").toString(); |
| | | String year = string.substring(0, 4); |
| | | String[] split = string.substring(5).split("-"); |
| | | String month = split[0]; |
| | | String day = split[1]; |
| | | if (split[0].length() < 2) { |
| | | month = 0 + split[0]; |
| | | } |
| | | if (split[1].length() < 2) { |
| | | day = 0 + split[1]; |
| | | } |
| | | String time = year + "-" + month + "-" + day; |
| | | String cityName = parameters.get("city").toString(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String type = parameters.get("type").toString(); |
| | | String range = parameters.get("range").toString(); |
| | | Area area = areaMapper.getAreaByName(cityName); |
| | | Integer code; |
| | | String name; |
| | | if (area == null) { |
| | | City city = cityMapper.getCityByName(cityName); |
| | | if (city == null) { |
| | | Province province = provinceMapper.getProvinceByName(cityName); |
| | | code = province.getProvinceCode(); |
| | | name = province.getProvinceName(); |
| | | } else { |
| | | code = city.getCityCode(); |
| | | name = city.getCityName(); |
| | | } |
| | | } else { |
| | | code = area.getAreaCode(); |
| | | name = area.getAreaName(); |
| | | } |
| | | parameters.put("cityCode", code); |
| | | parameters.put("name", name); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | if (code.toString().endsWith("0000")) { |
| | | map.put("provinceCode", code); |
| | | } else if (code.toString().endsWith("00")) { |
| | | map.put("cityCode", code); |
| | | } else { |
| | | map.put("areaCode", code); |
| | | } |
| | | //List<String> sensorKeys = sensorMapper.getSensorKeys(); |
| | | |
| | | List<String> sensorKeys = new ArrayList<String>(); |
| | | Collections.addAll(sensorKeys, "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e10", "e11", "e12", "e13", "e14", "e15" |
| | | , "e16", "e17", "e18", "e21", "e23", "e25", "e26", "e27", "e28", "e31", "e33", "e45", "e49", "e97", "e98", "e102"); |
| | | |
| | | String timeUnits = "minutely_" + time.substring(0, 7).replace("-", ""); |
| | | map.put("sensorKeys", sensorKeys); |
| | | map.put("timeUnits", timeUnits); |
| | | Calendar cal = Calendar.getInstance(); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | int seg; |
| | | long startTime; |
| | | long endTime; |
| | | int minuteChange; |
| | | if ("m".equals(type)) { |
| | | cal.setTime(sdf.parse(time + " 00:05:00")); |
| | | startTime = cal.getTimeInMillis(); |
| | | cal.add(Calendar.DAY_OF_MONTH, 1); |
| | | endTime = cal.getTimeInMillis(); |
| | | seg = 5 * 60 * 1000; |
| | | minuteChange = -5; |
| | | } else { |
| | | cal.setTime(sdf.parse(time + " 00:30:00")); |
| | | startTime = cal.getTimeInMillis(); |
| | | cal.add(Calendar.DAY_OF_MONTH, 1); |
| | | endTime = cal.getTimeInMillis(); |
| | | seg = 30 * 60 * 1000; |
| | | minuteChange = -30; |
| | | } |
| | | for (long time1 = startTime; time1 < endTime; time1 += seg) { |
| | | Date end = new Date(time1); |
| | | cal.setTime(end); |
| | | cal.add(Calendar.MINUTE, minuteChange); |
| | | Date start = cal.getTime(); |
| | | map.put("start", start); |
| | | map.put("end", end); |
| | | Map<String, Object> dataMap = historyMinutelyMapper.get5MiutesOrHalfHourByDay(map); |
| | | if (dataMap == null) { |
| | | break; |
| | | } |
| | | dataMap.put("time", sdf.format(end)); |
| | | dataMap.put("city", name); |
| | | Set<String> set = dataMap.keySet(); |
| | | Iterator<String> it = set.iterator(); |
| | | List<String> listKey = new ArrayList<>(); |
| | | while (it.hasNext()) { |
| | | String key = it.next(); |
| | | if (dataMap.get(key) == null || "".equals(dataMap.get(key))) { |
| | | listKey.add(key); |
| | | } |
| | | } |
| | | for (String key : listKey) { |
| | | dataMap.remove(key); |
| | | } |
| | | resultList.add(dataMap); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getMultiDeviceSensorData(Map<String, Object> parameters) throws Exception { |
| | | String sensorKey = parameters.get("sensorKey").toString(); |
| | | String[] macs = parameters.get("macs").toString().split(","); |
| | | List<String> sensorKeys = new ArrayList<>(); |
| | | sensorKeys.add(sensorKey); |
| | | parameters.put("sensorKeys", sensorKeys); |
| | | parameters.put("sensors", sensorKeys); |
| | | String type = parameters.get("type").toString(); |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (int i = 0; i < 31; i++) { |
| | | list.add(null); |
| | | } |
| | | for (String mac : macs) { |
| | | parameters.put("mac", mac); |
| | | List<Map<String, Object>> data = getMonitorPointOrDeviceAvgData(parameters); |
| | | List<Map<String, Object>> l = new ArrayList<>(); |
| | | for (int i = 0; i < 31; i++) { |
| | | l.add(null); |
| | | } |
| | | for (Map<String, Object> dataMap : data) { |
| | | String time = dataMap.get("time").toString(); |
| | | Integer t = Integer.valueOf(time.substring(time.length() - 2)); |
| | | dataMap.put("time", t); |
| | | if ("day".equals(type)) { |
| | | l.set(t, dataMap); |
| | | } else { |
| | | l.set(t - 1, dataMap); |
| | | } |
| | | } |
| | | for (int i = 0; i < l.size(); i++) { |
| | | if (l.get(i) == null) { |
| | | Map<String, Object> m = new HashMap<>(); |
| | | List<String> v = new ArrayList<>(); |
| | | v.add(""); |
| | | if ("day".equals(type)) { |
| | | m.put("time", i); |
| | | } else { |
| | | m.put("time", i + 1); |
| | | } |
| | | m.put("values", v); |
| | | l.set(i, m); |
| | | } |
| | | } |
| | | data = l; |
| | | for (Map<String, Object> map : data) { |
| | | Map<String, Object> hashMap = new HashMap<>(); |
| | | if (map != null) { |
| | | int t = Integer.valueOf(map.get("time").toString()); |
| | | hashMap.put("time", t); |
| | | String value; |
| | | if (map.get(sensorKey) == null) { |
| | | value = ""; |
| | | } else { |
| | | value = map.get(sensorKey).toString(); |
| | | } |
| | | List<String> values; |
| | | |
| | | if ("day".equals(type)) { |
| | | t = t + 1; |
| | | } |
| | | if (list.get(t - 1) != null) { |
| | | values = (ArrayList<String>) list.get(t - 1).get("values"); |
| | | } else { |
| | | values = new ArrayList<>(); |
| | | } |
| | | values.add(value); |
| | | hashMap.put("values", values); |
| | | list.set(t - 1, hashMap); |
| | | } |
| | | } |
| | | } |
| | | boolean flag = false; |
| | | Iterator<Map<String, Object>> iterator = list.iterator(); |
| | | int digit = 0; |
| | | while (iterator.hasNext()) { |
| | | Map<String, Object> next = iterator.next(); |
| | | ArrayList<String> values = (ArrayList<String>) next.get("values"); |
| | | for (String value : values) { |
| | | if ("".equals(value)) { |
| | | flag = true; |
| | | } else { |
| | | digit = value.split("\\.")[1].length(); |
| | | flag = false; |
| | | break; |
| | | } |
| | | } |
| | | if (flag) { |
| | | iterator.remove(); |
| | | } |
| | | } |
| | | NumberFormat nf = NumberFormat.getNumberInstance(); |
| | | nf.setMaximumFractionDigits(digit); |
| | | for (Map<String, Object> map : list) { |
| | | int time = Integer.valueOf(map.get("time").toString()); |
| | | ArrayList<String> values = (ArrayList<String>) map.get("values"); |
| | | if (values.size() > 1) { |
| | | int length = 0; |
| | | double sum = 0.0; |
| | | for (String value : values) { |
| | | if (!"".equals(value)) { |
| | | Double v = Double.valueOf(value); |
| | | length += 1; |
| | | sum += v; |
| | | } |
| | | } |
| | | Double avg = sum / length; |
| | | String format = nf.format(avg); |
| | | values.add(format); |
| | | map.put("values", values); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getAllDeviceDataToExcel(Map<String, Object> parameters) throws Exception { |
| | | Calendar cal = Calendar.getInstance(); |
| | | int length = ((String) parameters.get("startTime")).split("-").length; |
| | | String time = parameters.remove("startTime").toString(); |
| | | String timeb; |
| | | if (parameters.get("endTime") == null) { |
| | | timeb = time; |
| | | } else { |
| | | timeb = parameters.remove("endTime").toString(); |
| | | } |
| | | String dateFormat = ""; |
| | | String typeFormat = ""; |
| | | String timeUnits = "daily"; |
| | | int i = 0; |
| | | if (length == 1) { |
| | | dateFormat = "yyyy"; |
| | | typeFormat = "%Y"; |
| | | i = Calendar.YEAR; |
| | | } else if (length == 2) { |
| | | dateFormat = "yyyy-MM"; |
| | | typeFormat = "%Y-%m"; |
| | | i = Calendar.MONTH; |
| | | } else if (length == 3) { |
| | | dateFormat = "yyyy-MM-dd"; |
| | | typeFormat = "%Y-%m-%d"; |
| | | i = Calendar.DAY_OF_MONTH; |
| | | } else if (length == 4) { |
| | | dateFormat = "yyyy-MM-dd HH"; |
| | | typeFormat = "%Y-%m-%d %H"; |
| | | timeUnits = "hourly"; |
| | | i = Calendar.HOUR_OF_DAY; |
| | | time = time.substring(0, 10) + " " + time.substring(11, 13); |
| | | timeb = timeb.substring(0, 10) + " " + timeb.substring(11, 13); |
| | | } |
| | | parameters.put("time", time); |
| | | parameters.put("timeb", timeb); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); |
| | | cal.setTime(sdf.parse(timeb)); |
| | | cal.add(i, 1); |
| | | Date endTime = cal.getTime(); |
| | | parameters.put("format", dateFormat); |
| | | parameters.put("typeFormat", typeFormat); |
| | | parameters.put("timeUnits", timeUnits); |
| | | parameters.put("field", i); |
| | | cal.setTime(sdf.parse(time)); |
| | | List<String> times = new ArrayList<>(); |
| | | for (long d = cal.getTimeInMillis(); d < endTime.getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) { |
| | | String format = sdf.format(d); |
| | | times.add(format); |
| | | } |
| | | String[] sensorKeys = parameters.get("sensorKey").toString().split(","); |
| | | List<String> keys = Arrays.asList(sensorKeys); |
| | | parameters.put("sensors", keys); |
| | | parameters.put("sensorKeys", keys); |
| | | parameters.put("timeb", sdf.format(endTime)); |
| | | int mpId = Integer.valueOf(parameters.get("monitorPoint").toString()); |
| | | String monitorPointName = monitorPointMapper.getMonitorName(mpId); |
| | | List<Map<String, Object>> devices = deviceMapper.getDevicesByMpId(mpId); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | for (Map<String, Object> map : devices) { |
| | | String mac = map.get("mac").toString(); |
| | | parameters.put("mac", mac); |
| | | String name = map.get("name").toString(); |
| | | List<Map<String, Object>> data = getMonitorPointOrDeviceAvgData(parameters); |
| | | for (String sensorKey : keys) { |
| | | Map<String, Object> sensor = sensorMapper.getSensorBySensorKey(sensorKey); |
| | | if (sensor == null) { |
| | | continue; |
| | | } |
| | | String description = sensor.get("description").toString(); |
| | | String unit = sensor.get("unit").toString(); |
| | | Map<String, Object> hashMap = new LinkedHashMap<>(); |
| | | for (String t : times) { |
| | | hashMap.put("monitorPointName", monitorPointName); |
| | | hashMap.put("name", name); |
| | | hashMap.put("sensor", description + "(" + unit + ")"); |
| | | hashMap.put(t, ""); |
| | | } |
| | | if (data.size() != 0) { |
| | | for (Map<String, Object> dataMap : data) { |
| | | String t = dataMap.get("time").toString(); |
| | | if (dataMap.get(sensorKey) != null) { |
| | | String value = dataMap.get(sensorKey).toString(); |
| | | hashMap.put(t, value); |
| | | } |
| | | } |
| | | } |
| | | resultList.add(hashMap); |
| | | } |
| | | } |
| | | return resultList; |
| | | } |
| | | } |