package com.moral.api.service.impl; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moral.api.entity.TbExcel; import com.moral.api.mapper.ExcelMapper; import com.moral.api.pojo.bo.ExcelBO; import com.moral.api.pojo.vo.excel.SyExcelVo; import com.moral.api.service.HnExcelService; import com.moral.api.utils.AqiUtils; import com.moral.api.utils.ExcelUtils; import com.moral.util.AQIUtils; import com.moral.util.AmendUtils; import com.moral.util.DateUtils; @Service public class HnExcelServiceImpl implements HnExcelService { @Autowired private ExcelMapper excelMapper; /** * 浑南区导入 * @param files * @param params * @return * @throws IOException * @throws ParseException */ @Override public ExcelBO getExcelImport(List files, Map params) throws IOException, ParseException { String time = (String) params.get("time"); String code = (String) params.get("code"); String time1 = (String) params.get("date"); Date date = DateUtils.getDate(time, "yyyy-MM-dd"); Date date1 = DateUtils.getDate(time1, "yyyy-MM-dd"); ExcelBO excelBO = new ExcelBO(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("time",date).eq("code",code); TbExcel excel = excelMapper.selectOne(wrapper); if (excel==null){ TbExcel excel1 = new TbExcel(); HashMap>> map = new HashMap<>(); MultipartFile file1 = files.get(0); Map> map1 = getMap1(file1, 0,time); map.put("浑南东路",map1); Map> map2 = getMap1(file1, 1,time); map.put("新秀街",map2); Map> map3 = getTo(file1,time); map.put("浑南区",map3); String s = JSON.toJSONString(map); excel1.setValue(s); excel1.setTime(date); excel1.setCode(code); excel1.setDate(date1); excelMapper.insert(excel1); excelBO.setId(excel1.getId()); excelBO.setTime(excel1.getTime()); excelBO.setDate(excel1.getDate()); }else { HashMap>> map = new HashMap<>(); MultipartFile file1 = files.get(0); Map> map1 = getMap1(file1, 0,time); map.put("浑南东路",map1); Map> map2 = getMap1(file1, 1,time); map.put("新秀街",map2); Map> map3 = getTo(file1,time); map.put("浑南区",map3); String s = JSON.toJSONString(map); excel.setValue(s); excelMapper.updateById(excel); excelBO.setId(excel.getId()); excelBO.setTime(excel.getTime()); excelBO.setDate(excel.getDate()); } return excelBO; } /** * 浑南区下载 * @param id * @return */ @Override public SyExcelVo SyExport(Integer id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); HashMap> rsMap = new HashMap<>(); SyExcelVo syExcelVo = new SyExcelVo(); TbExcel excel = excelMapper.selectById(id); String value = excel.getValue(); Date time = excel.getTime(); String format = sdf.format(time); String substring = format.substring(5, 7); int data= Integer.parseInt(substring); Map>> map = JSON.parseObject(value, Map.class); Set strings = map.keySet(); for (String string : strings) { if (string.equals("浑南东路")){ Map> stringListMap = map.get(string); Set strings1 = stringListMap.keySet(); for (String s : strings1) { if (s.equals("count")){ ArrayList list1 = getList(stringListMap, s); rsMap.put("hnd1",list1); } if (s.equals("PM2_5")){ ArrayList list1 = getArrayList(stringListMap, s); rsMap.put("hnd2",list1); } } } if (string.equals("新秀街")){ Map> stringListMap = map.get(string); Set strings1 = stringListMap.keySet(); for (String s : strings1) { if (s.equals("count")){ ArrayList list1 = getList(stringListMap, s); rsMap.put("xxj1",list1); } if (s.equals("PM2_5")){ ArrayList list1 = getArrayList(stringListMap, s); rsMap.put("xxj2",list1); } } } if (string.equals("浑南区")){ Map> stringListMap = map.get(string); Set strings1 = stringListMap.keySet(); for (String s : strings1) { if (s.equals("count")){ ArrayList list1 = new ArrayList<>(); List list = stringListMap.get(s); String season; Integer sum =0; if (data>=1 && data<=3){ sum = list.get(0) + list.get(1) + list.get(2); season = "一"; }else if (data>=4 &&data<=6){ sum = list.get(3) + list.get(4) + list.get(5); season = "二"; }else if (data>=7 &&data<=9){ sum = list.get(6) + list.get(7) + list.get(8); season = "三"; }else { sum = list.get(9) + list.get(10) + list.get(11); season = "四"; } syExcelVo.setCode5(sum.toString()); syExcelVo.setSeason(season); for (int i = 0; i < list.size()-1; i++) { if (list.get(i)==0){ list1.add(""); continue; } list1.add(list.get(i).toString()); } Integer integer = list.get(12); syExcelVo.setCode1(integer.toString()); Integer integer1 = list.get(13); int i = integer - integer1; String code =""; if (i<=0){ code = "减少"+i+"天"; }else { code = "增加"+i+"天"; } list1.add(code); syExcelVo.setCode2(code); list1.add(0,"优良天数"); rsMap.put("hnq1",list1); } if (s.equals("PM2_5")){ ArrayList list1 = new ArrayList<>(); List list = stringListMap.get(s); Integer sum =0; if (data>=1 && data<=3){ sum = list.get(0) + list.get(1) + list.get(2); }else if (data>=4 &&data<=6){ sum = list.get(3) + list.get(4) + list.get(5); }else if (data>=7 &&data<=9){ sum = list.get(6) + list.get(7) + list.get(8); }else { sum = list.get(9) + list.get(10) + list.get(11); } Integer round = Math.round(sum / 3); syExcelVo.setCode6(round.toString()); for (int i = 0; i < list.size()-1; i++) { if (list.get(i)==0){ list1.add(""); continue; } list1.add(list.get(i).toString()); } Integer integer = list.get(12); syExcelVo.setCode3(integer.toString()); Integer integer1 = list.get(13); double i = integer - integer1; double i1 = AmendUtils.sciCal((i / integer1)*100,1); String code = ""; if (i1<=0){ code = "下降"+i1+"%"; }else { code = "上升"+i1+"%"; } list1.add(code); syExcelVo.setCode4(code); list1.add(0,"PM2.5浓度微克/立方"); rsMap.put("hnq2",list1); } } } } ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); for (int i = 0; i < 15; i++) { if (i==0){ list1.add("优良天数"); continue; } list1.add(""); } list2.addAll(list1); list2.remove(0); list2.add(0,"PM2.5浓度微克/立方"); rsMap.put("sy1",list1); rsMap.put("sy2",list2); syExcelVo.setList(rsMap); return syExcelVo; } //计算pm2.5同比 private ArrayList getArrayList(Map> stringListMap, String s) { ArrayList list1 = new ArrayList<>(); List list = stringListMap.get(s); for (int i = 0; i < list.size()-1; i++) { if (list.get(i)==0){ list1.add(""); continue; } list1.add(list.get(i).toString()); } Integer integer = list.get(12); Integer integer1 = list.get(13); double i1 = 0.0; if (integer1!=0){ double i = integer - integer1; i1 = AmendUtils.sciCal((i / integer1)*100,1); } String code = ""; if (i1<=0){ code = "下降"+i1+"%"; }else { code = "上升"+i1+"%"; } list1.add(code); list1.add(0,"PM2.5浓度微克/立方"); return list1; } //计算优良天数 private ArrayList getList(Map> stringListMap, String s) { ArrayList list1 = new ArrayList<>(); List list = stringListMap.get(s); for (int i = 0; i < list.size()-1; i++) { if (list.get(i)==0){ list1.add(""); continue; } list1.add(list.get(i).toString()); } Integer integer = list.get(12); Integer integer1 = list.get(13); int i = integer - integer1; String code = ""; if (i<=0){ code = "减少"+i+"天"; }else { code = "增加"+i+"天"; } list1.add(code); list1.add(0,"优良天数"); return list1; } //解析文件 private Map> getMap1(MultipartFile file, int code,String time) throws IOException, ParseException { XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream()); HashMap rsMap1 = new HashMap<>(); HashMap rsMap2 = new HashMap<>(); HashMap rsMap3 = new HashMap<>(); XSSFSheet sheetAt = workbook.getSheetAt(code); //一共有多少行 int lastRowNum = sheetAt.getLastRowNum(); for (int i = 2; i <= lastRowNum; i++) { XSSFRow row = sheetAt.getRow(i); if (row == null) { continue; } short lastCellNum = row.getLastCellNum(); if (lastCellNum < 8) { continue; } Object[] objects = new Object[lastCellNum]; for (int j = 0; j < lastCellNum; j++) { Cell cell = row.getCell(j); if (cell != null) { Object value = ExcelUtils.getValue(cell); objects[j] = value; } } HashMap map = new HashMap<>(); HashMap map1 = new HashMap<>(); HashMap map2 = new HashMap<>(); if (objects.length == 9) { map.put("time", objects[0]); map.put("PM10", objects[1]); map.put("PM2_5", objects[2]); map.put("SO2", objects[3]); map.put("NO2", objects[4]); map.put("CO", objects[5]); map.put("O3_8H", objects[6]); map.put("AQI", objects[7]); rsMap1.put(objects[0].toString(), map); } if (objects.length == 18) { map.put("time", objects[0]); map.put("PM10", objects[1]); map.put("PM2_5", objects[2]); map.put("SO2", objects[3]); map.put("NO2", objects[4]); map.put("CO", objects[5]); map.put("O3_8H", objects[6]); map.put("AQI", objects[7]); rsMap1.put(objects[0].toString(), map); if (!objects[11].toString().contains("H") && !objects[12].toString().contains("H") && !objects[13].toString().contains("H") && !objects[14].toString().contains("H") && !objects[15].toString().contains("H") && !objects[16].toString().contains("H")) { map1.put("time", objects[10]); map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3_8H", objects[16]); map1.put("AQI", objects[17]); rsMap2.put(objects[10].toString(), map1); } } if (objects.length == 27) { map.put("time", objects[0]); map.put("PM10", objects[1]); map.put("PM2_5", objects[2]); map.put("SO2", objects[3]); map.put("NO2", objects[4]); map.put("CO", objects[5]); map.put("O3_8H", objects[6]); map.put("AQI", objects[7]); rsMap1.put(objects[0].toString(), map); if ( !objects[11].toString().contains("H") && !objects[12].toString().contains("H") && !objects[13].toString().contains("H") && !objects[14].toString().contains("H") && !objects[15].toString().contains("H") && !objects[16].toString().contains("H") ) { map1.put("time", objects[10]); map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3_8H", objects[16]); map1.put("AQI", objects[17]); rsMap2.put(objects[10].toString(), map1); } if ( !objects[20].toString().contains("H") && !objects[21].toString().contains("H") && !objects[22].toString().contains("H") && !objects[23].toString().contains("H") && !objects[24].toString().contains("H") && !objects[25].toString().contains("H") ) { map2.put("time", objects[19]); map2.put("PM10", objects[20]); map2.put("PM2_5", objects[21]); map2.put("SO2", objects[22]); map2.put("NO2", objects[23]); map2.put("CO", objects[24]); map2.put("O3_8H", objects[25]); map2.put("AQI", objects[26]); rsMap3.put(objects[19].toString(), map2); } } } //计算本年度pm2.5和优良天数 Map> map = getMap(rsMap3,time); //计算去年PM2.5和优良天数 Map> map2 = getResult(rsMap2, time); List list = map.get("count"); list.add(map2.get("count").get(0)); List list1 = map.get("PM2_5"); list1.add(map2.get("PM2_5").get(0)); map.put("去年",map2.get("去年")); return map; } /** * 计算PM2.5 和优良天数 * * @param rsMap3 */ private Map> getMap(HashMap rsMap3,String time) { HashMap> listMap = new HashMap<>(); Set strings = rsMap3.keySet(); ArrayList list1 = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); ArrayList list3 = new ArrayList<>(); ArrayList list4 = new ArrayList<>(); ArrayList list5 = new ArrayList<>(); ArrayList list6 = new ArrayList<>(); ArrayList list7 = new ArrayList<>(); ArrayList list8 = new ArrayList<>(); ArrayList list9 = new ArrayList<>(); ArrayList list10 = new ArrayList<>(); ArrayList list11 = new ArrayList<>(); ArrayList list12 = new ArrayList<>(); ArrayList resList = new ArrayList<>(); ArrayList list = new ArrayList<>(); ArrayList listPM = new ArrayList<>(); ArrayList AQI = new ArrayList<>(); ArrayList integers = new ArrayList<>(); int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; int count5 = 0; int count6 = 0; int count7 = 0; int count8 = 0; int count9 = 0; int count10 = 0; int count11 = 0; int count12 = 0; String rsTime = time.substring(0, 4); integers.add(Integer.parseInt(rsTime)); for (String string : strings) { String substring = string.substring(5, 7); if (substring.equals("01")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list1.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count1++; } } if (substring.equals("02")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list2.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count2++; } } if (substring.equals("03")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list3.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count3++; } } if (substring.equals("04")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list4.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count4++; } } if (substring.equals("05")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list5.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count5++; } } if (substring.equals("06")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list6.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count6++; } } if (substring.equals("07")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list7.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count7++; } } if (substring.equals("08")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list8.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count8++; } } if (substring.equals("09")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list9.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count9++; } } if (substring.equals("10")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list10.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count10++; } } if (substring.equals("11")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list11.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count11++; } } if (substring.equals("12")) { Object o = rsMap3.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list12.add(Double.parseDouble(pm2_5)); resList.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count12++; } } } int code1 =0; int code2 =0; int code3 =0; int code4 =0; int code5 =0; int code6 =0; int i; for (double aDouble : AQI) { i=(int)aDouble; String s = AQIUtils.classOfPollutionByAqi(i); if (s.equals("优")){ code1++; } if (s.equals("良")){ code2++; } if (s.equals("轻度污染")){ code3++; } if (s.equals("中度污染")){ code4++; } if (s.equals("重度污染")){ code5++; } if (s.equals("严重污染")){ code6++; } } integers.add(code1+code2); integers.add(code1); integers.add(code2); integers.add(code3); integers.add(code4); integers.add(code5); integers.add(code6); list.add(count1); list.add(count2); list.add(count3); list.add(count4); list.add(count5); list.add(count6); list.add(count7); list.add(count8); list.add(count9); list.add(count10); list.add(count11); list.add(count12); Integer sum = list.stream().reduce(Integer::sum).orElse(0); list.add(sum); listMap.put("count", list); Integer collect1 = 0; if (list1.size() > 0) { // collect1 = Math.toIntExact(Math.round(list1.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect1 = (int) AmendUtils.sciCal(list1.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect2 = 0; if (list2.size() > 0) { // collect2 = Math.toIntExact(Math.round(list2.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect2= (int) AmendUtils.sciCal(list2.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect3 = 0; if (list3.size() > 0) { // collect3 = Math.toIntExact(Math.round(list3.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect3 = (int) AmendUtils.sciCal(list3.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect4 = 0; if (list4.size() > 0) { // collect4 = Math.toIntExact(Math.round(list4.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect4 = (int) AmendUtils.sciCal(list4.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect5 = 0; if (list5.size() > 0) { // collect5 = Math.toIntExact(Math.round(list5.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect5 = (int) AmendUtils.sciCal(list5.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect6 = 0; if (list6.size() > 0) { // collect6 = Math.toIntExact(Math.round(list6.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect6 = (int) AmendUtils.sciCal(list6.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect7 = 0; if (list7.size() > 0) { // collect7 = Math.toIntExact(Math.round(list7.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect7 = (int) AmendUtils.sciCal(list7.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect8 = 0; if (list8.size() > 0) { // collect8 = Math.toIntExact(Math.round(list8.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect8 = (int) AmendUtils.sciCal(list8.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect9 = 0; if (list9.size() > 0) { // collect9 = Math.toIntExact(Math.round(list9.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect9 = (int) AmendUtils.sciCal(list9.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect10 = 0; if (list10.size() > 0) { // collect10 = Math.toIntExact(Math.round(list10.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect10 = (int) AmendUtils.sciCal(list10.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect11 = 0; if (list11.size() > 0) { // collect11 = Math.toIntExact(Math.round(list11.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect11 = (int) AmendUtils.sciCal(list11.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } Integer collect12 = 0; if (list12.size() > 0) { // collect12 = Math.toIntExact(Math.round(list12.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); collect12 = (int) AmendUtils.sciCal(list12.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); } //今年的pm2.5累计 // int resPM25 = Math.toIntExact(Math.round(resList.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); int resPM25 = (int) AmendUtils.sciCal(resList.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); listPM.add(collect1); listPM.add(collect2); listPM.add(collect3); listPM.add(collect4); listPM.add(collect5); listPM.add(collect6); listPM.add(collect7); listPM.add(collect8); listPM.add(collect9); listPM.add(collect10); listPM.add(collect11); listPM.add(collect12); listPM.add(resPM25); listMap.put("PM2_5",listPM); listMap.put("今年",integers); return listMap; } /** * 计算去年PM2.5和优良天数 * * @param rsMap2 */ private Map> getResult(HashMap rsMap2, String time) throws ParseException { HashMap> rsMap = new HashMap<>(); ArrayList list = new ArrayList<>(); ArrayList list2 = new ArrayList<>(); ArrayList list3 = new ArrayList<>(); ArrayList AQI = new ArrayList<>(); ArrayList rsAqi = new ArrayList<>(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); Calendar instance = Calendar.getInstance(); instance.setTime(sdf1.parse(time)); instance.add(Calendar.YEAR, -1); Date date = instance.getTime(); //获取年 String format = sdf1.format(date); String rsTime = format.substring(0, 4); //时间戳 long time1 = date.getTime(); int count = 0; Set strings = rsMap2.keySet(); for (String string : strings) { Date parse = sdf1.parse(string); long time2 = parse.getTime(); if (time2 <= time1) { Object o = rsMap2.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); String aqi = map.get("AQI").toString(); String pm2_5 = map.get("PM2_5").toString(); list.add(Double.parseDouble(pm2_5)); AQI.add(Double.parseDouble(aqi)); if (Double.parseDouble(aqi) <= 100) { count++; } } } list2.add(count); int code1 =0; int code2 =0; int code3 =0; int code4 =0; int code5 =0; int code6 =0; int i; for (double adouble : AQI) { i =(int) adouble; String s = AQIUtils.classOfPollutionByAqi(i); if (s.equals("优")){ code1++; } if (s.equals("良")){ code2++; } if (s.equals("轻度污染")){ code3++; } if (s.equals("中度污染")){ code4++; } if (s.equals("重度污染")){ code5++; } if (s.equals("严重污染")){ code6++; } } rsAqi.add(Integer.parseInt(rsTime)); rsAqi.add(code1+code2); rsAqi.add(code1); rsAqi.add(code2); rsAqi.add(code3); rsAqi.add(code4); rsAqi.add(code5); rsAqi.add(code6); //去年的pm2.5累积 // int PM2_5 = Math.toIntExact(Math.round(list.stream().collect(Collectors.averagingDouble(Double::doubleValue)))); int PM2_5 = (int) AmendUtils.sciCal(list.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 0); list3.add(PM2_5); rsMap.put("count", list2); rsMap.put("PM2_5", list3); rsMap.put("去年",rsAqi); return rsMap; } /** * 计算 浑南区的数据 * @param file * @throws IOException */ private Map> getTo(MultipartFile file,String time) throws IOException, ParseException { XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream()); HashMap rsMap1 = new HashMap<>(); HashMap rsMap2 = new HashMap<>(); HashMap rsMap3 = new HashMap<>(); HashMap rsMap4 = new HashMap<>(); //去年的浑南区 HashMap hnMap1 = new HashMap<>(); //今年的浑南区 HashMap hnMap2 = new HashMap<>(); XSSFSheet sheetAt = workbook.getSheetAt(0); //一共有多少行 //浑南东路 int lastRowNum = sheetAt.getLastRowNum(); for (int i = 2; i <= lastRowNum; i++) { XSSFRow row = sheetAt.getRow(i); if (row == null) { continue; } short lastCellNum = row.getLastCellNum(); if (lastCellNum < 8) { continue; } Object[] objects = new Object[lastCellNum]; for (int j = 0; j < lastCellNum; j++) { Cell cell = row.getCell(j); if (cell != null) { Object value = ExcelUtils.getValue(cell); objects[j] = value; } } HashMap map1 = new HashMap<>(); HashMap map2 = new HashMap<>(); if (objects.length == 18) { map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3", objects[16]); rsMap1.put(objects[10].toString(), map1); } if (objects.length == 27) { map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3", objects[16]); rsMap1.put(objects[10].toString(), map1); map2.put("PM10", objects[20]); map2.put("PM2_5", objects[21]); map2.put("SO2", objects[22]); map2.put("NO2", objects[23]); map2.put("CO", objects[24]); map2.put("O3", objects[25]); rsMap2.put(objects[19].toString(), map2); } } //新秀街 XSSFSheet sheetAt1 = workbook.getSheetAt(1); int lastRowNum1 = sheetAt1.getLastRowNum(); for (int i = 2; i < lastRowNum1; i++) { XSSFRow row = sheetAt1.getRow(i); if (row == null) { continue; } short lastCellNum = row.getLastCellNum(); if (lastCellNum < 8) { continue; } Object[] objects = new Object[lastCellNum]; for (int j = 0; j < lastCellNum; j++) { Cell cell = row.getCell(j); if (cell != null) { Object value = ExcelUtils.getValue(cell); objects[j] = value; } } HashMap map1 = new HashMap<>(); HashMap map2 = new HashMap<>(); if (objects.length == 18) { map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3", objects[16]); rsMap3.put(objects[10].toString(), map1); } if (objects.length == 27) { map1.put("PM10", objects[11]); map1.put("PM2_5", objects[12]); map1.put("SO2", objects[13]); map1.put("NO2", objects[14]); map1.put("CO", objects[15]); map1.put("O3", objects[16]); rsMap3.put(objects[10].toString(), map1); map2.put("PM10", objects[20]); map2.put("PM2_5", objects[21]); map2.put("SO2", objects[22]); map2.put("NO2", objects[23]); map2.put("CO", objects[24]); map2.put("O3", objects[25]); rsMap4.put(objects[19].toString(), map2); } } Set strings = rsMap1.keySet(); for (String string : strings) { Object o = rsMap1.get(string); String s = JSON.toJSONString(o); Map map = JSON.parseObject(s, Map.class); Object o1 = rsMap3.get(string); String s1 = JSON.toJSONString(o1); Map map1 = JSON.parseObject(s1, Map.class); Set sets = map1.keySet(); HashMap hs = new HashMap<>(); for (Object set : sets) { if (set.toString().equals("CO")){ String s3 = map1.get(set).toString(); String s2 = map.get(set).toString(); if (s3.contains("H") && !s2.contains("H")){ hs.put(set,Double.parseDouble(s2)); }else if(!s3.contains("H") && s2.contains("H")){ hs.put(set,Double.parseDouble(s3)); }else if(s3.contains("H") && s2.contains("H")){ break; }else { // hs.put(set,new BigDecimal((Double.parseDouble(s3)+Double.parseDouble(s2))/2).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()); hs.put(set, AmendUtils.sciCal((Double.parseDouble(s3)+Double.parseDouble(s2))/2,1)); } continue; } String s3 = map1.get(set).toString(); String s2 = map.get(set).toString(); if (s3.contains("H") && !s2.contains("H")){ hs.put(set,Double.parseDouble(s2)); }else if(!s3.contains("H") && s2.contains("H")){ hs.put(set,Double.parseDouble(s3)); }else if(s3.contains("H") && s2.contains("H")){ break; }else { // hs.put(set,Math.round((Double.parseDouble(s3)+Double.parseDouble(s2))/2)); hs.put(set, AmendUtils.sciCal((Double.parseDouble(s3)+Double.parseDouble(s2))/2,0)); } } if (hs.size()==6){ hnMap1.put(string,hs); } } Set strings1 = rsMap2.keySet(); for (String s : strings1) { Object o = rsMap2.get(s); String s1 = JSON.toJSONString(o); Map map = JSON.parseObject(s1, Map.class); Object o1 = rsMap4.get(s); String s2 = JSON.toJSONString(o1); Map map1 = JSON.parseObject(s2, Map.class); Set sets = map1.keySet(); HashMap hs = new HashMap<>(); for (Object set : sets) { if (set.toString().equals("CO")){ String s3 = map1.get(set).toString(); String s4 = map.get(set).toString(); if (s3.contains("H") && !s4.contains("H")){ hs.put(set,Double.parseDouble(s4)); }else if(!s3.contains("H") && s4.contains("H")){ hs.put(set,Double.parseDouble(s3)); }else if(s3.contains("H") && s4.contains("H")){ break; }else { hs.put(set, AmendUtils.sciCal((Double.parseDouble(s3)+Double.parseDouble(s4))/2,1)); } continue; } String s3 = map1.get(set).toString(); String s4 = map.get(set).toString(); if (s3.contains("H") && !s4.contains("H")){ hs.put(set,Double.parseDouble(s4)); }else if(!s3.contains("H") && s4.contains("H")){ hs.put(set,Double.parseDouble(s3)); }else if(s3.contains("H") && s4.contains("H")){ break; }else { hs.put(set, AmendUtils.sciCal((Double.parseDouble(s3)+Double.parseDouble(s4))/2,0)); } } if (hs.size()==6){ hnMap2.put(s,hs); } } //计算今年的aqi getHnMap(hnMap2); //计算去年的aqi getHnMap(hnMap1); // Map> map = getMap(hnMap2,time); Map> map2 = getResult(hnMap1, time); List list = map.get("count"); list.add(map2.get("count").get(0)); List list1 = map.get("PM2_5"); list1.add(map2.get("PM2_5").get(0)); return map; } //计算aqi private void getHnMap(HashMap hnMap2) { Set strings4 = hnMap2.keySet(); for (String s : strings4) { Object o = hnMap2.get(s); String s1 = JSON.toJSONString(o); Map rs = JSON.parseObject(s1, Map.class); Map aqi1 = AqiUtils.getAqi(rs); JSONObject jsonObject = JSON.parseObject(s1); jsonObject.put("AQI",aqi1.get("AQI")); String s2 = JSON.toJSONString(jsonObject); Map map = JSON.parseObject(s2, Map.class); hnMap2.put(s,map); } } }