jinpengyong
2022-09-26 36203a854c6d1958549ebf65ba234561ab5271b5
添加空气质量报告功能
8 files added
1 files modified
1241 ■■■■■ changed files
screen-api/pom.xml 16 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/ExcelController.java 89 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/entity/TbExcel.java 25 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/mapper/ExcelMapper.java 10 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/bo/ExcelBO.java 20 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/vo/excel/ExcelVo.java 68 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/ExcelService.java 25 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/ExcelServiceImpl.java 950 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/ExcelUtils.java 38 ●●●●● patch | view | raw | blame | history
screen-api/pom.xml
@@ -21,6 +21,22 @@
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- easyexcel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>
    <build>
screen-api/src/main/java/com/moral/api/controller/ExcelController.java
New file
@@ -0,0 +1,89 @@
package com.moral.api.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.moral.api.pojo.bo.ExcelBO;
import com.moral.api.pojo.vo.excel.ExcelVo;
import com.moral.api.service.ExcelService;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private ExcelService excelService;
    /**
     * 导入
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("excelImport")
    public ResultMessage excelImport(HttpServletRequest request) throws IOException {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        if (!params.containsKey("time") || !params.containsKey("code") || params.containsKey("data")){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files");
        ExcelBO excelBO = excelService.importTemplate(files, params);
        return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),excelBO);
    }
    /**
     * 导出
     * @param id
     * @return
     */
    @GetMapping("/excelExport")
    public ResultMessage excelExport(Integer id){
        if (id==null){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        ExcelVo export = excelService.export(id);
        if (export==null){
            return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg());
        }
        return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),export);
    }
    /**
     * 查询
     * @param startTime
     * @param code
     * @param endTime
     * @return
     */
    @GetMapping("/selectExcel")
    public  ResultMessage selectExcel(String startTime,String code, String endTime){
        if (startTime == null || code==null || endTime==null){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        List<ExcelBO> excelBOS = excelService.excelSelect(startTime, code, endTime);
        if (excelBOS==null){
            return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg());
        }
        return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),excelBOS);
    }
}
screen-api/src/main/java/com/moral/api/entity/TbExcel.java
New file
@@ -0,0 +1,25 @@
package com.moral.api.entity;
import lombok.Data;
import java.util.Date;
import javax.persistence.Table;
@Data
@Table(name = "tb_excel")
public class TbExcel {
    private  Integer id;
    private  String value;
    private  Date time;
    private  String  code;
    private  Date  date;
}
screen-api/src/main/java/com/moral/api/mapper/ExcelMapper.java
New file
@@ -0,0 +1,10 @@
package com.moral.api.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moral.api.entity.TbExcel;
public interface ExcelMapper extends BaseMapper<TbExcel> {
}
screen-api/src/main/java/com/moral/api/pojo/bo/ExcelBO.java
New file
@@ -0,0 +1,20 @@
package com.moral.api.pojo.bo;
import lombok.Data;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
@Data
public class ExcelBO {
    private  Integer id;
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private Date time;
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    private  Date date;
}
screen-api/src/main/java/com/moral/api/pojo/vo/excel/ExcelVo.java
New file
@@ -0,0 +1,68 @@
package com.moral.api.pojo.vo.excel;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class ExcelVo {
    //空气质量
    private  String quality;
    //首要污染物
    private  String  su;
    //空气因子
    private  Integer count;
    private  String  PM10;
    private  String  PM25;
    private  String  SO2;
    private  String  NO2;
    private  String  CO;
    private  String  O3_8H;
    private  String PM25m;
    private  String PM25ms;
    private  String O3m;
    private  String O3ms;
    private  String AQIm;
    private  String AQIms;
    private  String PM25y;
    private  String PM25ys;
    private  String O3y;
    private  String O3ys;
    private  String AQIy;
    private  String AQIys;
    private  String name1;
    private  String name2;
    private  String name3;
    private  String name4;
    private  String name5;
    private  String name6;
    private List<Map<String,Object>> list1;
    private  List<Map<String,Object>> list2;
    private  List<Map<String,Object>> list3;
    private  List<Map<String,Object>> list4;
    private  List<Map<String,Object>> list5;
    private  String time;
    private  String time1;
    private  String code1;
    private  String diff1;
    private  String code2;
    private  String diff2;
}
screen-api/src/main/java/com/moral/api/service/ExcelService.java
New file
@@ -0,0 +1,25 @@
package com.moral.api.service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.moral.api.pojo.bo.ExcelBO;
import com.moral.api.pojo.vo.excel.ExcelVo;
public interface ExcelService {
    public ExcelBO importTemplate(List<MultipartFile> files, Map<String, Object> params) throws IOException;
    public ExcelVo export(Integer id);
    public  List<ExcelBO> excelSelect(String startTime,String code,String endTime);
}
screen-api/src/main/java/com/moral/api/service/impl/ExcelServiceImpl.java
New file
@@ -0,0 +1,950 @@
package com.moral.api.service.impl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
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.ExcelVo;
import com.moral.api.service.ExcelService;
import com.moral.api.utils.ExcelUtils;
import com.moral.pojo.AQI;
import com.moral.util.AQIUtils;
import com.moral.util.DateUtils;
@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private ExcelMapper excelMapper;
    /**
     * 导入
     * @param files
     * @param params
     * @return
     * @throws IOException
     */
    @Override
    public ExcelBO importTemplate(List<MultipartFile> files, Map<String, Object> params) throws IOException {
        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<TbExcel> wrapper = new QueryWrapper<>();
        wrapper.eq("time",date).eq("code",code);
        TbExcel excel = excelMapper.selectOne(wrapper);
        if (excel==null){
            TbExcel excel1 = new TbExcel();
            HashMap<String, Object> map = new HashMap<>();
            MultipartFile file1 = files.get(0);
            Map<String, Object> map1 = getMap1(file1);
            map.put("c1",map1);
            MultipartFile file2 = files.get(1);
            Map<String, Object> map2 = getMap2(file2);
            map.put("c2",map2);
            MultipartFile file3 = files.get(2);
            Map<String, Object> map3 = getMap3(file3);
            map.put("c3",map3);
            MultipartFile file4 = files.get(3);
            Map<String, Object> map4 = getMap4(file4);
            map.put("c4",map4);
            MultipartFile file5 = files.get(4);
            Map<String, Object> map5 = getMap5(file5);
            map.put("c5",map5);
            MultipartFile file6 = files.get(5);
            Map<String, Object> map6 = getMap6(file6);
            map.put("c6",map6);
            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<String, Object> map = new HashMap<>();
            MultipartFile file1 = files.get(0);
            Map<String, Object> map1 = getMap1(file1);
            map.put("c1",map1);
            MultipartFile file2 = files.get(1);
            Map<String, Object> map2 = getMap2(file2);
            map.put("c2",map2);
            MultipartFile file3 = files.get(2);
            Map<String, Object> map3 = getMap3(file3);
            map.put("c3",map3);
            MultipartFile file4 = files.get(3);
            Map<String, Object> map4 = getMap4(file4);
            map.put("c4",map4);
            MultipartFile file5 = files.get(4);
            Map<String, Object> map5 = getMap5(file5);
            map.put("c5",map5);
            MultipartFile file6 = files.get(5);
            Map<String, Object> map6 = getMap6(file6);
            map.put("c6",map6);
            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 ExcelVo export(Integer id) {
        HashMap<String, Object> map1 = new HashMap<>();
        ArrayList<Map<String,Object>> list1 = new ArrayList<>();
        ArrayList<Map<String,Object>> list2 = new ArrayList<>();
        ArrayList<Map<String,Object>> list3 = new ArrayList<>();
        ArrayList<Map<String,Object>> list4 = new ArrayList<>();
        ArrayList<Map<String,Object>> list5= new ArrayList<>();
        ExcelVo excelVo = new ExcelVo();
        //类型转换
        TbExcel excel = excelMapper.selectById(id);
        String value = excel.getValue();
        JSONObject jsonObject = JSON.parseObject(value);
        Map map4 = JSON.parseObject(value, Map.class);
        Set sets = map4.keySet();
        for (Object set : sets) {
            if (set.equals("c1")){
                Object o = map4.get(set);
                String s = JSON.toJSONString(o);
                Map map5 = JSON.parseObject(s, Map.class);
                Set set1s = map5.keySet();
                for (Object set1 : set1s) {
                    Object o1 = map5.get(set1);
                    String s1 = JSON.toJSONString(o1);
                    Map map6 = JSON.parseObject(s1, Map.class);
                    list1.add(map6);
                }
            }
            if (set.equals("c2")){
                Object o = map4.get(set);
                String s = JSON.toJSONString(o);
                Map map5 = JSON.parseObject(s, Map.class);
                Set set1s = map5.keySet();
                for (Object set1 : set1s) {
                    Object o1 = map5.get(set1);
                    String s1 = JSON.toJSONString(o1);
                    Map map6 = JSON.parseObject(s1, Map.class);
                    list2.add(map6);
                }
            }
            if (set.equals("c3")){
                Object o = map4.get(set);
                String s = JSON.toJSONString(o);
                Map map5 = JSON.parseObject(s, Map.class);
                Set set1s = map5.keySet();
                for (Object set1 : set1s) {
                    Object o1 = map5.get(set1);
                    String s1 = JSON.toJSONString(o1);
                    Map map6 = JSON.parseObject(s1, Map.class);
                    map6.put("place",set1);
                    list3.add(map6);
                }
            }
            if (set.equals("c4")){
                Object o = map4.get(set);
                String s = JSON.toJSONString(o);
                Map map5 = JSON.parseObject(s, Map.class);
                Set set1s = map5.keySet();
                for (Object set1 : set1s) {
                    Object o1 = map5.get(set1);
                    String s1 = JSON.toJSONString(o1);
                    Map map6 = JSON.parseObject(s1, Map.class);
                    String o2 = String.valueOf(map6.get("PM2_5m"));
                    String place = String.valueOf(map6.get("place"));
                    String o3m = String.valueOf(map6.get("O3m"));
                    String aqIm = String.valueOf(map6.get("AQIm")) ;
                    String o2s =String.valueOf(map6.get("PM2_5y"));
                    String O3ms = String.valueOf(map6.get("O3y")) ;
                    String aqIms = String.valueOf(map6.get("AQIy"));
                    String result = o2+"、"+o3m+"、"+aqIm+"、"+o2s+"、"+O3ms+"、"+aqIms;
                    map1.put(place,result);
                    list4.add(map6);
                }
            }
            if (set.equals("c5")){
                Object o = map4.get(set);
                String s = JSON.toJSONString(o);
                Map map5 = JSON.parseObject(s, Map.class);
                Set set1s = map5.keySet();
                for (Object set1 : set1s) {
                    Object o1 = map5.get(set1);
                    String s1 = JSON.toJSONString(o1);
                    Map map6 = JSON.parseObject(s1, Map.class);
                    list5.add(map6);
                }
            }
        }
        //第四个文件
        String c4 = jsonObject.getString("c4");
        JSONObject jsonObject3 = JSON.parseObject(c4);
        String gxq = jsonObject3.getString("高新区");
        JSONObject jsonObject4 = JSON.parseObject(gxq);
        String PM25m = jsonObject4.getString("PM2_5m");
        String PM25ms = jsonObject4.getString("PM2_5ms");
        String code1 = getString(PM25ms);
        String O3m = jsonObject4.getString("O3m");
        String O3ms = jsonObject4.getString("O3ms");
        String code2 = getString(O3ms);
        String AQIm = jsonObject4.getString("AQIm");
        String AQIms = jsonObject4.getString("AQIms");
        String code3 = getString(AQIms);
        String PM25y = jsonObject4.getString("PM2_5y");
        String PM25ys = jsonObject4.getString("PM2_5ys");
        String code4 = getString(PM25ys);
        String O3y = jsonObject4.getString("O3y");
        String O3ys = jsonObject4.getString("O3ys");
        String code5 = getString(O3ys);
        String AQIy = jsonObject4.getString("AQIy");
        String AQIys = jsonObject4.getString("AQIys");
        String code6 = getString(AQIys);
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        StringBuilder sb3 = new StringBuilder();
        StringBuilder sb4 = new StringBuilder();
        StringBuilder sb5 = new StringBuilder();
        StringBuilder sb6 = new StringBuilder();
        Set<String> trims = map1.keySet();
        for (String trim : trims) {
            String o = (String) map1.get(trim);
            String[] split = o.split("、");
            if (Double.parseDouble(split[0])>Double.parseDouble(PM25m)){
                sb1.append(trim.substring(3)+"、");
            }
            if (Double.parseDouble(split[1])>Double.parseDouble(O3m)){
                sb2.append(trim.substring(3)+"、");
            }
            if (Double.parseDouble(split[2])<Double.parseDouble(AQIm)){
                sb3.append(trim.substring(3)+"、");
            }
            if (Double.parseDouble(split[3])>Double.parseDouble(PM25y)){
                sb4.append(trim.substring(3)+"、");
            }
            if (Double.parseDouble(split[4])>Double.parseDouble(O3y)){
                sb5.append(trim.substring(3)+"、");
            }
            if (Double.parseDouble(split[5])<Double.parseDouble(AQIy)){
                sb6.append(trim.substring(3)+"、");
            }
        }
        String builder1 = getBuilder(sb1);
        String builder2 = getBuilder(sb2);
        String builder3 = getBuilder(sb3);
        String builder4 = getBuilder(sb4);
        String builder5 = getBuilder(sb5);
        String builder6 = getBuilder(sb6);
        excelVo.setName1(builder1);
        excelVo.setName2(builder2);
        excelVo.setName3(builder3);
        excelVo.setName4(builder4);
        excelVo.setName5(builder5);
        excelVo.setName6(builder6);
        //第五个文件
        String c5 = jsonObject.getString("c5");
        JSONObject jsonObject5 = JSON.parseObject(c5);
        String cxq = jsonObject5.getString("彩香");
        JSONObject jsonObject6 = JSON.parseObject(cxq);
        String cxp = jsonObject6.getString("PM2_5y");
        double CX = 0.0;
        if (!cxp.equals("--")){
           CX = Double.parseDouble(cxp);
        }
        double aDouble = 0.0;
        if (!PM25y.equals("--")){
             aDouble = Double.parseDouble(PM25y);
        }
        double v = aDouble - CX;
        if (v>=0){
            excelVo.setDiff1("高于");
            excelVo.setCode1(String.valueOf(new BigDecimal(v).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()));
        }else {
            excelVo.setDiff1("低于");
            excelVo.setCode1(String.valueOf(new BigDecimal(Math.abs(v)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()));
        }
        String xcq = jsonObject5.getString("相城区");
        JSONObject jsonObject7 = JSON.parseObject(xcq);
        String xcp = jsonObject7.getString("PM2_5y");
         double XCP = 0.0;
        if (!xcp.equals("--")){
            XCP = Double.parseDouble(xcp);
        }
        double v1 = aDouble - XCP;
        if (v1>=0){
            excelVo.setDiff2("高于");
            excelVo.setCode2(String.valueOf(new BigDecimal(v1).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()));
        }else {
            excelVo.setDiff2("低于");
            excelVo.setCode2(String.valueOf(new BigDecimal(Math.abs(v1)).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()));
        }
        //第一个文件
        String c1 = jsonObject.getString("c1");
        JSONObject jsonObject1 = JSON.parseObject(c1);
        //高新区
        String cx = jsonObject1.getString("高新区");
        JSONObject jsonObject2 = JSON.parseObject(cx);
        String reTime = jsonObject2.getString("日期");
        Date reDate = DateUtils.getDate(reTime, "yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String format = simpleDateFormat.format(reDate);
        String substring = format.substring(5);
        String CO = jsonObject2.getString("CO");
        String NO2 = jsonObject2.getString("NO2");
        String SO2 = jsonObject2.getString("SO2");
        String PM10 = jsonObject2.getString("PM10");
        String PM25 = jsonObject2.getString("PM2_5");
        String s1 = jsonObject2.getString("SU");
        String O8 = jsonObject2.getString("O3_8H");
        String s3 = jsonObject2.getString("空气质量类别");
        String s4 = jsonObject2.getString("空气质量级别");
        int count = 0;
        if (Double.parseDouble(PM10)<150){
            count++;
        }
        if (Double.parseDouble(PM25)<75){
            count++;
        }
        if (Double.parseDouble(SO2)<150){
            count++;
        }
        if (Double.parseDouble(NO2)<80){
            count++;
        }
        if (Double.parseDouble(CO)<4){
            count++;
        }
        if (Double.parseDouble(O8)<160){
            count++;
        }
//        String res =  substring+",我区空气质量为"+s3+s4+",首要污染物为:"+s1+"。从空气6因子看," +
//                 count+"项达标:PM10为"+PM10+"ug/m³(标准值150μg/m³);PM2.5为"+PM25+"μg/m³(标准值75μg/m³);" +
//                 "SO2为"+SO2+"μg/m³(标准值150μg/m³);NO2为"+NO2+"μg/m³(标准值80μg/m³);CO为"+CO+"mg/m³" +
//                 "(标准值4mg/m³);O3-8H为"+O8+"μg/m³(标准值160μg/m³)。";
//        String month ="我区月度(9月1日-13日)PM2.5浓度为"+PM25m+"微克/立方米,同比"+code1+"%。" +
//                "臭氧8小时滑动平均值90百分位数浓度为"+O3m+"微克/立方米,同比"+code2+"%。" +
//                "空气质量优良天数比例为"+AQIm+"%,同比"+code3+"%。";
//        String  years= "今年以来(01月1日-"+substring+"),我区PM2.5浓度为"+PM25y+"微克/立方米,同比"+code4+"%。" +
//                "臭氧8小时滑动平均值90百分位数浓度为"+O3y+"微克/立方米,同比"+code5+"%。" +
//                "空气质量优良天数比例为"+AQIy+"%,同比"+code6+"%。";
        excelVo.setQuality(s3+s4);
        excelVo.setSu(s1);
        excelVo.setCount(count);
        excelVo.setPM10(PM10);
        excelVo.setPM25(PM25);
        excelVo.setSO2(SO2);
        excelVo.setNO2(NO2);
        excelVo.setCO(CO);
        excelVo.setO3_8H(O8);
        excelVo.setPM25m(PM25m);
        excelVo.setPM25ms(code1);
        excelVo.setO3m(O3m);
        excelVo.setO3ms(code2);
        excelVo.setAQIm(AQIm);
        excelVo.setAQIms(code3);
        excelVo.setPM25y(PM25y);
        excelVo.setPM25ys(code4);
        excelVo.setO3y(O3y);
        excelVo.setO3ys(code5);
        excelVo.setAQIy(AQIy);
        excelVo.setAQIys(code6);
        excelVo.setTime1(format);
        excelVo.setTime(substring);
        excelVo.setList1(list1);
        excelVo.setList2(list2);
        excelVo.setList3(list3);
        excelVo.setList4(list4);
        excelVo.setList5(list5);
        return excelVo;
    }
    /**
     * 查询
     * @param startTime
     * @param code
     * @return
     */
    @Override
    public List<ExcelBO> excelSelect(String startTime, String code,String endTime) {
        ArrayList<ExcelBO> excelBOS = new ArrayList<>();
        QueryWrapper<TbExcel> wrapper = new QueryWrapper<>();
        wrapper.between("time",startTime,endTime);
        wrapper.eq("code",code);
        List<TbExcel> tbExcels = excelMapper.selectList(wrapper);
        if (tbExcels==null){
            return null;
        }
        for (TbExcel tbExcel : tbExcels) {
            ExcelBO excelBO = new ExcelBO();
            BeanUtils.copyProperties(tbExcel,excelBO);
            excelBOS.add(excelBO);
        }
        return excelBOS;
    }
    private String getBuilder(StringBuilder sb) {
        if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '、') {
            sb.deleteCharAt(sb.length() -1 );
        }else {
            sb.append("无");
        }
        String s = sb.toString();
        return s;
    }
    private String getString(String value) {
        String code =null;
        double aDouble = 0.0;
        if (!value.equals("--")){
             aDouble = Double.parseDouble(value);
        }
        if (aDouble>=0){
            code = "上升"+ value;
        }else {
            String s = value.substring(1);
            code = "下降"+ s;
        }
        return code;
    }
    private Map<String, Object> getMap4(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HashMap<String, Object> rsMap = new HashMap<>();
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 5; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            if (lastCellNum < 10) {
                continue;
            }
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("place", objects[0]);
            map.put("PM2_5d", objects[1]);
            map.put("PM2_5m", objects[2]);
            map.put("PM2_5y", objects[3]);
            map.put("PM2_5r", objects[4]);
            map.put("PM2_5ms", objects[5]);
            map.put("PM2_5ys", objects[6]);
            map.put("PM2_5rs", objects[7]);
            map.put("AQId", objects[8]);
            map.put("AQIm", objects[9]);
            map.put("AQIy", objects[10]);
            map.put("AQIr", objects[11]);
            map.put("AQIms", objects[12]);
            map.put("AQIys", objects[13]);
            map.put("AQIrs", objects[14]);
            map.put("O3d", objects[15]);
            map.put("O3m", objects[16]);
            map.put("O3y", objects[17]);
            map.put("O3r", objects[18]);
            map.put("O3ms", objects[19]);
            map.put("O3ys", objects[20]);
            map.put("O3rs", objects[21]);
            rsMap.put(objects[0].toString(), map);
        }
        return rsMap;
    }
    private Map<String, Object> getMap1(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HashMap<String, Object> rsMap = new HashMap<>();
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 3; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            if (lastCellNum < 10) {
                continue;
            }
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("place", objects[0]);
            map.put("日期", objects[1]);
            map.put("SO2", objects[2]);
            map.put("SO2分指数", objects[3]);
            map.put("NO2", objects[4]);
            map.put("NO2分指数", objects[5]);
            map.put("PM10", objects[6]);
            map.put("PM10分指数", objects[7]);
            map.put("CO", objects[8]);
            map.put("CO分指数", objects[9]);
            map.put("O3_1h", objects[10]);
            map.put("O3一小时分指数", objects[11]);
            map.put("O3_8H", objects[12]);
            map.put("O3八小时分指数", objects[13]);
            map.put("PM2_5", objects[14]);
            map.put("PM2_5分指数", objects[15]);
            map.put("AQI", objects[16]);
            map.put("SU", objects[17]);
            map.put("空气质量级别", objects[18]);
            map.put("空气质量类别", objects[19]);
            rsMap.put(objects[0].toString(), map);
        }
        return rsMap;
    }
    private Map<String, Object> getMap2(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HashMap<String, Object> rsMap = new HashMap<>();
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 3; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            if (lastCellNum < 10) {
                continue;
            }
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("place", objects[0]);
            map.put("日期", objects[1]);
            map.put("SO2", objects[2]);
            map.put("SO2分指数", objects[3]);
            map.put("NO2", objects[4]);
            map.put("NO2分指数", objects[5]);
            map.put("PM10", objects[6]);
            map.put("PM10分指数", objects[7]);
            map.put("CO", objects[8]);
            map.put("CO分指数", objects[9]);
            map.put("O3_1h", objects[10]);
            map.put("O3一小时分指数", objects[11]);
            map.put("O3_8H", objects[12]);
            map.put("O3八小时分指数", objects[13]);
            map.put("PM2_5", objects[14]);
            map.put("PM2_5分指数", objects[15]);
            map.put("AQI", objects[16]);
            map.put("SU", objects[17]);
            map.put("空气质量级别", objects[18]);
            map.put("空气质量类别", objects[19]);
            rsMap.put(objects[0].toString(), map);
        }
        return rsMap;
    }
    private Map<String, Object> getMap3(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HashMap<String, List<Double>> rsMap = new HashMap<>();
        HashMap<String, Map<String, List<Double>>> maps = new HashMap<>();
        HashMap<String, List<Double>> map1 = new HashMap<>();
        HashMap<String, List<Double>> map2 = new HashMap<>();
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 3; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            if (lastCellNum < 10) {
                continue;
            }
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            //CO
            double CO=0.0;
            if (!(objects[8].toString()).equals("--")){
                CO = Double.parseDouble(objects[8].toString());
            }
            //o3
            double O3=0.0;
            if (!(objects[12].toString()).equals("--")){
               O3 = Double.parseDouble(objects[12].toString());
            }
            if (map1.get(objects[0].toString())==null){
                ArrayList<Double> doubles1 = new ArrayList<>();
                ArrayList<Double> doubles2 = new ArrayList<>();
                doubles1.add(CO);
                doubles2.add(O3);
                map1.put(objects[0].toString(),doubles1);
                map2.put(objects[0].toString(),doubles2);
            }else {
                List<Double> doubles = map1.get(objects[0].toString());
                doubles.add(CO);
                List<Double> lists = map2.get(objects[0].toString());
                lists.add(O3);
            }
            if (maps.get(objects[0].toString())==null){
                ArrayList<Double> list1 = new ArrayList<>();
                ArrayList<Double> list2 = new ArrayList<>();
                ArrayList<Double> list4 = new ArrayList<>();
                ArrayList<Double> list3 = new ArrayList<>();
                HashMap<String, List<Double>> ListHashMap = new HashMap<>();
                if (!(objects[2].toString()).equals("--")){
                    double SO21 = Double.parseDouble(objects[2].toString());
                    list1.add(SO21);
                }
                if (!(objects[4].toString()).equals("--")){
                    double NO21 = Double.parseDouble(objects[4].toString());
                    list2.add(NO21);
                }
                if (!(objects[6].toString()).equals("--")){
                  double  PM101 = Double.parseDouble(objects[6].toString());
                  list3.add(PM101);
                }
                if (!(objects[14].toString()).equals("--")){
                  double PM2_51 = Double.parseDouble(objects[14].toString());
                    list4.add(PM2_51);
               }
                ListHashMap.put("SO2",list1);
                ListHashMap.put("NO2",list2);
                ListHashMap.put("PM10",list3);
                ListHashMap.put("PM2_5",list4);
                maps.put(objects[0].toString(),ListHashMap);
            }else {
                Map<String, List<Double>> stringListMap = maps.get(objects[0].toString());
                List<Double> SO2List = stringListMap.get("SO2");
                if (!(objects[2].toString()).equals("--")){
                    double SO21 = Double.parseDouble(objects[2].toString());
                    SO2List.add(SO21);
                }
                List<Double> NO2List = stringListMap.get("NO2");
                if (!(objects[4].toString()).equals("--")){
                    double NO21 = Double.parseDouble(objects[4].toString());
                    NO2List.add(NO21);
                }
                List<Double> PM10List = stringListMap.get("PM10");
                if (!(objects[6].toString()).equals("--")){
                    double  PM101 = Double.parseDouble(objects[6].toString());
                    PM10List.add(PM101);
                }
                List<Double> PM2_5List = stringListMap.get("PM2_5");
                if (!(objects[14].toString()).equals("--")){
                    double PM2_51 = Double.parseDouble(objects[14].toString());
                    PM2_5List.add(PM2_51);
                }
            }
        }
        Set<String> strings2 = maps.keySet();
        for (String s : strings2) {
            Map<String, List<Double>> stringListMap = maps.get(s);
            ArrayList<Double> list = new ArrayList<>();
            Double so2 = getYz(stringListMap, "SO2");
            Double no2 = getYz(stringListMap, "NO2");
            Double pm10 = getYz(stringListMap, "PM10");
            Double pm2_5 = getYz(stringListMap, "PM2_5");
            list.add(so2);
            list.add(no2);
            list.add(pm10);
            list.add(pm2_5);
            rsMap.put(s,list);
        }
        HashMap<String, List<Double>> ListHashMap = new HashMap<>();
        Set<String> strings = rsMap.keySet();
        for (String string : strings) {
            List<Double> list = getList(rsMap, map1, map2, string);
            ListHashMap.put(string,list);
        }
        //计算aqi和首要污染物
        HashMap<String, Object> resultMap = new HashMap<>();
        Set<String> strings1 = ListHashMap.keySet();
        for (String s : strings1) {
            HashMap<String, Object> map = new HashMap<>();
            List<Double> doubles = ListHashMap.get(s);
            map.put("a21005",doubles.get(0));
            map.put("a21026",doubles.get(1));
            map.put("a21004",doubles.get(2));
            map.put("a34002",doubles.get(3));
            map.put("a34004",doubles.get(4));
            map.put("a05024",doubles.get(5));
            AQI aqi = AQIUtils.dailyAQI(map);
            Integer aqiValue = aqi.getAQIValue();
            map.put("AQI",aqiValue);
            List<String> primaryPollutantNames = aqi.getPrimaryPollutantNames();
            String primaryPollutant = "__";
            if (!ObjectUtils.isEmpty(primaryPollutantNames)) {
                primaryPollutant = primaryPollutantNames.stream().map(String::valueOf).collect(Collectors.joining(","));
            }
            map.put("SU", primaryPollutant);
            resultMap.put(s,map);
        }
        return resultMap;
    }
    /**
     * 计算平均值
     * @param stringListMap
     */
    private Double getYz(Map<String, List<Double>> stringListMap,String code) {
        List<Double> doubles = stringListMap.get(code);
        double v = new BigDecimal(doubles.stream().collect(Collectors.averagingDouble(Double::doubleValue))).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        return v;
    }
    /**
     * 计算月累计参数
     * @param rsMap
     * @param map1
     * @param map2
     * @param code
     * @return
     */
    private List<Double> getList(HashMap<String, List<Double>> rsMap, HashMap<String, List<Double>> map1, HashMap<String, List<Double>> map2,String code) {
        ArrayList<Double> list = new ArrayList<>();
        List<Double> doubles1 = map1.get(code);
        double percentiles1 = getPercentiles(doubles1, 0.95);
        double co = getDouble(percentiles1);
        list.add(co);
        List<Double> doubles2 = map2.get(code);
        double percentiles2 = getPercentiles(doubles2, 0.90);
        List<Double> doubles3 = rsMap.get(code);
        for (int i = 0; i < doubles3.size(); i++) {
           double anInt = getInt(doubles3.get(i));
            list.add(anInt);
        }
        double anInt = getInt(percentiles2);
        list.add(anInt);
        return list;
    }
    private Map<String, Object> getMap5(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HashMap<String, Object> rsMap = new HashMap<>();
        XSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 5; i <= lastRowNum; i++) {
            XSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            if (lastCellNum < 10) {
                continue;
            }
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("place", objects[0]);
            map.put("PM2_5d", objects[1]);
            map.put("PM2_5m", objects[2]);
            map.put("PM2_5y", objects[3]);
            map.put("PM2_5r", objects[4]);
            map.put("PM2_5ms", objects[5]);
            map.put("PM2_5ys", objects[6]);
            map.put("PM2_5rs", objects[7]);
            map.put("AQId", objects[8]);
            map.put("AQIm", objects[9]);
            map.put("AQIy", objects[10]);
            map.put("AQIr", objects[11]);
            map.put("AQIms", objects[12]);
            map.put("AQIys", objects[13]);
            map.put("AQIrs", objects[14]);
            map.put("O3d", objects[15]);
            map.put("O3m", objects[16]);
            map.put("O3y", objects[17]);
            map.put("O3r", objects[18]);
            map.put("O3ms", objects[19]);
            map.put("O3ys", objects[20]);
            map.put("O3rs", objects[21]);
            rsMap.put(objects[0].toString(), map);
        }
        return rsMap;
    }
    private Map<String, Object> getMap6(MultipartFile file) throws IOException {
//        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        HSSFWorkbook workbook= new HSSFWorkbook(file.getInputStream());
        HashMap<String, Object> rsMap = new HashMap<>();
//        XSSFSheet sheetAt = workbook.getSheetAt(0);
        HSSFSheet sheetAt = workbook.getSheetAt(0);
        //一共有多少行
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
//            XSSFRow row = sheetAt.getRow(i);
            HSSFRow row = sheetAt.getRow(i);
            if (row == null) {
                continue;
            }
            short lastCellNum = row.getLastCellNum();
            Object[] objects = new Object[lastCellNum];
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = ExcelUtils.getValue(cell);
                objects[j] = value;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("企业名称", objects[0]);
            map.put("设备名称", objects[1]);
            map.put("合计", objects[2]);
            map.put("油烟", objects[3]);
            map.put("颗粒物", objects[4]);
            map.put("设备离线", objects[5]);
            rsMap.put(objects[0].toString()+i, map);
        }
        return rsMap;
    }
    /**
     * excel计算公式
     * @param list
     * @param percentile
     * @return
     */
    private static double getPercentiles(List<Double> list, double percentile) {
        Collections.sort(list);
        double x = (list.size()-1) * percentile;
        int i = (int) x;
        double j = x - i;
        return (1 - j) * list.get(i) + j * list.get(i+1);
    }
    private  static double  getDouble(Double  code1){
        double floor1 = Math.floor(code1 * 10);
        //保留一位小数四舍五入
        double result = new BigDecimal(code1).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (code1*100-floor1*10==5 && (result*10) % 2 !=0){
            result= result-0.1;
        }else {
            result = result-0;
        }
        return result;
    }
    private  static double  getInt(Double  code){
        //向下取整数
        double floor = Math.floor(code);
        //四舍五入
        double round = Math.round(code);
        if (code*10-(floor*10)==5  && round % 2 !=0){
            round=round-1;
        }else {
            round=round-0;
        }
        return  round;
    }
}
screen-api/src/main/java/com/moral/api/utils/ExcelUtils.java
New file
@@ -0,0 +1,38 @@
package com.moral.api.utils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
public class ExcelUtils {
    public static Object getValue(Cell cell) {
        Object value = null;
        if (cell == null) {
            return null;
        }
        switch (cell.getCellType()){
            case STRING:
                // 字符串类型
                value = cell.getStringCellValue();
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)){
                value = cell.getDateCellValue();
                }else {
                 // double类型
                 value = cell.getNumericCellValue();
                }
                break;
            case BOOLEAN:
                // boolean类型
                value = cell.getBooleanCellValue();
                break;
            case FORMULA:
                value = cell.getCellFormula();
                break;
            default:
                break;
        }
        return value;
    }
}