package com.hz.pm.api.common.util;

import com.hz.pm.api.common.model.ExpertRegionModifyDiffBO;
import com.hz.pm.api.expert.model.RegionDtoMapKey;
import com.hz.pm.api.meta.model.ExpertRegionInfo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/7/28 下午4:33
 */

public class ExpertRegionInfoUtils {

    public static ExpertRegionModifyDiffBO modifyDiff(List<ExpertRegionInfo> beforeList, List<ExpertRegionInfo> afterList) {
        ExpertRegionModifyDiffBO expertRegionModifyDiffBO = new ExpertRegionModifyDiffBO();
        List<ExpertRegionInfo> addList = new ArrayList<>();
        List<ExpertRegionInfo> removeList = new ArrayList<>();
        if (CollectionUtils.isEmpty(beforeList) && CollectionUtils.isEmpty(afterList)) {
            expertRegionModifyDiffBO.setAddList(addList);
            expertRegionModifyDiffBO.setRemoveList(removeList);
            return expertRegionModifyDiffBO;
        } else if (CollectionUtils.isEmpty(beforeList)) {
            expertRegionModifyDiffBO.setAddList(afterList);
            expertRegionModifyDiffBO.setRemoveList(removeList);
            return expertRegionModifyDiffBO;
        } else if (CollectionUtils.isEmpty(afterList)) {
            expertRegionModifyDiffBO.setAddList(addList);
            expertRegionModifyDiffBO.setRemoveList(beforeList);
            return expertRegionModifyDiffBO;
        }

        List<RegionDtoMapKey> beforeRegionDtoMapKeyList = beforeList.stream().map(r -> {
            RegionDtoMapKey regionDtoMapKey = new RegionDtoMapKey();
            regionDtoMapKey.setRegionLevel(r.getRegionLevel());
            regionDtoMapKey.setRegionCode(r.getRegionCode());
            return regionDtoMapKey;
        }).collect(Collectors.toList());

        List<RegionDtoMapKey> afterRegionDtoMapKeyList = afterList.stream().map(r -> {
            RegionDtoMapKey regionDtoMapKey = new RegionDtoMapKey();
            regionDtoMapKey.setRegionLevel(r.getRegionLevel());
            regionDtoMapKey.setRegionCode(r.getRegionCode());
            return regionDtoMapKey;
        }).collect(Collectors.toList());

        for (RegionDtoMapKey key : beforeRegionDtoMapKeyList) {
            if (!afterRegionDtoMapKeyList.contains(key)) {
                ExpertRegionInfo expertRegionInfo = new ExpertRegionInfo();
                expertRegionInfo.setRegionCode(key.getRegionCode());
                expertRegionInfo.setRegionLevel(key.getRegionLevel());
                removeList.add(expertRegionInfo);
            }
        }

        for (RegionDtoMapKey key : afterRegionDtoMapKeyList) {
            if (!beforeRegionDtoMapKeyList.contains(key)) {
                ExpertRegionInfo expertRegionInfo = new ExpertRegionInfo();
                expertRegionInfo.setRegionCode(key.getRegionCode());
                expertRegionInfo.setRegionLevel(key.getRegionLevel());
                addList.add(expertRegionInfo);
            }
        }

        expertRegionModifyDiffBO.setAddList(addList);
        expertRegionModifyDiffBO.setRemoveList(removeList);
        return expertRegionModifyDiffBO;
    }
}
