
package com.lideeyunji.tool.framework.yunji.component.validate;

import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiValidate;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiValidate;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;
import com.lideeyunji.tool.framework.yunji.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 lideeYunjiValidateAspect {

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

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


}