cjl
2023-08-02 ee2d9a4049baa9ecc3f43d0576fd3745688171be
Merge remote-tracking branch 'origin/cjl' into qa
7 files added
4 files modified
516 ■■■■■ changed files
screen-api/pom.xml 6 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/SysTestController.java 345 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/entity/SysTest.java 52 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/mapper/SysTestMapper.java 16 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/SysTestService.java 19 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/SysTestServiceImpl.java 50 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/application-qa.yml 2 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/mapper/SysTestMapper.xml 19 ●●●●● patch | view | raw | blame | history
screen-api/src/main/resources/word/空气质量日报.docx patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/entity/Device.java 6 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/resources/mapper/DeviceMapper.xml 1 ●●●● patch | view | raw | blame | history
screen-api/pom.xml
@@ -47,7 +47,11 @@
            <artifactId>fastjson</artifactId>
            <version>1.2.66</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>
    <build>
screen-api/src/main/java/com/moral/api/controller/SysTestController.java
New file
@@ -0,0 +1,345 @@
package com.moral.api.controller;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.moral.api.entity.SysTest;
import com.moral.api.service.SysTestService;
import com.moral.util.DateUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @ClassName SysTestController
 * @Description TODO
 * @Author @lizijie
 * @Date 2023-08-01 13:45
 * @Version 1.0
 */
@Slf4j
@Api(tags = {"测试补偿数据导出"})
@RestController
@RequestMapping("/systest")
public class SysTestController {
    @Autowired
    private SysTestService sysTestService;
    @GetMapping("resultWord")
    public void adresultWordd(HttpServletResponse response,String time) {
        List<SysTest> listAll = sysTestService.listAll(time,"2022-06-22",null);
        if(CollectionUtils.isEmpty(listAll)){
            return;
        }
        Date endTime =listAll.get(0).getTime();
        String endTimeStr = DateUtils.dateToDateString(endTime,DateUtils.yyyyMMdd_EN);
        String  fileName = "空气质量日报.docx";
        Map<String,Object> map = resultMap(listAll);
        //实体转map
       // Map result = JSON.parseObject(JSON.toJSONString(entity), Map.class);
        //        Map<String, Object> result = EasyPoiUtil.entityToMap(entity);
        downloadWord(fileName, map, response,"空气质量日报"+endTimeStr+".docx");
    }
    public static void downloadWord(String fileName, Map<String, Object> params, HttpServletResponse response,String newFileName) {
        String path = getPath(fileName);//我这放 resources包下
        try {
            //获取模板文档
            File rootFile = new File(ResourceUtils.getURL("classpath:").getPath());
            File file= new File(rootFile, path);
            XWPFDocument word = WordExportUtil.exportWord07(file.getPath(), params);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newFileName, "utf-8"));
            OutputStream out = response.getOutputStream();
            word.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error ("导出失败,请联系网站管理员!", e);
        }
    }
    public static String getPath(String filename) {
        filename = "/word/" + filename;
        return filename;
    }
    /**
     实体类转Map
     */
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap<>();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
    private Map<String,Object> resultMap(List<SysTest> listAll){
        String first = "2020-06-22";
        String firstYear = "2020";
        Map<String,Object> resultMap = new HashMap<>();
        Date endTime =listAll.get(0).getTime();
        String endTimeStr = DateUtils.dateToDateString(endTime,DateUtils.yyyy_MM_dd_EN);
        List<String> endTimeList= Arrays.asList(endTimeStr.split("-"));
        if(Integer.parseInt(firstYear)<Integer.parseInt(endTimeList.get(0))){
            firstYear = endTimeList.get(0);
            first = endTimeList.get(0)+"-01-01";
        }
        String firstM = String.valueOf(Integer.parseInt(endTimeList.get(1)));
        String firstD = String.valueOf(Integer.parseInt(endTimeList.get(2)));
        Long day = DateUtils.getQuotByDays(first,endTimeStr)+1  ;
        resultMap.put("年",firstYear);
        resultMap.put("月",firstM);
        resultMap.put("日",firstD);
        resultMap.put("期",day.intValue());
        String name = "沙";
        for(SysTest s : listAll){
            if(s.getName().contains("友")){
                name = "友";
            }else if(s.getName().contains("中")){
                name = "中";
            }else {
                name = "沙";
            }
            resultMap.put(name+"AQI", StringUtils.isNotEmpty(s.getAqi())?s.getAqi():"--");
            resultMap.put(name+"PM10", StringUtils.isNotEmpty(s.getPm10())?s.getPm10():"--");
            resultMap.put(name+"PM2", StringUtils.isNotEmpty(s.getPm25())?s.getPm25():"--");
            resultMap.put(name+"SO2", StringUtils.isNotEmpty(s.getSo2())?s.getSo2():"--");
            resultMap.put(name+"NO2", StringUtils.isNotEmpty(s.getNo2())?s.getNo2():"--");
            resultMap.put(name+"CO", StringUtils.isNotEmpty(s.getCo())?s.getCo():"--");
            resultMap.put(name+"O3", StringUtils.isNotEmpty(s.getO3())?s.getO3():"--");
            resultMap.put(name+"TV", StringUtils.isNotEmpty(s.getVoc())?s.getVoc():"--");
            resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--");
        }
        String bodyName = stringButterStr(listAll);
        resultMap.put("主要内容",bodyName);
        return resultMap;
    }
    private String stringButterStr(List<SysTest> listAll){
        // 0 一样1 两高 2两低,3高,低
        int type = 0;
        // TODO: 2023-08-01 第一次循环比较最高值或者最低值
        int aqiHigh = Integer.parseInt(listAll.get(0).getAqi());
        int aqiMiddle = Integer.parseInt(listAll.get(1).getAqi());
        int aqiLow = Integer.parseInt(listAll.get(2).getAqi());
        List<Integer> numList = Arrays.asList(aqiHigh,aqiMiddle,aqiLow);
        StringBuffer stringBuffer = new StringBuffer();
        Set<Integer> stringSet = new HashSet<>(numList);
        List<Integer> listTest = stringSet.stream().collect(Collectors.toList());
        Collections.sort(listTest);
        int aqiHighResult = listTest.get(listTest.size()-1);
        int aqiLowResult = listTest.get(0);
        if(stringSet.size() == 1){
            type = 0;
            stringBuffer.append("整体空气质量(AQI)基本相同,无太大区别。");
            return stringBuffer.toString();
        }else if(stringSet.size() == 3){
            type = 3;
        }else {
            int count = Collections.frequency(numList, listTest.get(0));
            if(count == 1){
                type = 2;
            }else {
                type = 1;
            }
        }
        int sizeHighResult = 0;
        int sizeLowResult = 0;
        String highName = "沙岗村";
        String lowName = "沙岗村";
        //1 两高 2两低
        if(type == 1){
            SysTest ResultAqi = listAll.stream().min(Comparator.comparing(SysTest::getAqi)).get();
            if(ResultAqi.getName().contains("沙岗村")){
                lowName = "沙岗村";
                highName = "友爱村、中间村";
            }else if(ResultAqi.getName().contains("友爱村")){
                lowName = "友爱村";
                highName = "沙岗村、中间村";
            } else if(ResultAqi.getName().contains("中间村")){
                lowName = "中间村";
                highName = "沙岗村、友爱村";
            }
        }else if(type == 2){
            SysTest ResultAqi = listAll.stream().max(Comparator.comparing(SysTest::getAqi)).get();
            if(ResultAqi.getName().contains("沙岗村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            }else if(ResultAqi.getName().contains("友爱村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            } else if(ResultAqi.getName().contains("中间村")){
                highName = "沙岗村";
                lowName = "友爱村、中间村";
            }
        }else if(type == 3){
            if(aqiHigh == aqiHighResult){
                sizeHighResult = 0;
            }
            if(aqiMiddle == aqiHighResult){
                sizeHighResult = 1;
            }
            if(aqiLow == aqiHighResult){
                sizeHighResult = 2;
            }
            if(aqiHigh == aqiLowResult){
                sizeLowResult = 0;
            }
            if(aqiMiddle == aqiLowResult){
                sizeLowResult = 1;
            }
            if(aqiLow == aqiLowResult){
                sizeLowResult = 2;
            }
            SysTest HighResult = listAll.get(sizeHighResult);
            SysTest LowResult = listAll.get(sizeLowResult);
            if(HighResult.getName().contains("友爱村")){
                highName = "友爱村";
            }else if(HighResult.getName().contains("中尖村")){
                highName = "中尖村";
            }
            if(LowResult.getName().contains("友爱村")){
                lowName = "友爱村";
            }else if(LowResult.getName().contains("中尖村")){
                lowName = "中尖村";
            }
        }
        SysTest result0 = listAll.get(0);
        SysTest result1 = listAll.get(1);
        SysTest result2 = listAll.get(2);
        Map<String,String> pm10 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm10(),result1.getPm10(),result2.getPm10());
        Map<String,String> pm25 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getPm25(),result1.getPm25(),result2.getPm25());
        Map<String,String> so = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getSo2(),result1.getSo2(),result2.getSo2());
        Map<String,String> no = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getNo2(),result1.getNo2(),result2.getNo2());
        Map<String,String> co = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getCo(),result1.getCo(),result2.getCo());
        Map<String,String> o3 = nameCode(result0.getName(),result1.getName(),result2.getName(),result0.getO3(),result1.getO3(),result2.getO3());
        stringBuffer.append(highName+"整体空气质量(AQI)较高,"+lowName+"整体空气质量(AQI)较低。");
        if(pm10.isEmpty()&& pm25.isEmpty()&&so.isEmpty()&& no.isEmpty()&& co.isEmpty()&& o3.isEmpty()){
            return stringBuffer.toString();
        }
        stringBuffer.append("其中:");
        if(!pm10.isEmpty()){
            stringBuffer.append("PM10较高的是:"+pm10.get("high")+";较低的是:"+pm10.get("low")+"。");
        }
        if(!pm25.isEmpty()){
            stringBuffer.append("PM2.5较高的是:"+pm25.get("high")+";较低的是:"+pm25.get("low")+"。");
        }
        if(!so.isEmpty()){
            stringBuffer.append("SO2较高的是:"+so.get("high")+";较低的是:"+so.get("low")+"。");
        }
        if(!no.isEmpty()){
            stringBuffer.append("NO2较高的是:"+no.get("high")+";较低的是:"+no.get("low")+"。");
        }
        if(!co.isEmpty()){
            stringBuffer.append("CO较高的是:"+co.get("high")+";较低的是:"+co.get("low")+"。");
        }
        if(!o3.isEmpty()){
            stringBuffer.append("O3较高的是:"+o3.get("high")+";较低的是:"+o3.get("low")+"。");
        }
        return stringBuffer.toString();
    }
    private Map<String,String> nameCode(String name1,String name2,String name3,String nums1,String nums2,String nums3){
        double num1 = StringUtils.isNotEmpty(nums1)?Double.parseDouble(nums1):0;
        double num2 = StringUtils.isNotEmpty(nums2)?Double.parseDouble(nums2):0;
        double num3 = StringUtils.isNotEmpty(nums3)?Double.parseDouble(nums3):0;
        Map<String,String> map = new HashMap<>();
        if(num1 ==num2 && num2 ==num3){
            return map;
        }
        name1 = name1.substring(3,name1.length());
        name2 = name2.substring(3,name2.length());
        name3 = name3.substring(3,name3.length());
        if(num1 ==num2 && num2 < num3){
            map.put("high",name3);
            map.put("low",name1+"、"+name2);
            return map;
        }else if(num1 ==num2 && num2 > num3){
            map.put("low",name3);
            map.put("high",name1+"、"+name2);
            return map;
        }
        if(num1 == num3 && num2 < num3){
            map.put("low",name2);
            map.put("high",name1+"、"+name3);
            return map;
        }else if(num1 == num3 && num2 > num3){
            map.put("high",name2);
            map.put("low",name1+"、"+name3);
            return map;
        }
        if(num2 == num3 && num1 < num2){
            map.put("low",name1);
            map.put("high",name2+"、"+name3);
            return map;
        }else if(num2 == num3 && num1 > num2){
            map.put("high",name1);
            map.put("low",name2+"、"+name3);
            return map;
        }
        List<Double> numList = Arrays.asList(num1,num2,num3);
        int high = numList.indexOf(Collections.max(numList));
        int low = numList.indexOf(Collections.min(numList));
        if(high == 0){
            map.put("high",name1);
        }else if(high == 1){
            map.put("high",name2);
        }else if(high == 2){
            map.put("high",name3);
        }
        if(low == 0){
            map.put("low",name1);
        }else if(low == 1){
            map.put("low",name2);
        }else if(low == 2){
            map.put("low",name3);
        }
        return map;
    }
}
screen-api/src/main/java/com/moral/api/entity/SysTest.java
New file
@@ -0,0 +1,52 @@
package com.moral.api.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.time.LocalDateTime;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
 * <p>
 *
 * </p>
 *
 * @author moral
 * @since 2023-08-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SysTest extends Model<SysTest> {
    private static final long serialVersionUID = 1L;
    private String mac;
    private Date time;
    private String pm25;
    private String pm10;
    private String so2;
    private String no2;
    private String co;
    private String o3;
    private String voc;
    private String name;
    @TableField(exist = false)
    private String aqi;
    @TableField(exist = false)
    private String su;
}
screen-api/src/main/java/com/moral/api/mapper/SysTestMapper.java
New file
@@ -0,0 +1,16 @@
package com.moral.api.mapper;
import com.moral.api.entity.SysTest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author moral
 * @since 2023-08-01
 */
public interface SysTestMapper extends BaseMapper<SysTest> {
}
screen-api/src/main/java/com/moral/api/service/SysTestService.java
New file
@@ -0,0 +1,19 @@
package com.moral.api.service;
import com.moral.api.entity.SysTest;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
 * <p>
 *  服务类
 * </p>
 *
 * @author moral
 * @since 2023-08-01
 */
public interface SysTestService extends IService<SysTest> {
    List<SysTest> listAll(String startTime,String endTime ,String mac);
}
screen-api/src/main/java/com/moral/api/service/impl/SysTestServiceImpl.java
New file
@@ -0,0 +1,50 @@
package com.moral.api.service.impl;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.SysTest;
import com.moral.api.mapper.SysTestMapper;
import com.moral.api.service.SysTestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.utils.AqiUtils;
import com.moral.util.DateUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2023-08-01
 */
@Service
public class SysTestServiceImpl extends ServiceImpl<SysTestMapper, SysTest> implements SysTestService {
    @Override
    public List<SysTest> listAll(String startTime,String endTime, String mac) {
        QueryWrapper<SysTest> queryWrapper = new QueryWrapper<>();
       // queryWrapper.between(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime),"time",startTime,endTime);
        queryWrapper.eq(!StringUtils.isEmpty(startTime),"time", DateUtils.convertDate(startTime));
        queryWrapper.eq(!StringUtils.isEmpty(mac),"mac",mac);
        queryWrapper.orderByAsc("time","mac");
        List<SysTest> listAll = this.baseMapper.selectList(queryWrapper);
        for(SysTest s : listAll){
            Map<String,Object> map = new HashMap<>();
            map.put("PM2_5",s.getPm25());
            map.put("PM10",s.getPm10());
            map.put("CO",s.getCo());
            map.put("SO2",s.getSo2());
            map.put("NO2",s.getNo2());
            map.put("O3",s.getO3());
            Map<String,Object> getAqi = AqiUtils.getAqi(map);
            s.setSu(getAqi.get("SU").toString());
            s.setAqi(getAqi.get("AQI").toString());
        }
        return listAll;
    }
}
screen-api/src/main/resources/application-qa.yml
@@ -18,6 +18,8 @@
    include: bulletin, specialCity, announcement
  application:
    name: screen-api
  main:
    allow-bean-definition-overriding: true
  redis:
    host: r-bp1hez4g7rkqjswpaypd1.redis.rds.aliyuncs.com
    port: 6379
screen-api/src/main/resources/mapper/SysTestMapper.xml
New file
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.api.mapper.SysTestMapper">
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.moral.api.entity.SysTest">
                    <result column="mac" property="mac" />
                    <result column="time" property="time" />
                    <result column="pm25" property="pm25" />
                    <result column="pm10" property="pm10" />
                    <result column="so2" property="so2" />
                    <result column="no2" property="no2" />
                    <result column="co" property="co" />
                    <result column="o3" property="o3" />
                    <result column="voc" property="voc" />
                    <result column="name" property="name" />
        </resultMap>
</mapper>
screen-api/src/main/resources/word/??????.docx
Binary files differ
screen-manage/src/main/java/com/moral/api/entity/Device.java
@@ -145,6 +145,8 @@
    * */
    @TableField(exist = false)
    private Version version;
    /*
     *序号
     * */
    private Integer devNum;
}
screen-manage/src/main/resources/mapper/DeviceMapper.xml
@@ -26,6 +26,7 @@
        <result column="is_delete" property="isDelete"/>
        <result column="extend" property="extend"/>
        <result column="town_code" property="townCode"/>
        <result column="dev_num" property="devNum"/>
    </resultMap>
    <resultMap id="resultMap" type="com.moral.api.pojo.vo.device.DeviceVO" extends="BaseResultMap">