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

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.lideeyunji.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:52
 * @dedescription: 差集
 */
public class DifferenceCriteriaFilterAdvicePlugin extends BaseCriterFilterAdvicePlugin implements AfterAdvicePlugin {

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

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

        enhanceContext.getResult().setRecords(differ);
    }
}
