package com.lideeyunji.core.framework.config.aspect.enhance;

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceParam;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.lideeyunji.core.framework.config.aspect.enhancereport.model.EnhanceReportContext;
import com.lideeyunji.core.framework.config.aspect.enhancereport.model.EnhanceReportParam;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.model.ExecuteEnhanceModel;
import com.lideeyunji.tool.framework.yunji.model.ResultDataModel;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class LideeYunJiBaseEnhance {

    protected Object getResult(JoinPoint joinPoint,EnhanceContext context) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Class<?> returnType = signature.getMethod().getReturnType();

        if (returnType.equals(ResultDataModel.class)) {//查询相关
            EnhanceResult result = context.getResult();
            if(Func.isEmpty(result.getRecords())){
                return ResultDataModel.fomat(0L, new ArrayList<>());
            }
            List<Map<String, Object>> records = result.getRecords();
            String id = result.getId();
            if(FuncBase.isEmpty(id) && Func.isEmpty(records)){
                return ResultDataModel.fomat(0L, new ArrayList<>());
            }
            ResultDataModel resultDataModel = new ResultDataModel();
            resultDataModel.setRecords(context.getResult().getRecords());
            resultDataModel.setTotal(context.getResult().getTotal());
            resultDataModel.setExitFlag(context.getResult().isExitFlag());
            return resultDataModel;

        }
        //不是查询类型
        if (returnType.equals(ExecuteEnhanceModel.class)) {
            ExecuteEnhanceModel enhanceModel = new ExecuteEnhanceModel();
            enhanceModel.setId(context.getResult().getId());
            enhanceModel.setExitFlag(context.getResult().isExitFlag());

            return enhanceModel;
        }

        return null;
    }

    //报表
    protected ResultDataModel getResult(EnhanceReportContext context) {
        EnhanceResult result = context.getResult();
        if(Func.isEmpty(result.getRecords())){
            return ResultDataModel.fomat(0L, new ArrayList<>());
        }
        List<Map<String, Object>> records = result.getRecords();
        String id = result.getId();
        if(FuncBase.isEmpty(id) && Func.isEmpty(records)){
            return ResultDataModel.fomat(0L, new ArrayList<>());
        }
        ResultDataModel resultDataModel = new ResultDataModel();
        resultDataModel.setRecords(context.getResult().getRecords());
        resultDataModel.setTotal(context.getResult().getTotal());
        resultDataModel.setExitFlag(context.getResult().isExitFlag());
        return resultDataModel;

    }

    //获取参数
    protected Map<String,Object> getJoinPointParam(JoinPoint joinPoint){
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //处理参数
        Object[] args = joinPoint.getArgs();
        LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
        String[] parameterNames = discoverer.getParameterNames(methodSignature.getMethod());
        Map<String, Object> paramMap = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
        }
        return paramMap;
    }

    //获取增强参数
    protected EnhanceParam getEnhanceParam(Long dbFormId,Map<String,Object> paramMap){
        EnhanceParam param = new EnhanceParam();
        param.setDbFormId(dbFormId);
        param.setDataId((Long)paramMap.getOrDefault("id", null));
        param.setList((ArrayList) paramMap.getOrDefault("dataList", null));
        param.setParams((Map<String, Object>) paramMap.getOrDefault("params", null));
        return param;
    }


    protected EnhanceReportParam getReportEnhanceParam(String reportCode,Map<String,Object> paramMap){
        EnhanceReportParam param = new EnhanceReportParam();
        param.setReportCode(reportCode);
        param.setList((ArrayList) paramMap.getOrDefault("dataList", null));
        param.setParams((Map<String, Object>) paramMap.getOrDefault("params", null));
        return param;
    }

}
