Bläddra i källkod

算法服务日志更新

linfutong 2 år sedan
förälder
incheckning
32c72a6f27

+ 95 - 36
zd-modules/zd-algorithm/src/main/java/com/zd/alg/forward/serivce/CheckService.java

@@ -129,29 +129,30 @@ public class CheckService {
                 AlgorithmYml.CheckValid checkValid = algorithmYml.getCheckValid(Integer.valueOf(code));
                 MultiValueMap<String, Object> params = getStringObjectMultiValueMap(checkValid, String.valueOf(id));
                 HttpEntity<MultiValueMap<String, Object>> files = getHttpEntityMap(toFile, params);
-                //TODO 添加一条请求记录到数据库,
-                Algorithm algorithmFirst = new Algorithm();
-                R<Algorithm> alg= laboratoryService.saveAlgorithmData(algorithmFirst);
-                logger.info("===============算法数据添加结果,id="+alg.getData());
-                logger.info("===============4=================");
+                //添加一条算法请求记录
+                R<Long> alg = insertRequestRecordLog(file, subId, checkValid);
+                logger.info("==============>>>算法数据添加结果,alg="+ JSONObject.toJSONString(alg));
                 ImgPostResponse<AnalysisReturnData> send = HttpUtils.sendV5(restTemplateLocal, files, algorithmYml);
+                //算法记录更新
+                if (alg.getCode() == 200) {
+                    try {
+                        updateRequestRecordLog(alg.getData() != null?alg.getData():null, send);
+                    } catch (Exception e) {
+                        logger.error("【算法服务】 更新日志异常,异常信息",e);
+                    }
+                }
+                //判断算法
                 if (send == null || send.getStatus_code() != 1000) {
-                    //算法记录更新
-                    algorithmFirst.setRespCode(send.getStatus_code());
-                    algorithmFirst.setId(Long.valueOf(alg.getData().toString()));
-                    laboratoryService.update(algorithmFirst);
-                    logger.info("===============算法请求异常,更新数据,id="+alg.getData().getId());
                     assert send != null;
-                    logger.error("==================4.1==================="+send.getStatus_code()+"==========="+send.getMessage());
+                    logger.error("【算法服务】 请求为成功,响应信息:"+ JSONObject.toJSONString(send));
                     return R.fail("算法服务错误,请重试!");
                 }
-                logger.info("===============5=================");
-                R algorithm = saveAlgorithmV5(Long.valueOf(alg.getData().toString()),file,send, checkValid);
-                logger.info("===============算法请求正常,更新数据"+algorithm.getData());
+                //logger.info("===============5=================");
+                /*R algorithm = saveAlgorithmV5(Long.valueOf(alg.getData().toString()),file,send, checkValid);
                 if (algorithm.getCode() != 200) {
                     logger.error("==============="+algorithm.getCode()+"=================");
                     return algorithm;
-                }
+                }*/
                 logger.info("===============6=================");
                 AnalysisReturnData data = send.getData();
                 Map<String, Object> result = (Map<String, Object>) data.getResult();
@@ -186,13 +187,86 @@ public class CheckService {
                 return R.ok();
             }
         } catch (Exception e) {
-            logger.error("e.getMessage:"+e.getMessage());
-            e.printStackTrace();
+            logger.error("【算法服务异常】异常信息",e);
+            //e.printStackTrace();
         }
         return R.fail();
     }
 
     /**
+     * 添加日志记录
+     * @param file
+     * @param subId
+     * @param checkValid
+     * @return
+     */
+    private R<Long> insertRequestRecordLog(MultipartFile file, Long subId, AlgorithmYml.CheckValid checkValid) {
+        Algorithm algorithm = new Algorithm();
+        String img = "";
+        try {
+            R<SysFile> originalFile = remoteFileService.upload(file);
+            img = originalFile.getData().getUrl();
+        } catch (Exception e) {
+            logger.error("【算法服务】添加日志记录异常",e);
+        }
+        algorithm.setOriginalImg(img);
+        algorithm.setSubId(subId);
+        algorithm.setAlgorithmType("image");
+        algorithm.setAlgorithmName(checkValid.getAlgorithmName());
+        return laboratoryService.saveAlgorithmData(algorithm);
+    }
+
+
+    /**
+     * 算法调用日志更新
+     * @param id
+     * @param send
+     */
+    private void updateRequestRecordLog(Long id,ImgPostResponse<AnalysisReturnData> send) {
+        Algorithm algorithm = new Algorithm();
+        algorithm.setId(id);
+        //请求成功
+        if (send != null && send.getStatus_code() == 1000) {
+            AnalysisReturnData data =  send.getData();
+            //识别后的图片
+            String picture = data.getRet_image();
+            String header = "data:image/jpeg;base64,"+picture;
+            MultipartFile multipartFile = Base64DecodedMultipartFile.base64ToMultipart(header);
+            R<SysFile> sysFileR = remoteFileService.upload(multipartFile);
+            String imageUrl = sysFileR.getData().getUrl();
+            algorithm.setAlgorithmResult(imageUrl);
+            //识别id
+            algorithm.setSignId(Long.parseLong(data.getCid()));
+            //是否报警
+            Map<String, Object> result =(Map<String, Object>)data.getResult();
+            Map<String, Object> algorithmData = (Map<String, Object>) result.get("algorithm_data");
+            Map<String, Object> modelResult = (Map<String, Object>) result.get("model_data");
+            List<Map<String, Object>> objects = (List<Map<String, Object>>) modelResult.get("objects");
+            if(algorithmData.getOrDefault("is_alert", "").toString().equals("false") && objects.size()>0){
+                algorithm.setIsAlarm(0);
+            }else{
+                algorithm.setIsAlarm(1);
+            }
+            algorithm.setStatus(1);
+        } else {
+            algorithm.setStatus(0);
+        }
+        //存原始数据
+        AnalysisReturnData respData = send != null?send.getData():null;
+        if (respData != null) {
+            respData.setRet_image(null);
+            respData.setSrc_image(null);
+            algorithm.setRespData(JSONObject.toJSONString(send));
+            ImgPostResponse<AnalysisReturnData> dataImgPostResponse = new ImgPostResponse<>();
+            dataImgPostResponse.setData(respData);
+            algorithm.setRespData(JSONObject.toJSONString(dataImgPostResponse));
+            algorithm.setRespCode(send.getStatus_code());
+        }
+        algorithm.setUpdateTime(new Date());
+        laboratoryService.update(algorithm);
+    }
+
+    /**
      * @param id 进出记录ID
      */
     public R checkAndCommit(String code, MultipartFile file, Long id) {
@@ -249,20 +323,8 @@ public class CheckService {
             apply = R.fail("算法服务返回数据错误,请联系管理员!");
             return apply;
         } finally {
-//            //无论成功失败,插入记录数据,异步请求远程接口
+           //无论成功失败,插入记录数据,异步请求远程接口
             send(code, id, f, apply.getMsg());
-//            //保存识别记录
-//            String r = send == null ? null : send.toJSONString();
-//           sendSginAccessLogService.saveAlgorithmResult(cereataAlgorithm(r, "image", f, apply.getMsg(), id, fail.getData()));
-//            //持久化
-//            Map<String, Object> data = (Map<String, Object>) send.get("data");
-//           /* Object srcImage =  data.get("src_image");
-//            String srcImg = this.generateImage(srcImage.toString(),algorithmYml.getImgTemp());*/
-//            data.put("src_img", fileR.getData().getUrl());
-//            data.put("type", "image");
-//            data.put("algorithmName", checkValid.getAlgorithmName());
-////            restTemplateLocal.postForEntity("http://192.168.1.17:9218/algorithm/save",data,String.class).getBody();
-//            laboratoryService.saveData(Covert(data));
         }
         //=========算法服务返回结果验证结束
         logger.info(apply.toString());
@@ -273,7 +335,6 @@ public class CheckService {
         return apply;
     }
 
-
     /**
      * 三合一
      *
@@ -586,8 +647,6 @@ public class CheckService {
         String header = "data:image/jpeg;base64,"+picture;
         MultipartFile multipartFile = Base64DecodedMultipartFile.base64ToMultipart(header);
         R<SysFile> sysFileR = remoteFileService.upload(multipartFile);
-        R<SysFile> originalFile = remoteFileService.upload(file);
-        String originalUrl = originalFile.getData().getUrl();
         String imageUrl = sysFileR.getData().getUrl();
         logger.info("==================================imageUrl:"+imageUrl);
         AnalysisReturnData data =  send.getData();
@@ -605,13 +664,13 @@ public class CheckService {
         map.put("aid", data.getAid());
         map.put("cid", data.getCid());
         map.put("src_img", imageUrl);
-        map.put("originalImg", originalUrl);
+        //map.put("originalImg", originalUrl);
         map.put("type", "image");
         map.put("algorithmName", checkValid.getAlgorithmName());
         map.put("id",id);
         map.put("requestData", JSONObject.toJSONString(send));
         logger.info("=======================map.tostring()"+map.toString());
-        //return laboratoryService.saveAlgorithmData(CovertV5(map));
-        return laboratoryService.update(CovertV5(map));
+        return laboratoryService.saveAlgorithmData(CovertV5(map));
+       // return laboratoryService.update(CovertV5(map));
     }
 }