| | |
| | | package com.moral.service.impl; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import com.moral.common.exception.BusinessException; |
| | | import com.moral.common.util.ReportTimeFormat; |
| | | import com.moral.entity.Organization; |
| | | import com.moral.entity.charts.TimePeriod; |
| | | import com.moral.entity.charts.TimeUnits; |
| | | import com.moral.mapper.HangzhouAqiMapper; |
| | | import com.moral.mapper.OrganizationMapper; |
| | | import com.moral.service.HangzhouAqiService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import com.moral.service.HistoryMinutelyService; |
| | | |
| | | /** |
| | | * @Auther: fengxiang |
| | |
| | | * @Description:国控api服务实现类 |
| | | */ |
| | | @Service |
| | | public class HangzhouAqiServiceImpl implements HangzhouAqiService{ |
| | | @Resource |
| | | private OrganizationMapper organizationMapper; |
| | | @Resource |
| | | private HangzhouAqiMapper hangzhouAqiMapper; |
| | | /** |
| | | * 根据组织id获取最近24小时国控aqi数据 |
| | | * @param orgId |
| | | * @return 返回长度为24的数组,查不到的数据设置为0 |
| | | */ |
| | | @Override |
| | | public List<Map> queryAqi24Hours(Integer orgId){ |
| | | return queryAqi24Hours(orgId,false); |
| | | } |
| | | private List<Map> queryAqi24Hours(Integer orgId,boolean isGetCity) { |
| | | List<Map> aqi24HoursValues = new ArrayList<>(24); |
| | | public class HangzhouAqiServiceImpl implements HangzhouAqiService { |
| | | @Resource |
| | | private OrganizationMapper organizationMapper; |
| | | @Resource |
| | | private HangzhouAqiMapper hangzhouAqiMapper; |
| | | |
| | | @Resource |
| | | private HistoryMinutelyService historyMinutelyService; |
| | | |
| | | public List<Map> queryAqiOfTimePeriod(String code, TimePeriod timePeriod) { |
| | | return hangzhouAqiMapper.selectAqisByCodeAndTimePeriod(code, timePeriod); |
| | | } |
| | | |
| | | /** |
| | | * 根据组织id获取最近24小时国控aqi数据 |
| | | * |
| | | * @param orgId |
| | | * @return 返回长度为24的数组,查不到的数据设置为0 |
| | | */ |
| | | @SuppressWarnings("serial") |
| | | @Override |
| | | public List<Map<String, Object>> queryAqi24Hours(Integer orgId) { |
| | | List<Map<String, Object>> aqi24HoursValues = new ArrayList<>(24); |
| | | /* |
| | | Date now = ReportTimeFormat.getFormatDate(ReportTimeFormat.HOUR_FORMAT); |
| | | Date start = ReportTimeFormat.dateCalc(now, TimeUnits.HOUR,-24); |
| | | Date end = ReportTimeFormat.dateCalc(now, TimeUnits.MILLISECOND,-1);; |
| | | TimePeriod timePeriod = new TimePeriod(start,end,TimeUnits.HOUR); |
| | | Integer code = getCode(orgId,isGetCity); |
| | | if(code == null) { |
| | | throw new BusinessException("citeCode or areaCode is null;"); |
| | | } |
| | | List<Map> aqis = hangzhouAqiMapper.selectAqisByCodeAndTimePeriod(code.toString(),timePeriod); |
| | | Integer code = getCode(orgId,false); |
| | | List<Map> aqis = queryAqiOfTimePeriod(code.toString(),timePeriod); |
| | | List<String> timeList = ReportTimeFormat.makeTimeList(timePeriod); |
| | | if(aqis!=null && aqis.size()==0 && !isGetCity) { |
| | | return queryAqi24Hours(orgId,true); |
| | | if(CollectionUtils.isEmpty(aqis)) { |
| | | code = getCode(orgId,true); |
| | | aqis = queryAqiOfTimePeriod(code.toString(),timePeriod); |
| | | } |
| | | int mTemp = 0; |
| | | for(int n =0;n<timeList.size();n++) { |
| | |
| | | aqiItem.put("aqi",aqiValue); |
| | | aqi24HoursValues.add(aqiItem); |
| | | } |
| | | return aqi24HoursValues; |
| | | */ |
| | | Organization organization = organizationMapper.selectByPrimaryKey(orgId); |
| | | |
| | | Map<String, Object> parameters = new HashMap<String, Object>() {{ |
| | | put("end", LocalDateTime.now()); |
| | | put("cityCode", organization.getAreaCode()); |
| | | }}; |
| | | aqi24HoursValues = hangzhouAqiMapper.getAqisByOrganizationId(parameters); |
| | | if (ObjectUtils.isEmpty(aqi24HoursValues)) { |
| | | parameters.put("cityCode", organization.getCityCode()); |
| | | aqi24HoursValues = hangzhouAqiMapper.getAqisByOrganizationId(parameters); |
| | | } |
| | | private Integer getCode(Integer orgId,boolean isGetCityCode) |
| | | { |
| | | Organization org = organizationMapper.selectByPrimaryKey(orgId); |
| | | if(isGetCityCode) { |
| | | return org.getCityCode(); |
| | | }else{ |
| | | return org.getAreaCode()!=null? org.getAreaCode() : org.getCityCode(); |
| | | } |
| | | Collections.reverse(aqi24HoursValues); |
| | | return aqi24HoursValues; |
| | | } |
| | | |
| | | private Integer getCode(Integer orgId, boolean isGetCityCode) { |
| | | Organization org = organizationMapper.selectByPrimaryKey(orgId); |
| | | Integer code = null; |
| | | if (isGetCityCode) { |
| | | code = org.getCityCode(); |
| | | } else { |
| | | code = org.getAreaCode(); |
| | | } |
| | | if (code == null) { |
| | | throw new BusinessException("citeCode or areaCode is null;"); |
| | | } |
| | | return code; |
| | | } |
| | | |
| | | @Override |
| | | public Integer queryCityCode(Integer areaCode) { |
| | | return hangzhouAqiMapper.queryCityCode(areaCode); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getAreaAvgDataByAreaCode(Map<String, Object> parameters) throws Exception { |
| | | historyMinutelyService.convertQueryParam(parameters); |
| | | if (!ObjectUtils.isEmpty(parameters.get("compensate"))) { |
| | | parameters.put("timeUnits", "10min"); |
| | | } |
| | | |
| | | String sensors2 = parameters.get("sensors2").toString(); |
| | | |
| | | String sensors1 = sensors2; |
| | | String sensors=""; |
| | | switch (sensors2) { |
| | | case "e1": |
| | | sensors1 = "PM2_5"; |
| | | sensors="PM25C"; |
| | | break; |
| | | case "e2": |
| | | sensors1 = "PM10"; |
| | | sensors="PM10C"; |
| | | break; |
| | | case "e10": |
| | | sensors1 = "CO"; |
| | | sensors="COC"; |
| | | break; |
| | | case "e11": |
| | | sensors1 = "SO2"; |
| | | sensors="SO2C"; |
| | | break; |
| | | case "e15": |
| | | sensors1 = "O3"; |
| | | sensors="O3C"; |
| | | break; |
| | | case "e16": |
| | | sensors1 = "NO2"; |
| | | sensors="NO2C"; |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | parameters.put("sensors1", sensors1); |
| | | parameters.put("sensors",sensors); |
| | | List<Map<String, Object>> areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters); |
| | | if (areaList.isEmpty()) { |
| | | Integer areaCode = Integer.valueOf(parameters.get("areaCode").toString()); |
| | | areaCode = hangzhouAqiMapper.queryCityCode(areaCode); |
| | | parameters.put("areaCode", String.valueOf(areaCode)); |
| | | areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters); |
| | | } |
| | | return areaList; |
| | | |
| | | } |
| | | } |