| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryMonthlyMapper; |
| | | import com.moral.api.mapper.OrganizationMapper; |
| | | |
| | | import com.moral.api.pojo.dto.dataDisplay.MonitorPointDataDisplayDTO; |
| | | import com.moral.api.pojo.dto.dataDisplay.SensorComparisonDisplayDTO; |
| | | import com.moral.api.pojo.form.dataDisplay.MonitorPointDataDisplayForm; |
| | | import com.moral.api.pojo.form.dataDisplay.SensorComparisonDisplayForm; |
| | | |
| | | import com.moral.api.service.*; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.SeparateTableType; |
| | | import com.moral.pojo.AQI; |
| | | import com.moral.util.*; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | |
| | | /** |
| | | * @ClassName DataDisplayServiceImpl |
| | |
| | | * @Version TODO |
| | | **/ |
| | | @Service |
| | | @Slf4j |
| | | public class DataDisplayServiceImpl implements DataDisplayService { |
| | | |
| | | @Autowired |
| | |
| | | HistoryWeeklyService historyWeeklyService; |
| | | @Autowired |
| | | HistoryMonthlyService historyMonthlyService; |
| | | @Autowired |
| | | OrganizationMapper organizationMapper; |
| | | |
| | | @Autowired |
| | | DeviceMapper deviceMapper; |
| | | |
| | | @Override |
| | | public List<MonitorPointDataDisplayDTO> getMonitorPointDisplayData(MonitorPointDataDisplayForm form) { |
| | | //取参 |
| | | Integer monitorPointId = form.getMonitorPointId(); |
| | | // Integer monitorPointId = form.getMacs(); |
| | | List<String> macs = form.getMacs(); |
| | | String reportType = form.getReportType(); |
| | | Date startTime = form.getStartTime(); |
| | | Date endTime = form.getEndTime(); |
| | | //根据站点id获取站点下所有的设备 |
| | | List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPointId); |
| | | // List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPointId); |
| | | QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); |
| | | deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | deviceQueryWrapper.in("mac", macs); |
| | | List<Device> devices = deviceMapper.selectList(deviceQueryWrapper); |
| | | Map<String, Device> deviceMap = new HashMap<>(); |
| | | List<String> macs = new ArrayList<>(); |
| | | // List<String> ListMacs = new ArrayList<>(); |
| | | devices.forEach(value -> { |
| | | macs.add(value.getMac()); |
| | | // ListMacs.add(value.getMac()); |
| | | deviceMap.put(value.getMac(), value); |
| | | }); |
| | | List<MonitorPointDataDisplayDTO> dtos = new ArrayList<>(); |
| | |
| | | } |
| | | //处理小时报表请求,默认取上一个小时的值 |
| | | else if (reportType.equals(Constants.HOURLY_REPORT)) { |
| | | Map<String, HistoryHourly> macDataMap = new HashMap<>(); |
| | | Map<String, List<HistoryHourly>> macDataMap = new HashMap<>(); |
| | | //获取数据 |
| | | macs.forEach(value -> { |
| | | List<HistoryHourly> datas = historyHourlyService.getValueByMacAndTime(value, startTime, startTime); |
| | | List<HistoryHourly> datas = historyHourlyService.getValueByMacAndTime(value, startTime, endTime); |
| | | if (datas.size() != 0) |
| | | macDataMap.put(value, datas.get(0)); |
| | | macDataMap.put(value,datas); |
| | | }); |
| | | if (macDataMap.size() != 0) |
| | | dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime); |
| | | } |
| | | //处理日报请求,默认获取昨天的值 |
| | | else if (reportType.equals(Constants.DAILY_REPORT)) { |
| | | Map<String, List<HistoryDaily>> macDataMap = new HashMap<>(); |
| | | //获取数据 |
| | | Map<String, HistoryDaily> macDataMap = historyDailyService.getHistoryDailyByMacsAndDate(macs, startTime); |
| | | macs.forEach(value -> { |
| | | List<HistoryDaily> datas = historyDailyService.getHistoryDailyByMacAndTimeSlot(value, startTime, endTime); |
| | | if (datas.size() != 0) |
| | | macDataMap.put(value,datas); |
| | | }); |
| | | |
| | | if (macDataMap.size() != 0) |
| | | dtos = calculateReportData(macDataMap, deviceMap, reportType, startTime); |
| | | } |
| | |
| | | else if (reportType.equals(Constants.MONTHLY_REPORT)) { |
| | | //月报需要获取一个月每天的数据用来算综合指数,其他数据从月表中取出 |
| | | Map<String, List<HistoryDaily>> macDataDailyMap = new HashMap<>(); |
| | | Map<String, HistoryMonthly> macDataMonthlyMap = historyMonthlyService.getHistoryMonthlyByMacsAndDate(macs, startTime); |
| | | Map<String, List<HistoryMonthly>> macDataMonthlyMap = new HashMap<>(); |
| | | macs.forEach(mac -> { |
| | | QueryWrapper<HistoryMonthly> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("mac",mac); |
| | | wrapper.between("time",startTime,endTime); |
| | | List<HistoryMonthly> monthlyList = historyMonthlyMapper.selectList(wrapper); |
| | | if (!ObjectUtils.isEmpty(monthlyList)) |
| | | macDataMonthlyMap.put(mac, monthlyList); |
| | | }); |
| | | macs.forEach(mac -> { |
| | | List<HistoryDaily> dailyDatas = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, startTime, endTime); |
| | | if (!ObjectUtils.isEmpty(dailyDatas)) |
| | |
| | | for (String sensor : sensors) { |
| | | SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO(); |
| | | List<Map<String, Object>> dtoTimeValueList = new ArrayList<>(); |
| | | if(sensor.equals("a00e12") || sensor.equals("a00e03")){ |
| | | dto.setCode("1"); |
| | | }else if (sensor.equals("a01006")){ |
| | | dto.setCode("2"); |
| | | }else if (sensor.equals("a99054") || sensor.equals("a21005")){ |
| | | dto.setCode("3"); |
| | | }else { |
| | | dto.setCode("0"); |
| | | } |
| | | dto.setSensorCode(sensor); |
| | | timeValueMap.forEach((time, valueJson) -> { |
| | | Map<String, Object> listMap = new HashMap<>(); |
| | |
| | | dtos.add(dto); |
| | | } |
| | | return dtos; |
| | | } |
| | | |
| | | /** |
| | | * 热力图 |
| | | * @param code |
| | | * @param startTime |
| | | * @param type |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ArrayList<Map<String, Object>> getHeatMapData(String code, String startTime, String type,String form) { |
| | | HashMap<String, Object> map = new HashMap<>(); |
| | | map.put("start",startTime); |
| | | map.put("type","$."+ type); |
| | | |
| | | //获取用户信息 |
| | | Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo(); |
| | | Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization"); |
| | | Integer organizationId = (Integer) orgInfo.get("id"); |
| | | //大账号 |
| | | ArrayList<Integer> list = new ArrayList<>(); |
| | | if (organizationId==24){ |
| | | LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(Organization::getIsDelete,Constants.NOT_DELETE); |
| | | wrapper.eq(Organization::getCityCode,code); |
| | | List<Organization> organizations = organizationMapper.selectList(wrapper); |
| | | List<Integer> collect = organizations.stream().map(organization -> organization.getId()).collect(Collectors.toList()); |
| | | list.addAll(collect); |
| | | }else { |
| | | list.add(organizationId); |
| | | } |
| | | |
| | | ArrayList<Map<String, Object>> rsHeatMap = new ArrayList<>(); |
| | | // ArrayList<HeatMapDTO> rsHeatMap = new ArrayList<>(); |
| | | |
| | | if (form.equals("hour")){ //小时 |
| | | Date date1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN); |
| | | List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date1, date1, SeparateTableType.MONTH); |
| | | // for (Integer integer : list) { |
| | | map.put("organizationIds",list); |
| | | map.put("tableName",tableNames.get(0)); |
| | | List<Map<String, Object>> heatMap = deviceMapper.getHeatMap(map); |
| | | // List<HeatMapDTO> heatMap = deviceMapper.getHeatMap(map); |
| | | rsHeatMap.addAll(heatMap); |
| | | // } |
| | | |
| | | }else { //天 |
| | | // for (Integer integer : list) { |
| | | map.put("organizationIds",list); |
| | | List<Map<String, Object>> heatMap = deviceMapper.getHeatMap(map); |
| | | // List<HeatMapDTO> heatMap = deviceMapper.getHeatMap(map); |
| | | rsHeatMap.addAll(heatMap); |
| | | // } |
| | | } |
| | | |
| | | //除去为空数据 |
| | | ArrayList<Map<String, Object>> filteredList = new ArrayList<>(); |
| | | for (Map<String, Object> map1 : rsHeatMap) { |
| | | boolean hasEmptyValue = false; |
| | | for (Object value : map1.values()) { |
| | | if (value == null) { |
| | | hasEmptyValue = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!hasEmptyValue) { |
| | | filteredList.add(map1); |
| | | } |
| | | } |
| | | log.info(filteredList.size()+""); |
| | | return filteredList; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | private <T> List<MonitorPointDataDisplayDTO> calculateReportData(Map<String, T> macDataMap, Map<String, Device> deviceMap, String reportType, Date... date) { |
| | | List<MonitorPointDataDisplayDTO> dtos = new ArrayList<>(); |
| | | macDataMap.forEach((key, valueObject) -> { |
| | | MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | dto.setMac(key); |
| | | //数据的map |
| | | Map<String, Object> valueMap; |
| | | //获取对象的value属性值 |
| | | Object valueO = ClassUtils.getPropertyValue(valueObject, "value"); |
| | | if (valueO == null) |
| | | return; |
| | | String value = (String) valueO; |
| | | valueMap = JSON.parseObject(value, Map.class); |
| | | Set<String> strings = macDataMap.keySet(); |
| | | for (String key : strings) { |
| | | if (reportType.equals("0")){ |
| | | List<HistoryHourly> t = (List<HistoryHourly>)macDataMap.get(key); |
| | | for (HistoryHourly historyHourly : t) { |
| | | MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | String value = historyHourly.getValue(); |
| | | Map map = JSON.parseObject(value, Map.class); |
| | | String time = DateUtils.dateToDateString(historyHourly.getTime(), "yyyy-MM-dd HH:00:00"); |
| | | injectDataToDto(map, false, dto, reportType); |
| | | dto.setTime(time); |
| | | dto.setMac(key); |
| | | dto.setDeviceName(deviceMap.get(key).getName()); |
| | | dtos.add(dto); |
| | | } |
| | | } |
| | | if (reportType.equals("1")){ |
| | | List<HistoryDaily> t = (List<HistoryDaily>)macDataMap.get(key); |
| | | for (HistoryDaily historyDaily : t) { |
| | | MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | String value = historyDaily.getValue(); |
| | | Map map = JSON.parseObject(value, Map.class); |
| | | String time = DateUtils.dateToDateString(historyDaily.getTime(), "yyyy-MM-dd"); |
| | | injectDataToDto(map, false, dto, reportType); |
| | | dto.setTime(time); |
| | | dto.setMac(key); |
| | | dto.setDeviceName(deviceMap.get(key).getName()); |
| | | dtos.add(dto); |
| | | } |
| | | |
| | | } |
| | | if (reportType.equals("2")){ |
| | | HistoryWeekly historyWeekly = (HistoryWeekly)macDataMap.get(key); |
| | | MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | String value = historyWeekly.getValue(); |
| | | Map map = JSON.parseObject(value, Map.class); |
| | | String time = DateUtils.dateToDateString(historyWeekly.getTime(), "yyyy-MM-dd"); |
| | | injectDataToDto(map, false, dto, reportType); |
| | | dto.setTime(time); |
| | | dto.setMac(key); |
| | | dto.setDeviceName(deviceMap.get(key).getName()); |
| | | dtos.add(dto); |
| | | |
| | | } |
| | | if (reportType.equals("3")){ |
| | | List<HistoryMonthly> t = (List<HistoryMonthly>)macDataMap.get(key); |
| | | for (HistoryMonthly historyMonthly : t) { |
| | | MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | String value = historyMonthly.getValue(); |
| | | Map map = JSON.parseObject(value, Map.class); |
| | | String time = DateUtils.dateToDateString(historyMonthly.getTime(), "yyyy-MM"); |
| | | injectDataToDto(map, false, dto, reportType); |
| | | dto.setTime(time); |
| | | dto.setMac(key); |
| | | dto.setDeviceName(deviceMap.get(key).getName()); |
| | | dtos.add(dto); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // macDataMap.forEach((key, valueObject) -> { |
| | | // MonitorPointDataDisplayDTO dto = new MonitorPointDataDisplayDTO(); |
| | | // dto.setMac(key); |
| | | // //数据的map |
| | | // Map<String, Object> valueMap; |
| | | // List<Map<String, Object>> list; |
| | | // //获取对象的value属性值 |
| | | // Object valueO = ClassUtils.getPropertyValue(valueObject, "value"); |
| | | // if (valueO == null) |
| | | // return; |
| | | // String value = (String) valueO; |
| | | // list = JSON.parseObject(value, List.class); |
| | | // for (Map<String, Object> map : list) { |
| | | // String o = map.get("value").toString(); |
| | | // valueMap = JSON.parseObject(o, Map.class); |
| | | // injectDataToDto(valueMap, false, dto, reportType); |
| | | // String deviceName = deviceMap.get(key).getName(); |
| | | // dto.setDeviceName(deviceName); |
| | | // String startTime = DateUtils.dateToDateString((Date) map.get("time"), "yyyy-MM-dd HH:mm:ss"); |
| | | // dto.setTime(startTime); |
| | | // dtos.add(dto); |
| | | // } |
| | | //获取设备名称 |
| | | String deviceName = deviceMap.get(key).getName(); |
| | | dto.setDeviceName(deviceName); |
| | | |
| | | //拼接时间,时报的时间需要精确到小时,日报精确到当天,周报需要含有开始和结束时间 |
| | | String startTime = ""; |
| | | if (valueObject instanceof HistoryHourly && date.length == 1) |
| | | startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd HH:mm:ss"); |
| | | else if (valueObject instanceof HistoryDaily && date.length == 1) |
| | | startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd"); |
| | | else if (valueObject instanceof HistoryWeekly && date.length == 2) |
| | | startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd") + " -- " + DateUtils.dateToDateString(date[1], "yyyy-MM-dd"); |
| | | else if (valueObject instanceof HistoryMonthly && date.length == 1) |
| | | startTime = DateUtils.dateToDateString(date[0], "yyyy-MM"); |
| | | dto.setTime(startTime); |
| | | // String startTime = ""; |
| | | // if (valueObject instanceof HistoryHourly && date.length == 1) |
| | | // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd HH:mm:ss"); |
| | | // else if (valueObject instanceof HistoryDaily && date.length == 1) |
| | | // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd"); |
| | | // else if (valueObject instanceof HistoryWeekly && date.length == 2) |
| | | // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM-dd") + " -- " + DateUtils.dateToDateString(date[1], "yyyy-MM-dd"); |
| | | // else if (valueObject instanceof HistoryMonthly && date.length == 1) |
| | | // startTime = DateUtils.dateToDateString(date[0], "yyyy-MM"); |
| | | // dto.setTime(startTime); |
| | | //注入数据 |
| | | injectDataToDto(valueMap, false, dto, reportType); |
| | | dtos.add(dto); |
| | | }); |
| | | // }); |
| | | return dtos; |
| | | } |
| | | |