
package com.jeelowcode.framework.utils.component.validate;

import com.jeelowcode.framework.utils.adapter.IJeelowCodeValidate;
import com.jeelowcode.framework.utils.annotation.JeelowCodeValidate;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;
import com.jeelowcode.framework.utils.utils.FuncWebBase;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义校验
 */
@Aspect
@Component
public class JeelowCodeValidateAspect {

    @Around("@annotation(jeelowCodeValidate)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint,JeelowCodeValidate jeelowCodeValidate) throws Throwable {
        HttpServletRequest request = FuncWebBase.getRequest();
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        Class<? extends IJeelowCodeValidate>[] validateClassList = jeelowCodeValidate.validateClass();

        for (Class<? extends IJeelowCodeValidate> myclass : validateClassList) {
            IJeelowCodeValidate bean = SpringUtils.getBean(myclass);
            bean.validate(requestWrapper);
        }
        return joinPoint.proceed();
    }


}