package com.yonge.cooleshow.biz.dal.wordfilter.annotation;

import com.alibaba.fastjson.JSON;
import com.yonge.cooleshow.biz.dal.wordfilter.WordFilter;
import com.yonge.toolset.base.exception.BizException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description: 敏感词过滤切面
 * @Author: cy
 * @Date: 2022/7/6
 */
@Aspect
@Order(1)
@Component
public class CheckWordAspect {
    @Autowired
    private WordFilter wordFilter;
    @Autowired
    RedissonClient redissonClient;

    @Pointcut("@annotation(com.yonge.cooleshow.biz.dal.wordfilter.annotation.CheckWord)")
    private void checkWord() {
    }

    @Around("checkWord()")
    public Object checkWord(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        String text = JSON.toJSONString(args);
        if (wordFilter.include(text, 3)) {
            throw new BizException("文本包含敏感信息");
        }
        return joinPoint.proceed();
    }
}
