| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.config.mybatis.MybatisPlusConfig; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.HistoryFiveMinutely; |
| | | import com.moral.api.entity.HistoryHourly; |
| | | import com.moral.api.entity.Organization; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.mapper.HistoryHourlyMapper; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.api.service.OrganizationService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.SeparateTableType; |
| | | import com.moral.pojo.AQI; |
| | |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import com.moral.util.MybatisPLUSUtils; |
| | | import com.moral.util.PollutantUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | |
| | | @Autowired |
| | | private HistoryHourlyMapper historyHourlyMapper; |
| | | |
| | | @Autowired |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | @Autowired |
| | | private OrganizationService organizationService; |
| | | |
| | | @Override |
| | | public Map<String, Object> getHourlyAqiByMac(String mac) { |
| | |
| | | } |
| | | |
| | | /** |
| | | *@Description: 通过组织id和因子编码获取上一个小时数据 |
| | | *@Param: [map] |
| | | *@return: java.util.Map<java.lang.String,java.lang.Object> |
| | | *@Author: lizijie |
| | | *@Date: 2021/12/7 10:07 |
| | | **/ |
| | | @Override |
| | | public Map<String, Object> getLastHourDataByOrgIdAndSensorCode(Map parameters) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | int orgId = Integer.parseInt(parameters.get("organization_id").toString()); |
| | | //定义一个集合,存放所有id |
| | | List<Integer> allOrgId = new ArrayList<>(); |
| | | allOrgId.add(orgId); |
| | | //循环集合 |
| | | //所有子组织 |
| | | List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); |
| | | if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){ |
| | | for (Organization organization:allChildrenOrganization) { |
| | | allOrgId.add(organization.getId()); |
| | | } |
| | | } |
| | | //集合去重 |
| | | List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList()); |
| | | //声明一个list,存放设备mac |
| | | List<String> deviceMacList = new ArrayList<>(); |
| | | //声明一个map,Mac作为key,device作为value |
| | | Map<String,Device> deviceMap = new HashMap<>(); |
| | | for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) { |
| | | //根据id查询所属设备 |
| | | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); |
| | | wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates); |
| | | List<Device> 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); |
| | | } |
| | | }else { |
| | | continue; |
| | | } |
| | | } |
| | | //获取时间 |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH"); |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-1); |
| | | String time = df.format(calendar.getTime())+":00:00"; |
| | | resultMap.put("time",time); |
| | | String timeUnits = DateUtils.dateToDateString(calendar.getTime(), DateUtils.yyyyMM_EN); |
| | | List<Map<String,Object>> historyHourlyDatas = new ArrayList<>(); |
| | | historyHourlyDatas = historyHourlyMapper.selectDataByMacsAndTime(timeUnits, deviceMacList, time); |
| | | List<Object> list = new ArrayList<>(); |
| | | List<Double> longitudeList = new ArrayList<>(); |
| | | List<Double> latitudeList = new ArrayList<>(); |
| | | for (Map historyHourlyData:historyHourlyDatas) { |
| | | List<Object> list1 = new ArrayList<>(); |
| | | String mac = historyHourlyData.get("mac").toString(); |
| | | Device device = deviceMap.get(mac); |
| | | double longitude = device.getLongitude(); |
| | | double latitude = device.getLatitude(); |
| | | longitudeList.add(longitude); |
| | | latitudeList.add(latitude); |
| | | JSONObject value = JSONObject.parseObject(historyHourlyData.get("value").toString()); |
| | | double num = Double.parseDouble(value.get(parameters.get("sensor_code")).toString()); |
| | | int level = PollutantUtils.pollutantLevel(num, (parameters.get("sensor_code")).toString()); |
| | | list1.add(latitude); |
| | | list1.add(longitude); |
| | | 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<Object> bound = new ArrayList<>(); |
| | | List<Object> left_up = new ArrayList<>(); |
| | | left_up.add(latitudeMax); |
| | | left_up.add(longitudeMin); |
| | | List<Object> right_up = new ArrayList<>(); |
| | | right_up.add(latitudeMax); |
| | | right_up.add(longitudeMax); |
| | | List<Object> left_down = new ArrayList<>(); |
| | | left_down.add(latitudeMin); |
| | | left_down.add(longitudeMin); |
| | | List<Object> right_down = new ArrayList<>(); |
| | | right_down.add(latitudeMin); |
| | | right_down.add(longitudeMax); |
| | | bound.add(left_up); |
| | | bound.add(right_up); |
| | | bound.add(right_down); |
| | | bound.add(left_down); |
| | | resultMap.put("bound",bound); |
| | | return resultMap; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 查询一段时间内某一mac的数据 |
| | | * @Param: [mac, startDate, endDate] |
| | | * @return: java.util.List<com.moral.api.entity.HistoryHourly> |