| | |
| | | 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.moral.api.entity.*; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryMonthlyMapper; |
| | | 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.HistoryDailyService; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.HistoryMonthlyService; |
| | | import com.moral.api.service.MonitorPointService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.api.utils.MyLatLng; |
| | | import com.moral.api.utils.mapUtils; |
| | | 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.CollectionUtils; |
| | | import org.springframework.util.unit.DataUnit; |
| | | |
| | | import java.io.BufferedWriter; |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Autowired |
| | | HistoryHourlyService historyHourlyService; |
| | | |
| | | @Autowired |
| | | HistoryDailyService historyDailyService; |
| | | |
| | | @Autowired |
| | | HistoryMonthlyService historyMonthlyService; |
| | | @Autowired |
| | | HistoryMonthlyMapper historyMonthlyMapper; |
| | | |
| | | @Override |
| | | public List<MonitorPoint> queryByOrgIdAndRegionCode(MonitorPointQueryForm form) { |
| | | //取参 |
| | | Integer organizationId = form.getOrganizationId(); |
| | | Integer regionCode = form.getRegionCode(); |
| | | String region = null; |
| | | if (regionCode != null) { |
| | | if (regionCode != null && organizationId!=24) { |
| | | region = RegionCodeUtils.regionCodeConvertToName(regionCode); |
| | | } |
| | | // if (regionCode != null) { |
| | | // region = RegionCodeUtils.regionCodeConvertToName(regionCode); |
| | | // } |
| | | |
| | | //查询子组织 |
| | | List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId); |
| | |
| | | @Override |
| | | public List<Map<String, Object>> getHourlyDataDataV3(Map<String, Object> params) { |
| | | List<String> macs = (List<String>) params.remove("macs"); |
| | | List<String> times = (List<String>) params.remove("times"); |
| | | String type = params.get("type").toString(); |
| | | String startTime = times.get(0); |
| | | String endTime = times.get(1); |
| | | |
| | | // String[] macs = params.remove("macs").toString().split(","); |
| | | // List<String> macsList = Arrays.asList(macs); |
| | | QueryWrapper<Device> queryWrapper = new QueryWrapper<>(); |
| | |
| | | } |
| | | String[] sensors = params.remove("sensors").toString().split(","); |
| | | List<String> sensorsList = Arrays.asList(sensors); |
| | | List<Map<String,Object>> resultList = new ArrayList<>(); |
| | | |
| | | // List<String> sensorsList = (List<String>) params.remove("sensors"); |
| | | String startTime = params.get("startTime").toString(); |
| | | String endTime = params.get("endTime").toString(); |
| | | // String startTime = params.get("startTime").toString(); |
| | | // String endTime = params.get("endTime").toString(); |
| | | if (type.equals("hours")){ |
| | | 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)); |
| | | List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, 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<>(); |
| | |
| | | resultList.add(timeValueMap); |
| | | } |
| | | } |
| | | } 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); |
| | | 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); |
| | | 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()); |
| | | ArrayList<Double> doubleArrayList = new ArrayList<>(); |
| | | 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())); |
| | | 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); |
| | | resultList.add(timeValueMap); |
| | | } |
| | | } |
| | | }else { |
| | | for (Device device:devices) { |
| | | QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>(); |
| | | HistoryMonthlyWrapper.eq("mac",device.getMac()); |
| | | HistoryMonthlyWrapper.between("time",startTime,endTime); |
| | | List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper); |
| | | 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 (HistoryMonthly historyMonthly : historyMonthlyList) { |
| | | Date time = historyMonthly.getTime(); |
| | | String dateStr = DateUtils.dateToDateString(time, "yyyy-MM"); |
| | | String value = historyMonthly.getValue(); |
| | | JSONObject jsonObject = JSONObject.parseObject(value); |
| | | if (jsonObject.containsKey(sensor)){ |
| | | timeValueMap.put(dateStr, jsonObject.get(sensor).toString()); |
| | | } |
| | | } |
| | | resultList.add(timeValueMap); |
| | | } |
| | | } |
| | | } |
| | | return resultList; |
| | | } |
| | | |