| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.MonitorPoint; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.entity.*; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.MonitorPointMapper; |
| | | import com.moral.api.mapper.SensorMapper; |
| | | import com.moral.api.pojo.form.device.MonitorPointQueryForm; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.MonitorPointService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.util.DateUtils; |
| | | import com.moral.util.RegionCodeUtils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.unit.DataUnit; |
| | | |
| | | import java.io.BufferedWriter; |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | OrganizationService organizationService; |
| | | @Autowired |
| | | DeviceService deviceService; |
| | | |
| | | @Autowired |
| | | DeviceMapper deviceMapper; |
| | | |
| | | @Autowired |
| | | SensorMapper sensorMapper; |
| | | |
| | | @Autowired |
| | | HistoryHourlyService historyHourlyService; |
| | | |
| | | @Override |
| | | public List<MonitorPoint> queryByOrgIdAndRegionCode(MonitorPointQueryForm form) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<MonitorPoint> queryStateControlStationByRegionCode(Integer regionCode) { |
| | | //获取国控站组织 |
| | | Organization stateControlStationOrganization = organizationService.getStateControlStation(); |
| | | //获取国控站组织下的所有站点 |
| | | QueryWrapper<MonitorPoint> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | queryWrapper.eq("organization_id", stateControlStationOrganization.getId()); |
| | | queryWrapper.eq(RegionCodeUtils.regionCodeConvertToName(regionCode), regionCode); |
| | | return monitorPointMapper.selectList(queryWrapper); |
| | | public List<MonitorPoint> queryAllMonitorPoints(Integer organizationId) { |
| | | //查询子组织 |
| | | List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId); |
| | | List<Integer> organizationIds = new ArrayList<>(); |
| | | for (Organization organization : childrenOrganization) { |
| | | organizationIds.add(organization.getId()); |
| | | } |
| | | organizationIds.add(organizationId); |
| | | //查询站点 |
| | | QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>(); |
| | | queryMonitorPointsWrapper.select("id","name"); |
| | | queryMonitorPointsWrapper.in("organization_id",organizationIds); |
| | | queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper); |
| | | return monitorPoints; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getHourlyDataByMonitorPoint(Map map) { |
| | | int monitorPointId = Integer.parseInt(map.get("monitorPointId").toString()); |
| | | QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>(); |
| | | monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | monitorPointQueryWrapper.eq("id",monitorPointId); |
| | | MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper); |
| | | QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); |
| | | deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | deviceQueryWrapper.eq("monitor_point_id",monitorPointId); |
| | | List<Device> devices = deviceMapper.selectList(deviceQueryWrapper); |
| | | if (devices.size()<=0){ |
| | | return null; |
| | | } |
| | | String[] sensors = map.remove("sensors").toString().split(","); |
| | | List<String> sensorsList = Arrays.asList(sensors); |
| | | String startTime = map.get("startTime").toString(); |
| | | String endTime = map.get("endTime").toString(); |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | List<Map<String,Object>> resultList = new ArrayList<>(); |
| | | for (Device device:devices) { |
| | | List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(map.get("startTime").toString(), DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(map.get("endTime").toString(), DateUtils.yyyy_MM_dd_HH_EN)); |
| | | for (String sensor:sensorsList) { |
| | | Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json |
| | | timeValueMap.put("监测站点",monitorPoint.getName()); |
| | | timeValueMap.put("name",device.getName()); |
| | | QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); |
| | | sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | sensorQueryWrapper.eq("code",sensor); |
| | | Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper); |
| | | timeValueMap.put("sensor",sensorEntity.getName()); |
| | | for (HistoryHourly historyHourly : hourlies) { |
| | | Date time = historyHourly.getTime(); |
| | | String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); |
| | | String value = historyHourly.getValue(); |
| | | JSONObject jsonObject = JSONObject.parseObject(value); |
| | | if (jsonObject.containsKey(sensor)){ |
| | | timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); |
| | | } |
| | | } |
| | | //补上无数据时间 |
| | | Date middleDate = startDate; |
| | | while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){ |
| | | if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null) |
| | | timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null); |
| | | middleDate = DateUtils.addHours(middleDate,1); |
| | | } |
| | | resultList.add(timeValueMap); |
| | | } |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | /** |
| | | * 监测站点数据导出 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<Map<String, Object>> getHourlyDataDataV3(Map<String, Object> params) { |
| | | List<String> macs = (List<String>) params.remove("macs"); |
| | | // String[] macs = params.remove("macs").toString().split(","); |
| | | // List<String> macsList = Arrays.asList(macs); |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | queryWrapper.in("mac", macs); |
| | | List<Device> devices = deviceMapper.selectList(queryWrapper); |
| | | if (devices.size()<=0){ |
| | | return null; |
| | | } |
| | | String[] sensors = params.remove("sensors").toString().split(","); |
| | | List<String> sensorsList = Arrays.asList(sensors); |
| | | // List<String> sensorsList = (List<String>) params.remove("sensors"); |
| | | String startTime = params.get("startTime").toString(); |
| | | String endTime = params.get("endTime").toString(); |
| | | Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN); |
| | | List<Map<String,Object>> resultList = new ArrayList<>(); |
| | | for (Device device:devices) { |
| | | List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(params.get("startTime").toString(), DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(params.get("endTime").toString(), DateUtils.yyyy_MM_dd_HH_EN)); |
| | | 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); |
| | | timeValueMap.put("监测站点",monitorPoint.getName()); |
| | | timeValueMap.put("name",device.getName()); |
| | | QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); |
| | | sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE); |
| | | sensorQueryWrapper.eq("code",sensor); |
| | | Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper); |
| | | timeValueMap.put("sensor",sensorEntity.getName()); |
| | | for (HistoryHourly historyHourly : hourlies) { |
| | | Date time = historyHourly.getTime(); |
| | | String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH"); |
| | | String value = historyHourly.getValue(); |
| | | JSONObject jsonObject = JSONObject.parseObject(value); |
| | | if (jsonObject.containsKey(sensor)){ |
| | | timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); |
| | | } |
| | | } |
| | | //补上无数据时间 |
| | | Date middleDate = startDate; |
| | | while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){ |
| | | if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null) |
| | | timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null); |
| | | middleDate = DateUtils.addHours(middleDate,1); |
| | | } |
| | | resultList.add(timeValueMap); |
| | | } |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | } |