|
@@ -0,0 +1,151 @@
|
|
|
|
+package com.diagbot.util;
|
|
|
|
+
|
|
|
|
+import com.diagbot.dto.RespDTO;
|
|
|
|
+import com.diagbot.entity.TranLog;
|
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.springframework.validation.BindException;
|
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
|
+
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: Cdss日志工具类
|
|
|
|
+ * @author: gaodm
|
|
|
|
+ * @time: 2021/10/20 10:30
|
|
|
|
+ */
|
|
|
|
+public class CdssLogUtil {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 入参设置
|
|
|
|
+ *
|
|
|
|
+ * @param joinPoint
|
|
|
|
+ */
|
|
|
|
+ public static TranLog tranLogReqAspect(ProceedingJoinPoint joinPoint) {
|
|
|
|
+ TranLog TranLog = new TranLog();
|
|
|
|
+ //请求的参数
|
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
|
+ //请求的参数
|
|
|
|
+ String params = "";
|
|
|
|
+ for (Object o : args) {
|
|
|
|
+ params += FastJsonUtils.getBeanToJson(o);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ if (!StringUtil.isEmpty(params)) {
|
|
|
|
+ TranLog.setParams(params);
|
|
|
|
+ }
|
|
|
|
+ return TranLog;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 正常结束出参
|
|
|
|
+ *
|
|
|
|
+ * @param tranLog
|
|
|
|
+ * @param joinPoint
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Throwable
|
|
|
|
+ */
|
|
|
|
+ public static TranLog tranLogRespAspect(Object proceed, TranLog tranLog, long start, ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
|
+ String result = "";
|
|
|
|
+ result = FastJsonUtils.getBeanToJson(proceed);
|
|
|
|
+ tranLog.setSuccessFlag(1);
|
|
|
|
+ if (!StringUtil.isEmpty(result)) {
|
|
|
|
+ tranLog.setResult(result);
|
|
|
|
+ if (proceed instanceof RespDTO) {
|
|
|
|
+ RespDTO respDTO = (RespDTO) proceed;
|
|
|
|
+ if (!respDTO.code.equals("0")) {
|
|
|
|
+ tranLog.setSuccessFlag(0);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ long execTime = System.currentTimeMillis() - start;
|
|
|
|
+ tranLog.setGmtResp(DateUtil.now());
|
|
|
|
+ tranLog.setExecTime(String.valueOf(execTime));
|
|
|
|
+ return tranLog;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 异常结束
|
|
|
|
+ *
|
|
|
|
+ * @param joinPoint
|
|
|
|
+ * @param ex
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static TranLog tranLogRespAspectThrow(JoinPoint joinPoint, TranLog tranLog, long start, Throwable ex) {
|
|
|
|
+ //请求的参数
|
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
|
+ //请求的参数
|
|
|
|
+ String params = "";
|
|
|
|
+ for (Object o : args) {
|
|
|
|
+ params += FastJsonUtils.getBeanToJson(o);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ if (!StringUtil.isEmpty(params)) {
|
|
|
|
+ tranLog.setParams(params);
|
|
|
|
+ }
|
|
|
|
+ //出参设置
|
|
|
|
+ String result = "";
|
|
|
|
+ result = FastJsonUtils.getBeanToJson(handleException((Exception) ex));
|
|
|
|
+ tranLog.setResult(result);
|
|
|
|
+ tranLog.setSuccessFlag(0);
|
|
|
|
+ long execTime = System.currentTimeMillis() - start;
|
|
|
|
+ tranLog.setGmtResp(DateUtil.now());
|
|
|
|
+ tranLog.setExecTime(String.valueOf(execTime));
|
|
|
|
+ return tranLog;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 抛错信息处理
|
|
|
|
+ *
|
|
|
|
+ * @param e
|
|
|
|
+ * @return 结果参数
|
|
|
|
+ */
|
|
|
|
+ private static RespDTO handleException(Exception e) {
|
|
|
|
+ RespDTO resp = new RespDTO();
|
|
|
|
+ if (e instanceof BindException) {
|
|
|
|
+ BindException ex = (BindException) e;
|
|
|
|
+ Map<String, String> stringMap = new HashMap<>();
|
|
|
|
+ for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
|
|
|
|
+ stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
|
|
|
|
+ }
|
|
|
|
+ String msg = FastJsonUtils.getBeanToJson(stringMap);
|
|
|
|
+ resp.code = CommonErrorCode.PARAM_ERROR.getCode();
|
|
|
|
+ resp.msg = msg;
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+ if (e instanceof MethodArgumentNotValidException) {
|
|
|
|
+ MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
|
|
|
|
+ Map<String, String> stringMap = new HashMap<>();
|
|
|
|
+ for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
|
|
|
|
+ stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
|
|
|
|
+ }
|
|
|
|
+ String msg = FastJsonUtils.getBeanToJson(stringMap);
|
|
|
|
+ resp.code = CommonErrorCode.PARAM_ERROR.getCode();
|
|
|
|
+ resp.msg = msg;
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+ if (e instanceof MissingServletRequestParameterException) {
|
|
|
|
+ MissingServletRequestParameterException ex = (MissingServletRequestParameterException) e;
|
|
|
|
+ Map<String, String> stringMap = new HashMap<>();
|
|
|
|
+ stringMap.put(ex.getParameterName(), "不能为null");
|
|
|
|
+ String msg = FastJsonUtils.getBeanToJson(stringMap);
|
|
|
|
+ resp.code = CommonErrorCode.PARAM_ERROR.getCode();
|
|
|
|
+ resp.msg = msg;
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+ if (e instanceof CommonException) {
|
|
|
|
+ CommonException taiChiException = (CommonException) e;
|
|
|
|
+ resp.code = taiChiException.getCode();
|
|
|
|
+ resp.msg = e.getMessage();
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+ resp.code = CommonErrorCode.FAIL.getCode();
|
|
|
|
+ resp.msg = e.getMessage();
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+}
|