Forráskód Böngészése

1.龙洞堡优化

wxg 10 hónapja
szülő
commit
4134cb496a

+ 11 - 0
src/main/java/com/yx/face/boot/component/aop/MyAnnotation.java

@@ -0,0 +1,11 @@
+package com.yx.face.boot.component.aop;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MyAnnotation {
+}

+ 65 - 10
src/main/java/com/yx/face/boot/component/aop/ServiceLog.java

@@ -1,30 +1,40 @@
 package com.yx.face.boot.component.aop;
-
-import  com.yx.face.boot.uitls.JsonUtils;
+import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.Signature;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import java.lang.reflect.Method;
+
 /**
  * <p>
  * 使用 aop 切面记录请求日志信息
  * </p>
- * @description: 使用 aop 切面记录请求日志信息
  */
-//@Aspect
-//@Component
+@Aspect
+@Component
 @Slf4j
 public class ServiceLog {
+
+    private volatile static String logPrint = "less";
+
+    public static void updateLogPrint(String logPrint){
+        ServiceLog.logPrint = logPrint;
+    }
     /**
      * 切入点
      */
-    @Pointcut("execution(public *  com.yx.face.service.impl.*ServiceImpl.*(..))")
+    @Pointcut("execution(public * com.yx.face.service.impl.*.*(..))")
     public void log() {
     }
 
+
     /**
      * 环绕操作
      *
@@ -34,14 +44,59 @@ public class ServiceLog {
      */
     @Around("log()")
     public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
-        log.info("【请求类名】:{}", point.getSignature().getDeclaringTypeName());
-        log.info("【请求方法名】:{}【参数】:{}", point.getSignature().getName(), point.getArgs());
         long start = System.currentTimeMillis();
+        Signature signature = point.getSignature();
+        MyAnnotation annotation = getAnnotation(point);
+        if(annotation == null){
+            log.info("【请求日志级别】:{}【请求类名】:{}【请求方法名】:{}",logPrint,signature.getDeclaringTypeName(),signature.getName());
+            try {
+                String req = JSON.toJSONString(point.getArgs());
+                log.info("【参数】:{}",this.logPrint(req));//如果传的是流就会报错
+            }catch (Exception e){
+                log.info("打印参数报错");
+            }
+        }
+        //运行
         Object result = point.proceed();
-        log.info("【耗时】:{}毫秒", System.currentTimeMillis() - start);
-        //log.info("【返回值】:{}", JsonUtils.toJson(result));
+        if(annotation == null){
+            try {
+                String resp = JSON.toJSONString(result);
+                log.info("【返回值】:{}",this.logPrint(resp));//如果返回的是流就会报错
+            }catch (Exception e){
+                log.info("打印返回值报错");
+            }
+            log.info("【耗时】:{}",System.currentTimeMillis() - start);
+        }
+
         return result;
     }
 
+    private String logPrint(String req) {
+        String value = logPrint;
+        switch (value){
+            case "all" : return req;
+            case "less" : return this.getThousand(req);
+            default:return this.getThousand(req);
+        }
+    }
+
+    private String getThousand(String req) {
+        if(req.length() > 1000){
+            return req.substring(0,1000);
+        }
+        return req;
+    }
+
+    private MyAnnotation getAnnotation(ProceedingJoinPoint pjp) {
+        MethodSignature ms = (MethodSignature) pjp.getSignature();
+        Object target = pjp.getTarget();
+        Method method1 = null;
+        try {
+            method1 = target.getClass().getMethod(ms.getName(), ms.getParameterTypes());
+        } catch (NoSuchMethodException e) {
+
+        }
+        return method1 == null ? null : method1.getAnnotation(MyAnnotation.class);
+    }
 
 }

+ 15 - 5
src/main/java/com/yx/face/controller/TBFaceController.java

@@ -3,7 +3,9 @@ package com.yx.face.controller;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Maps;
+import com.yx.face.boot.component.aop.ServiceLog;
 import com.yx.face.boot.core.SnowflakeIdWorker;
+import com.yx.face.boot.restful.ServiceException;
 import com.yx.face.boot.uitls.JsonUtils;
 import com.yx.face.model.dto.AlcoholTestDto;
 import com.yx.face.service.FaceTBService;
@@ -13,9 +15,7 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.BufferedInputStream;
@@ -36,6 +36,7 @@ import java.util.*;
 public class TBFaceController {
 
     private final FaceTBService faceTBService;
+    private static String logInfo = "less";
 
     @ApiOperation("设备连接推送")
     @PostMapping("connect")
@@ -84,7 +85,9 @@ public class TBFaceController {
         Map<String, Object> param = null;
         try {
             param = getParam(request);
-//            log.info("--->推送刷脸刷卡记录入参:{}", JSONObject.toJSONString(param));
+            if(logInfo.equals("all")){
+                log.info("--->推送刷脸刷卡记录入参:{}", JSONObject.toJSONString(param));
+            }
             return faceTBService.eventRecords(param);
         } catch (Exception e) {
             log.info("--->推送刷脸刷卡记录入参:{}", JSONObject.toJSONString(param));
@@ -94,7 +97,14 @@ public class TBFaceController {
         result.put("errCode", "00");
         return result;
     }
-
+    @ApiOperation(value = "刷脸刷卡记录less/all")
+    @GetMapping(value = "/updateLogPrint")
+    public void updateLogPrint(@RequestParam String logPrint){
+        if(!logPrint.equals("less") && !logPrint.equals("all")){
+            throw new ServiceException("请传入less/all");
+        }
+        TBFaceController.logInfo = logPrint;
+    }
 //    /**
 //     * 酒测记录推送
 //     *

+ 10 - 0
src/main/java/com/yx/face/controller/UploadController.java

@@ -12,10 +12,12 @@ package com.yx.face.controller;
 
 
 import com.alibaba.excel.EasyExcel;
+import com.yx.face.boot.component.aop.ServiceLog;
 import com.yx.face.boot.core.BaseController;
 import com.yx.face.boot.core.Constants;
 import com.yx.face.boot.restful.RestResponse;
 import com.yx.face.boot.restful.RestResult;
+import com.yx.face.boot.restful.ServiceException;
 import com.yx.face.boot.uitls.ExcelListenerUtils;
 import com.yx.face.boot.uitls.FileUtils;
 import com.yx.face.model.excel.UploadExcel;
@@ -74,4 +76,12 @@ public class UploadController extends BaseController {
         List<Object> datas = listener.getDatas(); //把拿到的数据处理到集合里去(根据所需自行修改)
         faceService.uploadExcel(datas);
     }
+    @ApiOperation(value = "日志等级less/all")
+    @GetMapping(value = "/updateLogPrint")
+    public void updateLogPrint(@RequestParam String logPrint){
+        if(!logPrint.equals("less") && !logPrint.equals("all")){
+            throw new ServiceException("请传入less/all");
+        }
+        ServiceLog.updateLogPrint(logPrint);
+    }
 }

+ 1 - 2
src/main/java/com/yx/face/model/excel/UploadExcel.java

@@ -25,8 +25,7 @@ public class UploadExcel {
     private String photo;
 
     @ExcelProperty(value = "时间",index = 3)
-    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
-    private Date date;
+    private String date;
 
 
 

+ 9 - 4
src/main/java/com/yx/face/service/impl/FaceServiceImpl.java

@@ -1,6 +1,9 @@
 package com.yx.face.service.impl;
 
 import cn.hutool.core.codec.Base64;
+import cn.hutool.core.date.DatePattern;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSON;
@@ -689,10 +692,12 @@ public class FaceServiceImpl implements FaceService {
                         log.info("没照片{}",JSON.toJSONString(uploadExcel));
                         continue;
                     }
-                    if (StringUtils.isBlank(uploadExcel.getIdNumber())) {
-                        log.info("没卡号{}",JSON.toJSONString(uploadExcel));
+                    if (StringUtils.isBlank(uploadExcel.getDate())) {
+                        log.info("没时间{}",JSON.toJSONString(uploadExcel));
                         continue;
                     }
+                    DateTime parse = DateUtil.parse(uploadExcel.getDate());
+                    uploadExcel.setDate(DateUtil.format(parse, DatePattern.CHINESE_DATE_TIME_PATTERN));
 
                     File file = null;
                     try {
@@ -704,8 +709,8 @@ public class FaceServiceImpl implements FaceService {
 //                            time = sdf1.format(date);
 //                        }
 //                        String d = "D:\\4\\"+name+"-"+idNumber+"-"+time+".jpg";
-                        String idNumber = uploadExcel.getIdNumber();
-                        String d = "D:\\4\\"+idNumber+".jpg";
+                        String name = uploadExcel.getName().concat("-").concat(uploadExcel.getDate());
+                        String d = "D:\\4\\"+name+".jpg";
                         file = new File(d);
                         if(file.exists()){
                             log.info("已存在{}",JSON.toJSONString(uploadExcel));

+ 3 - 1
src/main/java/com/yx/face/service/impl/FaceTBServiceImpl.java

@@ -10,6 +10,7 @@ import com.alibaba.fastjson.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import com.yx.face.boot.component.aop.MyAnnotation;
 import com.yx.face.boot.core.SnowflakeIdWorker;
 import com.yx.face.boot.uitls.DateUtils;
 import com.yx.face.boot.uitls.FileUploadUtil;
@@ -395,6 +396,7 @@ public class FaceTBServiceImpl implements FaceTBService {
     }
 
     @Override
+    @MyAnnotation
     public Map<String, Object> eventRecords(Map<String, Object> dto) {
         Map<String, Object> result = Maps.newHashMapWithExpectedSize(1);
         result.put("errCode", "00");
@@ -683,7 +685,7 @@ public class FaceTBServiceImpl implements FaceTBService {
             }
             faceLog.setVerifyScore(verify_score);
             faceLog.setCreateTime(now);
-            log.info("进出记录推送内容,姓名:{}, 身份证:{}, 健康码状态:{}", faceLog.getName(), faceLog.getCardid(), faceLog.getJkmStatus());
+            log.info("进出记录推送内容,姓名:{}, 身份证:{}, 设备号:{}", faceLog.getName(), faceLog.getCardid(), faceLog.getDeviceSn());
             if (verifytype == 0 || verifytype == 2) {
                 //长期通行记录
                 faceLogDao.insertSelective(faceLog);