package com.engine.salary.formlua.func.logic;

import com.engine.salary.formlua.entity.parameter.DataType;
import com.engine.salary.formlua.entity.standard.FormulaFilterData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 逻辑函数构建条件工具类
 */
public class LogicUtils {
    protected static final Logger logger = LoggerFactory.getLogger(LogicUtils.class);
    /**
     * 构建AND和OR函数的条件
     * @param func
     * @param dataType
     * @param objs
     */
    public static void buildAndOrFilterParam(String func,DataType dataType,Object... objs){
        List<FormulaFilterData> filterFormDataList=new ArrayList<>();
        try {
            if(objs != null && objs.length > 0) {
                for(int i=0;i<objs.length;i++){
                    if(objs[i] instanceof DataType) {
                        DataType paramData=(DataType)objs[i];
                        List<FormulaFilterData> paramFilterDataList=paramData.getFormulaFilterDataList();
                        if(paramFilterDataList!=null){
                            paramFilterDataList.forEach(val->{
                                if(func.equalsIgnoreCase("AND")){
                                    val.setCondition(FormulaFilterData.CONDITION_AND);
                                }else if(func.equalsIgnoreCase("OR")){
                                    val.setCondition(FormulaFilterData.CONDITION_OR);
                                }

                            });
                            filterFormDataList.addAll(paramFilterDataList);
                        }
                    }
                }
//                dataType.setFormulaFilterDataList(filterFormDataList);
            }
        } catch (Exception e) {
            logger.error("err",e);
        }
    }

    /**
     * 构建Like函数的过滤条件
     * @param dataType
     * @param objs
     */
    public static void buildLikeFilterParam(DataType dataType,Object... objs){
        try {
            if(dataType != null && objs.length > 0) {
                if(dataType.getDataType().equalsIgnoreCase(DataType.DATASOURCE) || dataType.getDataType().equalsIgnoreCase(DataType.OPTION) || dataType.getDataType().equalsIgnoreCase(DataType.FORM)) {
                    throw new RuntimeException("筛选条件函数[LIKE]:第一个参数不能是选项型");
                }
                List<FormulaFilterData> formulaFilterDataList=new ArrayList<>();
                FormulaFilterData formulaFilterData=new FormulaFilterData();
                List<String>  contents = new ArrayList<String>();
                for(Object str : objs) {
                    if(str instanceof DataType){
                        DataType data=(DataType)str;
                        contents.add(data.getContent()+"");
                    }else {
                        contents.add(str+"");
                    }
                }
                formulaFilterData.setContents(contents);
//                formulaFilterData.setTerm(FilterFormData.TERM_LIKE);
//                formulaFilterData.setFieldId(dataType.getFieldId()+"");
//                formulaFilterDataList.add(formulaFilterData);
//                dataType.setFormulaFilterDataList(formulaFilterDataList);
            }
        } catch (RuntimeException RuntimeException) {
            RuntimeException.printStackTrace();
        }
    }

    public static void buildInFilterParam(DataType dataType, Object... objs){
        try {
            if(dataType != null && objs.length > 0) {
                if(!dataType.getDataType().equalsIgnoreCase(DataType.DATASOURCE) && !dataType.getDataType().equalsIgnoreCase(DataType.OPTION)) {
                    return;
                }
                List<FormulaFilterData> formulaFilterDataList=new ArrayList<>();
                FormulaFilterData formulaFilterData=new FormulaFilterData();
                List<String> ids = new ArrayList<String>();
                for(Object option : objs) {
                    if(option instanceof DataType){
                        DataType data=(DataType)option;
                        ids.add(data.getContent()+"");
                    }
                }
                formulaFilterData.setIds(ids);
                formulaFilterData.setFieldId(dataType.getFieldId()+"");
//                formulaFilterData.setTerm(FilterFormData.TERM_EQ);
//                formulaFilterDataList.add(formulaFilterData);
//                dataType.setFormulaFilterDataList(formulaFilterDataList);
            }
        } catch (Exception e) {
            logger.error("err",e);
        }
    }

}
