Jelajahi Sumber

1.异常描述查询改为按照更新时间倒叙排序
2.根据请求适配分析结果文件的内外网

chenhongyan1989 11 bulan lalu
induk
melakukan
445edb6ded

+ 9 - 2
energy-manage-service/src/main/java/com/energy/manage/service/controller/analysis/AnalysisController.java

@@ -9,12 +9,16 @@ import com.energy.manage.service.domain.dto.analysis.AnalysisDto;
 import com.energy.manage.service.domain.dto.analysis.AnalysisResultDto;
 import com.energy.manage.service.domain.vo.analysis.AnalysisResultVo;
 import com.energy.manage.service.service.analysis.AnalysisService;
+import com.energy.manage.service.util.IPUtils;
+import com.energy.manage.service.util.NetUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * 分析
  * @author chy
@@ -63,9 +67,12 @@ public class AnalysisController extends BaseServiceController {
     @PostMapping("/analysisDetail")
     public ResultResp analysisDetail(@RequestParam("batchCode") String batchCode,
                                      @RequestParam("analysisTypeCode") String analysisTypeCode,
-                                     @RequestParam("fieldEngineCode") String fieldEngineCode)
+                                     @RequestParam("fieldEngineCode") String fieldEngineCode,
+                                     HttpServletRequest request)
     {
-        return success(analysisService.analysisDetail(batchCode,analysisTypeCode,fieldEngineCode));
+        //判断是否是内网
+        boolean isIntranet = NetUtils.isIntranet(IPUtils.getIpAddr(request));
+        return success(analysisService.analysisDetail(batchCode,analysisTypeCode,fieldEngineCode,isIntranet));
     }
 
 }

+ 3 - 0
energy-manage-service/src/main/java/com/energy/manage/service/controller/windfieldbatch/WindFieldBatchController.java

@@ -9,12 +9,15 @@ import com.energy.manage.service.domain.dto.windfieldbatch.WindFieldBatchDto;
 import com.energy.manage.service.domain.dto.windfieldbatch.WindFileBatchPageDto;
 import com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo;
 import com.energy.manage.service.service.windfieldbatch.WindFieldBatchService;
+import com.energy.manage.service.util.IPUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * @author chy
  * @date 2024/5/20 17:32

+ 12 - 0
energy-manage-service/src/main/java/com/energy/manage/service/property/analysis/AlgorithmProperties.java

@@ -67,4 +67,16 @@ public class AlgorithmProperties {
     @Value("${analysis.array.properties}")
     public List<String> analysisArrayProperties;
 
+    /**
+     * 分析文件内网地址
+     */
+    @Value("${analysis.file.intranet.addr}")
+    public String analysisFileIntranetAddr;
+
+    /**
+     * 分析文件外网地址
+     */
+    @Value("${analysis.file.external.addr}")
+    public String analysisFileExternalAddr;
+
 }

+ 2 - 1
energy-manage-service/src/main/java/com/energy/manage/service/service/analysis/AnalysisService.java

@@ -45,9 +45,10 @@ public interface AnalysisService {
      * @param batchCode
      * @param analysisTypeCode
      * @param fieldEngineCode
+     * @param isIntranet
      * @return
      */
-    List<AnalysisDetailVo> analysisDetail(String batchCode, String analysisTypeCode, String fieldEngineCode);
+    List<AnalysisDetailVo> analysisDetail(String batchCode, String analysisTypeCode, String fieldEngineCode, boolean isIntranet);
 
 
     /**

+ 63 - 30
energy-manage-service/src/main/java/com/energy/manage/service/service/analysis/impl/AnalysisServiceImpl.java

@@ -18,6 +18,7 @@ import com.energy.manage.service.mappers.windfieldbatch.WindFieldBatchMapper;
 import com.energy.manage.service.property.analysis.AlgorithmProperties;
 import com.energy.manage.service.service.analysis.AnalysisService;
 import com.energy.manage.service.util.BeanMapUtils;
+import com.energy.manage.service.util.NetUtils;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -32,8 +33,12 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -85,6 +90,12 @@ public class AnalysisServiceImpl implements AnalysisService {
      */
     private static final String OPTION_CONTENT = "手动分析";
 
+    /**
+     * 分析文件前缀
+     */
+    private static final String FILE_ADDR_FREFIX = "http://";
+
+
     private static ObjectMapper mapper = new ObjectMapper();
 
     /**
@@ -154,7 +165,7 @@ public class AnalysisServiceImpl implements AnalysisService {
      * @return
      */
     @Override
-    public List<AnalysisDetailVo> analysisDetail(String batchCode, String analysisTypeCode, String fieldEngineCode) {
+    public List<AnalysisDetailVo> analysisDetail(String batchCode, String analysisTypeCode, String fieldEngineCode, boolean isIntranet) {
         List<AnalysisDetailVo> analysisDetailVos = new ArrayList<>();
         //获取所有的分析类型
         List<AnalysisTypeVo> analysisTypeVos = analysisTypeMapper.selectByTypeCode(analysisTypeCode);
@@ -162,9 +173,9 @@ public class AnalysisServiceImpl implements AnalysisService {
             return analysisDetailVos;
         }
         //最新总文件
-        List<AnalysisGeneralFileVo> newGeneralFileVos = getNewGeneralFiles(batchCode,analysisTypeCode);
+        List<AnalysisGeneralFileVo> newGeneralFileVos = getNewGeneralFiles(batchCode,analysisTypeCode,isIntranet);
         //获取最新分文件
-        List<AnalysisDiagramRelationVo> newDiagramRelationVos = getNewDiagramRelations(batchCode, analysisTypeCode, fieldEngineCode);
+        List<AnalysisDiagramRelationVo> newDiagramRelationVos = getNewDiagramRelations(batchCode, analysisTypeCode, fieldEngineCode,isIntranet);
         //查询评论
         List<AnalysisCommentDescriptionVo> commentDescriptionVos = commentDescriptionMapper.selecByBatchCodeAndAnalysisTypeCode(batchCode,analysisTypeCode);
         //根据分析类型设置分析结果
@@ -228,8 +239,12 @@ public class AnalysisServiceImpl implements AnalysisService {
      * @param analysisTypeCode
      * @return
      */
-    private List<AnalysisGeneralFileVo> getNewGeneralFiles(String batchCode,String analysisTypeCode){
+    private List<AnalysisGeneralFileVo> getNewGeneralFiles(String batchCode,String analysisTypeCode, boolean isIntranet){
+        //从数据库获取当前批次、当前分析类型的所有的分析总文件
         List<AnalysisGeneralFileVo> generalFileVos = generalFileMapper.selectByCondition(batchCode,analysisTypeCode);
+        //为每个文件赋值前缀
+        generalFileVos.forEach(item -> item.setFileAddr(getFilePath(isIntranet,item.getFileAddr())));
+        //根据创建时间排序
         generalFileVos.sort(Comparator.comparing(AnalysisGeneralFileVo::getCreateTime,Comparator.reverseOrder()));
         //删除地址相同的文件
         return generalFileVos.stream().collect(Collectors.collectingAndThen(
@@ -245,11 +260,13 @@ public class AnalysisServiceImpl implements AnalysisService {
      * @param fieldEngineCode
      * @return
      */
-    private List<AnalysisDiagramRelationVo> getNewDiagramRelations(String batchCode,String analysisTypeCode,String fieldEngineCode){
+    private List<AnalysisDiagramRelationVo> getNewDiagramRelations(String batchCode,String analysisTypeCode,String fieldEngineCode, boolean intranet){
+        //获取当前批次、当前分析类型的所有分析结果文件
         List<AnalysisDiagramRelationVo> diagramRelationVos = diagramRelationMapper.selectByCondition(batchCode,analysisTypeCode,fieldEngineCode);
         if(CollectionUtils.isEmpty(diagramRelationVos)){
             return diagramRelationVos;
         }
+        diagramRelationVos.forEach(item -> item.setFileAddr(getFilePath(intranet,item.getFileAddr())));
         //如果风机编号不为空的话则不对风机编号进行排序
         if (StringUtils.isNotBlank(fieldEngineCode)) {
             diagramRelationVos.sort(Comparator.comparing(AnalysisDiagramRelationVo::getCreateTime, Comparator.reverseOrder()));
@@ -329,31 +346,6 @@ public class AnalysisServiceImpl implements AnalysisService {
         });
     }
 
-    /**
-     * 测试排序去重
-     * @param args
-     * @throws InterruptedException
-     */
-    public static void main(String[] args) throws InterruptedException {
-        List<AnalysisGeneralFileVo> generalFileVos = new ArrayList<>();
-        AnalysisGeneralFileVo a1 = new AnalysisGeneralFileVo();
-        a1.setFileAddr("http://123/412/89.html");
-        a1.setCreateTime(new Date());
-        a1.setBatchCode("123");
-        generalFileVos.add(a1);
-        Thread.sleep(1000);
-        AnalysisGeneralFileVo a2 = new AnalysisGeneralFileVo();
-        a2.setFileAddr("http://123/412/89.html");
-        a2.setCreateTime(new Date());
-        generalFileVos.add(a2);
-        a2.setBatchCode("453");
-        generalFileVos.sort(Comparator.comparing(AnalysisGeneralFileVo::getCreateTime,Comparator.reverseOrder()));
-        //删除地址相同的文件
-        List<AnalysisGeneralFileVo> returnList =  generalFileVos.stream().collect(Collectors.collectingAndThen(
-                                                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AnalysisGeneralFileVo::getFileAddr))),
-                                                    ArrayList :: new));
-        returnList.forEach(System.out::println);
-    }
 
     /**
      * 根据属性修改json值
@@ -505,4 +497,45 @@ public class AnalysisServiceImpl implements AnalysisService {
         AnalysisServiceImpl.windFieldBatchMapper = windFieldBatchMapper;
     }
 
+
+    /**
+     * 获取分析结果文件地址前缀并替换或添加
+     * @param isIntranet
+     * @param fileAddr
+     * @return
+     */
+    private String getFilePath(boolean isIntranet,String fileAddr){
+        String minIoIpPort = isIntranet ? algorithmProperties.getAnalysisFileIntranetAddr() : algorithmProperties.getAnalysisFileExternalAddr();
+        if(StringUtils.startsWith(fileAddr,FILE_ADDR_FREFIX)){
+            return fileAddr.replace(NetUtils.getIpAndPort(fileAddr),minIoIpPort);
+        }
+        return minIoIpPort + fileAddr;
+    }
+
+    /**
+     * 测试排序去重
+     * @param args
+     * @throws InterruptedException
+     */
+    public static void main(String[] args) throws InterruptedException, URISyntaxException {
+        List<AnalysisGeneralFileVo> generalFileVos = new ArrayList<>();
+        AnalysisGeneralFileVo a1 = new AnalysisGeneralFileVo();
+        a1.setFileAddr("http://123/412/89.html");
+        a1.setCreateTime(new Date());
+        a1.setBatchCode("123");
+        generalFileVos.add(a1);
+        Thread.sleep(1000);
+        AnalysisGeneralFileVo a2 = new AnalysisGeneralFileVo();
+        a2.setFileAddr("http://123/412/89.html");
+        a2.setCreateTime(new Date());
+        generalFileVos.add(a2);
+        a2.setBatchCode("453");
+        generalFileVos.sort(Comparator.comparing(AnalysisGeneralFileVo::getCreateTime,Comparator.reverseOrder()));
+        //删除地址相同的文件
+        List<AnalysisGeneralFileVo> returnList =  generalFileVos.stream().collect(Collectors.collectingAndThen(
+                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AnalysisGeneralFileVo::getFileAddr))),
+                ArrayList :: new));
+        returnList.forEach(System.out::println);
+    }
+
 }

+ 8 - 0
energy-manage-service/src/main/java/com/energy/manage/service/test/TestController.java

@@ -11,6 +11,7 @@ import com.energy.manage.service.mappers.analysis.AnalysisTypeMapper;
 import com.energy.manage.service.mappers.datatransfer.DataTransferMapper;
 import com.energy.manage.service.property.analysis.AlgorithmProperties;
 import com.energy.manage.service.service.analysis.AnalysisService;
+import com.energy.manage.service.util.IPUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -74,6 +76,12 @@ public class TestController extends BaseServiceController {
         return success(downloadUrl);
     }
 
+    @UserLoginToken
+    @PostMapping("/testIpUtils")
+    public ResultResp testIpUtils(HttpServletRequest request) {
+        return success(IPUtils.getIpAddr(request));
+    }
+
     /**
      * 设置分析参数
      * @param dataTransferVo

+ 3 - 0
energy-manage-service/src/main/java/com/energy/manage/service/util/IPUtils.java

@@ -36,6 +36,9 @@ public class IPUtils {
                 ip = request.getHeader("x-forwarded-for");
             }
             if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
+                ip = request.getHeader("X-Real-IP");
+            }
+            if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
                 ip = request.getHeader("Proxy-Client-IP");
             }
             if (StringUtils.isEmpty(ip) || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {

+ 52 - 0
energy-manage-service/src/main/java/com/energy/manage/service/util/NetUtils.java

@@ -0,0 +1,52 @@
+package com.energy.manage.service.util;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author chy
+ * @date 2024/7/9 14:08
+ * @desc
+ */
+public class NetUtils {
+
+
+    /**
+     * 内网前缀
+     */
+    private final static String INTRANET_PREFIX = "192.168";
+
+    /**
+     * ip:port 正则
+     */
+    private final static String IP_PORT_REGEX = "^(http[s]?://)?([^:/\\s]+)(:)+[0-9]{1,}/";
+
+
+    /**
+     * 判断当前host 是否是内网
+     * @param ip
+     * @return
+     */
+    public static boolean isIntranet(String ip) {
+        if(StringUtils.isBlank(ip)){
+            return false;
+        }
+        return ip.startsWith(INTRANET_PREFIX);
+    }
+
+    /**
+     * 根据url获取IP地址与端口号
+     * @param url
+     * @return
+     */
+    public static String getIpAndPort(String url){
+        if(StringUtils.isBlank(url)){
+            return null;
+        }
+        Pattern p = Pattern.compile(IP_PORT_REGEX);
+        Matcher matcher = p.matcher(url);
+        return matcher.find() ? matcher.group() : null ;
+    }
+}

+ 1 - 1
energy-manage-service/src/main/resources/mybatis/windexceptioncount/WindExceptionCountMapper.xml

@@ -46,7 +46,7 @@
         <if test="analysisTypeCode != null and analysisTypeCode != ''">
             and count.analysis_type_code = #{analysisTypeCode}
         </if>
-        order by count.create_time desc
+        order by count.update_time desc
     </select>
 
     <select id="selectByfieldCodes" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.windexceptioncount.WindExceptionCountVo">