| | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.google.common.reflect.TypeToken; |
| | | import com.google.gson.Gson; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryMonthlyMapper; |
| | |
| | | import com.moral.util.RegionCodeUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.text.DateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | } else if("day".equals(type)){ |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); |
| | | Map<String,Map<String,Object>> map = resultMap(historyDailyByMacAndTimeSlot); |
| | | for (Device device:devices) { |
| | | List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(device.getMac(), startDate, endDate); |
| | | for (String sensor:sensorsList) { |
| | | Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json |
| | | QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>(); |
| | | monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | monitorPointQueryWrapper.eq("id",device.getMonitorPointId()); |
| | | MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper); |
| | | for (String sensor:sensorsList) { |
| | | Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd"); |
| | | Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1); |
| | | Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json |
| | | timeValueMap.put("监测站点",monitorPoint.getName()); |
| | | timeValueMap.put("name",device.getName()); |
| | | QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); |
| | |
| | | Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper); |
| | | timeValueMap.put("sensor",sensorEntity.getName()); |
| | | ArrayList<Double> doubleArrayList = new ArrayList<>(); |
| | | for (HistoryDaily historyDaily : historyDailyByMacAndTimeSlot) { |
| | | while (DateUtils.isTimeBefor(js,ks)){ |
| | | String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd"); |
| | | String k = c+"_"+device.getMac(); |
| | | if(map.containsKey(k)){ |
| | | Object o = map.get(k).get(sensor); |
| | | timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0); |
| | | }else { |
| | | timeValueMap.put(c,0); |
| | | } |
| | | ks = DateUtils.getDateOfDay(ks,1); |
| | | } |
| | | |
| | | /*for (HistoryDaily historyDaily : historyDailyByMacAndTimeSlot) { |
| | | Date time = historyDaily.getTime(); |
| | | String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd"); |
| | | String value = historyDaily.getValue(); |
| | | JSONObject jsonObject = JSONObject.parseObject(value); |
| | | doubleArrayList.add(Double.parseDouble(jsonObject.get(sensor).toString())); |
| | | doubleArrayList.add(Objects.nonNull(jsonObject)&&Objects.nonNull(jsonObject.get(sensor))?Double.parseDouble(jsonObject.get(sensor).toString()):0); |
| | | if (jsonObject.containsKey(sensor)){ |
| | | timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); |
| | | } |
| | | } |
| | | }*/ |
| | | Double ListAvg = doubleArrayList.stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | timeValueMap.put("累计值",rsAvg); |
| | |
| | | return resultList; |
| | | } |
| | | |
| | | |
| | | private Map<String,Map<String,Object>> resultMap(List<HistoryDaily> list ){ |
| | | Map<String,Map<String,Object>> map = new HashMap<>(); |
| | | for(HistoryDaily h : list){ |
| | | String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM-dd")+"_"+h.getMac(); |
| | | Map<String,Object> jsonMap = new HashMap<>(); |
| | | JSONObject jsonObject = JSONObject.parseObject(h.getValue()); |
| | | jsonMap = jsonObject.getInnerMap(); |
| | | map.put(dateStr,jsonMap); |
| | | } |
| | | return map; |
| | | } |
| | | } |
| | | |
| | | |