123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- package com.diagbot.util;
- import com.diagbot.annotation.BiLogger;
- import com.diagbot.annotation.BiLoggerResult;
- import com.diagbot.annotation.SysLogger;
- import com.diagbot.annotation.SysLoggerExport;
- import com.diagbot.biz.log.entity.BiRecord;
- import com.diagbot.biz.log.entity.SysLog;
- import com.diagbot.dto.RespDTO;
- import com.diagbot.enums.BiSourceEnum;
- import com.diagbot.exception.CommonErrorCode;
- import com.diagbot.exception.CommonException;
- import com.diagbot.vo.BaseBiVO;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.reflect.MethodSignature;
- 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:
- * @author: gaodm
- * @time: 2019/11/12 18:17
- */
- public class AopUtil {
- /**
- * SysLoggerAspect入参设置
- *
- * @param joinPoint
- * @param sysType
- */
- public static SysLog sysLoggerAspect(JoinPoint joinPoint, Integer sysType) {
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- Method method = signature.getMethod();
- SysLog sysLog = new SysLog();
- SysLogger sysLogger = method.getAnnotation(SysLogger.class);
- if (sysLogger != null) {
- //注解上的描述
- sysLog.setOperation(sysLogger.value());
- }
- //请求的方法名
- String className = joinPoint.getTarget().getClass().getName();
- String methodName = signature.getName();
- sysLog.setMethod(className + "." + methodName + "()");
- //请求的参数
- Object[] args = joinPoint.getArgs();
- String params = "";
- for (Object o : args) {
- params += FastJsonUtils.getBeanToJson(o);
- }
- if (!StringUtil.isEmpty(params)) {
- sysLog.setParams(params);
- }
- //设置IP地址
- sysLog.setIp(HttpUtils.getIpAddress());
- //用户名
- String username = UserUtils.getCurrentPrinciple();
- if (!StringUtil.isEmpty(username)) {
- sysLog.setUsername(username);
- }
- sysLog.setGmtCreate(new Date());
- sysLog.setSysType(sysType);
- return sysLog;
- }
- /**
- * SysLoggerExprotAspect入参设置
- *
- * @param joinPoint
- * @param sysType
- */
- public static SysLog sysLoggerExprotAspect(JoinPoint joinPoint, Integer sysType) {
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- Method method = signature.getMethod();
- SysLog sysLog = new SysLog();
- SysLoggerExport sysLogger = method.getAnnotation(SysLoggerExport.class);
- if (sysLogger != null) {
- //注解上的描述
- sysLog.setOperation(sysLogger.value());
- }
- //请求的方法名
- String className = joinPoint.getTarget().getClass().getName();
- String methodName = signature.getName();
- sysLog.setMethod(className + "." + methodName + "()");
- //请求的参数
- Object[] args = joinPoint.getArgs();
- String params = "";
- for (Object o : args) {
- params += FastJsonUtils.getBeanToJson(o);
- break;
- }
- if (!StringUtil.isEmpty(params)) {
- sysLog.setParams(params);
- }
- //设置IP地址
- sysLog.setIp(HttpUtils.getIpAddress());
- //用户名
- String username = UserUtils.getCurrentPrinciple();
- if (!StringUtil.isEmpty(username)) {
- sysLog.setUsername(username);
- }
- sysLog.setGmtCreate(new Date());
- sysLog.setSysType(sysType);
- return sysLog;
- }
- /**
- * Bi日志有返回值处理(普通)
- *
- * @param biRecord
- * @param joinPoint
- * @param productType
- * @return
- * @throws Throwable
- */
- public static Object biLoggerAspect(BiRecord biRecord, ProceedingJoinPoint joinPoint,
- Integer productType) throws Throwable {
- long start = System.currentTimeMillis();
- Object object = joinPoint.proceed();
- if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_NORMAL.getKey())) {
- biRecord = null;
- return object;
- }
- //出参设置
- String result = "";
- result = FastJsonUtils.getBeanToJson(object);
- if (!StringUtil.isEmpty(result)) {
- biRecord.setResult(result);
- }
- biRecord.setSuccessFlag(1);
- long execTime = System.currentTimeMillis() - start;
- biRecord.setExecTime(String.valueOf(execTime));
- return object;
- }
- /**
- * Bi日志异常统一处理(普通)
- *
- * @param joinPoint
- * @param ex
- * @param productType
- * @return
- */
- public static BiRecord biLoggerAspectThrow(JoinPoint joinPoint, Throwable ex, Integer productType) {
- BiRecord biRecord = new BiRecord();
- if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_NORMAL.getKey())) {
- return null;
- }
- //出参设置
- String result = "";
- result = FastJsonUtils.getBeanToJson(handleException((Exception) ex));
- biRecord.setResult(result);
- biRecord.setSuccessFlag(0);
- return biRecord;
- }
- /**
- * Bi日志有返回值处理(特殊)
- *
- * @param biRecord
- * @param joinPoint
- * @param productType
- * @return
- * @throws Throwable
- */
- public static Object biLoggerResultAspect(BiRecord biRecord, ProceedingJoinPoint joinPoint,
- Integer productType) throws Throwable {
- long start = System.currentTimeMillis();
- Object object = joinPoint.proceed();
- if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_RESULT.getKey())) {
- biRecord = null;
- return object;
- }
- //出参设置
- String result = "";
- if (object instanceof RespDTO) {
- RespDTO respDTO = (RespDTO) object;
- RespDTO rs = new RespDTO();
- rs.code = respDTO.code;
- rs.msg = respDTO.msg;
- rs.data = new Object();
- result = FastJsonUtils.getBeanToJson(rs);
- } else {
- result = FastJsonUtils.getBeanToJson(object);
- }
- if (!StringUtil.isEmpty(result)) {
- biRecord.setResult(result);
- }
- biRecord.setSuccessFlag(1);
- long execTime = System.currentTimeMillis() - start;
- biRecord.setExecTime(String.valueOf(execTime));
- return object;
- }
- /**
- * Bi日志异常统一处理(特殊)
- *
- * @param joinPoint
- * @param ex
- * @param productType
- * @return
- */
- public static BiRecord biLoggerResultAspectThrow(JoinPoint joinPoint, Throwable ex, Integer productType) {
- BiRecord biRecord = new BiRecord();
- if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_RESULT.getKey())) {
- return null;
- }
- //出参设置
- String result = "";
- result = FastJsonUtils.getBeanToJson(handleException((Exception) ex));
- biRecord.setResult(result);
- biRecord.setSuccessFlag(0);
- return biRecord;
- }
- /**
- * 消息设定
- *
- * @param biRecord
- * @param joinPoint
- * @return 是否可以继续下去
- */
- private static Boolean biRecordSet(BiRecord biRecord, JoinPoint joinPoint,
- Integer productType, Integer biSourceType) {
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
- Method method = signature.getMethod();
- //请求的参数
- Object[] args = joinPoint.getArgs();
- String params = "";
- for (Object o : args) {
- if (o instanceof BaseBiVO) {
- BaseBiVO baseBiVO = (BaseBiVO) o;
- if (StringUtil.isBlank(baseBiVO.getHospitalCode())) {
- if (StringUtil.isNotBlank(baseBiVO.getHosCode())) {
- BeanUtil.copyProperties(baseBiVO, biRecord);
- biRecord.setHospitalCode(baseBiVO.getHosCode());
- } else {
- return false;
- }
- } else {
- BeanUtil.copyProperties(baseBiVO, biRecord);
- }
- }
- params += FastJsonUtils.getBeanToJson(o);
- }
- if (!StringUtil.isEmpty(params)) {
- biRecord.setParams(params);
- }
- //设置功能编码
- if (biSourceType.equals(BiSourceEnum.BI_NORMAL.getKey())) {
- BiLogger biLogger = method.getAnnotation(BiLogger.class);
- if (biLogger != null) {
- //注解上的描述
- biRecord.setCode(biLogger.value());
- }
- } else if (biSourceType.equals(BiSourceEnum.BI_RESULT.getKey())) {
- BiLoggerResult biLoggerResult = method.getAnnotation(BiLoggerResult.class);
- if (biLoggerResult != null) {
- //注解上的描述
- biRecord.setCode(biLoggerResult.value());
- }
- } else {
- return false;
- }
- biRecord.setGmtCreate(new Date());
- biRecord.setProductType(productType);
- biRecord.setSource(biSourceType);
- return true;
- }
- /**
- * 抛错信息处理
- *
- * @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;
- }
- }
|