package com.jiayue.ipfcst.aop;

import com.jiayue.ipfcst.common.core.util.SpringContextHolder;
import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldStatrsEnum;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.console.service.ElectricFieldService;
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 org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * service层aop，根据注解确定哪些方法需要执行某些操作
 *
 * @author bizy
 * @version 3.0
 */
@Aspect
@Component
@Slf4j
public class ControllerAspect {

    @Pointcut("@annotation(com.jiayue.ipfcst.aop.SaveValidate)")
    public void aroundPoinCut() {
    }

    @Around("aroundPoinCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("----------------------------------进入SaveValidate拦截器-----------------------------------------------");
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();
        SaveValidate saveValidate = method.getAnnotation(SaveValidate.class);
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
        // 记录下请求内容
        log.info("方法描述 " + saveValidate.desc());
        log.info("请求方法: {}.{}", proceedingJoinPoint.getSignature().getDeclaringTypeName(), proceedingJoinPoint.getSignature().getName());
        log.info("请求入参: {}: ", Arrays.toString(proceedingJoinPoint.getArgs()));

        try {
            ElectricFieldService electricFieldService = SpringContextHolder.getBean(ElectricFieldService.class);
            ElectricField electricField = electricFieldService.get();

            if (null != electricField) {
                ElectricFieldStatrsEnum electricFieldStatrsEnum = electricField.getStationStatusEnum();
                if (electricFieldStatrsEnum.getCode() == 2) {
                    log.info("----------------------------------场站状态为运维中，不通过-----------------------------------------------");
                    response.sendError(200, "场站状态为运维中，不允许修改任何信息，有需要请联系运维人员推送。");
                    return null;
                } else {
                    log.info("----------------------------------场站状态为未运维，通过-----------------------------------------------");
                }
            } else {
                log.info("----------------------------------场站不存在，通过-----------------------------------------------");
            }
        } catch (Exception e) {
            log.error("拦截器异常", e);
        }
        return proceedingJoinPoint.proceed();
    }
}
