package com.palmnest.application.core.util.hangdian;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MapUtils {


	/**
	 * 功能说明：将map转成 (name=123&password=456)格式的字符串
	 *
	 * @param params
	 * @return
	 * @createTime 2018年5月18日 上午10:43:38
	 * @author liuli
	 */
	public static String mapToString(Map<String, String> params) {
		List<String> keys = new ArrayList<String>();
		keys.addAll(params.keySet());
		Collections.sort(keys);
		StringBuilder signStr = new StringBuilder();
		for (String key : keys) {
			if (StringUtils.isEmpty(params.get(key))) {
				continue;
			}
			signStr.append(key).append("=").append(params.get(key)).append("&");
		}
		return signStr.deleteCharAt(signStr.length() - 1).toString();
	}

	/**
	 * 将paramsMap中的值拷贝到resultMap中
	 *
	 * @param paramsMap
	 * @param resultMap
	 */
	public static void mapCopy(Map paramsMap, Map resultMap) {
		if (resultMap == null) resultMap = new HashMap();
		if (paramsMap == null) return;

		Iterator it = paramsMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			resultMap.put(key, paramsMap.get(key) != null ? paramsMap.get(key) : "");
		}
	}
}
