package com.reyun.saas.web.aop;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.reyun.saas.report.util.ReportParamsUril;
import com.reyun.saas.report.vo.ReportParamsVO;
import lombok.extern.slf4j.Slf4j;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @desc
 */
@Component // 让spring扫描到
@Aspect // 告诉spring这是个切面类
@Slf4j
public class ReportDateProcessAspect {

    /* 切点的类路径:填上面那个自定注解路径 */
    @Pointcut("@annotation(com.reyun.saas.web.aop.ReportDateProcess)")
    private void pointcut() {}

    @Around("pointcut()")
    public Object sayAround(ProceedingJoinPoint joinPoint) throws Throwable{

        Object[] args = joinPoint.getArgs(); // 获取方法参数值数组

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();// 得到其方法签名

        Class[] paramTypeArray = methodSignature.getParameterTypes(); // 获取方法参数类型数组
        ReportParamsVO reportParamsVO = new ReportParamsVO();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                if (ReportParamsVO.class.isAssignableFrom(paramTypeArray[i])) {
                    reportParamsVO = (ReportParamsVO)args[i]; // 如果方法的参数列表参数有ReportParamsVO类型，则给其处理加工

                    reportParamsVO.setAppkeys(ReportParamsUril.handParamStr(reportParamsVO.getAppkeys()));
                    reportParamsVO.setCids(ReportParamsUril.handParamStr(reportParamsVO.getCids()));
                    reportParamsVO.setSdkVersions(ReportParamsUril.handParamStr(reportParamsVO.getSdkVersions()));
                    reportParamsVO.setAppVersions(ReportParamsUril.handParamStr(reportParamsVO.getAppVersions()));
                    reportParamsVO.setSysVersions(ReportParamsUril.handParamStr(reportParamsVO.getSysVersions()));
                    reportParamsVO.setModels(ReportParamsUril.handParamStr(reportParamsVO.getModels()));
                    reportParamsVO.setBrands(ReportParamsUril.handParamStr(reportParamsVO.getBrands()));

                    args[i]=ReportParamsUril.handDate(reportParamsVO);//时间处理的关键逻辑
                    break;
                }
            }
        }
        log.debug("请求参数为{}",args);
        /*
         * 动态修改其参数
         * joinPoint.proceed(Object[] args),将刚刚修改的args参数传过去才算修改
         * joinPoint.proceed() 不修改参数的执行
         */
        Object result = joinPoint.proceed(args);
        log.debug("响应结果为{}",result);

        return result; // 如果这里不返回result，则目标对象实际返回值会被置为null
    }




    public static void main(String[] args) {
        String startDateStr = "2021-09-02";
        String endDateStr = "2021-09-04";
        Date startDate = DateUtil.parse(startDateStr, DatePattern.NORM_DATE_PATTERN).toJdkDate();
        Date endDate = DateUtil.parse(endDateStr, DatePattern.NORM_DATE_PATTERN).toJdkDate();
        Date beDate = DateUtil.parse("2021-09-05", DatePattern.NORM_DATE_PATTERN).toJdkDate();
        System.out.println(ReportParamsUril.checkBetweenDate(startDate,endDate,beDate));
    }
}
