
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:12
 * @dedescription: 并集
 */
public class UnionCriteriaFilterAdvicePlugin extends BaseCriterFilterAdvicePlugin implements AfterAdvicePlugin {


    public UnionCriteriaFilterAdvicePlugin(AfterAdvicePlugin leftCriterFilter, AfterAdvicePlugin rightCriterFilter) {
        super(leftCriterFilter,rightCriterFilter);
    }

    @Override
    public void execute(EnhanceContext enhanceContext) {
        List<Map<String,Object>> union = 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();
        union = (List<Map<String, Object>>) CollectionUtils.union(leftRecords, rightRecords);
        enhanceContext.getResult().setRecords(union);
    }
}
