| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | 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.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.moral.api.dto.MonitoringStationDTO; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryMonthlyMapper; |
| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.util.ComprehensiveIndexUtils; |
| | | import com.moral.util.DateUtils; |
| | | import com.moral.util.RegionCodeUtils; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import springfox.documentation.schema.Entry; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.text.DateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | private Map<String,Map<String,Object>> resultMap(List<HistoryDaily> list ,List<HistoryMonthly> historyMonthlyList){ |
| | | @Override |
| | | public List<MonitoringStationDTO> listMonitoringStationDTO(Map<String, Object> params, int reportType, String startTime, String endTime) { |
| | | // List<String> macs = Arrays.asList(mac.split(",")); |
| | | List<String> macs = (List<String>) params.remove("mac"); |
| | | List<MonitoringStationDTO> resultList = new ArrayList<>(); |
| | | |
| | | if(reportType == 1||reportType == 4){ |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); |
| | | List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); |
| | | resultList = resultList(historyDailyByMacAndTimeSlot); |
| | | }else if(reportType == 2){ |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); |
| | | Date endDate = DateUtils.addDays(startDate,6); |
| | | List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate); |
| | | resultList = resultList(historyDailyByMacAndTimeSlot); |
| | | }else if(reportType == 3){ |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN); |
| | | QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>(); |
| | | HistoryMonthlyWrapper.in("mac",macs); |
| | | HistoryMonthlyWrapper.between("time",startDate,endDate); |
| | | List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper); |
| | | List<HistoryDaily> historyDailies = new ArrayList<>(); |
| | | for(HistoryMonthly h : historyMonthlyList) { |
| | | HistoryDaily historyDaily = new HistoryDaily(); |
| | | historyDaily.setMac(h.getMac()); |
| | | historyDaily.setTime(h.getTime()); |
| | | historyDaily.setValue(h.getValue()); |
| | | historyDailies.add(historyDaily); |
| | | } |
| | | resultList = resultList(historyDailies); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | |
| | | private List<MonitoringStationDTO> resultList(List<HistoryDaily> historyDailyByMacAndTimeSlot){ |
| | | List<MonitoringStationDTO> list = new ArrayList<>(); |
| | | if(CollectionUtils.isEmpty(historyDailyByMacAndTimeSlot)){ |
| | | return list; |
| | | } |
| | | Map<String,Map<String,ArrayList<Double>>> resultMap = new HashMap<>(); |
| | | int zz = 0; |
| | | for(HistoryDaily h : historyDailyByMacAndTimeSlot) { |
| | | JSONObject jsonObject = JSONObject.parseObject(h.getValue()); |
| | | Map<String, Object> map = jsonObject.getInnerMap(); |
| | | Map<String,ArrayList<Double>> maps = Objects.nonNull(resultMap.get(h.getMac()))?resultMap.get(h.getMac()):new HashMap<>(); |
| | | if(Objects.nonNull(map.get("a34004"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("PM2_5"))?maps.get("PM2_5"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a34004").toString())); |
| | | maps.put("PM2_5",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a05024"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("O3"))?maps.get("O3"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a05024").toString())); |
| | | maps.put("O3",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a34002"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("PM10"))?maps.get("PM10"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a34002").toString())); |
| | | maps.put("PM10",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a21026"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("SO2"))?maps.get("SO2"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a21026").toString())); |
| | | maps.put("SO2",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a21004"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("NO2"))?maps.get("NO2"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a21004").toString())); |
| | | maps.put("NO2",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a21005"))){ |
| | | System.out.println(">>>"+map.toString()); |
| | | System.out.println(">>>"+map.get("a21005").toString()); |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("CO"))?maps.get("CO"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a21005").toString())); |
| | | maps.put("CO",numResult); |
| | | } |
| | | if(Objects.nonNull(map.get("a99054"))){ |
| | | ArrayList<Double> numResult = Objects.nonNull(maps.get("TVOC"))?maps.get("TVOC"):new ArrayList<>(); |
| | | numResult.add(Double.parseDouble(map.get("a99054").toString())); |
| | | maps.put("TVOC",numResult); |
| | | } |
| | | resultMap.put(h.getMac(),maps); |
| | | zz++; |
| | | } |
| | | for(Map.Entry entry : resultMap.entrySet()){ |
| | | String mapKey = (String) entry.getKey(); |
| | | Map<String,ArrayList<Double>> mapValue = (Map<String,ArrayList<Double>>)entry.getValue(); |
| | | Map<String, Object> data = new HashMap<>(); |
| | | if(mapValue.containsKey("PM2_5")){ |
| | | Double ListAvg = mapValue.get("PM2_5").stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("PM2_5",rsAvg); |
| | | }else { |
| | | data.put("PM2_5",0); |
| | | } |
| | | if(mapValue.containsKey("PM10")){ |
| | | Double ListAvg = mapValue.get("PM10").stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("PM10",rsAvg); |
| | | }else { |
| | | data.put("PM10",0); |
| | | } |
| | | if(mapValue.containsKey("SO2")){ |
| | | Double ListAvg = mapValue.get("SO2").stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("SO2",rsAvg); |
| | | }else { |
| | | data.put("SO2",0); |
| | | } |
| | | if(mapValue.containsKey("NO2")){ |
| | | Double ListAvg = mapValue.get("NO2").stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("NO2",rsAvg); |
| | | }else { |
| | | data.put("NO2",0); |
| | | } |
| | | if(mapValue.containsKey("CO")){ |
| | | Double ListAvg = percentile(mapValue.get("CO"),95d); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("CO",rsAvg); |
| | | }else { |
| | | data.put("CO",0); |
| | | } |
| | | if(mapValue.containsKey("O3")){ |
| | | Double ListAvg = percentile(mapValue.get("O3"),90d); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("O3",rsAvg); |
| | | }else { |
| | | data.put("O3",0); |
| | | } |
| | | if(mapValue.containsKey("TVOC")){ |
| | | Double ListAvg = mapValue.get("TVOC").stream().collect(Collectors.averagingDouble(Double::doubleValue)); |
| | | double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); |
| | | data.put("TVOC",rsAvg); |
| | | }else { |
| | | data.put("TVOC",0); |
| | | } |
| | | Double resultNum = ComprehensiveIndexUtils.dailyData(data); |
| | | MonitoringStationDTO monitoringStationDTO = new MonitoringStationDTO(); |
| | | monitoringStationDTO.setPM25(BigDecimal.valueOf(Double.parseDouble(data.get("PM2_5").toString()))); |
| | | monitoringStationDTO.setO3(BigDecimal.valueOf(Double.parseDouble(data.get("O3").toString()))); |
| | | monitoringStationDTO.setTovc(BigDecimal.valueOf(Double.parseDouble(data.get("TVOC").toString()))); |
| | | monitoringStationDTO.setComposite(BigDecimal.valueOf(resultNum)); |
| | | monitoringStationDTO.setMac(mapKey); |
| | | list.add(monitoringStationDTO); |
| | | } |
| | | List<MonitoringStationDTO> compositeList = fun3(list,1); |
| | | List<MonitoringStationDTO> pM25List = fun3(list,2); |
| | | List<MonitoringStationDTO> o3List = fun3(list,3); |
| | | List<MonitoringStationDTO> tovcList = fun3(list,4); |
| | | List<MonitoringStationDTO> resultList = new ArrayList<>(); |
| | | LambdaQueryChainWrapper<Device> wrapper = deviceService.lambdaQuery(); |
| | | wrapper.eq(Device::getIsDelete,0); |
| | | Map<String,String> deviceMap = new HashMap<>(); |
| | | wrapper.list().forEach(it->deviceMap.put(it.getMac(),it.getName())); |
| | | int num = 1; |
| | | for (MonitoringStationDTO m :compositeList ){ |
| | | MonitoringStationDTO monit = new MonitoringStationDTO(); |
| | | String mac = m.getMac(); |
| | | monit.setMac(mac); |
| | | monit.setName(deviceMap.get(mac)); |
| | | monit.setComposite(m.getComposite()); |
| | | monit.setCompositeNum(m.getCompositeNum()); |
| | | List<String> listPM25 = pM25List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); |
| | | List<String> listO3 = o3List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); |
| | | List<String> listTovc = tovcList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList()); |
| | | monit.setO3(o3List.get(listO3.indexOf(mac)).getO3()); |
| | | monit.setO3Num(o3List.get(listO3.indexOf(mac)).getO3Num()); |
| | | monit.setPM25(pM25List.get(listPM25.indexOf(mac)).getPM25()); |
| | | monit.setPM25Num(pM25List.get(listPM25.indexOf(mac)).getPM25Num()); |
| | | monit.setTovc(tovcList.get(listTovc.indexOf(mac)).getTovc()); |
| | | monit.setTOVCNum(tovcList.get(listTovc.indexOf(mac)).getTOVCNum()); |
| | | //num++; |
| | | resultList.add(monit); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | public List<MonitoringStationDTO> fun3(List<MonitoringStationDTO> monList,int type) { |
| | | List<MonitoringStationDTO> resultList = new ArrayList<>(); |
| | | List<MonitoringStationDTO> compositeList = new ArrayList<>(); |
| | | Map<BigDecimal,List<MonitoringStationDTO>> map1 = new HashMap<>(); |
| | | if(type == 1){ |
| | | compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getComposite)).collect(Collectors.toList()); |
| | | map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getComposite,LinkedHashMap::new,Collectors.toList())); |
| | | }else if(type == 2){ |
| | | compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getPM25)).collect(Collectors.toList()); |
| | | map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getPM25,LinkedHashMap::new,Collectors.toList())); |
| | | }else if(type == 3){ |
| | | compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getO3)).collect(Collectors.toList()); |
| | | map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getO3,LinkedHashMap::new,Collectors.toList())); |
| | | }else if(type == 4){ |
| | | compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getTovc)).collect(Collectors.toList()); |
| | | map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getTovc,LinkedHashMap::new,Collectors.toList())); |
| | | } |
| | | int index = 1; |
| | | for (Map.Entry entry : map1.entrySet()) { |
| | | List<MonitoringStationDTO> mapValue = (List<MonitoringStationDTO>)entry.getValue(); |
| | | for(MonitoringStationDTO m : mapValue){ |
| | | MonitoringStationDTO stationDTO = new MonitoringStationDTO(); |
| | | stationDTO.setMac(m.getMac()); |
| | | if(type == 1){ |
| | | stationDTO.setComposite(m.getComposite()); |
| | | stationDTO.setCompositeNum(index); |
| | | }else if(type == 2){ |
| | | stationDTO.setPM25(m.getPM25()); |
| | | stationDTO.setPM25Num(index); |
| | | }else if(type == 3){ |
| | | stationDTO.setO3(m.getO3()); |
| | | stationDTO.setO3Num(index); |
| | | }else if(type == 4){ |
| | | stationDTO.setTovc(m.getTovc()); |
| | | stationDTO.setTOVCNum(index); |
| | | } |
| | | resultList.add(stationDTO); |
| | | } |
| | | index = index +mapValue.size(); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | private Map<String,Map<String,Object>> resultMap(List<HistoryDaily> list , List<HistoryMonthly> historyMonthlyList){ |
| | | Map<String,Map<String,Object>> map = new HashMap<>(); |
| | | for(HistoryDaily h : list){ |
| | | String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM-dd")+"_"+h.getMac(); |
| | |
| | | return map; |
| | | } |
| | | private Double percentile(List<Double> date,double num){ |
| | | if(CollectionUtils.isNotEmpty(date)&&date.size() == 1){ |
| | | return date.get(0); |
| | | } |
| | | Collections.sort(date); |
| | | double position = (num / 100) * (date.size() - 1); |
| | | int index = (int) position; |