AopUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.diagbot.util;
  2. import com.diagbot.annotation.BiLogger;
  3. import com.diagbot.annotation.BiLoggerResult;
  4. import com.diagbot.annotation.SysLogger;
  5. import com.diagbot.annotation.SysLoggerExport;
  6. import com.diagbot.biz.log.entity.BiRecord;
  7. import com.diagbot.biz.log.entity.SysLog;
  8. import com.diagbot.dto.RespDTO;
  9. import com.diagbot.enums.BiSourceEnum;
  10. import com.diagbot.exception.CommonErrorCode;
  11. import com.diagbot.exception.CommonException;
  12. import com.diagbot.vo.BaseBiVO;
  13. import org.aspectj.lang.JoinPoint;
  14. import org.aspectj.lang.ProceedingJoinPoint;
  15. import org.aspectj.lang.reflect.MethodSignature;
  16. import org.springframework.validation.BindException;
  17. import org.springframework.validation.FieldError;
  18. import org.springframework.web.bind.MethodArgumentNotValidException;
  19. import org.springframework.web.bind.MissingServletRequestParameterException;
  20. import java.lang.reflect.Method;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. /**
  25. * @Description:
  26. * @author: gaodm
  27. * @time: 2019/11/12 18:17
  28. */
  29. public class AopUtil {
  30. /**
  31. * SysLoggerAspect入参设置
  32. *
  33. * @param joinPoint
  34. * @param sysType
  35. */
  36. public static SysLog sysLoggerAspect(JoinPoint joinPoint, Integer sysType) {
  37. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  38. Method method = signature.getMethod();
  39. SysLog sysLog = new SysLog();
  40. SysLogger sysLogger = method.getAnnotation(SysLogger.class);
  41. if (sysLogger != null) {
  42. //注解上的描述
  43. sysLog.setOperation(sysLogger.value());
  44. }
  45. //请求的方法名
  46. String className = joinPoint.getTarget().getClass().getName();
  47. String methodName = signature.getName();
  48. sysLog.setMethod(className + "." + methodName + "()");
  49. //请求的参数
  50. Object[] args = joinPoint.getArgs();
  51. String params = "";
  52. for (Object o : args) {
  53. params += FastJsonUtils.getBeanToJson(o);
  54. }
  55. if (!StringUtil.isEmpty(params)) {
  56. sysLog.setParams(params);
  57. }
  58. //设置IP地址
  59. sysLog.setIp(HttpUtils.getIpAddress());
  60. //用户名
  61. String username = UserUtils.getCurrentPrinciple();
  62. if (!StringUtil.isEmpty(username)) {
  63. sysLog.setUsername(username);
  64. }
  65. sysLog.setGmtCreate(new Date());
  66. sysLog.setSysType(sysType);
  67. return sysLog;
  68. }
  69. /**
  70. * SysLoggerExprotAspect入参设置
  71. *
  72. * @param joinPoint
  73. * @param sysType
  74. */
  75. public static SysLog sysLoggerExprotAspect(JoinPoint joinPoint, Integer sysType) {
  76. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  77. Method method = signature.getMethod();
  78. SysLog sysLog = new SysLog();
  79. SysLoggerExport sysLogger = method.getAnnotation(SysLoggerExport.class);
  80. if (sysLogger != null) {
  81. //注解上的描述
  82. sysLog.setOperation(sysLogger.value());
  83. }
  84. //请求的方法名
  85. String className = joinPoint.getTarget().getClass().getName();
  86. String methodName = signature.getName();
  87. sysLog.setMethod(className + "." + methodName + "()");
  88. //请求的参数
  89. Object[] args = joinPoint.getArgs();
  90. String params = "";
  91. for (Object o : args) {
  92. params += FastJsonUtils.getBeanToJson(o);
  93. break;
  94. }
  95. if (!StringUtil.isEmpty(params)) {
  96. sysLog.setParams(params);
  97. }
  98. //设置IP地址
  99. sysLog.setIp(HttpUtils.getIpAddress());
  100. //用户名
  101. String username = UserUtils.getCurrentPrinciple();
  102. if (!StringUtil.isEmpty(username)) {
  103. sysLog.setUsername(username);
  104. }
  105. sysLog.setGmtCreate(new Date());
  106. sysLog.setSysType(sysType);
  107. return sysLog;
  108. }
  109. /**
  110. * Bi日志有返回值处理(普通)
  111. *
  112. * @param biRecord
  113. * @param joinPoint
  114. * @param productType
  115. * @return
  116. * @throws Throwable
  117. */
  118. public static Object biLoggerAspect(BiRecord biRecord, ProceedingJoinPoint joinPoint,
  119. Integer productType) throws Throwable {
  120. long start = System.currentTimeMillis();
  121. Object object = joinPoint.proceed();
  122. if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_NORMAL.getKey())) {
  123. biRecord = null;
  124. return object;
  125. }
  126. //出参设置
  127. String result = "";
  128. result = FastJsonUtils.getBeanToJson(object);
  129. if (!StringUtil.isEmpty(result)) {
  130. biRecord.setResult(result);
  131. }
  132. biRecord.setSuccessFlag(1);
  133. long execTime = System.currentTimeMillis() - start;
  134. biRecord.setExecTime(String.valueOf(execTime));
  135. return object;
  136. }
  137. /**
  138. * Bi日志异常统一处理(普通)
  139. *
  140. * @param joinPoint
  141. * @param ex
  142. * @param productType
  143. * @return
  144. */
  145. public static BiRecord biLoggerAspectThrow(JoinPoint joinPoint, Throwable ex, Integer productType) {
  146. BiRecord biRecord = new BiRecord();
  147. if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_NORMAL.getKey())) {
  148. return null;
  149. }
  150. //出参设置
  151. String result = "";
  152. result = FastJsonUtils.getBeanToJson(handleException((Exception) ex));
  153. biRecord.setResult(result);
  154. biRecord.setSuccessFlag(0);
  155. return biRecord;
  156. }
  157. /**
  158. * Bi日志有返回值处理(特殊)
  159. *
  160. * @param biRecord
  161. * @param joinPoint
  162. * @param productType
  163. * @return
  164. * @throws Throwable
  165. */
  166. public static Object biLoggerResultAspect(BiRecord biRecord, ProceedingJoinPoint joinPoint,
  167. Integer productType) throws Throwable {
  168. long start = System.currentTimeMillis();
  169. Object object = joinPoint.proceed();
  170. if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_RESULT.getKey())) {
  171. biRecord = null;
  172. return object;
  173. }
  174. //出参设置
  175. String result = "";
  176. if (object instanceof RespDTO) {
  177. RespDTO respDTO = (RespDTO) object;
  178. RespDTO rs = new RespDTO();
  179. rs.code = respDTO.code;
  180. rs.msg = respDTO.msg;
  181. rs.data = new Object();
  182. result = FastJsonUtils.getBeanToJson(rs);
  183. } else {
  184. result = FastJsonUtils.getBeanToJson(object);
  185. }
  186. if (!StringUtil.isEmpty(result)) {
  187. biRecord.setResult(result);
  188. }
  189. biRecord.setSuccessFlag(1);
  190. long execTime = System.currentTimeMillis() - start;
  191. biRecord.setExecTime(String.valueOf(execTime));
  192. return object;
  193. }
  194. /**
  195. * Bi日志异常统一处理(特殊)
  196. *
  197. * @param joinPoint
  198. * @param ex
  199. * @param productType
  200. * @return
  201. */
  202. public static BiRecord biLoggerResultAspectThrow(JoinPoint joinPoint, Throwable ex, Integer productType) {
  203. BiRecord biRecord = new BiRecord();
  204. if (!biRecordSet(biRecord, joinPoint, productType, BiSourceEnum.BI_RESULT.getKey())) {
  205. return null;
  206. }
  207. //出参设置
  208. String result = "";
  209. result = FastJsonUtils.getBeanToJson(handleException((Exception) ex));
  210. biRecord.setResult(result);
  211. biRecord.setSuccessFlag(0);
  212. return biRecord;
  213. }
  214. /**
  215. * 消息设定
  216. *
  217. * @param biRecord
  218. * @param joinPoint
  219. * @return 是否可以继续下去
  220. */
  221. private static Boolean biRecordSet(BiRecord biRecord, JoinPoint joinPoint,
  222. Integer productType, Integer biSourceType) {
  223. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  224. Method method = signature.getMethod();
  225. //请求的参数
  226. Object[] args = joinPoint.getArgs();
  227. String params = "";
  228. for (Object o : args) {
  229. if (o instanceof BaseBiVO) {
  230. BaseBiVO baseBiVO = (BaseBiVO) o;
  231. if (StringUtil.isBlank(baseBiVO.getHospitalCode())) {
  232. if (StringUtil.isNotBlank(baseBiVO.getHosCode())) {
  233. BeanUtil.copyProperties(baseBiVO, biRecord);
  234. biRecord.setHospitalCode(baseBiVO.getHosCode());
  235. } else {
  236. return false;
  237. }
  238. } else {
  239. BeanUtil.copyProperties(baseBiVO, biRecord);
  240. }
  241. }
  242. params += FastJsonUtils.getBeanToJson(o);
  243. }
  244. if (!StringUtil.isEmpty(params)) {
  245. biRecord.setParams(params);
  246. }
  247. //设置功能编码
  248. if (biSourceType.equals(BiSourceEnum.BI_NORMAL.getKey())) {
  249. BiLogger biLogger = method.getAnnotation(BiLogger.class);
  250. if (biLogger != null) {
  251. //注解上的描述
  252. biRecord.setCode(biLogger.value());
  253. }
  254. } else if (biSourceType.equals(BiSourceEnum.BI_RESULT.getKey())) {
  255. BiLoggerResult biLoggerResult = method.getAnnotation(BiLoggerResult.class);
  256. if (biLoggerResult != null) {
  257. //注解上的描述
  258. biRecord.setCode(biLoggerResult.value());
  259. }
  260. } else {
  261. return false;
  262. }
  263. biRecord.setGmtCreate(new Date());
  264. biRecord.setProductType(productType);
  265. biRecord.setSource(biSourceType);
  266. return true;
  267. }
  268. /**
  269. * 抛错信息处理
  270. *
  271. * @param e
  272. * @return 结果参数
  273. */
  274. private static RespDTO handleException(Exception e) {
  275. RespDTO resp = new RespDTO();
  276. if (e instanceof BindException) {
  277. BindException ex = (BindException) e;
  278. Map<String, String> stringMap = new HashMap<>();
  279. for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
  280. stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
  281. }
  282. String msg = FastJsonUtils.getBeanToJson(stringMap);
  283. resp.code = CommonErrorCode.PARAM_ERROR.getCode();
  284. resp.msg = msg;
  285. return resp;
  286. }
  287. if (e instanceof MethodArgumentNotValidException) {
  288. MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
  289. Map<String, String> stringMap = new HashMap<>();
  290. for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
  291. stringMap.put(fieldError.getField(), fieldError.getDefaultMessage());
  292. }
  293. String msg = FastJsonUtils.getBeanToJson(stringMap);
  294. resp.code = CommonErrorCode.PARAM_ERROR.getCode();
  295. resp.msg = msg;
  296. return resp;
  297. }
  298. if (e instanceof MissingServletRequestParameterException) {
  299. MissingServletRequestParameterException ex = (MissingServletRequestParameterException) e;
  300. Map<String, String> stringMap = new HashMap<>();
  301. stringMap.put(ex.getParameterName(), "不能为null");
  302. String msg = FastJsonUtils.getBeanToJson(stringMap);
  303. resp.code = CommonErrorCode.PARAM_ERROR.getCode();
  304. resp.msg = msg;
  305. return resp;
  306. }
  307. if (e instanceof CommonException) {
  308. CommonException taiChiException = (CommonException) e;
  309. resp.code = taiChiException.getCode();
  310. resp.msg = e.getMessage();
  311. return resp;
  312. }
  313. resp.code = CommonErrorCode.FAIL.getCode();
  314. resp.msg = e.getMessage();
  315. return resp;
  316. }
  317. }