package com.moral.api.service.impl; import com.moral.api.entity.*; import com.moral.api.mapper.ServicesScopeDeviceMapper; import com.moral.api.mapper.ServicesScopeMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.OptionalDouble; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.Stream; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HistoryDailyMapper; import com.moral.api.service.HistoryDailyService; import com.moral.api.service.OrganizationService; import com.moral.api.utils.GetCenterPointFromListOfCoordinates; import com.moral.constant.Constants; import com.moral.util.DateUtils; import com.moral.util.PollutantUtils; /** *

* 日数据 服务实现类 *

* * @author moral * @since 2021-07-14 */ @Service public class HistoryDailyServiceImpl extends ServiceImpl implements HistoryDailyService { @Autowired private HistoryDailyMapper historyDailyMapper; @Autowired private DeviceMapper deviceMapper; @Autowired private OrganizationService organizationService; @Autowired private ServicesScopeMapper servicesScopeMapper; @Autowired private ServicesScopeDeviceMapper servicesScopeDeviceMapper; @Override public Map getMonthAvg(Map params) { params.put("start", DateUtils.getFirstDayOfCurrMonth()); params.put("end", DateUtils.getLastDayOfCurrMonth()); Map data = historyDailyMapper.getAvgByMac(params); Map result = new HashMap<>(); if (ObjectUtils.isEmpty(data)) { result.put("avg", Constants.NULL_VALUE); return result; } double aDouble = Double.parseDouble(data.get(params.get("sensorCode")).toString()); double v = new BigDecimal(aDouble).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); // result.put("avg", data.get(params.get("sensorCode"))); result.put("avg", ""+v); return result; } @Override public HistoryDaily getHistoryDailyByMacAndDate(String mac, Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("mac", mac); wrapper.eq("time", date); List historyDailies = historyDailyMapper.selectList(wrapper); if (ObjectUtils.isEmpty(historyDailies)) return null; return historyDailies.get(0); } @Override public Map getHistoryDailyByMacsAndDate(List mac, Date date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("mac", mac); wrapper.eq("time", date); List historyDailies = historyDailyMapper.selectList(wrapper); Map map = new HashMap<>(); for (HistoryDaily historyDaily : historyDailies) { map.put(historyDaily.getMac(), historyDaily); } return map; } @Override public List getHistoryDailyByMacAndTimeSlot(String mac, Date startDate, Date endDate) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("mac", mac); wrapper.between("time", startDate, endDate); List historyDailies = historyDailyMapper.selectList(wrapper); return historyDailies; } @Override public List getHistoryDailyByMacAndTimeSlot( List macs, Date startDate, Date endDate) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("mac", macs); wrapper.between("time", startDate, endDate); List historyDailies = historyDailyMapper.selectList(wrapper); return historyDailies; } @Override public Map getThermodynamicDiagramDataByOrgIdSensorCodeTime(Map parameters) { Map resultMap = new HashMap<>(); int orgId = Integer.parseInt(parameters.get("organization_id").toString()); //定义一个集合,存放所有id List allOrgId = new ArrayList<>(); allOrgId.add(orgId); //循环集合 //所有子组织 List allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1) { for (Organization organization : allChildrenOrganization) { allOrgId.add(organization.getId()); } } //集合去重 List allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList()); //声明一个list,存放设备mac List deviceMacList = new ArrayList<>(); //声明一个map,Mac作为key,device作为value Map deviceMap = new HashMap<>(); List longitudeList = new ArrayList<>(); List latitudeList = new ArrayList<>(); for (Integer orgIdWithoutDuplicates : allOrgIdWithoutDuplicates) { //根据id查询所属设备 QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.eq("is_delete", Constants.NOT_DELETE).eq("organization_id", orgIdWithoutDuplicates); List devices = new ArrayList<>(); devices = deviceMapper.selectList(wrapper_device); if (devices.size() > 0) { for (Device device : devices) { String mac = device.getMac(); deviceMacList.add(mac); deviceMap.put(mac, device); double longitude = device.getLongitude(); double latitude = device.getLatitude(); longitudeList.add(longitude); latitudeList.add(latitude); } } else { continue; } } //获取时间 String time = parameters.get("time").toString().substring(0, 10) + " 00:00:00"; resultMap.put("time", time); QueryWrapper historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List list = new ArrayList<>(); for (HistoryDaily historyDailyData : historyDailies) { List list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); double longitude = device.getLongitude(); double latitude = device.getLatitude(); JSONObject value = JSONObject.parseObject(historyDailyData.getValue()); double num = Double.parseDouble(value.get(parameters.get("sensor_code")).toString()); int level = PollutantUtils.pollutantLevel(num, (parameters.get("sensor_code")).toString()); list1.add(longitude); list1.add(latitude); list1.add(level); list.add(list1); } resultMap.put("list", list); double latitudeMin = Collections.min(latitudeList) - 0.0018; double latitudeMax = Collections.max(latitudeList) + 0.0018; double longitudeMin = Collections.min(longitudeList) - 0.2 / (111 * Math.cos(latitudeMin)); double longitudeMax = Collections.max(longitudeList) + 0.2 / (111 * Math.cos(latitudeMin)); List bound = new ArrayList<>(); bound.add(longitudeMin); bound.add(latitudeMin); bound.add(longitudeMax); bound.add(latitudeMax); resultMap.put("bound", bound); List bound1 = new ArrayList<>(); List left_up = new ArrayList<>(); left_up.add(latitudeMax); left_up.add(longitudeMin); List right_up = new ArrayList<>(); right_up.add(latitudeMax); right_up.add(longitudeMax); List left_down = new ArrayList<>(); left_down.add(latitudeMin); left_down.add(longitudeMin); List right_down = new ArrayList<>(); right_down.add(latitudeMin); right_down.add(longitudeMax); bound1.add(left_up); bound1.add(right_up); bound1.add(right_down); bound1.add(left_down); List geoCoordinates = new ArrayList<>(); for (List bo : bound1) { GeoCoordinate g = new GeoCoordinate(); g.setLatitude(Double.parseDouble(bo.get(0).toString())); g.setLongitude(Double.parseDouble(bo.get(1).toString())); geoCoordinates.add(g); } GeoCoordinate centerPoint400 = GetCenterPointFromListOfCoordinates.getCenterPoint400(geoCoordinates); List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); resultMap.put("centerPoint", centerPoint); return resultMap; } @Override public Map getThermodynamicDiagramDataByOrgIdSensorCodeTimeV2(Map parameters) { Map resultMap = new HashMap<>(); int servicesScopeId = Integer.parseInt(parameters.get("servicesScopeId").toString()); QueryWrapper servicesScopeDeviceQueryWrapper = new QueryWrapper<>(); servicesScopeDeviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); servicesScopeDeviceQueryWrapper.eq("services_scope_id",servicesScopeId); List servicesScopeDevices = servicesScopeDeviceMapper.selectList(servicesScopeDeviceQueryWrapper); List deviceIds = servicesScopeDevices.stream().map(p -> p.getDeviceId()).collect(Collectors.toList()); //声明一个list,存放设备mac List deviceMacList = new ArrayList<>(); //声明一个map,Mac作为key,device作为value Map deviceMap = new HashMap<>(); //根据id查询所属设备 QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.eq("is_delete", Constants.NOT_DELETE); wrapper_device.in("id",deviceIds); List devices = new ArrayList<>(); devices = deviceMapper.selectList(wrapper_device); if (devices.size() > 0) { for (Device device : devices) { String mac = device.getMac(); deviceMacList.add(mac); deviceMap.put(mac, device); } } //获取时间 String time = parameters.get("time").toString().substring(0, 10) + " 00:00:00"; resultMap.put("time", time); QueryWrapper historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List list = new ArrayList<>(); for (HistoryDaily historyDailyData : historyDailies) { List list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); double longitude = device.getLongitude(); double latitude = device.getLatitude(); JSONObject value = JSONObject.parseObject(historyDailyData.getValue()); double num = Double.parseDouble(value.get(parameters.get("sensor_code")).toString()); int level = PollutantUtils.pollutantLevel(num, (parameters.get("sensor_code")).toString()); list1.add(longitude); list1.add(latitude); list1.add(level); list.add(list1); } resultMap.put("list", list); ServicesScope servicesScope = servicesScopeMapper.selectById(servicesScopeId); String boundary = servicesScope.getBoundary(); String[] boundary_points = boundary.split(";"); List boundary_pointList = new ArrayList(); List bound = new ArrayList(); for (String boundary_point:boundary_points) { List boundary_point_one = new ArrayList(); String[] boundary_point_one_array = boundary_point.split(","); boundary_point_one.add(Double.parseDouble(boundary_point_one_array[0])); boundary_point_one.add(Double.parseDouble(boundary_point_one_array[1])); boundary_pointList.add(boundary_point_one); } if (boundary_pointList.size()>0){ boundary_pointList.add(boundary_pointList.get(0)); } bound.add(boundary_pointList); resultMap.put("bound", bound); List centerPoint = new ArrayList(); centerPoint.add(servicesScope.getCenterLongitude()); centerPoint.add(servicesScope.getCenterLatitude()); resultMap.put("centerPoint", centerPoint); return resultMap; } @Override public List> getThermodynamicDiagramDataByOrgIdSensorCodeTimeSlot(Map parameters) { int orgId = Integer.parseInt(parameters.get("organization_id").toString()); //定义一个集合,存放所有id List allOrgId = new ArrayList<>(); allOrgId.add(orgId); //循环集合 //所有子组织 List allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1) { for (Organization organization : allChildrenOrganization) { allOrgId.add(organization.getId()); } } //集合去重 List allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList()); //声明一个list,存放设备mac List deviceMacList = new ArrayList<>(); //声明一个map,Mac作为key,device作为value Map deviceMap = new HashMap<>(); List longitudeList = new ArrayList<>(); List latitudeList = new ArrayList<>(); for (Integer orgIdWithoutDuplicates : allOrgIdWithoutDuplicates) { //根据id查询所属设备 QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.eq("is_delete", Constants.NOT_DELETE).eq("organization_id", orgIdWithoutDuplicates); List devices = new ArrayList<>(); devices = deviceMapper.selectList(wrapper_device); if (devices.size() > 0) { for (Device device : devices) { String mac = device.getMac(); deviceMacList.add(mac); deviceMap.put(mac, device); double longitude = device.getLongitude(); double latitude = device.getLatitude(); longitudeList.add(longitude); latitudeList.add(latitude); } } else { continue; } } double latitudeMin = Collections.min(latitudeList) - 0.0018; double latitudeMax = Collections.max(latitudeList) + 0.0018; double longitudeMin = Collections.min(longitudeList) - 0.2 / (111 * Math.cos(latitudeMin)); double longitudeMax = Collections.max(longitudeList) + 0.2 / (111 * Math.cos(latitudeMin)); List bound = new ArrayList<>(); bound.add(longitudeMin); bound.add(latitudeMin); bound.add(longitudeMax); bound.add(latitudeMax); List bound1 = new ArrayList<>(); List left_up = new ArrayList<>(); left_up.add(latitudeMax); left_up.add(longitudeMin); List right_up = new ArrayList<>(); right_up.add(latitudeMax); right_up.add(longitudeMax); List left_down = new ArrayList<>(); left_down.add(latitudeMin); left_down.add(longitudeMin); List right_down = new ArrayList<>(); right_down.add(latitudeMin); right_down.add(longitudeMax); bound1.add(left_up); bound1.add(right_up); bound1.add(right_down); bound1.add(left_down); List geoCoordinates = new ArrayList<>(); for (List bo : bound1) { GeoCoordinate g = new GeoCoordinate(); g.setLatitude(Double.parseDouble(bo.get(0).toString())); g.setLongitude(Double.parseDouble(bo.get(1).toString())); geoCoordinates.add(g); } GeoCoordinate centerPoint400 = GetCenterPointFromListOfCoordinates.getCenterPoint400(geoCoordinates); List centerPoint = new ArrayList(); centerPoint.add(centerPoint400.getLongitude()); centerPoint.add(centerPoint400.getLatitude()); List> resultList = new ArrayList<>(); //获取时间 String endTime = parameters.get("endTime").toString().substring(0, 10) + " 00:00:00"; //获取时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); int days = Integer.parseInt(parameters.get("days").toString()); Date newEndTime = new Date(); for (int i = days; i >= 0; i--) { Map resultMap = new HashMap<>(); //先存放中心点和边界点 resultMap.put("centerPoint", centerPoint); resultMap.put("bound", bound); Calendar calendar = Calendar.getInstance(); try { newEndTime = df.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } calendar.setTime(newEndTime); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - i); String time = df.format(calendar.getTime()) + " 00:00:00"; //存放时间 resultMap.put("time", time); QueryWrapper historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List list = new ArrayList<>(); for (HistoryDaily historyDailyData : historyDailies) { List list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); double longitude = device.getLongitude(); double latitude = device.getLatitude(); JSONObject value = JSONObject.parseObject(historyDailyData.getValue()); double num = Double.parseDouble(value.get(parameters.get("sensor_code")).toString()); int level = PollutantUtils.pollutantLevel(num, (parameters.get("sensor_code")).toString()); list1.add(longitude); list1.add(latitude); list1.add(level); list.add(list1); } resultMap.put("list", list); resultList.add(resultMap); } return resultList; } @Override public List> getThermodynamicDiagramDataByOrgIdSensorCodeTimeSlotV2(Map parameters) { int servicesScopeId = Integer.parseInt(parameters.get("servicesScopeId").toString()); QueryWrapper servicesScopeDeviceQueryWrapper = new QueryWrapper<>(); servicesScopeDeviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); servicesScopeDeviceQueryWrapper.eq("services_scope_id",servicesScopeId); List servicesScopeDevices = servicesScopeDeviceMapper.selectList(servicesScopeDeviceQueryWrapper); List deviceIds = servicesScopeDevices.stream().map(p -> p.getDeviceId()).collect(Collectors.toList()); //声明一个list,存放设备mac List deviceMacList = new ArrayList<>(); //声明一个map,Mac作为key,device作为value Map deviceMap = new HashMap<>(); //根据id查询所属设备 QueryWrapper wrapper_device = new QueryWrapper<>(); wrapper_device.eq("is_delete", Constants.NOT_DELETE); wrapper_device.in("id",deviceIds); List devices = new ArrayList<>(); devices = deviceMapper.selectList(wrapper_device); if (devices.size() > 0) { for (Device device : devices) { String mac = device.getMac(); deviceMacList.add(mac); deviceMap.put(mac, device); } } ServicesScope servicesScope = servicesScopeMapper.selectById(servicesScopeId); String boundary = servicesScope.getBoundary(); String[] boundary_points = boundary.split(";"); List boundary_pointList = new ArrayList(); List bound = new ArrayList(); for (String boundary_point:boundary_points) { List boundary_point_one = new ArrayList(); String[] boundary_point_one_array = boundary_point.split(","); boundary_point_one.add(Double.parseDouble(boundary_point_one_array[0])); boundary_point_one.add(Double.parseDouble(boundary_point_one_array[1])); boundary_pointList.add(boundary_point_one); } if (boundary_pointList.size()>0){ boundary_pointList.add(boundary_pointList.get(0)); } bound.add(boundary_pointList); List centerPoint = new ArrayList(); centerPoint.add(servicesScope.getCenterLongitude()); centerPoint.add(servicesScope.getCenterLatitude()); List> resultList = new ArrayList<>(); //获取时间 String endTime = parameters.get("endTime").toString().substring(0, 10) + " 00:00:00"; //获取时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); int days = Integer.parseInt(parameters.get("days").toString()); Date newEndTime = new Date(); for (int i = days; i >= 0; i--) { Map resultMap = new HashMap<>(); //先存放中心点和边界点 resultMap.put("centerPoint", centerPoint); resultMap.put("bound", bound); Calendar calendar = Calendar.getInstance(); try { newEndTime = df.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } calendar.setTime(newEndTime); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - i); String time = df.format(calendar.getTime()) + " 00:00:00"; //存放时间 resultMap.put("time", time); QueryWrapper historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.eq("time", time); historyDailyQueryWrapper.in("mac", deviceMacList); List historyDailies = historyDailyMapper.selectList(historyDailyQueryWrapper); List list = new ArrayList<>(); for (HistoryDaily historyDailyData : historyDailies) { List list1 = new ArrayList<>(); String mac = historyDailyData.getMac(); Device device = deviceMap.get(mac); double longitude = device.getLongitude(); double latitude = device.getLatitude(); JSONObject value = JSONObject.parseObject(historyDailyData.getValue()); double num = Double.parseDouble(value.get(parameters.get("sensor_code")).toString()); int level = PollutantUtils.pollutantLevel(num, (parameters.get("sensor_code")).toString()); list1.add(longitude); list1.add(latitude); list1.add(level); list.add(list1); } resultMap.put("list", list); resultList.add(resultMap); } return resultList; } @Override public List getValueByMacs(List macs, String time) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("time", "value") .likeRight("time", time) .in("mac", macs); return historyDailyMapper.selectList(queryWrapper); } @Override public Double calculatedValue(List list, String sensorCode, String type) { Supplier> supplier = list::stream; DoubleStream doubleStream = supplier.get() .flatMapToDouble(v -> { Map dataValue = JSONObject.parseObject(v.getValue(), Map.class); Object sensorValue = dataValue.get(sensorCode); if (ObjectUtils.isEmpty(sensorValue)) { return null; } double aDouble = Double.parseDouble(sensorValue.toString()); return DoubleStream.of(aDouble); }); Double result = null; OptionalDouble optionalDouble = null; if ("sum".equals(type)) { result = doubleStream.sum(); } else { if ("min".equals(type)) { optionalDouble = doubleStream.min(); } else if ("max".equals(type)) { optionalDouble = doubleStream.max(); } else if ("avg".equals(type)) { optionalDouble = doubleStream.average(); } if (optionalDouble.isPresent()) { result = optionalDouble.getAsDouble(); } } return result; } }