package com.unis.crk.aop;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.unis.crk.mq.constant.BizOperateType;
import com.unis.crk.mq.constant.BizType;
import com.unis.crk.mq.constant.TopicConstant;
import com.unis.crk.mq.dto.BaseDTO;
import com.unis.crk.mq.dto.UploadCountryDTO;
import com.unis.crk.mq.pub.PublishService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * 处理国家局上传数据
 */
@Aspect
@Component
@SuppressWarnings("unused")
public class uploadCountryerceptor {

    @Resource
    private PublishService publishService;

    //配置接入点
    @Pointcut("@annotation(com.unis.crk.aop.SystemCountryLog)")
    private void serviceCountryAspect() {
    }//定义一个切入点


    @AfterReturning(value = "serviceCountryAspect()",returning="param1")
    public void doAfterReturning(JoinPoint pjp,Object param1) throws Throwable {

        // 拦截的实体类，就是当前正在执行的controller
        Object target = pjp.getTarget();
        // 拦截的方法名称。当前正在执行的方法
        String methodName = pjp.getSignature().getName();
        // 拦截的方法参数
        Object[] args = pjp.getArgs();
        // 拦截的放参数类型
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
        Class[] parameterTypes = msig.getMethod().getParameterTypes();

        Object object = null;
        // 获得被拦截的方法
        Method method = null;

            method = target.getClass().getMethod(methodName, parameterTypes);
            if (null != method) {
                // 判断是否包含自定义的注解，说明一下这里的SystemLog就是我自己自定义的注解
                if (method.isAnnotationPresent(SystemCountryLog.class)) {
                    //通过特定的注解,取得需要上传的数据对应的QUEUE_NAME,执行业务进行上传
                    SystemCountryLog systemCountryLog = method.getAnnotation(SystemCountryLog.class);
                    try {
                        JSONObject jsonObject = (JSONObject)param1;
                        //在此启动redismq进行数据接口的处理
//                        System.out.println(jsonObject);
//                        System.out.println(systemCountryLog.methods());
//                        System.out.println(systemCountryLog.module());
//                        System.out.println(systemCountryLog.uploadName());

                        UploadCountryDTO uploadCountryDTO = new UploadCountryDTO();
                        uploadCountryDTO.setReturnJson(jsonObject);
                        uploadCountryDTO.setMethods(systemCountryLog.methods());
                        uploadCountryDTO.setModule(systemCountryLog.module());
                        uploadCountryDTO.setUploadName(systemCountryLog.uploadName());

                        BaseDTO<UploadCountryDTO> baseDTO = new BaseDTO(BizType.getBizTypeByDesc(uploadCountryDTO.getUploadName()), BizOperateType.getBizOperateTypeByDesc(uploadCountryDTO.getMethods()), uploadCountryDTO);
                        publishService.sendMessage(TopicConstant.CRK_TOPIC_NAME, JSON.toJSONString(baseDTO));
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }finally {
                            }
                } else {//没有包含注解
                }
            } else { //不需要拦截直接执行
            }


    }
}