package com.lantone.qc.trans.comsis;

import com.google.common.collect.Maps;
import com.lantone.qc.pub.util.MapUtil;
import com.lantone.qc.pub.util.StringUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/4/2 14:04
 */
public class OrdinaryAssistant {

    public static Map<String, String> mapKeyContrast(Map<String, String> sourceMap, List<String> keyContrasts) {
        Map<String, String> retMap = Maps.newHashMap();
        Map<String, String> sourceMap_ = MapUtil.copyMap((Map) sourceMap);
        String[] arry = null;
        String sourceKey = null, targetKey;
        Set<String> removeKey = new HashSet<>();
        for (String keyContrast : keyContrasts) {
            arry = keyContrast.split("=");
            sourceKey = arry[0];
            if (arry.length == 1) {
                targetKey = arry[0];
            } else {
                targetKey = arry[1];
            }
            if (!retMap.containsKey(targetKey) || StringUtil.isNotBlank(sourceMap_.get(sourceKey))) {
                retMap.put(targetKey, sourceMap_.get(sourceKey));
            }
            removeKey.add(sourceKey);
            //            sourceMap_.remove(sourceKey); // 如果直接remove，多个标准key对应xml中同一个key就会出问题
        }
        //        retMap.putAll(sourceMap_); //如果key相同会覆盖之前的值，存在bug
        Set<String> keySet = retMap.keySet();
        for (String key : sourceMap_.keySet()) {
            if (!keySet.contains(key) && !removeKey.contains(key)) { // 如果之前已放过key就不用放了
                retMap.put(key, sourceMap_.get(key));
            }
        }
        return retMap;
    }

}