
package com.jeelowcode.core.framework.config.aspect.enhance.criteria;

import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.jeelowcode.core.framework.config.aspect.enhance.plugin.AfterAdvicePlugin;
import org.apache.commons.collections4.CollectionUtils;

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

/**
 * @author JX
 * @create 2024-08-19 14:56
 * @dedescription: 合集
 */
public class OrCriteriaFilterAdvicePlugin extends BaseCriterFilterAdvicePlugin implements AfterAdvicePlugin {

    public OrCriteriaFilterAdvicePlugin(AfterAdvicePlugin leftCriterFilter, AfterAdvicePlugin rightCriterFilter) {

        super(leftCriterFilter,rightCriterFilter);
    }

    @Override
    public void execute(EnhanceContext enhanceContext) {
        List<Map<String,Object>> or = new ArrayList<>();
        EnhanceContext leftEnhanceContext = enhanceContext.clone();
        EnhanceContext rightEnhanceContext = enhanceContext.clone();
        //执行增强
        leftCriterFilter.execute(leftEnhanceContext);
        rightCriterFilter.execute(rightEnhanceContext);
        //合并结果
        EnhanceResult leftResult = leftEnhanceContext.getResult();
        EnhanceResult rightResult = rightEnhanceContext.getResult();
        List<Map<String, Object>> leftRecords = leftResult.getRecords();
        List<Map<String, Object>> rightRecords = rightResult.getRecords();
        CollectionUtils.addAll(leftRecords,rightRecords);
        or = leftRecords;
        enhanceContext.getResult().setRecords(or);
    }
}
