Search in sources :

Example 1 with SqlTypeEnum

use of com.winfun.log.sdk.entity.enums.SqlTypeEnum in project study-by-myself by Howinfun.

the class LogRecordAspect method around.

@Around("@annotation(logRecordAnno))")
public Object around(ProceedingJoinPoint point, LogRecordAnno logRecordAnno) throws Throwable {
    Method method = this.getMethod(point);
    Object[] args = point.getArgs();
    // 日志记录
    LogRecord logRecord = new LogRecord();
    // 日志类型
    LogTypeEnum logType = logRecordAnno.logType();
    // 业务名称
    String businessName = logRecordAnno.businessName();
    EvaluationContext context = this.bindParam(method, args);
    // 获取操作者
    String operator = this.getOperator(logRecordAnno.operator(), context);
    logRecord.setLogType(logType);
    logRecord.setBusinessName(businessName);
    logRecord.setOperator(operator);
    Object proceedResult = null;
    // 记录实体记录
    if (LogTypeEnum.RECORD.equals(logType)) {
        SqlTypeEnum sqlType = logRecordAnno.sqlType();
        Class mapperClass = logRecordAnno.mapperName();
        if (mapperClass.isAssignableFrom(BaseMapper.class)) {
            throw new RuntimeException("mapperClass 属性传入 Class 不是 BaseMapper 的子类");
        }
        BaseMapper mapper = (BaseMapper) applicationContext.getBean(mapperClass);
        String id;
        Object beforeRecord;
        Object afterRecord;
        switch(sqlType) {
            // 新增
            case INSERT:
                logRecord.setSqlType(SqlTypeEnum.INSERT);
                proceedResult = point.proceed();
                // 根据spel表达式获取id
                id = (String) this.getId(logRecordAnno.id(), context);
                Object result = mapper.selectById(id);
                logRecord.setBeforeRecord("");
                logRecord.setAfterRecord(JSON.toJSONString(result));
                break;
            // 更新
            case UPDATE:
                logRecord.setSqlType(SqlTypeEnum.UPDATE);
                // 根据spel表达式获取id
                id = (String) this.getId(logRecordAnno.id(), context);
                beforeRecord = mapper.selectById(id);
                proceedResult = point.proceed();
                afterRecord = mapper.selectById(id);
                logRecord.setBeforeRecord(JSON.toJSONString(beforeRecord));
                logRecord.setAfterRecord(JSON.toJSONString(afterRecord));
                break;
            // 删除
            case DELETE:
                logRecord.setSqlType(SqlTypeEnum.DELETE);
                // 根据spel表达式获取id
                id = (String) this.getId(logRecordAnno.id(), context);
                beforeRecord = mapper.selectById(id);
                proceedResult = point.proceed();
                logRecord.setBeforeRecord(JSON.toJSONString(beforeRecord));
                logRecord.setAfterRecord("");
                break;
            default:
                break;
        }
    // 记录信息
    } else if (LogTypeEnum.MESSAGE.equals(logType)) {
        try {
            proceedResult = point.proceed();
            String successMsg = logRecordAnno.successMsg();
            // 对成功信息做表达式提取
            Matcher successMatcher = PATTERN.matcher(successMsg);
            while (successMatcher.find()) {
                String temp = successMatcher.group();
                Expression tempExpression = parser.parseExpression(temp);
                String result = (String) tempExpression.getValue(context);
                temp = "{{" + temp + "}}";
                successMsg = successMsg.replace(temp, result);
            }
            logRecord.setSuccessMsg(successMsg);
        } catch (Exception e) {
            String errorMsg = logRecordAnno.errorMsg();
            String exceptionMsg = e.getMessage();
            errorMsg = errorMsg.replace(LogRecordContants.ERROR_MSG_PATTERN, exceptionMsg);
            logRecord.setSuccessMsg(errorMsg);
            // 插入记录
            logRecord.setCreateTime(LocalDateTime.now());
            this.logRecordSDKService.insertLogRecord(logRecord);
            // 回抛异常
            throw new Exception(errorMsg);
        }
    }
    // 插入记录
    logRecord.setCreateTime(LocalDateTime.now());
    this.logRecordSDKService.insertLogRecord(logRecord);
    return proceedResult;
}
Also used : Matcher(java.util.regex.Matcher) Method(java.lang.reflect.Method) LogRecord(com.winfun.log.sdk.entity.LogRecord) Expression(org.springframework.expression.Expression) LogTypeEnum(com.winfun.log.sdk.entity.enums.LogTypeEnum) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) SqlTypeEnum(com.winfun.log.sdk.entity.enums.SqlTypeEnum) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Around(org.aspectj.lang.annotation.Around)

Aggregations

BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 LogRecord (com.winfun.log.sdk.entity.LogRecord)1 LogTypeEnum (com.winfun.log.sdk.entity.enums.LogTypeEnum)1 SqlTypeEnum (com.winfun.log.sdk.entity.enums.SqlTypeEnum)1 Method (java.lang.reflect.Method)1 Matcher (java.util.regex.Matcher)1 Around (org.aspectj.lang.annotation.Around)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 Expression (org.springframework.expression.Expression)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1