|
@@ -4,6 +4,8 @@ import com.diagbot.annotation.BiLoggerResult;
|
|
|
import com.diagbot.dto.RespDTO;
|
|
|
import com.diagbot.enums.BiSourceEnum;
|
|
|
import com.diagbot.enums.ProductTypeEnum;
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
import com.diagbot.rabbit.MySender;
|
|
|
import com.diagbot.util.BeanUtil;
|
|
|
import com.diagbot.util.GsonUtil;
|
|
@@ -12,13 +14,22 @@ import com.diagbot.vo.BaseBiVO;
|
|
|
import com.diagbot.vo.BiRecord;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.annotation.AfterReturning;
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.validation.BindException;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description: 日志拦截切面
|
|
@@ -84,5 +95,95 @@ public class BiLoggerResultAspect {
|
|
|
//保存BI日志
|
|
|
mySender.outputBiLogSend(biRecord);
|
|
|
}
|
|
|
+
|
|
|
+ @AfterThrowing(throwing="ex"
|
|
|
+ , pointcut="@annotation(com.diagbot.annotation.BiLoggerResult)")
|
|
|
+ public void saveBiRecordThrow(JoinPoint joinPoint, Throwable ex) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+
|
|
|
+ BiRecord biRecord = new BiRecord();
|
|
|
+ //请求的参数
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ String params = "";
|
|
|
+ for (Object o : args) {
|
|
|
+ if (o instanceof BaseBiVO) {
|
|
|
+ BaseBiVO baseBiVO = (BaseBiVO) o;
|
|
|
+ if (StringUtil.isBlank(baseBiVO.getHospitalCode())) {
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ BeanUtil.copyProperties(baseBiVO, biRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ params += GsonUtil.toJson(o);
|
|
|
+ }
|
|
|
+ if (!StringUtil.isEmpty(params)) {
|
|
|
+ biRecord.setParams(params);
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置功能编码
|
|
|
+ BiLoggerResult biLoggerResult = method.getAnnotation(BiLoggerResult.class);
|
|
|
+ if (biLoggerResult != null) {
|
|
|
+ //注解上的描述
|
|
|
+ biRecord.setCode(biLoggerResult.value());
|
|
|
+ }
|
|
|
+
|
|
|
+ //出参设置
|
|
|
+ String result = "";
|
|
|
+ result = GsonUtil.toJson(handleException((Exception)ex));
|
|
|
+ biRecord.setResult(result);
|
|
|
+
|
|
|
+ biRecord.setGmtCreate(new Date());
|
|
|
+ biRecord.setProductType(ProductTypeEnum.LTAPI.getKey());
|
|
|
+ biRecord.setSource(BiSourceEnum.BI_RESULT.getKey());
|
|
|
+ //保存BI日志
|
|
|
+ mySender.outputBiLogSend(biRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private 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 = GsonUtil.toJson(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 = GsonUtil.toJson(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 = GsonUtil.toJson(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();
|
|
|
+ e.printStackTrace();
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
}
|
|
|
|